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

Read command #736

Merged
merged 11 commits into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
"postinstall": "node ./node_modules/vscode/bin/install && gulp init"
},
"dependencies": {
"child-process": "^1.0.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent :)

"copy-paste": "^1.3.0",
"diff-match-patch": "^1.0.0",
"lodash": "^4.12.0"
Expand Down
81 changes: 81 additions & 0 deletions src/cmd_line/commands/read.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"use strict";

import * as node from "../node";
import {readFile} from 'fs';
import {exec} from 'child_process';
import {TextEditor} from '../../textEditor';

export interface IReadCommandArguments extends node.ICommandArgs {
file?: string;
cmd?: string;
}

//
// Implements :read and :read!
// http://vimdoc.sourceforge.net/htmldoc/insert.html#:read
// http://vimdoc.sourceforge.net/htmldoc/insert.html#:read!
//
export class ReadCommand extends node.CommandBase {
protected _arguments : IReadCommandArguments;

constructor(args : IReadCommandArguments) {
super();
this._name = 'read';
this._shortName = 'r';
this._arguments = args;
}

get arguments() : IReadCommandArguments {
return this._arguments;
}

async execute() : Promise<void> {
const textToInsert = await this.getTextToInsert();
if (textToInsert) {
await TextEditor.insert(textToInsert);
}
}

async getTextToInsert() : Promise<string> {
if (this.arguments.file && this.arguments.file.length > 0) {
return await this.getTextToInsertFromFile();
} else if (this.arguments.cmd && this.arguments.cmd.length > 0) {
return await this.getTextToInsertFromCmd();
} else {
throw Error('Invalid arguments');
}
}

async getTextToInsertFromFile() : Promise<string> {
// TODO: Substitute utf8 with current file encoding, couldn't find this anywhere in vscode's api.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue tracking this with VSCode? Some initial Google says there's some config at the workspace level where you can set the encoding. Maybe we can read that config?

return new Promise<string>((resolve, reject) => {
try {
readFile(this.arguments.file as string, 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
} catch (e) {
reject(e);
}
});
}

async getTextToInsertFromCmd() : Promise<string> {
return new Promise<string>((resolve, reject) => {
try {
exec(this.arguments.cmd as string, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve(stdout);
}
});
} catch (e) {
reject(e);
}
});
}
}
8 changes: 8 additions & 0 deletions src/cmd_line/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ export class Scanner {
return s;
}

// Returns the text from the current position to the end.
remaining() : string {
while (!this.isAtEof) {
this.next();
}
return this.emit();
}

backup(): void {
this.pos--;
}
Expand Down
6 changes: 5 additions & 1 deletion src/cmd_line/subparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as tabCmd from './subparsers/tab';
import * as fileCmd from './subparsers/file';
import {parseOptionsCommandArgs} from './subparsers/setoptions';
import {parseSubstituteCommandArgs} from './subparsers/substitute';
import {parseReadCommandArgs} from './subparsers/read';

// maps command names to parsers for said commands.
export const commandParsers = {
Expand Down Expand Up @@ -58,5 +59,8 @@ export const commandParsers = {
vnew: fileCmd.parseEditNewFileInNewWindowCommandArgs,

set: parseOptionsCommandArgs,
se: parseOptionsCommandArgs
se: parseOptionsCommandArgs,

read: parseReadCommandArgs,
r: parseReadCommandArgs
};
31 changes: 31 additions & 0 deletions src/cmd_line/subparsers/read.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";

import {ReadCommand, IReadCommandArguments} from '../commands/read';
import {Scanner} from '../scanner';

export function parseReadCommandArgs(args : string) : ReadCommand {
if (!args) {
throw Error('Expected arguments.');
}

var scannedArgs : IReadCommandArguments = {};
var scanner = new Scanner(args);

scanner.skipWhiteSpace();
let c = scanner.next();
if (c === '!') {
scanner.ignore();
scanner.skipWhiteSpace();
scannedArgs.cmd = scanner.remaining();
if (!scannedArgs.cmd || scannedArgs.cmd.length === 0) {
throw Error('Expected shell command.');
}
} else {
scannedArgs.file = scanner.remaining();
if (!scannedArgs.file || scannedArgs.file.length === 0) {
throw Error('Expected file path.');
}
}

return new ReadCommand(scannedArgs);
}
24 changes: 24 additions & 0 deletions test/cmd_line/read.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

import { ModeHandler } from '../../src/mode/modeHandler';
import { setupWorkspace, cleanUpWorkspace, assertEqualLines } from './../testUtils';
import { runCmdLine } from '../../src/cmd_line/main';

suite("read", () => {
let modeHandler: ModeHandler;

setup(async () => {
await setupWorkspace();
modeHandler = new ModeHandler();
});

teardown(cleanUpWorkspace);

test("Can read shell command output", async () => {
await runCmdLine('r! echo "hey ho"', modeHandler);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the result of this command differs on different platform, which currently breaks the test on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just didn't realise that Windows prints the "s in echo "..". I modified the test so that it passes on Windows too.

assertEqualLines([
'hey ho',
''
]);
});
});