Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Support editor.formatOnSave #578

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -360,4 +359,4 @@
}
}
}
}
}
7 changes: 3 additions & 4 deletions src/goFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 && (<any>err).code === 'ENOENT') {
promptForMissingTool(this.formatCommand);
Expand All @@ -53,6 +53,7 @@ export class Formatter {
reject('Internal issues while getting diff from formatted content');
}
});
childProcess.stdin.end(document.getText());
});
}
}
Expand All @@ -65,8 +66,6 @@ export class GoDocumentFormattingEditProvider implements vscode.DocumentFormatti
}

public provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable<vscode.TextEdit[]> {
return document.save().then(() => {
return this.formatter.formatDocument(document);
});
return this.formatter.formatDocument(document);
}
}
10 changes: 9 additions & 1 deletion src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -147,6 +148,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() {
Expand Down Expand Up @@ -214,7 +222,7 @@ function startBuildOnSaveWatcher(subscriptions: vscode.Disposable[]) {
let goConfig = vscode.workspace.getConfiguration('go');
let textEditor = vscode.window.activeTextEditor;
let formatPromise: PromiseLike<void> = 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 => {
Expand Down