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

Toggle Vim #1364

Closed
wants to merge 2 commits 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
67 changes: 62 additions & 5 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.window.onDidChangeActiveTextEditor(handleActiveEditorChange, this);

vscode.workspace.onDidChangeTextDocument((event) => {
if (!Globals.active) {
return;
}

/**
* Change from vscode editor should set document.isDirty to true but they initially don't!
* There is a timing issue in vscode codebase between when the isDirty flag is set and
Expand Down Expand Up @@ -158,7 +162,7 @@ export async function activate(context: vscode.ExtensionContext) {
}, 0);
});

registerCommand(context, 'type', async (args) => {
overrideCommand(context, 'type', async (args) => {
taskQueue.enqueueTask({
promise: async () => {
const mh = await getAndUpdateModeHandler();
Expand All @@ -173,7 +177,7 @@ export async function activate(context: vscode.ExtensionContext) {
});
});

registerCommand(context, 'replacePreviousChar', async (args) => {
overrideCommand(context, 'replacePreviousChar', async (args) => {
taskQueue.enqueueTask({
promise: async () => {
const mh = await getAndUpdateModeHandler();
Expand All @@ -193,7 +197,7 @@ export async function activate(context: vscode.ExtensionContext) {
});
});

registerCommand(context, 'compositionStart', async (args) => {
overrideCommand(context, 'compositionStart', async (args) => {
taskQueue.enqueueTask({
promise: async () => {
const mh = await getAndUpdateModeHandler();
Expand All @@ -203,7 +207,7 @@ export async function activate(context: vscode.ExtensionContext) {
});
});

registerCommand(context, 'compositionEnd', async (args) => {
overrideCommand(context, 'compositionEnd', async (args) => {
taskQueue.enqueueTask({
promise: async () => {
const mh = await getAndUpdateModeHandler();
Expand Down Expand Up @@ -238,15 +242,54 @@ export async function activate(context: vscode.ExtensionContext) {
registerCommand(context, keybinding.command, () => handleKeyEvent(`${ bracketedKey }`));
}

registerCommand(context, 'toggleVim', async () => {
Globals.active = !Globals.active;
if (Globals.active) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine somewhere in here we want to do this for each mh we currently have?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or does having it just here impact all mh correctly already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

await vscode.commands.executeCommand('setContext', 'vim.active', Globals.active);
compositionState = new CompositionState();
modeHandlerToEditorIdentity = {};
let mh = await getAndUpdateModeHandler();
mh.updateView(mh.vimState, { drawSelection: false, revealRange: false });
} else {
let cursorStyle = await vscode.workspace.getConfiguration('editor').get('cursorStyle', 'line');
vscode.window.visibleTextEditors.forEach(editor => {
let options = editor.options;
switch (cursorStyle) {
case 'line':
options.cursorStyle = vscode.TextEditorCursorStyle.Line;
break;
case 'block':
options.cursorStyle = vscode.TextEditorCursorStyle.Block;
break;
case 'underline':
options.cursorStyle = vscode.TextEditorCursorStyle.Underline;
break;

default:
break;
}
editor.options = options;
});
await vscode.commands.executeCommand('setContext', 'vim.active', Globals.active);
}
});

vscode.commands.executeCommand('setContext', 'vim.active', Globals.active);

// Initialize mode handler for current active Text Editor at startup.
if (vscode.window.activeTextEditor) {
let mh = await getAndUpdateModeHandler();
mh.updateView(mh.vimState, {drawSelection: false, revealRange: false});
}
}

function registerCommand(context: vscode.ExtensionContext, command: string, callback: (...args: any[]) => any) {
function overrideCommand(context: vscode.ExtensionContext, command: string, callback: (...args: any[]) => any) {
let disposable = vscode.commands.registerCommand(command, async (args) => {
if (!Globals.active) {
await vscode.commands.executeCommand("default:" + command, args);
return;
}

if (!vscode.window.activeTextEditor) {
return;
}
Expand All @@ -261,6 +304,17 @@ function registerCommand(context: vscode.ExtensionContext, command: string, call
context.subscriptions.push(disposable);
}

function registerCommand(context: vscode.ExtensionContext, command: string, callback: (...args: any[]) => any) {
let disposable = vscode.commands.registerCommand(command, async (args) => {
if (!vscode.window.activeTextEditor) {
return;
}

callback(args);
});
context.subscriptions.push(disposable);
}

async function handleKeyEvent(key: string): Promise<void> {
const mh = await getAndUpdateModeHandler();

Expand All @@ -280,6 +334,9 @@ function handleContentChangedFromDisk(document : vscode.TextDocument) : void {
}

async function handleActiveEditorChange(): Promise<void> {
if (!Globals.active) {
return;
}

// Don't run this event handler during testing
if (Globals.isTesting) {
Expand Down
88 changes: 46 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,222 +39,226 @@
{
"command": "extension.showCmdLine",
"title": "Vim: Show Command Line"
},
{
"command": "toggleVim",
"title": "Vim: Toggle Vim Mode"
}
],
"keybindings": [
{
"key": "Escape",
"command": "extension.vim_escape",
"when": "editorTextFocus && !inDebugRepl"
"when": "editorTextFocus && vim.active && !inDebugRepl"
},
{
"key": "Home",
"command": "extension.vim_home",
"when": "editorTextFocus && !inDebugRepl && vim.mode != 'Insert'"
"when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode != 'Insert'"
},
{
"key": "End",
"command": "extension.vim_end",
"when": "editorTextFocus && !inDebugRepl && vim.mode != 'Insert'"
"when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode != 'Insert'"
},
{
"key": "cmd+left",
"command": "extension.vim_cmd+left",
"when": "editorTextFocus && !inDebugRepl && vim.mode != 'Insert'"
"when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode != 'Insert'"
},
{
"key": "cmd+right",
"command": "extension.vim_cmd+right",
"when": "editorTextFocus && !inDebugRepl && vim.mode != 'Insert'"
"when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode != 'Insert'"
},
{
"key": "cmd+d",
"command": "extension.vim_cmd+d",
"when": "editorTextFocus && !inDebugRepl"
"when": "editorTextFocus && vim.active && !inDebugRepl"
},
{
"key": "cmd+a",
"command": "extension.vim_cmd+a",
"when": "editorTextFocus && !inDebugRepl && vim.mode != 'Insert'"
"when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode != 'Insert'"
},
{
"key": "ctrl+d",
"command": "extension.vim_ctrl+d",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+alt+down",
"linux": "shift+alt+down",
"mac": "cmd+alt+down",
"command": "extension.vim_cmd+alt+down",
"when": "editorTextFocus && !inDebugRepl"
"when": "editorTextFocus && vim.active && !inDebugRepl"
},
{
"key": "ctrl+alt+up",
"linux": "shift+alt+up",
"mac": "cmd+alt+up",
"command": "extension.vim_cmd+alt+up",
"when": "editorTextFocus && !inDebugRepl"
"when": "editorTextFocus && vim.active && !inDebugRepl"
},
{
"key": "Backspace",
"command": "extension.vim_backspace",
"when": "editorTextFocus && !inDebugRepl"
"when": "editorTextFocus && vim.active && !inDebugRepl"
},
{
"key": "shift+backspace",
"command": "extension.vim_shift+backspace",
"when": "editorTextFocus && vim.mode == 'SearchInProgressMode' && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.mode == 'SearchInProgressMode' && !inDebugRepl"
},
{
"key": "Delete",
"command": "extension.vim_delete",
"when": "editorTextFocus && vim.mode == 'Normal' && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.mode == 'Normal' && !inDebugRepl"
},
{
"key": "tab",
"command": "extension.vim_tab",
"when": "editorFocus && vim.mode == 'Normal' && !inDebugRepl"
"when": "editorFocus && vim.active && vim.mode == 'Normal' && !inDebugRepl"
},
{
"key": "ctrl+r",
"command": "extension.vim_ctrl+r",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+f",
"command": "extension.vim_ctrl+f",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+b",
"command": "extension.vim_ctrl+b",
"when": "editorTextFocus && vim.useCtrlKeys && vim.mode != 'Insert' && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && vim.mode != 'Insert' && !inDebugRepl"
},
{
"key": "ctrl+j",
"command": "extension.vim_ctrl+j",
"when": "editorTextFocus && vim.useCtrlKeys && vim.mode != 'Insert' && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && vim.mode != 'Insert' && !inDebugRepl"
},
{
"key": "ctrl+k",
"command": "extension.vim_ctrl+k",
"when": "editorTextFocus && vim.useCtrlKeys && vim.mode != 'Insert' && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && vim.mode != 'Insert' && !inDebugRepl"
},
{
"key": "ctrl+h",
"command": "extension.vim_ctrl+h",
"when": "editorTextFocus && vim.useCtrlKeys && vim.mode == 'Insert' && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && vim.mode == 'Insert' && !inDebugRepl"
},
{
"key": "ctrl+e",
"command": "extension.vim_ctrl+e",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+y",
"command": "extension.vim_ctrl+y",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+u",
"command": "extension.vim_ctrl+u",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+v",
"command": "extension.vim_ctrl+v",
"when": "editorTextFocus && vim.mode != 'Insert' && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.mode != 'Insert' && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "cmd+v",
"command": "extension.vim_cmd+v",
"when": "editorTextFocus && vim.mode == 'SearchInProgressMode' && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.mode == 'SearchInProgressMode' && !inDebugRepl"
},
{
"key": "ctrl+[",
"command": "extension.vim_ctrl+[",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+w",
"command": "extension.vim_ctrl+w",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+c",
"command": "extension.vim_ctrl+c",
"when": "editorTextFocus && !inDebugRepl && vim.overrideCtrlC"
"when": "editorTextFocus && vim.active && !inDebugRepl && vim.overrideCtrlC"
},
{
"key": "cmd+c",
"command": "extension.vim_cmd+c",
"when": "editorTextFocus && vim.overrideCopy && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.overrideCopy && !inDebugRepl"
},
{
"key": "ctrl+a",
"command": "extension.vim_ctrl+a",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+o",
"command": "extension.vim_ctrl+o",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+n",
"command": "extension.vim_ctrl+n",
"when": "suggestWidgetVisible && vim.useCtrlKeys"
"when": "suggestWidgetVisible && vim.active && vim.useCtrlKeys"
},
{
"key": "ctrl+p",
"command": "extension.vim_ctrl+p",
"when": "suggestWidgetVisible && vim.useCtrlKeys"
"when": "suggestWidgetVisible && vim.active && vim.useCtrlKeys"
},
{
"key": "ctrl+x",
"command": "extension.vim_ctrl+x",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+shift+2",
"command": "extension.vim_ctrl+shift+2",
"when": "editorTextFocus && vim.useCtrlKeys"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys"
},
{
"key": "ctrl+t",
"command": "extension.vim_ctrl+t",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+pagedown",
"command": "extension.vim_ctrl+pagedown",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+pageup",
"command": "extension.vim_ctrl+pageup",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
"when": "editorTextFocus && vim.active && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "left",
"command": "extension.vim_left",
"when": "editorTextFocus && !inDebugRepl"
"when": "editorTextFocus && vim.active && !inDebugRepl"
},
{
"key": "right",
"command": "extension.vim_right",
"when": "editorTextFocus && !inDebugRepl"
"when": "editorTextFocus && vim.active && !inDebugRepl"
},
{
"key": "up",
"command": "extension.vim_up",
"when": "editorTextFocus && !inDebugRepl && !suggestWidgetVisible && !suggestWidgetMultipleSuggestions"
"when": "editorTextFocus && vim.active && !inDebugRepl && !suggestWidgetVisible && !suggestWidgetMultipleSuggestions"
},
{
"key": "down",
"command": "extension.vim_down",
"when": "editorTextFocus && !inDebugRepl && !suggestWidgetVisible && !suggestWidgetMultipleSuggestions"
"when": "editorTextFocus && vim.active && !inDebugRepl && !suggestWidgetVisible && !suggestWidgetMultipleSuggestions"
}
],
"configuration": {
Expand Down
Loading