Port IME support from alexandrudima/vscode-vim

This commit is contained in:
Alex Dima
2016-08-16 11:05:42 +02:00
parent 4a77b9840c
commit 134de4eff9
2 changed files with 83 additions and 19 deletions

View File

@ -4,14 +4,14 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import {
TextEditorCursorStyle,
Position,
Range,
Selection,
TextEditor,
TextEditorRevealType
TextEditorRevealType,
window
} from 'vscode';
import {Words} from './words';
@ -139,6 +139,29 @@ export class Controller implements IController {
return `VIM:> ${label}` + (this._currentInput ? ` >${this._currentInput}` : ``);
}
private _isInComposition = false;
private _composingText = '';
public compositionStart(editor: TextEditor): void {
this._isInComposition = true;
this._composingText = '';
}
public compositionEnd(editor: TextEditor): Thenable<ITypeResult> {
this._isInComposition = false;
let text = this._composingText;
this._composingText = '';
if (text.length === 0) {
return Promise.resolve({
hasConsumedInput: true,
executeEditorCommand: null
});
}
return this.type(editor, text, {});
}
public type(editor: TextEditor, text: string, modifierKeys: ModifierKeys): Thenable<ITypeResult> {
if (this._currentMode !== Mode.NORMAL && this._currentMode !== Mode.REPLACE) {
return Promise.resolve({
@ -146,6 +169,15 @@ export class Controller implements IController {
executeEditorCommand: null
});
}
if (this._isInComposition) {
this._composingText += text;
return Promise.resolve({
hasConsumedInput: true,
executeEditorCommand: null
});
}
if (this._currentMode === Mode.REPLACE) {
let pos = editor.selection.active;
editor.edit((builder) => {
@ -167,6 +199,12 @@ export class Controller implements IController {
if (this._currentMode !== Mode.NORMAL && this._currentMode !== Mode.REPLACE) {
return false;
}
if (this._isInComposition) {
this._composingText = this._composingText.substr(0, this._composingText.length - replaceCharCnt) + text;
return true;
}
if (this._currentMode === Mode.REPLACE) {
let pos = editor.selection.active;
editor.edit((builder) => {
@ -175,13 +213,13 @@ export class Controller implements IController {
return true;
}
// Not supporting IME building in NORMAL mode
return true;
}
private _interpretNormalModeInput(editor: TextEditor, modifierKeys: ModifierKeys): Thenable<ITypeResult> {
if (this._currentInput.startsWith(':')) {
return vscode.window.showInputBox({value: 'tabm'}).then((value) => {
return window.showInputBox({value: 'tabm'}).then((value) => {
let result = this._findMapping(value || '', editor, modifierKeys);
return Promise.resolve(result);
});

View File

@ -40,11 +40,22 @@ export function activate(context: vscode.ExtensionContext) {
}
vimExt.replacePrevChar(args.text, args.replaceCharCnt);
});
registerCommandNice('compositionStart', function (args) {
if (!vscode.window.activeTextEditor) {
return;
}
vimExt.compositionStart();
});
registerCommandNice('compositionEnd', function (args) {
if (!vscode.window.activeTextEditor) {
return;
}
vimExt.compositionEnd();
});
registerCommandNice('vim.goToNormalMode', function (args) {
vimExt.goToNormalMode();
});
registerCommandNice('vim.clearInput', function (args) {
console.log(args);
vimExt.clearInput();
});
// registerCommandNice('paste', function(args) {
@ -143,22 +154,20 @@ class VimExt {
}
public type(text: string, modifierKeys: ModifierKeys = {ctrl: false, shifit: false, alt: false}): void {
let r = this._controller.type(vscode.window.activeTextEditor, text, modifierKeys).then(
(r) => {
if (r.hasConsumedInput) {
this._ensureState();
if (r.executeEditorCommand) {
let args = [r.executeEditorCommand.commandId];
args = args.concat(r.executeEditorCommand.args);
vscode.commands.executeCommand.apply(this, args);
}
return;
this._controller.type(vscode.window.activeTextEditor, text, modifierKeys).then((r) => {
if (r.hasConsumedInput) {
this._ensureState();
if (r.executeEditorCommand) {
let args = [r.executeEditorCommand.commandId];
args = args.concat(r.executeEditorCommand.args);
vscode.commands.executeCommand.apply(this, args);
}
vscode.commands.executeCommand('default:type', {
text: text
});
return;
}
);
vscode.commands.executeCommand('default:type', {
text: text
});
});
}
public replacePrevChar(text: string, replaceCharCnt: number): void {
@ -172,6 +181,23 @@ class VimExt {
});
}
public compositionStart(): void {
this._controller.compositionStart(vscode.window.activeTextEditor);
}
public compositionEnd(): void {
this._controller.compositionEnd(vscode.window.activeTextEditor).then((r) => {
if (r.hasConsumedInput) {
this._ensureState();
if (r.executeEditorCommand) {
let args = [r.executeEditorCommand.commandId];
args = args.concat(r.executeEditorCommand.args);
vscode.commands.executeCommand.apply(this, args);
}
}
});
}
private _ensureState(): void {
// 0. position
this._ensurePosition();