英文:
How to run `go fmt` on save, in Visual Studio Code?
问题
如何使Visual Studio Code(或Go编程语言扩展)在保存时运行go fmt
(或其他工具/命令)?甚至自动保存?
更新:
现在在VSCode中完美运行,只需在*.vscode*目录中添加一些配置文件(我使用这些)。
更新2019:
这个问题已经过时了。VSCode Go扩展现在具备了你在Go开发中所需的一切。
最后更新2019年
顺便提一下,在你的测试文件的包声明上方会出现一个“运行包测试”的标记。如果你点击它,你可以看到你的代码覆盖率。覆盖和未覆盖的部分会用不同的颜色进行标记。
更新2020年
现在,VSCode的Go扩展正在Go团队的监督下进行开发!🎉
英文:
How to make Visual Studio Code (or Go Programming Language Extension) to run go fmt
(or other tools/commands) on save? Even auto save?
Update:
It is working now perfectly inside VSCode, at this time; just need to add some config files inside .vscode directory (I use these).
Update 2019:
This question is old. The VSCode Go extension has all you need to develop in Go, now.
Last Update 2019
BTW It worth mentioning that right above the package declaration inside your test files appears a run package tests. If you click it, you can see your code coverage of your code. The covered and not-covered parts are highlighted in different colors.
Update 2020
And now, the Go Extension for VSCode, is under the supervision of Go Team! 🎉
答案1
得分: 46
现在,该功能已经实现,您可以在保存时启用格式化:
- 打开设置(
Ctrl + ,
) - 搜索
editor.formatOnSave
并将其设置为true
您的 Go 代码将在按下 Ctrl + s
后自动格式化。
英文:
Now, the feature has been implemented, you can enable format on save:
- Open Settings (
Ctrl + ,
) - Search for
editor.formatOnSave
and set it totrue
Your Go code will be formatted automatically on Ctrl + s
答案2
得分: 4
目前还不可能,但正在进行相关工作。请参考此链接:https://github.com/Microsoft/vscode-go/issues/14
英文:
Its not possible at the moment but its being worked on https://github.com/Microsoft/vscode-go/issues/14
答案3
得分: 4
我对于"go fmt"不太熟悉,但是你可以创建一个简单的VSCode扩展来处理保存事件,并执行任意命令并将文件路径作为参数传递。
以下是一个示例,它只是调用echo $filepath
:
import * as vscode from 'vscode';
import { exec } from 'child_process';
export function activate(context: vscode.ExtensionContext) {
vscode.window.showInformationMessage('Run command on save enabled.');
var cmd = vscode.commands.registerCommand('extension.executeOnSave', () => {
var onSave = vscode.workspace.onDidSaveTextDocument((e: vscode.TextDocument) => {
// execute some child process on save
var child = exec('echo ' + e.fileName);
child.stdout.on('data', (data) => {
vscode.window.showInformationMessage(data);
});
});
context.subscriptions.push(onSave);
});
context.subscriptions.push(cmd);
}
还有一个package文件:
{
"name": "Custom onSave",
"description": "Execute commands on save.",
"version": "0.0.1",
"publisher": "Emeraldwalk",
"engines": {
"vscode": "^0.10.1"
},
"categories": [
"Other"
],
"activationEvents": [
"onCommand:extension.executeOnSave"
],
"main": "./out/src/extension",
"contributes": {
"commands": [{
"command": "extension.executeOnSave",
"title": "Execute on Save"
}]
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
},
"devDependencies": {
"typescript": "^1.6.2",
"vscode": "0.10.x"
}
}
通过按下cmd+shift+p,然后输入"Execute on Save"来启用该扩展,但也可以通过其他命令重新配置启动,包括"*",这将导致它在VSCode加载时始终加载。
一旦启用了该扩展,事件处理程序将在保存文件时触发(注意:在首次创建文件或另存为时似乎不起作用)。
这只是一个稍作修改的yo code脚手架扩展,详细信息请参考:https://code.visualstudio.com/docs/extensions/example-hello-world
更新
这是我为在文件保存时运行命令编写的Visual Studio Code扩展:https://marketplace.visualstudio.com/items/emeraldwalk.RunOnSave。
英文:
I'm not familiar with 'go fmt' specifically, but you can create a simple vscode extension to handle on save event and execute any arbitrary command passing the file path as an argument.
Here's a sample that just calls echo $filepath
:
import * as vscode from 'vscode';
import {exec} from 'child_process';
export function activate(context: vscode.ExtensionContext) {
vscode.window.showInformationMessage('Run command on save enabled.');
var cmd = vscode.commands.registerCommand('extension.executeOnSave', () => {
var onSave = vscode.workspace.onDidSaveTextDocument((e: vscode.TextDocument) => {
// execute some child process on save
var child = exec('echo ' + e.fileName);
child.stdout.on('data', (data) => {
vscode.window.showInformationMessage(data);
});
});
context.subscriptions.push(onSave);
});
context.subscriptions.push(cmd);
}
And the package file:
{
"name": "Custom onSave",
"description": "Execute commands on save.",
"version": "0.0.1",
"publisher": "Emeraldwalk",
"engines": {
"vscode": "^0.10.1"
},
"categories": [
"Other"
],
"activationEvents": [
"onCommand:extension.executeOnSave"
],
"main": "./out/src/extension",
"contributes": {
"commands": [{
"command": "extension.executeOnSave",
"title": "Execute on Save"
}]
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
},
"devDependencies": {
"typescript": "^1.6.2",
"vscode": "0.10.x"
}
}
The extension is enabled via cmd+shift+p then typing "Execute on Save", but it could be reconfigured to start via another command including "*" which would cause it to load any time VSCode loads.
Once the extension is enabled, the event handler will fire whenever a file is saved (NOTE: this doesn't appear to work when file is first created or on Save as...)
This is just a minor modification of a yo code scaffolded extension as outlined here: https://code.visualstudio.com/docs/extensions/example-hello-world
Update
Here's a Visual Studio Code extension I wrote for running commands on file save. https://marketplace.visualstudio.com/items/emeraldwalk.RunOnSave.
答案4
得分: 0
我之前习惯使用CTRL+S手动保存文件。然而,在VS Code中,我有一个不同的绑定,它以CTRL+S开头(例如,swagger扩展有一个和按下CTRL+S CTRL+W会生成swagger注释的组合键)。这导致VS Code等待我按下第二个组合键(CTRL+W),因此完全忽略了保存部分。我重新配置了所有以CTRL+S作为第一个组合键的按键绑定,改为其他组合键。之后,按下CTRL+S会正确地使用gofmt格式化我的文件。
英文:
I was using CTRL+S for saving the file manually (this is habitual for me). However, I had a different binding in VS Code that would start with CTRL+S (e.g. swagger extension has a chord where pressing CTRL+S CTRL+W would generate the swagger annotation). This resulted in VS Code waiting for me to press the second chord (CTRL+W) and as such it ignored the save part altogether. I reconfigured all the keybinding chords which had CTRL+S as the first chord to a different combination. After this, pressing CTRL+S formatted my file with gofmt properly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论