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

Commit

Permalink
Support editor.formatOnSave (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a authored Oct 31, 2016
1 parent 2df9204 commit b143a0d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
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 @@ -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() {
Expand Down Expand Up @@ -186,7 +194,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

0 comments on commit b143a0d

Please sign in to comment.