Skip to content

Commit

Permalink
Merge pull request #43 from VSCodeVim/fixes
Browse files Browse the repository at this point in the history
fixes; add VimError class
  • Loading branch information
guillermooo committed Nov 27, 2015
2 parents 0743ca3 + d434382 commit b6ca2dc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/cmd_line/commands/quit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import vscode = require('vscode');

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

export interface QuitCommandArguments extends node.CommandArgs {
bang?: boolean;
Expand Down Expand Up @@ -33,7 +34,7 @@ export class QuitCommand extends node.CommandBase {
// See https://github.com/Microsoft/vscode/issues/723
if ((this.activeTextEditor.document.isDirty || this.activeTextEditor.document.isUntitled)
&& !this.arguments.bang) {
throw new Error("unsaved changes");
throw error.VimError.fromCode(error.ErrorCode.E37);
}

vscode.commands.executeCommand('workbench.action.closeActiveEditor');
Expand Down
23 changes: 13 additions & 10 deletions src/cmd_line/commands/write.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// XXX: use graceful-fs ??
import fs = require('fs');

import vscode = require('vscode');

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

export interface WriteCommandArguments extends node.CommandArgs {
opt? : string;
Expand Down Expand Up @@ -35,40 +34,44 @@ export class WriteCommand extends node.CommandBase {
}

execute() : void {
if (this._arguments.opt) {
if (this.arguments.opt) {
util.showError("Not implemented.");
return;
} else if (this._arguments.file) {
} else if (this.arguments.file) {
util.showError("Not implemented.");
return;
} else if (this._arguments.append) {
} else if (this.arguments.append) {
util.showError("Not implemented.");
return;
} else if (this._arguments.cmd) {
} else if (this.arguments.cmd) {
util.showError("Not implemented.");
return;
}

if (this.activeTextEditor.document.isUntitled) {
throw error.VimError.fromCode(error.ErrorCode.E32);
}

fs.access(this.activeTextEditor.document.fileName, fs.W_OK, (accessErr) => {
if (accessErr) {
if (this._arguments.bang) {
if (this.arguments.bang) {
fs.chmod(this.activeTextEditor.document.fileName, 666, (e) => {
if (e) {
util.showError(e.message);
} else {
this.save(this.activeTextEditor);
this.save();
}
});
} else {
util.showError(accessErr.message);
}
} else {
this.save(this.activeTextEditor);
this.save();
}
});
}

private save(textEditor : vscode.TextEditor) {
private save() {
this.activeTextEditor.document.save().then(
(ok) => {
if (ok) {
Expand Down
16 changes: 14 additions & 2 deletions src/cmd_line/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import * as util from "../util";

// Shows the vim command line.
export function showCmdLine(initialText = "") {
if (!vscode.window.activeTextEditor) {
util.showInfo("No active document.");
return;
}

const options : vscode.InputBoxOptions = {
prompt: "Vim command line",
value: initialText
Expand All @@ -15,7 +20,7 @@ export function showCmdLine(initialText = "") {
}

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

Expand All @@ -33,6 +38,13 @@ function runCmdLine(s : string) : void {
try {
cmd.execute(vscode.window.activeTextEditor);
} catch (e) {
util.showError(e);
try {
e.display();
return;
} catch (ee) {
// ignore
}

util.showError(e);
}
}
52 changes: 52 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as util from "./util";

interface VimErrors {
[index: number] : string;
}

export enum ErrorCode {
E37 = 37,
E32 = 32
}

const errors : VimErrors = {
32: "No file name",
37: "No write since last change (add ! to override)"
};


export class VimError extends Error {

private _code : number;
private _message : string;

constructor(code : number, message : string) {
super();
this._code = code;
this._message = message;
}

static fromCode(code : ErrorCode) : VimError {
if (errors[code]) {
return new VimError(code, errors[code]);
}

throw new Error("unknown error code: " + code);
}

get code() : number {
return this._code;
}

get message() : string {
return this._message;
}

display() : void {
util.showError(this.toString());
}

toString() : string {
return "E" + this.code.toString() + ": " + this.message;
}
}

0 comments on commit b6ca2dc

Please sign in to comment.