Skip to content

Commit

Permalink
Commands can write to status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
frarees committed Mar 5, 2016
1 parent 00f39c9 commit dea34b1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
8 changes: 7 additions & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export function activate(context: vscode.ExtensionContext) {

extensionContext = context;

registerCommand(context, 'extension.showCmdLine', () => showCmdLine(""));
registerCommand(context, 'extension.showCmdLine', () => {
if (!modeHandler) {
modeHandler = new ModeHandler();
extensionContext.subscriptions.push(modeHandler);
}
showCmdLine("", modeHandler);
});

registerCommand(context, 'extension.vim_esc', () => handleKeyEvent("esc"));
registerCommand(context, 'extension.vim_colon', () => handleKeyEvent(":"));
Expand Down
24 changes: 12 additions & 12 deletions src/cmd_line/commands/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

// XXX: use graceful-fs ??
import * as fs from 'fs';
import * as path from 'path';

import * as node from '../node';
import * as util from '../../util';
import * as error from '../../error';
import {ModeHandler} from "../../mode/modeHandler";

export interface IWriteCommandArguments extends node.ICommandArgs {
opt? : string;
Expand Down Expand Up @@ -35,7 +37,7 @@ export class WriteCommand extends node.CommandBase {
return this._arguments;
}

execute() : void {
execute(modeHandler : ModeHandler) : void {
if (this.arguments.opt) {
util.showError("Not implemented.");
return;
Expand All @@ -59,30 +61,28 @@ export class WriteCommand extends node.CommandBase {
if (this.arguments.bang) {
fs.chmod(this.activeTextEditor.document.fileName, 666, (e) => {
if (e) {
util.showError(e.message);
modeHandler.setupStatusBarItem(e.message);
} else {
this.save();
this.save(modeHandler);
}
});
} else {
util.showError(accessErr.message);
modeHandler.setupStatusBarItem(accessErr.message);
}
} else {
this.save();
this.save(modeHandler);
}
});
}

private save() {
private save(modeHandler : ModeHandler) {
this.activeTextEditor.document.save().then(
(ok) => {
if (ok) {
util.showInfo("File saved.");
} else {
util.showError("File not saved.");
}
modeHandler.setupStatusBarItem('"' + path.basename(this.activeTextEditor.document.fileName) +
'" ' + this.activeTextEditor.document.lineCount + 'L ' +
this.activeTextEditor.document.getText().length + 'C written');
},
(e) => util.showError(e)
(e) => modeHandler.setupStatusBarItem(e)
);
}
}
14 changes: 7 additions & 7 deletions src/cmd_line/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import * as vscode from "vscode";
import * as parser from "./parser";
import * as util from "../util";
import {ModeHandler} from "../mode/modeHandler";

// Shows the vim command line.
export async function showCmdLine(initialText: string): Promise<{}> {
export async function showCmdLine(initialText: string, modeHandler : ModeHandler): Promise<{}> {
if (!vscode.window.activeTextEditor) {
console.log("No active document.");
return;
Expand All @@ -17,13 +17,13 @@ export async function showCmdLine(initialText: string): Promise<{}> {
};

try {
runCmdLine(await vscode.window.showInputBox(options));
await runCmdLine(await vscode.window.showInputBox(options), modeHandler);
} catch (e) {
return util.showError(e.toString());
modeHandler.setupStatusBarItem(e.toString());
}
}

function runCmdLine(command : string) : Promise<{}> {
function runCmdLine(command : string, modeHandler : ModeHandler) : Promise<{}> {
if (!command || command.length === 0) {
return;
}
Expand All @@ -34,8 +34,8 @@ function runCmdLine(command : string) : Promise<{}> {
return;
}

cmd.execute(vscode.window.activeTextEditor);
cmd.execute(vscode.window.activeTextEditor, modeHandler);
} catch (e) {
return util.showError(e);
modeHandler.setupStatusBarItem(e.toString());
}
}
7 changes: 4 additions & 3 deletions src/cmd_line/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as vscode from "vscode";
import * as token from "./token";
import {ModeHandler} from "../mode/modeHandler";

export class LineRange {
left : token.Token[];
Expand Down Expand Up @@ -86,14 +87,14 @@ export class CommandLine {
return ":" + this.range.toString() + " " + this.command.toString();
}

execute(document : vscode.TextEditor) : void {
execute(document : vscode.TextEditor, modeHandler : ModeHandler) : void {
if (!this.command) {
this.range.execute(document);
return;
}

// TODO: calc range
this.command.execute();
this.command.execute(modeHandler);
}
}

Expand Down Expand Up @@ -123,5 +124,5 @@ export abstract class CommandBase {
}
protected _arguments : ICommandArgs;

abstract execute() : void;
abstract execute(modeHandler : ModeHandler) : void;
}
6 changes: 3 additions & 3 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ModeHandler implements vscode.Disposable {
}

const statusBarText = (this.currentMode.name === ModeName.Normal) ? '' : ModeName[modeName];
this.setupStatusBarItem(statusBarText.toUpperCase());
this.setupStatusBarItem(statusBarText ? `-- ${statusBarText.toUpperCase()} --` : '');
}

handleKeyEvent(key : string) : void {
Expand Down Expand Up @@ -81,12 +81,12 @@ export class ModeHandler implements vscode.Disposable {
}
}

private setupStatusBarItem(text : string) : void {
setupStatusBarItem(text : string) : void {
if (!this._statusBarItem) {
this._statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
}

this._statusBarItem.text = text ? `--${text}--` : '';
this._statusBarItem.text = text || '';
this._statusBarItem.show();
}

Expand Down
2 changes: 1 addition & 1 deletion src/mode/modeNormal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {DeleteOperator} from './../operator/delete';

export class NormalMode extends Mode {
protected keyHandler : { [key : string] : (motion : Motion) => Promise<{}>; } = {
":" : async () => { return showCmdLine(""); },
":" : async () => { return showCmdLine("", this._modeHandler); },
"u" : async () => { return vscode.commands.executeCommand("undo"); },
"ctrl+r" : async () => { return vscode.commands.executeCommand("redo"); },
"h" : async (c) => { return c.left().move(); },
Expand Down

0 comments on commit dea34b1

Please sign in to comment.