Skip to content

Commit

Permalink
implement :quit
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermooo committed Nov 26, 2015
1 parent 5bc44cd commit 49e79fa
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
37 changes: 37 additions & 0 deletions src/cmd_line/command_quit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import vscode = require('vscode');

import node = require('./node');

export interface QuitCommandArguments {
bang? : boolean;
range? : node.LineRange;
}

//
// Implements :quit
// http://vimdoc.sourceforge.net/htmldoc/editing.html#:quit
//
export class QuitCommand implements node.CommandBase {
name : string;
shortName : string;
args : QuitCommandArguments;

constructor(args : QuitCommandArguments = {}) {
this.name = 'quit';
this.shortName = 'q';
this.args = args;
}

private doQuit(textEditor : vscode.TextEditor) {
if ((textEditor.document.isDirty || textEditor.document.isUntitled)
&& !this.args.bang) {
throw new Error("unsaved changes");
}

vscode.commands.executeCommand('workbench.action.closeActiveEditor');
};

runOn(textEditor : vscode.TextEditor) : void {
this.doQuit(textEditor);
}
}
16 changes: 12 additions & 4 deletions src/cmd_line/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ export function showCmdLine(initialText = "") {
}

function runCmdLine(s : string) : void {
if (!(s || s.trim())) {
return;
}

try {
var cmd = parser.parse(s);
} catch (e) {
util.showInfo(e);
util.showError(e);
return;
}

if (cmd.isEmpty) {
vscode.window.showInformationMessage("empty cmdline");
} else {
return;
}

try {
cmd.runOn(vscode.window.activeTextEditor);
} catch (e) {
util.showError(e);
}
}
5 changes: 4 additions & 1 deletion src/cmd_line/subparser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as node from "./node";
import * as command_node from './command_node';
import {Scanner} from './scanner';
import {parseQuitCommandArgs} from './subparser_quit';

// maps command names to parsers for said commands.
export const commandParsers = {
w: parseWriteCommandArgs,
write: parseWriteCommandArgs
write: parseWriteCommandArgs,
quit: parseQuitCommandArgs,
q: parseQuitCommandArgs
};

function parseWriteCommandArgs(args : string) : node.WriteCommand {
Expand Down
21 changes: 21 additions & 0 deletions src/cmd_line/subparser_quit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as node from "./command_quit";
import {Scanner} from './scanner';

export function parseQuitCommandArgs(args : string) : node.QuitCommand {
if (!args) {
return new node.QuitCommand();
}
var scannedArgs : node.QuitCommandArguments = {};
var scanner = new Scanner(args);
const c = scanner.next();
if (c === '!') {
scannedArgs.bang = true;
} else if (c !== ' ') {
throw new Error('bad command');
}
scanner.skipWhiteSpace();
if (!scanner.isAtEof) {
throw new Error('bad command');
}
return new node.QuitCommand(scannedArgs);
}

0 comments on commit 49e79fa

Please sign in to comment.