Skip to content

Commit

Permalink
Add yank highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
darfink committed Oct 11, 2018
1 parent 00bb9af commit eae14df
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ These settings are specific to VSCodeVim.
| vim.substituteGlobalFlag | Similar to Vim's `gdefault` setting. `/g` flag in a substitute command replaces all occurrences in the line. Without this flag, replacement occurs only for the first occurrence in each line. With this setting enabled, the `g` is on by default. | Boolean | false |
| vim.useCtrlKeys | Enable Vim ctrl keys overriding common VSCode operations such as copy, paste, find, etc. | Boolean | true |
| vim.visualstar | In visual mode, start a search with `*` or `#` using the current selection | Boolean | false |
| vim.yankHighlightColor | Set the color of yank highlights | String | rgba(250, 240, 170, 0.5) |

### Neovim Integration

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@
"description": "Color of the search highlight.",
"default": "rgba(150, 150, 255, 0.3)"
},
"vim.yankHighlightColor": {
"type": "string",
"description": "Color of the yank highlight.",
"default": "rgba(250, 240, 170, 0.5)"
},
"vim.useSystemClipboard": {
"type": "boolean",
"description": "Use system clipboard for unnamed register.",
Expand Down
14 changes: 10 additions & 4 deletions src/actions/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as vscode from 'vscode';
import { Position, PositionDiff } from './../common/motion/position';
import { Range } from './../common/motion/range';
import { configuration } from './../configuration/configuration';
import { Decoration } from '../configuration/decoration';
import { ModeName } from './../mode/mode';
import { Register, RegisterMode } from './../register/register';
import { VimState } from './../state/vimState';
Expand Down Expand Up @@ -265,7 +266,8 @@ export class YankOperator extends BaseOperator {
end = end.getLineEnd();
}

let text = TextEditor.getText(new vscode.Range(start, end));
const range = new vscode.Range(start, end);
let text = TextEditor.getText(range);

// If we selected the newline character, add it as well.
if (
Expand All @@ -275,6 +277,7 @@ export class YankOperator extends BaseOperator {
text = text + '\n';
}

Decoration.yankHighlight(vimState, [range]);
Register.put(text, vimState, this.multicursorIndex);

vimState.currentMode = ModeName.Normal;
Expand Down Expand Up @@ -636,17 +639,20 @@ export class YankVisualBlockMode extends BaseOperator {
return false;
}

public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
public async run(vimState: VimState, startPos: Position, endPos: Position): Promise<VimState> {
let toCopy: string = '';
const ranges: vscode.Range[] = [];

for (const { line } of Position.IterateLine(vimState)) {
for (const { line, start, end } of Position.IterateLine(vimState)) {
ranges.push(new vscode.Range(start, end));
toCopy += line + '\n';
}

Decoration.yankHighlight(vimState, ranges);
Register.put(toCopy, vimState, this.multicursorIndex);

vimState.currentMode = ModeName.Normal;
vimState.cursorPosition = start;
vimState.cursorPosition = startPos;
return vimState;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ class Configuration implements IConfiguration {

searchHighlightColor = 'rgba(150, 150, 255, 0.3)';

yankHighlightColor = 'rgba(250, 240, 170, 0.5)';

@overlapSetting({ settingName: 'tabSize', defaultValue: 8 })
tabstop: number;

Expand Down
10 changes: 10 additions & 0 deletions src/configuration/decoration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';

import { configuration } from '../configuration/configuration';
import { VimState } from '../state/vimState';

export class Decoration {
static readonly Default = vscode.window.createTextEditorDecorationType({
Expand All @@ -24,4 +25,13 @@ export class Decoration {
static readonly EasyMotion = vscode.window.createTextEditorDecorationType({
backgroundColor: configuration.searchHighlightColor,
});

static yankHighlight(vimState: VimState, range: vscode.Range[]) {
const decoration = vscode.window.createTextEditorDecorationType({
backgroundColor: configuration.yankHighlightColor,
});

vimState.editor.setDecorations(decoration, range);
setTimeout(() => decoration.dispose(), 200);
}
}
5 changes: 5 additions & 0 deletions src/configuration/iconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ export interface IConfiguration {
*/
searchHighlightColor: string;

/**
* Color of yank highlights.
*/
yankHighlightColor: string;

/**
* Size of a tab character.
*/
Expand Down
1 change: 1 addition & 0 deletions test/testConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class Configuration implements IConfiguration {
loggingLevel: 'warn';
};
searchHighlightColor = 'rgba(150, 150, 255, 0.3)';
yankHighlightColor = 'rgba(250, 240, 170, 0.5)';
tabstop = 2;
editorCursorStyle = vscode.TextEditorCursorStyle.Line;
expandtab = true;
Expand Down

0 comments on commit eae14df

Please sign in to comment.