From b143a0d5cde4c6579a23d12f0c277370084b0975 Mon Sep 17 00:00:00 2001 From: Ramya Rao Date: Mon, 31 Oct 2016 15:01:40 -0700 Subject: [PATCH] Support editor.formatOnSave (#578) --- package.json | 9 ++++----- src/goFormat.ts | 7 +++---- src/goMain.ts | 10 +++++++++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 529a7f85b..68cbc5eee 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,7 @@ "test": "node ./node_modules/vscode/bin/test", "lint": "node ./node_modules/tslint/bin/tslint ./src/*.ts ./src/debugAdapter/*.ts ./test/*.ts" }, - "extensionDependencies": [ - ], + "extensionDependencies": [], "dependencies": { "console-stamp": "^0.2.2", "diff": "~3.0.0", @@ -117,7 +116,7 @@ "title": "Go: Generate unit tests for current file", "description": "Generates unit tests for the current file" }, - { + { "command": "go.test.generate.function", "title": "Go: Generate unit tests for current function", "description": "Generates unit tests for the selected function in the current file" @@ -325,7 +324,7 @@ "go.formatOnSave": { "type": "boolean", "default": true, - "description": "Run formatting tool on save." + "description": "Deprecated from VS Code 1.7 onwards in favor of editor.formatOnSave. Runs formatting tool on save." }, "go.coverOnSave": { "type": "boolean", @@ -360,4 +359,4 @@ } } } -} +} \ No newline at end of file diff --git a/src/goFormat.ts b/src/goFormat.ts index 6db996ee8..57973c335 100644 --- a/src/goFormat.ts +++ b/src/goFormat.ts @@ -33,7 +33,7 @@ export class Formatter { formatFlags.push('-d'); } - cp.execFile(formatCommandBinPath, [...formatFlags, filename], {}, (err, stdout, stderr) => { + let childProcess = cp.execFile(formatCommandBinPath, [...formatFlags], {}, (err, stdout, stderr) => { try { if (err && (err).code === 'ENOENT') { promptForMissingTool(this.formatCommand); @@ -53,6 +53,7 @@ export class Formatter { reject('Internal issues while getting diff from formatted content'); } }); + childProcess.stdin.end(document.getText()); }); } } @@ -65,8 +66,6 @@ export class GoDocumentFormattingEditProvider implements vscode.DocumentFormatti } public provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable { - return document.save().then(() => { - return this.formatter.formatDocument(document); - }); + return this.formatter.formatDocument(document); } } diff --git a/src/goMain.ts b/src/goMain.ts index 99ed9f0c8..192931ef6 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -30,6 +30,7 @@ import { addImport } from './goImport'; import { installAllTools } from './goInstallTools'; let diagnosticCollection: vscode.DiagnosticCollection; +let goFormatOnSaveDeprecated = true; export function activate(ctx: vscode.ExtensionContext): void { @@ -119,6 +120,13 @@ export function activate(ctx: vscode.ExtensionContext): void { let goConfig = vscode.workspace.getConfiguration('go'); runBuilds(vscode.window.activeTextEditor.document, goConfig); } + + let matches = /(\d)\.(\d).*/.exec(vscode.version); + if (matches) { + let major = parseInt(matches[1]); + let minor = parseInt(matches[2]); + goFormatOnSaveDeprecated = (major > 1) || (major === 1 && minor > 6); + } } function deactivate() { @@ -186,7 +194,7 @@ function startBuildOnSaveWatcher(subscriptions: vscode.Disposable[]) { let goConfig = vscode.workspace.getConfiguration('go'); let textEditor = vscode.window.activeTextEditor; let formatPromise: PromiseLike = Promise.resolve(); - if (goConfig['formatOnSave'] && textEditor.document === document) { + if (!goFormatOnSaveDeprecated && goConfig['formatOnSave'] && textEditor.document === document) { let formatter = new Formatter(); formatPromise = formatter.formatDocument(document).then(edits => { return textEditor.edit(editBuilder => {