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 yank highlighting (REBASED) #3593

Merged
merged 3 commits into from
Mar 17, 2019
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ 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.highlightedyank.enable | Enable highlighting when yanking | Boolean | false |
| vim.highlightedyank.color | Set the color of yank highlights | String | rgba(250, 240, 170, 0.5) |
| vim.highlightedyank.duration | Set the duration of yank highlights | Number | 200 |

### Neovim Integration

Expand Down
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,21 @@
"type": "string",
"description": "Color of the search highlight."
},
"vim.highlightedyank.enable": {
"type": "boolean",
"description": "Enable highlighting when yanking.",
"default": false
},
"vim.highlightedyank.color": {
"type": "string",
"description": "Color of the yank highlight.",
"default": "rgba(250, 240, 170, 0.5)"
},
"vim.highlightedyank.duration": {
"type": "number",
"description": "Duration in milliseconds of the yank highlight.",
"default": 200
},
"vim.useSystemClipboard": {
"type": "boolean",
"description": "Use system clipboard for unnamed register.",
Expand Down
35 changes: 28 additions & 7 deletions src/actions/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { BaseAction, RegisterAction } from './base';
import { CommandNumber } from './commands/actions';
import { TextObjectMovement } from './textobject';
import { ReportLinesChanged, ReportLinesYanked } from '../util/statusBarTextUtils';
import { IHighlightedYankConfiguration } from '../configuration/iconfiguration';

export class BaseOperator extends BaseAction {
constructor(multicursorIndex?: number) {
Expand Down Expand Up @@ -101,6 +102,19 @@ export class BaseOperator extends BaseAction {
position.getDownByCount(Math.max(0, count - 1)).getLineEnd()
);
}

public highlightYankedRanges(vimState: VimState, ranges: vscode.Range[]) {
if (!configuration.highlightedyank.enable) {
return;
}

const yankDecoration = vscode.window.createTextEditorDecorationType({
backgroundColor: configuration.highlightedyank.color,
});

vimState.editor.setDecorations(yankDecoration, ranges);
setTimeout(() => yankDecoration.dispose(), configuration.highlightedyank.duration);
}
}

@RegisterAction
Expand Down Expand Up @@ -268,7 +282,8 @@ export class YankOperator extends BaseOperator {
extendedEnd = extendedEnd.getLineEnd();
}

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

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

this.highlightYankedRanges(vimState, [range]);

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

if (vimState.currentMode === ModeName.Visual || vimState.currentMode === ModeName.VisualLine) {
Expand Down Expand Up @@ -648,12 +665,14 @@ 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 = start.line !== end.line;
const isMultiline = startPos.line !== endPos.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 {
Expand All @@ -663,16 +682,18 @@ export class YankVisualBlockMode extends BaseOperator {

vimState.currentRegisterMode = RegisterMode.BlockWise;

this.highlightYankedRanges(vimState, ranges);

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

vimState.historyTracker.addMark(start, '<');
vimState.historyTracker.addMark(end, '>');
vimState.historyTracker.addMark(startPos, '<');
vimState.historyTracker.addMark(endPos, '>');

const numLinesYanked = toCopy.split('\n').length;
ReportLinesYanked(numLinesYanked, vimState);

await vimState.setCurrentMode(ModeName.Normal);
vimState.cursorStopPosition = start;
vimState.cursorStopPosition = startPos;
return vimState;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
IModeSpecificStrings,
IAutoSwitchInputMethod,
IDebugConfiguration,
IHighlightedYankConfiguration,
ICamelCaseMotionConfiguration,
} from './iconfiguration';

Expand Down Expand Up @@ -240,6 +241,12 @@ class Configuration implements IConfiguration {
})
searchHighlightColor: string;

highlightedyank: IHighlightedYankConfiguration = {
enable: false,
color: 'rgba(250, 240, 170, 0.5)',
duration: 200,
};

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

Expand Down
22 changes: 22 additions & 0 deletions src/configuration/iconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ export interface IDebugConfiguration {
loggingLevelForConsole: 'error' | 'warn' | 'info' | 'verbose' | 'debug';
}

export interface IHighlightedYankConfiguration {
/**
* Boolean indicating whether yank highlighting should be enabled.
*/
enable: boolean;

/**
* Color of the yank highlight.
*/
color: string;

/**
* Duration in milliseconds of the yank highlight.
*/
duration: number;
}

export interface ICamelCaseMotionConfiguration {
/**
* Enable CamelCaseMotion plugin or not
Expand Down Expand Up @@ -185,6 +202,11 @@ export interface IConfiguration {
*/
searchHighlightColor: string;

/**
* Yank highlight settings.
*/
highlightedyank: IHighlightedYankConfiguration;

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