Skip to content

Commit

Permalink
Resolve #10: Add option to save the file before running
Browse files Browse the repository at this point in the history
  • Loading branch information
formulahendry committed Aug 14, 2016
1 parent 8fd716b commit 5a42d5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
"type": "boolean",
"default": false,
"description": "Whether to clear previous output before each run."
},
"code-runner.saveFileBeforeRun": {
"type": "boolean",
"default": false,
"description": "Whether to save the file before running."
}
}
},
Expand Down
20 changes: 13 additions & 7 deletions src/codeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export class CodeManager {
return;
}

this.getCodeFile(editor, fileExtension);

this.ExecuteCommand(executor);
this.getCodeFileAndExecute(editor, fileExtension, executor);
}

public runByLanguage(): void {
Expand Down Expand Up @@ -76,12 +74,18 @@ export class CodeManager {
this._cwd = TmpDir;
}

private getCodeFile(editor: vscode.TextEditor, fileExtension: string): void {
private getCodeFileAndExecute(editor: vscode.TextEditor, fileExtension: string, executor: string): any {
let selection = editor.selection;

if (selection.isEmpty && !editor.document.isUntitled) {
this._isTmpFile = false;
this._codeFile = editor.document.fileName;

if (this._config.get<boolean>('saveFileBeforeRun')) {
return editor.document.save().then(() => {
this.executeCommand(executor);
});
}
} else {
let text = selection.isEmpty ? editor.document.getText() : editor.document.getText(selection);

Expand All @@ -94,8 +98,10 @@ export class CodeManager {

this._isTmpFile = true;
let folder = editor.document.isUntitled ? this._cwd : dirname(editor.document.fileName);
this.createRandomFile(text, folder, fileExtension);
this.createRandomFile(text, folder, fileExtension);
}

this.executeCommand(executor);
}

private rndName(): string {
Expand Down Expand Up @@ -148,7 +154,7 @@ export class CodeManager {
}
}

private ExecuteCommand(executor: string) {
private executeCommand(executor: string) {
this._isRunning = true;
let clearPreviousOutput = this._config.get<boolean>('clearPreviousOutput');
if (clearPreviousOutput) {
Expand All @@ -172,7 +178,7 @@ export class CodeManager {
this._process.on('close', (code) => {
this._isRunning = false;
let endTime = new Date();
let elapsedTime = (endTime.getTime() - startTime.getTime())/1000;
let elapsedTime = (endTime.getTime() - startTime.getTime()) / 1000;
this._outputChannel.appendLine('');
this._outputChannel.appendLine('[Done] exited with code=' + code + ' in ' + elapsedTime + ' seconds');
this._outputChannel.appendLine('');
Expand Down

0 comments on commit 5a42d5e

Please sign in to comment.