diff --git a/README.md b/README.md index 520aa55..1acf181 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 074443f..6279b99 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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, diff --git a/src/trailing-spaces/settings.ts b/src/trailing-spaces/settings.ts index 20d25a2..e8aca8b 100644 --- a/src/trailing-spaces/settings.ts +++ b/src/trailing-spaces/settings.ts @@ -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 @@ -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; @@ -52,7 +54,8 @@ export class Settings implements TrailingSpacesSettings { this.regexp = config.get('regexp'); this.liveMatching = config.get('liveMatching'); this.deleteModifiedLinesOnly = config.get('deleteModifiedLinesOnly'); - this.languagesToIgnore = this.getLanguagesToIgnore(config.get('syntaxIgnore')); + this.languagesToIgnore = this.getMapFromStringArray(config.get('syntaxIgnore')); + this.schemesToIgnore = this.getMapFromStringArray(config.get('schemeIgnore')); this.trimOnSave = config.get('trimOnSave'); this.showStatusBarMessage = config.get('showStatusBarMessage'); this.textEditorDecorationType = this.getTextEditorDecorationType(config.get('backgroundColor'), config.get('borderColor')); @@ -70,6 +73,7 @@ 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); @@ -77,12 +81,12 @@ export class Settings implements TrailingSpacesSettings { 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 { diff --git a/src/trailing-spaces/trailing-spaces.ts b/src/trailing-spaces/trailing-spaces.ts index 4eab895..ea2c9b1 100644 --- a/src/trailing-spaces/trailing-spaces.ts +++ b/src/trailing-spaces/trailing-spaces.ts @@ -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 { @@ -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[] = []; @@ -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]); } } diff --git a/test/extension.test.ts b/test/extension.test.ts index 4bd8c46..ad851b1 100644 --- a/test/extension.test.ts +++ b/test/extension.test.ts @@ -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);