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

Refactor CommandKeyMap #228

Merged
merged 1 commit into from
May 20, 2016
Merged
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
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,21 @@ _Note_: Currently, by defining keybindings for a mode, all bindings for that mod
Example:
```json
{
"vim.normalModeKeybindings": {
"vim.normalModeKeyBindings": {
"d": "DeleteChar",
"D": "DeleteLastChar"
},
"vim.insertModeKeybindings": {
"vim.insertModeKeyBindings": {
"e": "InsertAtCursor",
"E": "InsertAfterCursor"
}
}
```

Supported Actions
* vim.normalModeKeyBindings
* Supported Actions:

* vim.normalModeKeybindings

```
```
MoveUp
MoveDown
MoveLeft
Expand Down Expand Up @@ -70,7 +69,13 @@ Supported Actions

// Find
Find


// Folding
Fold
Unfold
FoldAll
UnfoldAll

// Text Modification
Undo
Redo
Expand Down Expand Up @@ -103,9 +108,9 @@ Supported Actions
ExitMessages
```

* vim.insertModeKeybindings

```
* vim.insertModeKeyBindings
* Supported Actions:
```
// Enter insert mode
InsertAtCursor
InsertAtLineBegin
Expand All @@ -115,8 +120,8 @@ Supported Actions
InsertNewLineAbove
```

* vim.visualModeKeybindings

* vim.visualModeKeyBindings
* Supported Actions:
```
EnterVisualMode
```
Expand Down
2 changes: 1 addition & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function activate(context: vscode.ExtensionContext) {
}

console.log(args.text);
console.log(args);

var isHandled = await handleKeyEvent(args.text);

if (!isHandled) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@
"type": "string",
"description": "Keyboard layout to use to translated key presses."
},
"vim.normalModeKeybindings": {
"vim.normalModeKeyBindings": {
"type": "object",
"description": "Keybinding overrides to use for normal mode."
},
"vim.insertModeKeybindings": {
"vim.insertModeKeyBindings": {
"type": "object",
"description": "Keybinding overrides to use for insert mode."
},
"vim.visualModeKeybindings": {
"vim.visualModeKeyBindings": {
"type": "object",
"description": "Keybinding overrides to use for visual mode."
}
Expand Down
17 changes: 0 additions & 17 deletions src/configuration.ts

This file was deleted.

205 changes: 205 additions & 0 deletions src/configuration/commandKeyMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import * as vscode from 'vscode';

export enum Command {
// Enter insert mode
InsertAtCursor = 1,
InsertAtLineBegin,
InsertAfterCursor,
InsertAtLineEnd,
InsertNewLineBelow,
InsertNewLineAbove,

// Movement
MoveUp,
MoveDown,
MoveLeft,
MoveRight,

MoveLineBegin,
MoveLineEnd,
MoveWordBegin,
MoveWordEnd,
MoveFullWordBegin,
MoveFullWordEnd,
MoveLastWord,
MoveLastFullWord,
MoveLastWordEnd,
MoveLastFullWordEnd,

// MoveHalfPageUp,
// MoveHalfPageDown,
MoveFullPageUp,
MoveFullPageDown,
// MoveFirstLine,
// MoveLastLine,

MoveParagraphBegin,
MoveParagraphEnd,

MoveNonBlank,
MoveNonBlankFirst,
MoveNonBlankLast,
MoveMatchingBracket,

// Find
Find,

// Folding
Fold,
Unfold,
FoldAll,
UnfoldAll,

// Text Modification
Undo,
Redo,
Copy,
Paste,

ChangeWord,
ChangeFullWord,
ChangeCurrentWord,
ChangeCurrentWordToNext,
ChangeToLineEnd,

DeleteLine,
DeleteToNextWord,
DeleteToFullNextWord,
DeleteToWordEnd,
DeleteToFullWordEnd,
DeleteToWordBegin,
DeleteToFullWordBegin,
DeleteToLineEnd,

DeleteChar,
DeleteLastChar,

Indent,
Outdent,

// Misc
EnterVisualMode,
EnterCommand,
ExitMessages,
}

export type CommandKeyHandler = {[key: string]: Command};

export class CommandKeyMap {

public normalModeKeyMap : CommandKeyHandler;
public insertModeKeyMap : CommandKeyHandler;
public visualModeKeyMap : CommandKeyHandler;

constructor(
normalModeKeyMap : CommandKeyHandler,
insertModeKeyMap : CommandKeyHandler,
visualModeKeyMap : CommandKeyHandler) {
this.normalModeKeyMap = normalModeKeyMap;
this.insertModeKeyMap = insertModeKeyMap;
this.visualModeKeyMap = visualModeKeyMap;
}

static fromUserConfiguration() : CommandKeyMap {
let getConfig = function(keyHandlers : CommandKeyHandler, configName : string) : CommandKeyHandler {
let overrides = vscode.workspace.getConfiguration("vim")
.get(configName, keyHandlers);

// merge
for (let key in overrides) {
if (overrides.hasOwnProperty(key)) {
keyHandlers[key] = overrides[key];
}
}
return keyHandlers;
};

let normalMode = getConfig(CommandKeyMap.DefaultNormalKeyMap(), "normalModeKeyBindings");
let insertMode = getConfig(CommandKeyMap.DefaultInsertKeyMap(), "insertModeKeyBindings");
let visualMode = getConfig(CommandKeyMap.DefaultVisualKeyMap(), "visualModeKeyBindings");

return new CommandKeyMap(normalMode, insertMode, visualMode);
}

static DefaultNormalKeyMap() : CommandKeyHandler {
return {
"h": Command.MoveLeft,
"j": Command.MoveDown,
"k": Command.MoveUp,
"l": Command.MoveRight,
"0": Command.MoveLineBegin,
"$": Command.MoveLineEnd,

"^": Command.MoveNonBlank,
"gg": Command.MoveNonBlankFirst,
"G": Command.MoveNonBlankLast,

"w": Command.MoveWordBegin,
"W": Command.MoveFullWordBegin,
"e": Command.MoveWordEnd,
"E": Command.MoveLastFullWordEnd,
"ge": Command.MoveLastWordEnd,
"gE": Command.MoveLastFullWordEnd,
"b": Command.MoveLastWord,
"B": Command.MoveLastFullWord,

"{": Command.MoveParagraphBegin,
"}": Command.MoveParagraphEnd,
"%": Command.MoveMatchingBracket,

">>": Command.Indent,
"<<": Command.Outdent,

"u": Command.Undo,
"ctrl+r": Command.Redo,
"yy": Command.Copy,
"p": Command.Paste,

"cw": Command.ChangeWord,
"cW": Command.ChangeFullWord,
"ciw": Command.ChangeCurrentWord,
"caw": Command.ChangeCurrentWordToNext,
"C": Command.ChangeToLineEnd,

"dd": Command.DeleteLine,
"dw": Command.DeleteToNextWord,
"dW": Command.DeleteToFullNextWord,
"db": Command.DeleteToWordBegin,
"dB": Command.DeleteToFullWordBegin,
"de": Command.DeleteToWordEnd,
"dE": Command.DeleteToFullWordEnd,
"D" : Command.DeleteToLineEnd,

"x": Command.DeleteChar,
"X": Command.DeleteLastChar,

"/": Command.Find,

"zc": Command.Fold,
"zo": Command.Unfold,
"zC": Command.FoldAll,
"zO": Command.UnfoldAll,

":": Command.EnterCommand,
"v": Command.EnterVisualMode,
"esc": Command.ExitMessages
};
}

static DefaultInsertKeyMap() : CommandKeyHandler {
return {
"i": Command.InsertAtCursor,
"I": Command.InsertAtLineBegin,
"a": Command.InsertAfterCursor,
"A": Command.InsertAtLineEnd,
"o": Command.InsertNewLineBelow,
"O": Command.InsertNewLineAbove,
};
}

static DefaultVisualKeyMap() : CommandKeyHandler {
return {
"v": Command.EnterVisualMode
};
}
}
22 changes: 22 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

import { CommandKeyMap } from './commandKeyMap';
import { KeyboardLayout } from './keyboard';

export class Configuration {

keyboardLayout : KeyboardLayout;
commandKeyMap : CommandKeyMap;

constructor(keyboard : KeyboardLayout, keyMap : CommandKeyMap) {
this.keyboardLayout = keyboard;
this.commandKeyMap = keyMap;
}

static fromUserFile() {
return new Configuration(
KeyboardLayout.fromUserConfiguration(),
CommandKeyMap.fromUserConfiguration()
);
}
}
Loading