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

Fake a Block cursor when in Normal mode #69

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 5 additions & 3 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {showCmdLine} from './src/cmd_line/main';
import * as cc from './src/cmd_line/lexer';
import ModeHandler from "./src/mode/modeHandler";
import {ModeName} from "./src/mode/mode";
import Cursor from "./src/cursor/cursor";

var modeHandler : ModeHandler;

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
modeHandler = new ModeHandler();
Cursor.blockCursor(modeHandler);

console.log('Congratulations, your extension "vim" is now active!');

Expand Down Expand Up @@ -85,17 +87,17 @@ export function activate(context: vscode.ExtensionContext) {
registerCommand(context, 'extension.vim_6', () => handleKeyEvent("6"));
registerCommand(context, 'extension.vim_7', () => handleKeyEvent("7"));
registerCommand(context, 'extension.vim_8', () => handleKeyEvent("8"));
registerCommand(context, 'extension.vim_9', () => handleKeyEvent("9"));
registerCommand(context, 'extension.vim_9', () => handleKeyEvent("9"));

registerCommand(context, 'extension.vim_$', () => handleKeyEvent("$"));
registerCommand(context, 'extension.vim_^', () => handleKeyEvent("^"));

registerCommand(context, 'extension.vim_ctrl_r', () => handleKeyEvent("ctrl+r"));
registerCommand(context, 'extension.vim_ctrl_[', () => handleKeyEvent("ctrl+["));

registerCommand(context, 'extension.vim_<', () => handleKeyEvent("<"));
registerCommand(context, 'extension.vim_>', () => handleKeyEvent(">"));

registerCommand(context, 'extension.vim_backslash', () => handleKeyEvent("\\"));
}

Expand Down
35 changes: 35 additions & 0 deletions src/cursor/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import * as _ from "lodash";
import * as vscode from "vscode";
import TextEditor from "./../textEditor";
import {ModeName} from './../mode/mode';
import ModeHandler from "./../mode/modeHandler";

const blockCursorDecoration = vscode.window.createTextEditorDecorationType({
dark: {
backgroundColor: 'rgba(224, 224, 224, 0.5)',
borderColor: 'rgba(240, 240, 240, 0.8)'
},
light: {
backgroundColor: 'rgba(32, 32, 32, 0.5)',
borderColor: 'rgba(16, 16, 16, 0.8)'
},
borderStyle: 'solid',
borderWidth: '1px'
});

export default class Cursor {
private static prevColumn: number = 0;
Expand Down Expand Up @@ -154,6 +169,26 @@ export default class Cursor {
return new vscode.Position(line, column);
}

static blockCursor(modeHandler: ModeHandler) : void {
vscode.window.onDidChangeTextEditorSelection((e) => {
if (modeHandler.currentMode.Name !== ModeName.Normal) {
return;
}
if (e.selections.length === 1) {
let sel = e.selections[0];
if (sel.start.isEqual(sel.end)) {
let range = new vscode.Range(sel.start, sel.end.translate(0, 1));
e.textEditor.setDecorations(blockCursorDecoration, [range]);
}
}
});
modeHandler.onModeChanged((mode) => {
if (mode.Name !== ModeName.Normal) {
vscode.window.activeTextEditor.setDecorations(blockCursorDecoration, []);
}
});
}

private static isLineBeginning(position : vscode.Position) : boolean {
return position.character === 0;
}
Expand Down
11 changes: 11 additions & 0 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import InsertMode from './modeInsert';
import VisualMode from './modeVisual';
import Configuration from '../configuration';

type ModeChangedHandler = (mode: Mode) => void;

export default class ModeHandler {
private modes : Mode[];
private modeChangedHandlers: ModeChangedHandler[] = [];
private statusBarItem : vscode.StatusBarItem;
configuration : Configuration;

Expand Down Expand Up @@ -39,6 +42,10 @@ export default class ModeHandler {

var statusBarText = (this.currentMode.Name === ModeName.Normal) ? '' : ModeName[modeName];
this.setupStatusBarItem(statusBarText.toUpperCase());

for (let handler of this.modeChangedHandlers) {
handler(this.currentMode);
}
}

handleKeyEvent(key : string) : void {
Expand Down Expand Up @@ -68,6 +75,10 @@ export default class ModeHandler {
this.currentMode.HandleKeyEvent(key);
}

onModeChanged(handler: (newMode: Mode) => void) : void {
this.modeChangedHandlers.push(handler);
}

private setupStatusBarItem(text : string) : void {
if (!this.statusBarItem) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
Expand Down
2 changes: 1 addition & 1 deletion test/caret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ suite("caret", () => {
let range = new vscode.Range(Caret.documentBegin(), Caret.documentEnd());
TextEditor.delete(range).then(() => done());
});

test("right on right-most column should stay at the same location", () => {
Caret.move(new vscode.Position(0, 7));

Expand Down