VSCode 自动格式化为 2 个空格而不是 4 个

问题描述 投票:0回答:5

每当我让 vscode 自动格式化我的代码时,它都会格式化为 2 个空格而不是 4 个空格,尽管显然默认值是 4 个空格,而且我还没有触及 settings.json

Editor.tabsize 也显示 4。

在右下角,它显示空格:4,但自动格式化后,它显示空格:2,并且无论我将其更改为4多少次,它都会恢复为2。

任何人都知道发生了什么事。 Ruby 正在发生这种情况,如果这很重要的话。

另外,有人知道 VScode 的替代压痕器/自动格式化程序吗?

visual-studio-code
5个回答
44
投票

我认为您可能对“自动检测缩进”有问题。您应该尝试将其关闭,看看是否有帮助。

// The number of spaces a tab is equal to. This setting is overriden based on the file contents when `editor.detectIndentation` is on.
"editor.tabSize": 4,

// When opening a file, `editor.tabSize` and `editor.insertSpaces` will be detected based on the file contents.
"editor.detectIndentation": false

22
投票

对于那些像我一样无法使用以上内容的人,这里有一些 GUI 指南。

按 = >

CTRL
+
,
(评论建议)打开设置或通过菜单进入设置

File->Preferences->Settings   

您可以在搜索框中找到两者(找到

tab size
detect indentation
),如下图所示(请参阅文本“让我们玩吧”)

detect indentation
=> 取消选中它(关闭)

tab size
=> 将其设置为 4

如何?需要可视化吗?在这里,

右键单击以下图片并在新选项卡中打开看清楚


1
投票

我使用https://editorconfig.org/

解决了这个问题
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

确保还安装了 VS Code 扩展


1
投票

另一个对我有用的解决方案:

  1. 进入设置
  2. 在搜索栏中输入“Prettier:使用选项卡”
  3. 确保此设置已开启

0
投票

对我来说,问题是由 prettier 扩展引起的,因为它的默认制表符宽度为 2。

转到设置 -> 并搜索

prettier: tab width
并将该值更改为 4。

Prettier: Tab Width

© www.soinside.com 2019 - 2024. All rights reserved.