Skip to content

Commit

Permalink
Allow specific schemes to be ignored. Implements #25
Browse files Browse the repository at this point in the history
  • Loading branch information
shardulm94 committed Apr 3, 2019
1 parent 123c175 commit 1fa7990
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,18 @@ Here is a list of all languages that VS Code supports (as of 28 March 2019):
bat, c, clojure, coffeescript, cpp, csharp, css, diff, dockerfile, fsharp, git-commit, git-rebase, go, groovy, handlebars, hexdump, hlsl, hocon, html, ignore, ini, jade, java, javascript, javascriptreact, jinja, json, jsonc, jsx-tags, jupyter, less, Log, log, lua, makefile, markdown, objective-c, objective-cpp, perl, perl6, php, pig, pip-requirements, plaintext, powershell, properties, python, r, razor, ruby, rust, scss, shaderlab, shellscript, sql, swift, toml, typescript, typescriptreact, vb, xml, xsl, yaml
```

For the most recent list of langauges, please use the `languages.getLanguages()` function (details [here](https://code.visualstudio.com/docs/extensionAPI/vscode-api#languages.getLanguages)).
For the most recent list of langauges, please use the [known language identifiers](https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers) page or the `languages.getLanguages()` function (details [here](https://code.visualstudio.com/docs/extensionAPI/vscode-api#languages.getLanguages)).

### Ignore Scheme

*Default: []*

With this option you can ignore documents with a specific scheme. An item has to match the case-sensitive string of the scheme of the document. For instance, if you want to ignore VSCode output windows:

``` js
// documents with the scheme "output" are ignored
{ "trailing-spaces.schemeIgnore": ["output"]}
```

### Show Status Bar Message

Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "trailing-spaces",
"displayName": "Trailing Spaces",
"description": "Highlight trailing spaces and delete them in a flash!",
"version": "0.3.0-rc3",
"version": "0.3.0-rc4",
"publisher": "shardulm94",
"icon": "icon.png",
"engines": {
Expand Down Expand Up @@ -59,6 +59,11 @@
"default": [],
"description": "With this option you can ignore specific files based on the syntax used. An item has to match the case-sensitive string of the language used in the file."
},
"trailing-spaces.schemeIgnore": {
"type": "array",
"default": [],
"description": "With this option you can ignore specific schemes. An item has to match the case-sensitive string of the scheme of the document."
},
"trailing-spaces.trimOnSave": {
"type": "boolean",
"default": false,
Expand Down
16 changes: 10 additions & 6 deletions src/trailing-spaces/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface TrailingSpacesSettings {
liveMatching: boolean,
deleteModifiedLinesOnly: boolean,
languagesToIgnore: { [id: string]: boolean; },
schemesToIgnore: { [id: string]: boolean; },
trimOnSave: boolean,
showStatusBarMessage: boolean,
textEditorDecorationType: vscode.TextEditorDecorationType
Expand All @@ -28,6 +29,7 @@ export class Settings implements TrailingSpacesSettings {
liveMatching!: boolean;
deleteModifiedLinesOnly!: boolean;
languagesToIgnore!: { [id: string]: boolean; };
schemesToIgnore!: { [id: string]: boolean; };
trimOnSave!: boolean;
showStatusBarMessage!: boolean;
textEditorDecorationType!: vscode.TextEditorDecorationType;
Expand All @@ -52,7 +54,8 @@ export class Settings implements TrailingSpacesSettings {
this.regexp = config.get<string>('regexp');
this.liveMatching = config.get<boolean>('liveMatching');
this.deleteModifiedLinesOnly = config.get<boolean>('deleteModifiedLinesOnly');
this.languagesToIgnore = this.getLanguagesToIgnore(config.get<string[]>('syntaxIgnore'));
this.languagesToIgnore = this.getMapFromStringArray(config.get<string[]>('syntaxIgnore'));
this.schemesToIgnore = this.getMapFromStringArray(config.get<string[]>('schemeIgnore'));
this.trimOnSave = config.get<boolean>('trimOnSave');
this.showStatusBarMessage = config.get<boolean>('showStatusBarMessage');
this.textEditorDecorationType = this.getTextEditorDecorationType(config.get<string>('backgroundColor'), config.get<string>('borderColor'));
Expand All @@ -70,19 +73,20 @@ export class Settings implements TrailingSpacesSettings {
config.update('liveMatching', undefined, true);
config.update('deleteModifiedLinesOnly', undefined, true);
config.update('syntaxIgnore', undefined, true);
config.update('schemeIgnore', undefined, true);
config.update('trimOnSave', undefined, true);
config.update('showStatusBarMessage', undefined, true);
config.update('backgroundColor', undefined, true);
config.update('borderColor', undefined, true);
this.refreshSettings();
}

private getLanguagesToIgnore(syntaxIgnore: string[]): { [id: string]: boolean; } {
let languagesToIgnore: { [id: string]: boolean; } = {};
syntaxIgnore.forEach((language: string) => {
languagesToIgnore[language] = true;
private getMapFromStringArray(array: string[]): { [id: string]: boolean; } {
let map: { [id: string]: boolean; } = {};
array.forEach((element: string) => {
map[element] = true;
});
return languagesToIgnore;
return map;
}

private getTextEditorDecorationType(backgroundColor: string, borderColor: string): vscode.TextEditorDecorationType {
Expand Down
17 changes: 10 additions & 7 deletions src/trailing-spaces/trailing-spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ILogger, Logger } from './logger';
import { Settings, TrailingSpacesSettings } from './settings';
import * as utils from './utils';
import fs = require('fs');
import { isNullOrUndefined } from 'util';


export class TrailingSpaces {
Expand Down Expand Up @@ -146,8 +147,8 @@ export class TrailingSpaces {
* @returns {vscode.Range[]} An array of ranges containing the trailing spaces
*/
private findTrailingSpaces(document: vscode.TextDocument): vscode.Range[] {
if (this.ignoreFile(document.languageId)) {
this.logger.info(`File with langauge '${document.languageId}' ignored - ${document.fileName}`);
if (this.ignoreDocument(document.languageId, document.uri.scheme)) {
this.logger.info(`File with langauge '${document.languageId}' and scheme '${document.uri.scheme}' ignored - ${document.fileName}`);
return [];
} else {
let offendingRanges: vscode.Range[] = [];
Expand All @@ -172,13 +173,15 @@ export class TrailingSpaces {
}

/**
* Checks if the language of the file is set to be ignored.
* Checks if the language or the scheme of the document is set to be ignored.
*
* @private
* @param {string} language The language of the file to be checked
* @returns {boolean} A boolean indicating if the file needs to be ignored
* @param {string} language The language of the document to be checked
* @param {string} scheme The scheme of the document to be checked
* @returns {boolean} A boolean indicating if the document needs to be ignored
*/
private ignoreFile(language: string): boolean {
return language ? (this.settings.languagesToIgnore[language] == true) : false;
private ignoreDocument(language: string, scheme: string): boolean {
return (!isNullOrUndefined(language) && this.settings.languagesToIgnore[language]
|| !isNullOrUndefined(scheme) && this.settings.schemesToIgnore[scheme]);
}
}
5 changes: 5 additions & 0 deletions test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ describe("Extension Tests", () => {
assertDeleteTrailingSpaces(testEditor, './files/should_not_delete_spaces.js', done);
});

it("should not delete trailing spaces when file scheme is set in schemeIgnore", (done: MochaDone) => {
settings.schemesToIgnore[testEditor.document.uri.scheme] = true;
assertDeleteTrailingSpaces(testEditor, './files/should_not_delete_spaces.js', done);
});

it("should delete all trailing spaces including blank lines when regex is [\\s]+", (done: MochaDone) => {
settings.regexp = "[\\s]+";
assertDeleteTrailingSpaces(testEditor, './files/delete_all_trailing_spaces_including_blank_lines.js', done);
Expand Down

0 comments on commit 1fa7990

Please sign in to comment.