-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Read command #736
Changes from 3 commits
3d908d2
dfe0940
3241ca5
7b3b822
770fd0e
7b904f7
3c99ff4
2e441cc
77e3d1f
0faed06
1c65324
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
}); | ||
} | ||
} |
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); | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
'' | ||
]); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent :)