Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a regex setting to ignore files based their paths. #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
],
"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.pathToIgnore": {
"type": "string",
"default": "",
"description": "With this option you can ignore files whose paths contain a match for the regular expression (case insensitive)."
},
"trailing-spaces.trimOnSave": {
"type": "boolean",
"default": false,
Expand Down
4 changes: 4 additions & 0 deletions src/trailing-spaces/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface TrailingSpacesSettings {
deleteModifiedLinesOnly: boolean,
languagesToIgnore: { [id: string]: boolean; },
schemesToIgnore: { [id: string]: boolean; },
pathToIgnore: string,
trimOnSave: boolean,
showStatusBarMessage: boolean,
textEditorDecorationType: vscode.TextEditorDecorationType
Expand All @@ -30,6 +31,7 @@ export class Settings implements TrailingSpacesSettings {
deleteModifiedLinesOnly!: boolean;
languagesToIgnore!: { [id: string]: boolean; };
schemesToIgnore!: { [id: string]: boolean; };
pathToIgnore!: string;
trimOnSave!: boolean;
showStatusBarMessage!: boolean;
textEditorDecorationType!: vscode.TextEditorDecorationType;
Expand All @@ -56,6 +58,7 @@ export class Settings implements TrailingSpacesSettings {
this.deleteModifiedLinesOnly = config.get<boolean>('deleteModifiedLinesOnly');
this.languagesToIgnore = this.getMapFromStringArray(config.get<string[]>('syntaxIgnore'));
this.schemesToIgnore = this.getMapFromStringArray(config.get<string[]>('schemeIgnore'));
this.pathToIgnore = config.get<string>('pathToIgnore');
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 @@ -74,6 +77,7 @@ export class Settings implements TrailingSpacesSettings {
config.update('deleteModifiedLinesOnly', undefined, true);
config.update('syntaxIgnore', undefined, true);
config.update('schemeIgnore', undefined, true);
config.update('pathToIgnore', undefined, true);
config.update('trimOnSave', undefined, true);
config.update('showStatusBarMessage', undefined, true);
config.update('backgroundColor', undefined, true);
Expand Down
19 changes: 15 additions & 4 deletions src/trailing-spaces/trailing-spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class TrailingSpaces {
* @returns {vscode.Range[]} An array of ranges containing the trailing spaces
*/
private findTrailingSpaces(document: vscode.TextDocument): vscode.Range[] {
if (this.ignoreDocument(document.languageId, document.uri.scheme)) {
if (this.ignoreDocument(document.languageId, document.uri)) {
this.logger.info(`File with langauge '${document.languageId}' and scheme '${document.uri.scheme}' ignored - ${document.fileName}`);
return [];
} else {
Expand Down Expand Up @@ -180,8 +180,19 @@ export class TrailingSpaces {
* @param {string} scheme The scheme of the document to be checked
* @returns {boolean} A boolean indicating if the document needs to be ignored
*/
private ignoreDocument(language: string, scheme: string): boolean {
return (!isNullOrUndefined(language) && this.settings.languagesToIgnore[language]
|| !isNullOrUndefined(scheme) && this.settings.schemesToIgnore[scheme]);
private ignoreDocument(language: string, uri: vscode.Uri): boolean {
if (!isNullOrUndefined(language) && this.settings.languagesToIgnore[language])
return true;

if (!isNullOrUndefined(uri.scheme) && this.settings.schemesToIgnore[uri.scheme])
return true;

if (!isNullOrUndefined(uri.path)
&& !isNullOrUndefined(this.settings.pathToIgnore)
&& this.settings.pathToIgnore !== ''
&& RegExp(this.settings.pathToIgnore, 'i').test(uri.path))
return true;

return false;
}
}