Skip to content

Commit

Permalink
feature jpotterm#4: add configuration for cursor styles
Browse files Browse the repository at this point in the history
  • Loading branch information
McDermott, Michael (CORP) authored and Achilleas Buisman committed May 19, 2020
1 parent c4dd739 commit 5d02ffa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@
"type": "string",
"default": "#F8F3AB",
"description": "Background color that flashes to show the range when yanking."
},
"simpleVim.insertModeCursorStyle": {
"type": "string",
"enum": ["line", "block", "underline", "lineThin", "blockOutline", "underlineThin"],
"default": "line",
"description": "Cursor style for editor when in INSERT mode."
},
"simpleVim.normalModeCursorStyle": {
"type": "string",
"enum": ["line", "block", "underline", "lineThin", "blockOutline", "underlineThin"],
"default": "underline",
"description": "Cursor style for editor when in NORMAL mode."
},
"simpleVim.visualModeCursorStyle": {
"type": "string",
"enum": ["line", "block", "underline", "lineThin", "blockOutline", "underlineThin"],
"default": "lineThin",
"description": "Cursor style for editor when in VISUAL mode."
},
"simpleVim.visualLineModeCursorStyle": {
"type": "string",
"enum": ["line", "block", "underline", "lineThin", "blockOutline", "underlineThin"],
"default": "lineThin",
"description": "Cursor style for editor when in VISUAL LINE mode."
}
}
}
Expand Down
36 changes: 35 additions & 1 deletion src/modes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';

import { Mode } from './modes_types';
import { Mode, CursorConfigurationStyle } from './modes_types';
import { VimState } from './vim_state_types';

export function enterInsertMode(vimState: VimState): void {
Expand Down Expand Up @@ -36,7 +36,41 @@ function setModeContext(key: string) {
});
}

const configToStyleMapping: { [key in CursorConfigurationStyle]: vscode.TextEditorCursorStyle } = {
line: vscode.TextEditorCursorStyle.Line,
block: vscode.TextEditorCursorStyle.Block,
underline: vscode.TextEditorCursorStyle.Underline,
lineThin: vscode.TextEditorCursorStyle.LineThin,
blockOutline: vscode.TextEditorCursorStyle.BlockOutline,
underlineThin: vscode.TextEditorCursorStyle.UnderlineThin,
};

function isValidCursorConfigStyle(input: any): input is CursorConfigurationStyle {
return typeof input === 'string' && Object.keys(configToStyleMapping).indexOf(input) !== -1;
}

function getCursorConfigStyle(mode: Mode) {
const simpleVimConfig = vscode.workspace.getConfiguration('simpleVim');

switch (mode) {
case Mode.Insert:
return simpleVimConfig.get('insertModeCursorStyle');
case Mode.Normal:
return simpleVimConfig.get('normalModeCursorStyle');
case Mode.Visual:
return simpleVimConfig.get('visualModeCursorStyle');
case Mode.VisualLine:
return simpleVimConfig.get('visualLineModeCursorStyle');
}
}

export function setModeCursorStyle(mode: Mode, editor: vscode.TextEditor): void {
const cursorConfigStyle = getCursorConfigStyle(mode);
if (isValidCursorConfigStyle(cursorConfigStyle)) {
editor.options.cursorStyle = configToStyleMapping[cursorConfigStyle];
return;
}

if (mode === Mode.Insert) {
editor.options.cursorStyle = vscode.TextEditorCursorStyle.Line;
} else if (mode === Mode.Normal) {
Expand Down
2 changes: 2 additions & 0 deletions src/modes_types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type CursorConfigurationStyle = 'line' | 'block' | 'underline' | 'lineThin' | 'blockOutline' | 'underlineThin';

export enum Mode {
Insert,
Normal,
Expand Down

0 comments on commit 5d02ffa

Please sign in to comment.