Skip to content

Commit

Permalink
Added options to change background and border colors of highlighting.…
Browse files Browse the repository at this point in the history
… Implements #12
  • Loading branch information
shardulm94 committed Mar 29, 2019
1 parent 85f645e commit 7ead91f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 31 deletions.
49 changes: 42 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ This extension is a port of the popular [Sublime Text](https://www.sublimetext.c
- [Include Empty Lines](#include-empty-lines)
- [Delete Modified Lines Only](#delete-modified-lines-only)
- [Trim On Save](#trim-on-save)
- [Save After Trim](#save-after-trim)
- [~~Save After Trim~~ *[REMOVED]*](#save-after-trim-removed)
- [Live Matching vs On-demand Matching](#live-matching-vs-on-demand-matching)
- [Ignore Syntax](#ignore-syntax)
- [Show Status Bar Message](#show-status-bar-message)
- [Background Color](#background-color)
- [Border Color](#border-color)
- [For power-users only!](#for-power-users-only)
- [The matching pattern](#the-matching-pattern)

Expand Down Expand Up @@ -140,9 +143,9 @@ Setting this to `true` will ensure trailing spaces are deleted when you save you
{ "trailing-spaces.trimOnSave": true }
```

### Save After Trim **[REMOVED]**
### ~~Save After Trim~~ **[REMOVED]**

*NOTE: Current VSCode lifecycle for a text editor commands do not provide a clean way to implement this feature. Since I did not see a lot of folks using this option, it was better to remove it.*
*NOTE: The current VSCode lifecycle for text editor commands does not provide a clean way to implement this feature. Since I did not see a lot of folks using this option, it was better to remove it.*

*Default: false*

Expand All @@ -156,7 +159,7 @@ It is obviously ignored if *Trim On Save* is on.

### Live Matching vs On-demand Matching

*Default: true (reopen VS Code to update)*
*Default: true (reload VS Code Window to update)*

By default, trailing regions are matched every time you edit the document, and when you open it.

Expand All @@ -179,14 +182,45 @@ With this option you can ignore specific files based on the language used. An it
{ "trailing-spaces.syntaxIgnore": ["markdown"]}
```

Here is a list of all languages that VS Code supports (as of 12 March 2016):
Here is a list of all languages that VS Code supports (as of 28 March 2019):

```js
bat, c, clojure, coffeescript, cpp, css, dockerfile, fsharp, go, groovy, handlebars, html, ini, jade, java, javascript, javascriptreact, json, less, Log, lua, makefile, markdown, objective-c, perl, perl6, php, plaintext, powershell, python, r, razor, ruby, rust, sass, shaderlab, shellscript, sql, swift, typescript, typescriptreact, vb, xml, xsl, ya
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)).

### Show Status Bar Message

*Default: true*

By default, trailing space deletions will be communicated through a status bar message. Set this to `false` as below to disable these messages:

``` js
{ "trailing-spaces.showStatusBarMessage": false }
```

### Background Color

*Default: rgba(255,0,0,0.3)*

You can control the background color of the highlighting performed by Trailing Spaces using this option. To set up another color change the setting:

``` js
{ "trailing-spaces.backgroundColor": "rgba(255,0,0,0.3)" }
```

### Border Color

*Default: rgba(255,100,100,0.15)*

You can control the border color of the highlighting performed by Trailing Spaces using this option. To set up another color change the setting:

``` js
{ "trailing-spaces.borderColor": "rgba(255,100,100,0.15)" }
```


### For power-users only!

#### The matching pattern
Expand All @@ -202,4 +236,5 @@ Trailing spaces are line-ending regions containing at least one simple space, ta

Contributions
-------------
- [@HookyQR](https://github.com/HookyQR): Fixed error while deleting last line of text [PR #9](https://github.com/shardulm94/vscode-trailingspaces/pull/9)
- [@HookyQR](https://github.com/HookyQR): Fixed error while deleting last line of text [PR #9](https://github.com/shardulm94/vscode-trailingspaces/pull/9)
- [@yustnip](https://github.com/yustnip): Added options to change background and border colors of highlighting [PR #17](https://github.com/shardulm94/vscode-trailingspaces/pull/17)
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,22 @@
"trailing-spaces.trimOnSave": {
"type": "boolean",
"default": false,
"description": "By default, nothing happens on save. Set to true to trim trailing spaces before saving, with respect to the other settings."
"description": "Controls whether trailing spaces are trimmed automatically when saving a file."
},
"trailing-spaces.showStatusBarMessage": {
"type": "boolean",
"default": true,
"description": "By default, trailing space deletions will be communicated through a status bar message. Set to false to disable these messages."
},
"trailing-spaces.backgroundColor": {
"type": "string",
"default": "rgba(255,0,0,0.3)",
"description": "Controls the background color of the trailing space decoration."
},
"trailing-spaces.borderColor": {
"type": "string",
"default": "rgba(255,100,100,0.15)",
"description": "Controls the color of the border around the trailing space decoration."
}
}
},
Expand Down
40 changes: 27 additions & 13 deletions src/trailing-spaces/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export interface TrailingSpacesSettings {
deleteModifiedLinesOnly: boolean,
languagesToIgnore: { [id: string]: boolean; },
trimOnSave: boolean,
showStatusBarMessage: boolean
showStatusBarMessage: boolean,
textEditorDecorationType: vscode.TextEditorDecorationType
}

export class Settings implements TrailingSpacesSettings {

private static instance: Settings = new Settings();
private config!: vscode.WorkspaceConfiguration;
private logger!: ILogger;

logLevel!: LogLevel;
Expand All @@ -30,6 +30,7 @@ export class Settings implements TrailingSpacesSettings {
languagesToIgnore!: { [id: string]: boolean; };
trimOnSave!: boolean;
showStatusBarMessage!: boolean;
textEditorDecorationType!: vscode.TextEditorDecorationType;

constructor() {
if (!Settings.instance) {
Expand All @@ -44,16 +45,17 @@ export class Settings implements TrailingSpacesSettings {
}

public refreshSettings(): void {
this.config = vscode.workspace.getConfiguration('trailing-spaces');
this.logLevel = LogLevel[this.config.get<keyof typeof LogLevel>('logLevel')];
this.includeEmptyLines = this.config.get<boolean>('includeEmptyLines');
this.highlightCurrentLine = this.config.get<boolean>('highlightCurrentLine');
this.regexp = this.config.get<string>('regexp');
this.liveMatching = this.config.get<boolean>('liveMatching');
this.deleteModifiedLinesOnly = this.config.get<boolean>('deleteModifiedLinesOnly');
this.languagesToIgnore = this.getLanguagesToIgnore(this.config.get<string[]>('syntaxIgnore'));
this.trimOnSave = this.config.get<boolean>('trimOnSave');
this.showStatusBarMessage = this.config.get<boolean>('showStatusBarMessage');
let config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('trailing-spaces');
this.logLevel = LogLevel[config.get<keyof typeof LogLevel>('logLevel')];
this.includeEmptyLines = config.get<boolean>('includeEmptyLines');
this.highlightCurrentLine = config.get<boolean>('highlightCurrentLine');
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.trimOnSave = config.get<boolean>('trimOnSave');
this.showStatusBarMessage = config.get<boolean>('showStatusBarMessage');
this.textEditorDecorationType = this.getTextEditorDecorationType(config.get<string>('backgroundColor'), config.get<string>('borderColor'));
this.logger.setLogLevel(this.logLevel);
this.logger.setPrefix('Trailing Spaces');
this.logger.log('Configuration loaded');
Expand All @@ -70,7 +72,9 @@ export class Settings implements TrailingSpacesSettings {
config.update('syntaxIgnore', undefined, true);
config.update('trimOnSave', undefined, true);
config.update('showStatusBarMessage', undefined, true);
this.refreshSettings()
config.update('backgroundColor', undefined, true);
config.update('borderColor', undefined, true);
this.refreshSettings();
}

private getLanguagesToIgnore(syntaxIgnore: string[]): { [id: string]: boolean; } {
Expand All @@ -80,4 +84,14 @@ export class Settings implements TrailingSpacesSettings {
});
return languagesToIgnore;
}

private getTextEditorDecorationType(backgroundColor: string, borderColor: string): vscode.TextEditorDecorationType {
return vscode.window.createTextEditorDecorationType({
borderRadius: "3px",
borderWidth: "1px",
borderStyle: "solid",
backgroundColor: backgroundColor,
borderColor: borderColor
});
}
}
11 changes: 1 addition & 10 deletions src/trailing-spaces/trailing-spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,10 @@ import fs = require('fs');
export class TrailingSpaces {
private logger: ILogger;
private settings: TrailingSpacesSettings;
private decorationOptions: vscode.DecorationRenderOptions = {
borderRadius: "3px",
borderWidth: "1px",
borderStyle: "solid",
backgroundColor: "rgba(255,0,0,0.3)",
borderColor: "rgba(255,100,100,0.15)"
};
private decorationType: vscode.TextEditorDecorationType;

constructor() {
this.logger = Logger.getInstance();
this.settings = Settings.getInstance();
this.decorationType = vscode.window.createTextEditorDecorationType(this.decorationOptions);
}

public highlight(editor: vscode.TextEditor, editorEdit: vscode.TextEditorEdit | undefined = undefined): void {
Expand All @@ -44,7 +35,7 @@ export class TrailingSpaces {
* @param {vscode.TextEditor} editor The editor in which the spaces have to be highlighted
*/
private highlightTrailingSpaces(editor: vscode.TextEditor): void {
editor.setDecorations(this.decorationType, this.getRangesToHighlight(editor.document, editor.selection));
editor.setDecorations(this.settings.textEditorDecorationType, this.getRangesToHighlight(editor.document, editor.selection));
}

/**
Expand Down

0 comments on commit 7ead91f

Please sign in to comment.