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

Commands: u and dw #38

Merged
merged 2 commits into from
Nov 26, 2015
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
2 changes: 1 addition & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as vscode from 'vscode';
import {showCmdLine} from './src/cmd_line/main';
import * as cc from './src/cmd_line/lexer';
import ModeHandler from "./src/mode/mode_handler";
import ModeHandler from "./src/mode/modeHandler";
import {ModeName} from "./src/mode/mode";

var modeHandler;
Expand Down
7 changes: 3 additions & 4 deletions src/mode/mode_handler.ts → src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as _ from 'lodash';

import * as vscode from 'vscode';

import {Mode, ModeName} from './mode';
import NormalMode from './mode_normal';
import InsertMode from './mode_insert';
import VisualMode from './mode_visual';
import NormalMode from './modeNormal';
import InsertMode from './modeInsert';
import VisualMode from './modeVisual';

export default class ModeHandler {
private modes : Mode[];
Expand Down
20 changes: 5 additions & 15 deletions src/mode/mode_insert.ts → src/mode/modeInsert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ModeName, Mode} from './mode';
import * as vscode from 'vscode';
import {ModeName, Mode} from './mode';
import TextEditor from './../textEditor';

export default class InsertMode extends Mode {
private activationKeyHandler : { [ key : string] : (position : vscode.Position) => vscode.Position; } = {};
Expand Down Expand Up @@ -39,24 +40,13 @@ export default class InsertMode extends Mode {
}

HandleActivation(key : string) : void {
const editor = vscode.window.activeTextEditor;
const currentPosition = editor.selection.active;

var newPosition = this.activationKeyHandler[key](currentPosition);
var newSelection = new vscode.Selection(newPosition, newPosition);

editor.selection = newSelection;
var newPosition = this.activationKeyHandler[key](TextEditor.GetCurrentPosition());
TextEditor.SetCurrentPosition(newPosition);
}

HandleKeyEvent(key : string) : void {
this.keyHistory.push(key);

const editor = vscode.window.activeTextEditor;
const position = editor.selection.active;

editor.edit(t => {
t.insert(position, this.ResolveKeyValue(key));
});
TextEditor.Insert(this.ResolveKeyValue(key));
}

// Some keys have names that are different to their value.
Expand Down
17 changes: 9 additions & 8 deletions src/mode/mode_normal.ts → src/mode/modeNormal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ export default class CommandMode extends Mode {
super(ModeName.Normal);

this.keyHandler = {
":" : () => { showCmdLine(); },
":" : () => { showCmdLine(); },
"u" : () => { vscode.commands.executeCommand("undo"); },
"h" : () => { vscode.commands.executeCommand("cursorLeft"); },
"j" : () => { vscode.commands.executeCommand("cursorDown"); },
"k" : () => { vscode.commands.executeCommand("cursorUp"); },
"k" : () => { vscode.commands.executeCommand("cursorUp"); },
"l" : () => { vscode.commands.executeCommand("cursorRight"); },
">>" : () => { vscode.commands.executeCommand("editor.action.indentLines"); },
"<<" : () => { vscode.commands.executeCommand("editor.action.outdentLines"); },
"dd" : () => { vscode.commands.executeCommand("editor.action.deleteLines"); }
"dd" : () => { vscode.commands.executeCommand("editor.action.deleteLines"); },
"dw" : () => { vscode.commands.executeCommand("deleteWordRight"); }
};
}

Expand All @@ -30,10 +32,11 @@ export default class CommandMode extends Mode {
}

HandleKeyEvent(key : string) : void {
this.keyHistory.push(key);

let keyHandled = false;
for (let window = 0; window <= this.keyHistory.length; window++) {
var keysPressed = key + _.takeRight(this.keyHistory, window).join('');

for (let window = 1; window <= this.keyHistory.length; window++) {
var keysPressed = _.takeRight(this.keyHistory, window).join('');
if (this.keyHandler[keysPressed] !== undefined) {
keyHandled = true;
this.keyHandler[keysPressed]();
Expand All @@ -43,8 +46,6 @@ export default class CommandMode extends Mode {

if (keyHandled) {
this.keyHistory = [];
} else {
this.keyHistory.push(key);
}
}
}
File renamed without changes.
46 changes: 46 additions & 0 deletions src/textEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as vscode from "vscode";

export default class TextEditor {
static Insert(text: string, position: vscode.Position = null) {
if (position === null) {
position = vscode.window.activeTextEditor.selection.active;
}

vscode.window.activeTextEditor.edit((editBuilder) => {
editBuilder.insert(position, text);
});
}

static Delete(range: vscode.Range) {
vscode.window.activeTextEditor.edit((editBuilder) => {
editBuilder.delete(range);
});
}

static Replace(range: vscode.Range, text: string) {
vscode.window.activeTextEditor.edit((editBuilder) => {
editBuilder.replace(range, text);
});
}

static ReadLine(lineNo: number = null): string {
if (lineNo === null) {
lineNo = vscode.window.activeTextEditor.selection.active.line;
}

if (vscode.window.activeTextEditor.document.lineCount < lineNo) {
throw new RangeError();
}

return vscode.window.activeTextEditor.document.lineAt(lineNo).text;
}

static GetCurrentPosition(): vscode.Position {
return vscode.window.activeTextEditor.selection.active;
}

static SetCurrentPosition(position: vscode.Position) {
var newSelection = new vscode.Selection(position, position);
vscode.window.activeTextEditor.selection = newSelection;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'assert';
import {ModeName} from '../../src/mode/mode';
import ModeHandler from '../../src/mode/mode_handler';
import ModeHandler from '../../src/mode/modeHandler';

suite("Mode Handler", () => {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'assert';

import ModeNormal from '../../src/mode/mode_normal';
import ModeNormal from '../../src/mode/modeNormal';
import {ModeName} from '../../src/mode/mode';

suite("Mode Normal", () => {
Expand Down