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

Add ex mode #3

Merged
merged 21 commits into from
Nov 16, 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
13 changes: 11 additions & 2 deletions extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// The module 'vscode' contains the VS Code extensibility API
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

import * as vscode from 'vscode';

import {showCmdLine} from './src/cmd_line/main';
import * as cc from './src/cmd_line/lexer';

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
Expand All @@ -19,6 +23,11 @@ export function activate(context: vscode.ExtensionContext) {
// Display a message box to the user
vscode.window.showInformationMessage('Hello World!');
});

var cmdLineDisposable = vscode.commands.registerCommand('extension.showCmdLine', () => {
showCmdLine();
});

context.subscriptions.push(disposable);
context.subscriptions.push(cmdLineDisposable);
}
35 changes: 10 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,20 @@
"Others"
],
"activationEvents": [
"onCommand:extension.sayHello"
"*"
],
"main": "./out/extension",
"contributes": {
"commands": [{
"command": "extension.sayHello",
"title": "Hello World"
}],
"commands": [
{ "command": "extension.sayHello", "title": "Hello World" },
{ "command": "extension.showCmdLine", "title": "Vim: Show Command Line" }
],
"keybindings": [
{
"key": "Ctrl+H",
"command": "cursorLeft",
"when": "editorTextFocus"
},
{
"key": "Ctrl+J",
"command": "cursorDown",
"when": "editorTextFocus"
},
{
"key": "Ctrl+K",
"command": "cursorUp",
"when": "editorTextFocus"
},
{
"key": "Ctrl+L",
"command": "cursorRight",
"when": "editorTextFocus"
}
{ "key": "Ctrl+H", "command": "cursorLeft", "when": "editorTextFocus" },
{ "key": "Ctrl+J", "command": "cursorDown", "when": "editorTextFocus" },
{ "key": "Ctrl+K", "command": "cursorUp", "when": "editorTextFocus" },
{ "key": "Ctrl+L", "command": "cursorRight", "when": "editorTextFocus" },
{ "key": "Ctrl+Shift+.", "command": "extension.showCmdLine", "when": "editorTextFocus" }
]
},
"scripts": {
Expand Down
23 changes: 23 additions & 0 deletions src/cmd_line/command_node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as vscode from 'vscode';
import * as token from './token';
import * as node from './node';
import * as lexer from './lexer';
import * as util from '../util';

export class WriteCommand implements node.CommandBase {
name : string;
shortName : string;
args : Object;

constructor(args : Object = null) {
// TODO: implement other arguments.
this.name = 'write';
this.shortName = 'w';
this.args = args;
}

runOn(textEditor : vscode.TextEditor) : void {
if (this.args || !textEditor.document.fileName) util.showInfo("Not implemented.");
textEditor.document.save();
}
}
171 changes: 171 additions & 0 deletions src/cmd_line/lexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import {State} from './lexer_state';
import * as token from './token';

interface ScanFunction {
(state: State, tokens: token.Token[]) : ScanFunction;
}

export function scan(input : string) : token.Token[] {
var state = new State(input);
var tokens : token.Token[] = [];
var f : ScanFunction = scanRange; // first scanning function
while (f) {
// Each scanning function returns the next scanning function or null.
f = f(state, tokens);
}
return tokens;
}

function scanRange(state : State, tokens : token.Token[]): ScanFunction {
while (true) {
if (state.isAtEof) {
break;
}
var c = state.next();
switch (c) {
case ',':
tokens.push(new token.TokenComma());
state.ignore();
continue;
case '%':
tokens.push(new token.TokenPercent());
state.ignore();
continue;
case '$':
tokens.push(new token.TokenDollar());
state.ignore();
continue;
case '.':
tokens.push(new token.TokenDot());
state.ignore();
continue;
case '/':
return scanForwardSearch;
case '?':
return scanReverseSearch
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return scanLineRef;
case '+':
tokens.push(new token.TokenPlus());
state.ignore();
continue;
case '-':
tokens.push(new token.TokenMinus());
state.ignore();
continue;
default:
state.backup();
return scanCommand;
}
}
return null;
}

function scanLineRef(state : State, tokens : token.Token[]): ScanFunction {
while (true) {
if (state.isAtEof) {
var emitted = state.emit();
if (emitted) tokens.push(new token.TokenLineNumber(emitted));
return null;
}
var c = state.next();
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
continue;
default:
state.backup();
var emitted = state.emit();
if (emitted) tokens.push(new token.TokenLineNumber(emitted));
return scanRange;
}
}
return null;
}

function scanCommand(state : State, tokens : token.Token[]): ScanFunction {
state.skipWhiteSpace();
while (true) {
if (state.isAtEof) {
var emitted = state.emit();
if (emitted) tokens.push(new token.TokenCommandName(emitted));
break;
}
var c = state.next();
var lc = c.toLowerCase();
if (lc >= 'a' && lc <= 'z') {
continue;
}
else {
state.backup();
tokens.push(new token.TokenCommandName(state.emit()));
state.skipWhiteSpace();
while (!state.isAtEof) state.next();
var args = state.emit();
if (args) tokens.push(new token.TokenCommandArgs(args));
break;
}
}
return null;
}

function scanForwardSearch(state : State, tokens : token.Token[]): ScanFunction {
state.skip('/');
var escaping : boolean;
var searchTerm = '';
while(!state.isAtEof) {
var c = state.next();
if (c == '/' && !escaping) break;
if (c == '\\') {
escaping = true;
continue;
}
else {
escaping = false;
}
searchTerm += c != '\\' ? c : '\\\\';
}
tokens.push(new token.TokenSlashSearch(searchTerm));
state.ignore();
if (!state.isAtEof) state.skip('/');
return scanRange;
}

function scanReverseSearch(state : State, tokens : token.Token[]): ScanFunction {
state.skip('?');
var escaping : boolean;
var searchTerm = '';
while(!state.isAtEof) {
var c = state.next();
if (c == '?' && !escaping) break;
if (c == '\\') {
escaping = true;
continue;
}
else {
escaping = false;
}
searchTerm += c != '\\' ? c : '\\\\';
}
tokens.push(new token.TokenQuestionMarkSearch(searchTerm));
state.ignore();
if (!state.isAtEof) state.skip('?');
return scanRange;
}
73 changes: 73 additions & 0 deletions src/cmd_line/lexer_state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

// Lexer state.
export class State {
static EOF : string = '__EOF__';
start : number = 0;
pos : number = 0;
input : string;

constructor(input : string) {
this.input = input;
}

// Returns the next character in the input, or EOF.
next() : string {
if (this.isAtEof) {
this.pos = this.input.length;
return State.EOF;
}
let c = this.input[this.pos];
this.pos++;
return c;
}

// Returns whether we've reached EOF.
get isAtEof() : boolean {
return this.pos >= this.input.length;
}

// Ignores the span of text between the current start and the current position.
ignore() : void {
this.start = this.pos;
}

// Returns the span of text between the current start and the current position.
emit() : string {
let s = this.input.substring(this.start, this.pos);
this.ignore();
return s;
}

backup(): void {
this.pos--;
}

skip(c : string) : void {
var s = this.next();
while (!this.isAtEof) {
if (s !== c) break;
s = this.next();
}
this.backup();
this.ignore();
}

skipRun(...chars : string[]) : void {
while(!this.isAtEof) {
var c = this.next();
if (chars.indexOf(c) == -1) break;
}
this.backup();
this.ignore();
}

skipWhiteSpace(): void {
while (true) {
var c = this.next();
if (c == ' ' || c == '\t') continue;
break;
}
this.backup();
this.ignore();
}
}
32 changes: 32 additions & 0 deletions src/cmd_line/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as vscode from 'vscode';
import * as parser from './parser';
import * as util from '../util';

// Shows the vim command line.
export function showCmdLine(initialText = "") {
const options : vscode.InputBoxOptions = {
prompt: "Vim command line",
value: initialText
};
vscode.window.showInputBox(options).then(
runCmdLine,
vscode.window.showErrorMessage
);
}

function runCmdLine(s : string) : void {
try {
var cmd = parser.parse(s);
}
catch (e) {
util.showInfo(e);
}

if (cmd.isEmpty) {
vscode.window.showInformationMessage("empty cmdline");
}
else {
cmd.runOn(vscode.window.activeTextEditor);
// vscode.window.showInformationMessage(s);
}
}
Loading