Skip to content

Commit

Permalink
Use vscode built in support for block cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
Pär Björklund committed Jun 7, 2016
1 parent a3b2832 commit 2ce044f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
35 changes: 8 additions & 27 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,6 @@ export class ModeHandler implements vscode.Disposable {
private _configuration: Configuration;
private _vimState: VimState;

// Caret Styling
private _caretDecoration = vscode.window.createTextEditorDecorationType(
{
dark: {
// used for dark colored themes
backgroundColor: 'rgba(224, 224, 224, 0.4)',
borderColor: 'rgba(240, 240, 240, 0.8)'
},
light: {
// used for light colored themes
backgroundColor: 'rgba(32, 32, 32, 0.4)',
borderColor: 'rgba(16, 16, 16, 0.8)'
},
borderStyle: 'solid',
borderWidth: '1px'
});

private get currentModeName(): ModeName {
return this.currentMode.name;
}
Expand Down Expand Up @@ -248,6 +231,14 @@ export class ModeHandler implements vscode.Disposable {

mode.isActive = (mode.name === modeName);
}
// Draw block cursor.
// The reason we make a copy of options is because it's a
// getter/setter and it won't trigger it's event if we modify
// the object in place
const options = vscode.window.activeTextEditor.options;
options.cursorStyle = modeName === ModeName.Insert ?
vscode.TextEditorCursorStyle.Line : vscode.TextEditorCursorStyle.Block;
vscode.window.activeTextEditor.options = options;

const statusBarText = (this.currentMode.name === ModeName.Normal) ? '' : ModeName[modeName];
this.setupStatusBarItem(statusBarText ? `-- ${statusBarText.toUpperCase()} --` : '');
Expand Down Expand Up @@ -379,16 +370,6 @@ export class ModeHandler implements vscode.Disposable {
}
}

// Draw block cursor.

if (this.currentMode.name !== ModeName.Insert) {
let range = new vscode.Range(stop, stop.getRight());
vscode.window.activeTextEditor.revealRange(range, vscode.TextEditorRevealType.InCenterIfOutsideViewport);
vscode.window.activeTextEditor.setDecorations(this._caretDecoration, [range]);
} else {
vscode.window.activeTextEditor.setDecorations(this._caretDecoration, []);
}

// Draw selection (or cursor)

if (this.currentMode.name === ModeName.Visual) {
Expand Down
14 changes: 14 additions & 0 deletions test/mode/modeHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

import * as assert from 'assert';
import * as vscode from 'vscode';
import {setupWorkspace, cleanUpWorkspace} from './../testUtils';
import {ModeName} from '../../src/mode/mode';
import {ModeHandler} from '../../src/mode/modeHandler';
Expand Down Expand Up @@ -32,4 +33,17 @@ suite("Mode Handler", () => {
assert.equal(modeHandler.currentMode.Name, ModeName.Visual);
*/
});

test("Uses correct cursor style depending on mode", async () => {
const modeHandler = new ModeHandler();

modeHandler.setCurrentModeByName(ModeName.Normal);
assert.equal(vscode.window.activeTextEditor.options.cursorStyle, vscode.TextEditorCursorStyle.Block);

modeHandler.setCurrentModeByName(ModeName.Insert);
assert.equal(vscode.window.activeTextEditor.options.cursorStyle, vscode.TextEditorCursorStyle.Line);

modeHandler.setCurrentModeByName(ModeName.Visual);
assert.equal(vscode.window.activeTextEditor.options.cursorStyle, vscode.TextEditorCursorStyle.Block);
});
});

0 comments on commit 2ce044f

Please sign in to comment.