Skip to content

feature #4: add configuration for cursor styles #18

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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