Skip to content

Commit 8f81613

Browse files
committed
docs/settings: make the custom formatter support more visible
Custom formatter support was added in #1238. But due to the limitation in the VS Code setting UI (which assumes the acceptable values list is static) and the setting validation logic (VS Code thinks it's an error to use a value outside of the supplied enum list is invalid), this is not visible to users. Instead, this change introduces an extra enum 'custom' for the "go.formatTool" setting. If this is chosen, the extension uses the tool specified as `customFormatter` in the "go.alternateTools" setting section for formatting. The extension expects the custom formatter to accept input as STDIN and output the result as STDOUT. Users can also supply "go.formatFlags". Changed the descriptions to use markdown - which allows to reference other settings ("`#...#`"). The document generation tool does not handle this special syntax nicely but I hope this isn't too confusing. I wish we could add an extra validation on the allowed value for "go.formatTool" in favor of this new 'custom' option. I am not adding the validation in this CL because there could be users who depend on this behavior. For #1238 For #1603 For #2503 Change-Id: I5d9564f331845b6b07f0b54148834118404f3553 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/446298 Reviewed-by: Jamal Carvalho <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]>
1 parent 752577e commit 8f81613

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

docs/settings.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ Default:
5151
Alternate tools or alternate paths for the same tools used by the Go extension. Provide either absolute path or the name of the binary in GOPATH/bin, GOROOT/bin or PATH. Useful when you want to use wrapper script for the Go tools.
5252
| Properties | Description |
5353
| --- | --- |
54+
| `customFormatter` | Custom formatter to use instead of the language server. This should be used with the `custom` option in `#go.formatTool#`. <br/> Default: `""` |
5455
| `dlv` | Alternate tool to use instead of the dlv binary or alternate path to use for the dlv binary. <br/> Default: `"dlv"` |
5556
| `go` | Alternate tool to use instead of the go binary or alternate path to use for the go binary. <br/> Default: `"go"` |
56-
| `go-outline` | Alternate tool to use instead of the go-outline binary or alternate path to use for the go-outline binary. <br/> Default: `"go-outline"` |
5757
| `gopls` | Alternate tool to use instead of the gopls binary or alternate path to use for the gopls binary. <br/> Default: `"gopls"` |
5858
### `go.autocompleteUnimportedPackages`
5959

@@ -221,8 +221,16 @@ Default:
221221
Flags to pass to format tool (e.g. ["-s"]). Not applicable when using the language server.
222222
### `go.formatTool`
223223

224-
When the language server is enabled and one of default/gofmt/goimports/gofumpt is chosen, the language server will handle formatting. Otherwise, the extension will use the specified tool for formatting.<br/>
225-
Allowed Options: `default`, `gofmt`, `goimports`, `goformat`, `gofumpt`
224+
When the language server is enabled and one of `default`/`gofmt`/`goimports`/`gofumpt` is chosen, the language server will handle formatting. If `custom` tool is selected, the extension will use the `customFormatter` tool in the `#go.alternateTools#` section.<br/>
225+
Allowed Options:
226+
227+
* `default`: If the language server is enabled, format via the language server, which already supports gofmt, goimports, goreturns, and gofumpt. Otherwise, goimports.
228+
* `gofmt`: Formats the file according to the standard Go style. (not applicable when the language server is enabled)
229+
* `goimports`: Organizes imports and formats the file with gofmt. (not applicable when the language server is enabled)
230+
* `goformat`: Configurable gofmt, see https://github.com/mbenkmann/goformat.
231+
* `gofumpt`: Stricter version of gofmt, see https://github.com/mvdan/gofumpt. . Use `#gopls.format.gofumpt#` instead)
232+
* `custom`: Formats using the custom tool specified as `customFormatter` in the `#go.alternateTools#` setting. The tool should take the input as STDIN and output the formatted code as STDOUT.
233+
226234

227235
Default: `"default"`
228236
### `go.generateTestsFlags`

package.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -1206,23 +1206,23 @@
12061206
"go.formatTool": {
12071207
"type": "string",
12081208
"default": "default",
1209-
"description": "When the language server is enabled and one of default/gofmt/goimports/gofumpt is chosen, the language server will handle formatting. Otherwise, the extension will use the specified tool for formatting.",
1209+
"markdownDescription": "When the language server is enabled and one of `default`/`gofmt`/`goimports`/`gofumpt` is chosen, the language server will handle formatting. If `custom` tool is selected, the extension will use the `customFormatter` tool in the `#go.alternateTools#` section.",
12101210
"scope": "resource",
12111211
"enum": [
12121212
"default",
12131213
"gofmt",
12141214
"goimports",
12151215
"goformat",
1216-
"gofumpt"
1216+
"gofumpt",
1217+
"custom"
12171218
],
1218-
"additionalItems": true,
1219-
"enumDescriptions": [
1219+
"markdownEnumDescriptions": [
12201220
"If the language server is enabled, format via the language server, which already supports gofmt, goimports, goreturns, and gofumpt. Otherwise, goimports.",
12211221
"Formats the file according to the standard Go style. (not applicable when the language server is enabled)",
12221222
"Organizes imports and formats the file with gofmt. (not applicable when the language server is enabled)",
12231223
"Configurable gofmt, see https://github.com/mbenkmann/goformat.",
1224-
"Stricter version of gofmt, see https://github.com/mvdan/gofumpt. (not applicable when the language server is enabled)",
1225-
"Applies gofumpt formatting and organizes imports."
1224+
"Stricter version of gofmt, see https://github.com/mvdan/gofumpt. . Use `#gopls.format.gofumpt#` instead)",
1225+
"Formats using the custom tool specified as `customFormatter` in the `#go.alternateTools#` setting. The tool should take the input as STDIN and output the formatted code as STDOUT."
12261226
]
12271227
},
12281228
"go.formatFlags": {
@@ -2030,15 +2030,15 @@
20302030
"default": "gopls",
20312031
"description": "Alternate tool to use instead of the gopls binary or alternate path to use for the gopls binary."
20322032
},
2033-
"go-outline": {
2034-
"type": "string",
2035-
"default": "go-outline",
2036-
"description": "Alternate tool to use instead of the go-outline binary or alternate path to use for the go-outline binary."
2037-
},
20382033
"dlv": {
20392034
"type": "string",
20402035
"default": "dlv",
20412036
"description": "Alternate tool to use instead of the dlv binary or alternate path to use for the dlv binary."
2037+
},
2038+
"customFormatter": {
2039+
"type": "string",
2040+
"default": "",
2041+
"markdownDescription": "Custom formatter to use instead of the language server. This should be used with the `custom` option in `#go.formatTool#`."
20422042
}
20432043
},
20442044
"additionalProperties": true

src/language/legacy/goFormat.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class GoDocumentFormattingEditProvider implements vscode.DocumentFormatti
3939
// Handle issues:
4040
// https://github.com/Microsoft/vscode-go/issues/613
4141
// https://github.com/Microsoft/vscode-go/issues/630
42-
if (formatTool === 'goimports' || formatTool === 'goreturns' || formatTool === 'gofumports') {
42+
if (formatTool === 'goimports' || formatTool === 'goreturns') {
4343
formatFlags.push('-srcdir', filename);
4444
}
4545

@@ -75,10 +75,10 @@ export class GoDocumentFormattingEditProvider implements vscode.DocumentFormatti
7575
): Thenable<vscode.TextEdit[]> {
7676
const formatCommandBinPath = getBinPath(formatTool);
7777
if (!path.isAbsolute(formatCommandBinPath)) {
78+
// executable not found.
7879
promptForMissingTool(formatTool);
7980
return Promise.reject('failed to find tool ' + formatTool);
8081
}
81-
8282
return new Promise<vscode.TextEdit[]>((resolve, reject) => {
8383
const env = toolExecutionEnvironment();
8484
const cwd = path.dirname(document.fileName);
@@ -94,7 +94,7 @@ export class GoDocumentFormattingEditProvider implements vscode.DocumentFormatti
9494
p.on('error', (err) => {
9595
if (err && (<any>err).code === 'ENOENT') {
9696
promptForMissingTool(formatTool);
97-
return reject();
97+
return reject(`failed to find format tool: ${formatTool}`);
9898
}
9999
});
100100
p.on('close', (code) => {
@@ -139,8 +139,12 @@ export function usingCustomFormatTool(goConfig: { [key: string]: any }): boolean
139139
}
140140

141141
export function getFormatTool(goConfig: { [key: string]: any }): string {
142-
if (goConfig['formatTool'] === 'default') {
142+
const formatTool = goConfig['formatTool'];
143+
if (formatTool === 'default') {
143144
return 'goimports';
144145
}
145-
return goConfig['formatTool'];
146+
if (formatTool === 'custom') {
147+
return goConfig['alternateTools']['customFormatter'] || 'goimports';
148+
}
149+
return formatTool;
146150
}

test/gopls/extension.test.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,8 @@ suite('Go Extension Tests With Gopls', function () {
286286
}
287287
});
288288

289-
test('Nonexistent formatter', async () => {
289+
async function testCustomFormatter(goConfig: vscode.WorkspaceConfiguration, customFormatter: string) {
290290
const config = require('../../src/config');
291-
const goConfig = Object.create(getGoConfig(), {
292-
formatTool: { value: 'nonexistent' } // this should make the formatter fail.
293-
}) as vscode.WorkspaceConfiguration;
294291
sandbox.stub(config, 'getGoConfig').returns(goConfig);
295292

296293
await env.startGopls(path.resolve(testdataDir, 'gogetdocTestData', 'test.go'), goConfig);
@@ -308,7 +305,26 @@ suite('Go Extension Tests With Gopls', function () {
308305
);
309306
assert.fail(`formatter unexpectedly succeeded and returned a result: ${JSON.stringify(result)}`);
310307
} catch (e) {
311-
assert(`${e}`.includes('errors when formatting with nonexistent'), `${e}`);
308+
assert(`${e}`.includes(`errors when formatting with ${customFormatter}`), `${e}`);
312309
}
310+
}
311+
312+
test('Nonexistent formatter', async () => {
313+
const customFormatter = 'nonexistent';
314+
const goConfig = Object.create(getGoConfig(), {
315+
formatTool: { value: customFormatter } // this should make the formatter fail.
316+
}) as vscode.WorkspaceConfiguration;
317+
318+
await testCustomFormatter(goConfig, customFormatter);
319+
});
320+
321+
test('Custom formatter', async () => {
322+
const customFormatter = 'coolCustomFormatter';
323+
const goConfig = Object.create(getGoConfig(), {
324+
formatTool: { value: 'custom' }, // this should make the formatter fail.
325+
alternateTools: { value: { customFormatter: customFormatter } } // this should make the formatter fail.
326+
}) as vscode.WorkspaceConfiguration;
327+
328+
await testCustomFormatter(goConfig, customFormatter);
313329
});
314330
});

0 commit comments

Comments
 (0)