Files
vscode-extension-samples/vim-sample/src/mappings.ts

234 lines
8.5 KiB
TypeScript
Raw Normal View History

2016-07-15 12:39:15 +02:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
2016-12-30 11:17:41 +01:00
import { TextEditor } from 'vscode';
import { Motion, Motions } from './motions';
import { Operator, Operators } from './operators';
import { IController, Command, AbstractCommandDescriptor, ModifierKeys } from './common';
2016-07-15 12:39:15 +02:00
2016-08-04 18:08:09 +02:00
const CHAR_TO_BINDING: { [char: string]: any; } = {};
function defineBinding(char: string, value: any, modifierKeys: ModifierKeys): void {
let key = modifierKeys.ctrl ? 'CTRL + ' + char : char;
CHAR_TO_BINDING[key] = value;
};
function getBinding(char: string, modifierKeys: ModifierKeys): any {
let key = modifierKeys.ctrl ? 'CTRL + ' + char : char;
return CHAR_TO_BINDING[key];
};
function defineOperator(char: string, operator: Operator, modifierKeys: ModifierKeys = {}): void {
defineBinding(char + '__operator__', operator, modifierKeys);
};
function getOperator(char: string, modifierKeys: ModifierKeys = {}): Operator {
return getBinding(char + '__operator__', modifierKeys);
};
2016-07-15 12:39:15 +02:00
2016-08-04 18:08:09 +02:00
function defineCommand(char: string, commandId: string, modifierKeys: ModifierKeys = {}): void {
2016-12-30 11:17:41 +01:00
defineBinding(char + '__command__', { commandId: commandId }, modifierKeys);
2016-08-04 18:08:09 +02:00
};
function getCommand(char: string, modifierKeys: ModifierKeys = {}): Command {
return getBinding(char + '__command__', modifierKeys);
2016-07-15 12:39:15 +02:00
};
2016-07-19 12:57:26 +02:00
2016-08-04 18:08:09 +02:00
function defineMotion(char: string, motion: Motion, modifierKeys: ModifierKeys = {}): void {
defineBinding(char + '__motion__', motion, modifierKeys);
2016-07-19 12:57:26 +02:00
};
2016-08-04 18:08:09 +02:00
function getMotion(char: string, modifierKeys: ModifierKeys = {}): Motion {
return getBinding(char + '__motion__', modifierKeys);
};
function defineMotionCommand(char: string, motionCommand: AbstractCommandDescriptor, modifierKeys: ModifierKeys = {}): void {
defineBinding(char + '__motioncommand__', motionCommand, modifierKeys);
};
function getMotionCommand(char: string, modifierKeys: ModifierKeys = {}): AbstractCommandDescriptor {
return getBinding(char + '__motioncommand__', modifierKeys);
};
// Operators
defineOperator('x', Operators.DeleteCharUnderCursor);
defineOperator('i', Operators.Insert);
defineOperator('a', Operators.Append);
defineOperator('A', Operators.AppendEndOfLine);
defineOperator('d', Operators.DeleteTo);
defineOperator('p', Operators.Put);
defineOperator('r', Operators.Replace);
defineOperator('R', Operators.ReplaceMode);
defineOperator('c', Operators.Change);
defineOperator('v', Operators.Visual);
// Commands
defineCommand('u', 'undo');
defineCommand('U', 'undo');
2016-07-19 12:57:26 +02:00
// Left-right motions
2016-07-21 21:13:00 +02:00
defineMotionCommand('h', Motions.Left);
defineMotionCommand('l', Motions.Right);
2016-07-19 12:57:26 +02:00
defineMotion('0', Motions.StartOfLine);
defineMotion('$', Motions.EndOfLine);
2016-07-21 21:13:00 +02:00
defineMotionCommand('g0', Motions.WrappedLineStart);
defineMotionCommand('g^', Motions.WrappedLineFirstNonWhiteSpaceCharacter);
defineMotionCommand('gm', Motions.WrappedLineColumnCenter);
defineMotionCommand('g$', Motions.WrappedLineEnd);
2016-07-27 11:05:23 +02:00
defineMotionCommand('g_', Motions.WrappedLineLastNonWhiteSpaceCharacter);
2016-07-21 21:13:00 +02:00
2016-08-04 18:08:09 +02:00
// Cursor scroll motions
defineMotionCommand('zh', Motions.CursorScrollLeft);
defineMotionCommand('zl', Motions.CursorScrollRight);
defineMotionCommand('zH', Motions.CursorScrollLeftByHalfLine);
defineMotionCommand('zL', Motions.CursorScrollRightByHalfLine);
2016-07-19 12:57:26 +02:00
// Up-down motions
defineMotionCommand('j', Motions.Down);
defineMotionCommand('k', Motions.Up);
2016-07-21 21:13:00 +02:00
defineMotionCommand('gj', Motions.WrappedLineDown);
defineMotionCommand('gk', Motions.WrappedLineUp);
2016-07-15 12:39:15 +02:00
defineMotion('G', Motions.GoToLine);
defineMotion('gg', Motions.GoToFirstLine);
2016-07-21 21:13:00 +02:00
defineMotionCommand('H', Motions.ViewPortTop);
defineMotionCommand('M', Motions.ViewPortCenter);
defineMotionCommand('L', Motions.ViewPortBottom);
2016-07-19 12:57:26 +02:00
// Word motions
defineMotion('w', Motions.NextWordStart);
defineMotion('e', Motions.NextWordEnd);
2016-07-15 12:39:15 +02:00
2016-07-25 17:42:04 +02:00
// Tab motions
2016-07-25 16:59:01 +02:00
defineMotionCommand('tabm', Motions.MoveActiveEditor);
defineMotionCommand('tabm<', Motions.MoveActiveEditorLeft);
defineMotionCommand('tabm>', Motions.MoveActiveEditorRight);
defineMotionCommand('tabm<<', Motions.MoveActiveEditorFirst);
defineMotionCommand('tabm>>', Motions.MoveActiveEditorLast);
defineMotionCommand('tabm.', Motions.MoveActiveEditorCenter);
2016-07-15 12:39:15 +02:00
2016-08-04 18:08:09 +02:00
// Scroll motions
2016-12-30 11:17:41 +01:00
defineMotionCommand('e', Motions.ScrollDownByLine, { ctrl: true });
defineMotionCommand('d', Motions.ScrollDownByHalfPage, { ctrl: true });
defineMotionCommand('f', Motions.ScrollDownByPage, { ctrl: true });
defineMotionCommand('y', Motions.ScrollUpByLine, { ctrl: true });
defineMotionCommand('u', Motions.ScrollUpByHalfPage, { ctrl: true });
defineMotionCommand('b', Motions.ScrollUpByPage, { ctrl: true });
defineMotionCommand('zt', Motions.RevealCurrentLineAtTop);
defineMotionCommand('zz', Motions.RevealCurrentLineAtCenter);
defineMotionCommand('zb', Motions.RevealCurrentLineAtBottom);
2016-07-15 12:39:15 +02:00
2016-08-29 19:37:16 +02:00
// Folding
defineMotionCommand('zc', Motions.FoldUnder);
defineMotionCommand('zo', Motions.UnfoldUnder);
2016-07-15 12:39:15 +02:00
export interface IFoundOperator {
2016-12-30 11:17:41 +01:00
runNormal(controller: IController, editor: TextEditor): boolean;
runVisual(controller: IController, editor: TextEditor): boolean;
2016-07-15 12:39:15 +02:00
}
export class Mappings {
public static findMotion(input: string): Motion {
let parsed = _parseNumberAndString(input);
2016-08-04 18:08:09 +02:00
let motion = getMotion(parsed.input.substr(0, 1));
2016-07-15 12:39:15 +02:00
if (!motion) {
2016-08-04 18:08:09 +02:00
motion = getMotion(parsed.input.substr(0, 2));
2016-07-15 12:39:15 +02:00
if (!motion) {
return null;
}
}
return motion.repeat(parsed.hasRepeatCount, parsed.repeatCount);
}
2016-08-04 18:08:09 +02:00
public static findMotionCommand(input: string, isVisual: boolean, modifierKeys: ModifierKeys): Command {
2016-07-15 12:39:15 +02:00
let parsed = _parseNumberAndString(input);
2016-08-04 18:08:09 +02:00
let command = Mappings.findMotionCommandFromNumberAndString(parsed, isVisual, modifierKeys);
2016-07-25 17:42:04 +02:00
if (!command) {
parsed = _parseNumberAndString(input, false);
2016-12-30 11:17:41 +01:00
command = Mappings.findMotionCommandFromNumberAndString(parsed, isVisual, modifierKeys);
2016-07-25 17:42:04 +02:00
}
return command;
}
2016-08-04 18:08:09 +02:00
private static findMotionCommandFromNumberAndString(numberAndString: INumberAndString, isVisual: boolean, modifierKeys: ModifierKeys): Command {
let motionCommand = getMotionCommand(numberAndString.input.substr(0, 1), modifierKeys);
2016-07-15 12:39:15 +02:00
if (!motionCommand) {
2016-08-04 18:08:09 +02:00
motionCommand = getMotionCommand(numberAndString.input.substr(0, 2), modifierKeys);
2016-07-15 12:39:15 +02:00
}
2016-07-19 12:57:26 +02:00
if (!motionCommand) {
2016-08-04 18:08:09 +02:00
motionCommand = getMotionCommand(numberAndString.input.substr(1, 2), modifierKeys);
2016-07-19 12:57:26 +02:00
}
if (!motionCommand) {
2016-08-04 18:08:09 +02:00
motionCommand = getMotionCommand(numberAndString.input.substr(1, 3), modifierKeys);
2016-07-19 12:57:26 +02:00
}
2016-07-25 16:59:01 +02:00
if (!motionCommand) {
2016-08-04 18:08:09 +02:00
motionCommand = getMotionCommand(numberAndString.input, modifierKeys);
2016-07-25 16:59:01 +02:00
}
2016-12-30 11:17:41 +01:00
return motionCommand ? motionCommand.createCommand({ isVisual: isVisual, repeat: numberAndString.hasRepeatCount ? numberAndString.repeatCount : undefined }) : null;
2016-07-15 12:39:15 +02:00
}
2016-08-04 18:08:09 +02:00
public static findOperator(input: string, modifierKeys: ModifierKeys): IFoundOperator {
2016-07-15 12:39:15 +02:00
let parsed = _parseNumberAndString(input);
2016-08-04 18:08:09 +02:00
let operator = getOperator(parsed.input.substr(0, 1), modifierKeys);
2016-07-15 12:39:15 +02:00
if (!operator) {
return null;
}
let operatorArgs = parsed.input.substr(1);
return {
2016-07-25 16:59:01 +02:00
runNormal: (controller: IController, editor: TextEditor) => {
2016-07-15 12:39:15 +02:00
return operator.runNormalMode(controller, editor, parsed.repeatCount, operatorArgs);
},
2016-07-25 16:59:01 +02:00
runVisual: (controller: IController, editor: TextEditor) => {
2016-07-15 12:39:15 +02:00
return operator.runVisualMode(controller, editor, operatorArgs);
}
};
}
2016-08-04 18:08:09 +02:00
public static findCommand(input: string, modifierKeys: ModifierKeys): Command {
return getCommand(input, modifierKeys) || null;
2016-07-15 12:39:15 +02:00
}
public static isMotionPrefix(input: string): boolean {
if (input.length === 0) {
return true;
}
2016-07-21 21:13:00 +02:00
if (input === 'g' || input === 'v' || input === 'z') {
2016-07-15 12:39:15 +02:00
return true;
}
2016-07-21 21:13:00 +02:00
return /^[1-9]\d*v?g?z?$/.test(input);
2016-07-15 12:39:15 +02:00
}
}
2016-07-25 17:42:04 +02:00
function _parseNumberAndString(input: string, numberAtBeginning: boolean = true): INumberAndString {
if (numberAtBeginning) {
let repeatCountMatch = input.match(/^([1-9]\d*)/);
if (repeatCountMatch) {
return {
hasRepeatCount: true,
repeatCount: parseInt(repeatCountMatch[0], 10),
input: input.substr(repeatCountMatch[0].length)
};
}
} else {
let repeatCountMatch = input.match(/(\d+)$/);
if (repeatCountMatch) {
return {
hasRepeatCount: true,
repeatCount: parseInt(repeatCountMatch[1], 10),
input: input.substr(0, input.length - repeatCountMatch[1].length)
};
}
2016-07-25 16:59:01 +02:00
}
2016-07-15 12:39:15 +02:00
return {
hasRepeatCount: false,
repeatCount: 1,
input: input
}
}
interface INumberAndString {
hasRepeatCount: boolean;
repeatCount: number;
input: string;
}