VS Code 有什么方法可以缩进到左括号?
Is there any way for VS Code to indent to an open bracket?
我正在寻找修改 VS Code 的缩进行为,这样如果我在输入如下一行后按回车键:
variable = function(param1,
它将缩进到左括号的水平,这样我就可以像这样轻松地格式化代码:
variable = function(param1,
param2)
我希望它也适用于左方括号和大括号:
variable = function([1, 2, 3, 4
5, 6, 7, 8],
param2,
{'a': 1, 'b': 2,
'c': 3, 'd': 4},
param4)
我希望它对我使用的几乎所有语言都有这种行为,尽管在 C++ 或 C# 中工作时花括号行为不是必需的(甚至可能是不受欢迎的)。
这与 Sublime Text 的 indent_to_bracket
设置非常相似。
有什么办法可以实现吗?如果没有设置,我愿意修补任何必要的东西。我也愿意接受可以执行此操作的扩展程序,甚至在必要且有意义的情况下编写扩展程序。
看起来 ESLint 会为你做这件事。
Examples of correct code for this rule with the 2, { "FunctionExpression": {"parameters": "first"} } option:
/*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
var foo = function(bar, baz,
qux, boop) {
qux();
}
来自 indent rule : Function Expressions。
安装扩展 vscode-eslint integration 然后
If you haven't installed ESLint either locally or globally do so by running npm install eslint in the workspace folder for a local install or npm install -g eslint for a global install.
然后像
这样的规则
"indent": ["error", 2, { "FunctionExpression": {"body": 1, "parameters": "first"} }],
在你的 .eslintrc.json 文件中会做你想做的事 - 至少 保存 如果你有
"eslint.autoFixOnSave": true
在您的设置中。 false 是默认值。我不相信您可以在输入时解决 "fix" 问题 - 但只能在保存时解决。你也可以修改这个设置
// An array of language ids which should be validated by ESLint
"eslint.validate": [
"javascript",
"javascriptreact"
],
GitHub 上有一个 closed issue 用于此功能。开发团队近期评论如下:
This feature request will not be considered in the next 6-12 months roadmap and as such will be closed to keep the number of issues we have to maintain actionable. Thanks for understanding and happy coding!
因此,在可预见的将来,它不会被收录。
现在唯一的选择是尝试创建一个完全可以做到这一点的扩展,甚至破解主编辑器的源代码。我建议你从这里开始:https://code.visualstudio.com/docs/extensions/overview
自 2019 年以来有一个名为 Python Indent 的扩展可用。你提到的方式在那里被称为“括号对之间”。这是该扩展的示例:
data = {'a': 0,
| # <- pressing enter should put your cursor at the "|"
| # <- This is where default VS Code puts your cursor.
在PEP 8中称为“与开始定界符对齐”。
我正在寻找修改 VS Code 的缩进行为,这样如果我在输入如下一行后按回车键:
variable = function(param1,
它将缩进到左括号的水平,这样我就可以像这样轻松地格式化代码:
variable = function(param1,
param2)
我希望它也适用于左方括号和大括号:
variable = function([1, 2, 3, 4
5, 6, 7, 8],
param2,
{'a': 1, 'b': 2,
'c': 3, 'd': 4},
param4)
我希望它对我使用的几乎所有语言都有这种行为,尽管在 C++ 或 C# 中工作时花括号行为不是必需的(甚至可能是不受欢迎的)。
这与 Sublime Text 的 indent_to_bracket
设置非常相似。
有什么办法可以实现吗?如果没有设置,我愿意修补任何必要的东西。我也愿意接受可以执行此操作的扩展程序,甚至在必要且有意义的情况下编写扩展程序。
看起来 ESLint 会为你做这件事。
Examples of correct code for this rule with the 2, { "FunctionExpression": {"parameters": "first"} } option:
/*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
var foo = function(bar, baz,
qux, boop) {
qux();
}
来自 indent rule : Function Expressions。
安装扩展 vscode-eslint integration 然后
If you haven't installed ESLint either locally or globally do so by running npm install eslint in the workspace folder for a local install or npm install -g eslint for a global install.
然后像
这样的规则"indent": ["error", 2, { "FunctionExpression": {"body": 1, "parameters": "first"} }],
在你的 .eslintrc.json 文件中会做你想做的事 - 至少 保存 如果你有
"eslint.autoFixOnSave": true
在您的设置中。 false 是默认值。我不相信您可以在输入时解决 "fix" 问题 - 但只能在保存时解决。你也可以修改这个设置
// An array of language ids which should be validated by ESLint
"eslint.validate": [ "javascript", "javascriptreact" ],
GitHub 上有一个 closed issue 用于此功能。开发团队近期评论如下:
This feature request will not be considered in the next 6-12 months roadmap and as such will be closed to keep the number of issues we have to maintain actionable. Thanks for understanding and happy coding!
因此,在可预见的将来,它不会被收录。
现在唯一的选择是尝试创建一个完全可以做到这一点的扩展,甚至破解主编辑器的源代码。我建议你从这里开始:https://code.visualstudio.com/docs/extensions/overview
自 2019 年以来有一个名为 Python Indent 的扩展可用。你提到的方式在那里被称为“括号对之间”。这是该扩展的示例:
data = {'a': 0,
| # <- pressing enter should put your cursor at the "|"
| # <- This is where default VS Code puts your cursor.
在PEP 8中称为“与开始定界符对齐”。