From d4343826d9e86578850b723ed1ba192028ced0ac Mon Sep 17 00:00:00 2001 From: guillermooo Date: Fri, 27 Nov 2015 23:53:28 +0100 Subject: [PATCH] fixes; add VimError class --- src/cmd_line/commands/quit.ts | 3 +- src/cmd_line/commands/write.ts | 23 ++++++++------- src/cmd_line/main.ts | 16 +++++++++-- src/error.ts | 52 ++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 src/error.ts diff --git a/src/cmd_line/commands/quit.ts b/src/cmd_line/commands/quit.ts index 5e4ce2d77af..e0f91f32ef5 100644 --- a/src/cmd_line/commands/quit.ts +++ b/src/cmd_line/commands/quit.ts @@ -1,6 +1,7 @@ import vscode = require('vscode'); import node = require('../node'); +import error = require('../../error'); export interface QuitCommandArguments extends node.CommandArgs { bang?: boolean; @@ -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'); diff --git a/src/cmd_line/commands/write.ts b/src/cmd_line/commands/write.ts index 98354b83b9d..97bbe4f682f 100644 --- a/src/cmd_line/commands/write.ts +++ b/src/cmd_line/commands/write.ts @@ -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; @@ -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) { diff --git a/src/cmd_line/main.ts b/src/cmd_line/main.ts index 28c10562727..b56a7bb8475 100644 --- a/src/cmd_line/main.ts +++ b/src/cmd_line/main.ts @@ -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 @@ -15,7 +20,7 @@ export function showCmdLine(initialText = "") { } function runCmdLine(s : string) : void { - if (!(s || s.trim())) { + if (!(s && s.trim())) { return; } @@ -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); } } diff --git a/src/error.ts b/src/error.ts new file mode 100644 index 00000000000..273447f9a77 --- /dev/null +++ b/src/error.ts @@ -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; + } +} \ No newline at end of file