Skip to content

Commit

Permalink
Add yank highlighting
Browse files Browse the repository at this point in the history
Fixes #2333, #2916
  • Loading branch information
darfink authored and jpoon committed Jan 25, 2019
1 parent fbbc1c9 commit 2722e62
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ 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 VS Code 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.yankHighlighting | Enable highlighting when yanking | Boolean | false |
| vim.yankHighlightColor | Set the color of yank highlights | String | rgba(250, 240, 170, 0.5) |

### Neovim Integration

Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@
"description": "Color of the search highlight.",
"default": "rgba(150, 150, 255, 0.3)"
},
"vim.yankHighlighting": {
"type": "boolean",
"description": "Enable highlighting when yanking.",
"default": false
},
"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
27 changes: 21 additions & 6 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 @@ -268,7 +269,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 @@ -278,6 +280,12 @@ export class YankOperator extends BaseOperator {
text = text + '\n';
}

if (configuration.yankHighlighting) {
const decoration = Decoration.YankHighlight;
vimState.editor.setDecorations(decoration, [range]);
setTimeout(() => decoration.dispose(), 200);
}

Register.put(text, vimState, this.multicursorIndex);

await vimState.setCurrentMode(ModeName.Normal);
Expand Down Expand Up @@ -645,19 +653,26 @@ 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[] = [];
const isMultiline = startPos.line !== endPos.line;

const isMultiline = start.line !== end.line;

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

if (configuration.yankHighlighting) {
const decoration = Decoration.YankHighlight;
vimState.editor.setDecorations(decoration, ranges);
setTimeout(() => decoration.dispose(), 200);
}

vimState.currentRegisterMode = RegisterMode.BlockWise;

Register.put(toCopy, vimState, this.multicursorIndex);
Expand All @@ -666,7 +681,7 @@ export class YankVisualBlockMode extends BaseOperator {
ReportLinesYanked(numLinesYanked, vimState);

await vimState.setCurrentMode(ModeName.Normal);
vimState.cursorPosition = start;
vimState.cursorPosition = startPos;
return vimState;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ class Configuration implements IConfiguration {

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

yankHighlighting = false;

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

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

Expand Down
6 changes: 6 additions & 0 deletions src/configuration/decoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ export class Decoration {
static readonly EasyMotion = vscode.window.createTextEditorDecorationType({
backgroundColor: configuration.searchHighlightColor,
});

static get YankHighlight() {
return vscode.window.createTextEditorDecorationType({
backgroundColor: configuration.yankHighlightColor,
});
}
}
10 changes: 10 additions & 0 deletions src/configuration/iconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ export interface IConfiguration {
*/
searchHighlightColor: string;

/**
* Enable highlighting when yanking.
*/
yankHighlighting: boolean;

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

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

0 comments on commit 2722e62

Please sign in to comment.