Visual Studio 2019 忽略选项卡首选项

Visual Studio 2019 ignoring tab preference

今天早上我在 VS2019 编程,然后开始我的一天。当我晚上坐下来继续编程时,我意识到它突然被放置了空格而不是制表符!我的首选项是为每种语言设置制表符而不是空格,并且在前几天一直这样做。我更新了 VS 以确保,但问题仍然存在 - 无论我的偏好设置如何,都会无处不在地放置空格而不是制表符。

PS:我知道这个问题已经被问过 here,但他们的解决方案是更新 VS - 这对我来说没有任何改变。

我找到了解决办法!在选项中,转到“文本编辑器”>“高级”并关闭 "Use adaptive formatting"。它正在根据给定文件的现有格式覆盖我的首选项

尝试所有这些(解决了我的问题):

工具 -> 选项 -> 文本编辑器 -> 高级 -> 关闭“使用自适应格式”

工具 -> 选项 -> 文本编辑器 -> 所有语言 -> 标签 -> 保留标签

工具 -> 选项 -> 文本编辑器 -> C# -> 选项卡 -> 保留选项卡

也许这有帮助,因为我遇到了同样的问题,检查了答案中提到的所有常见位置 Tools -> Options -> Editor -> yadda yadda ... 但没有任何帮助。

现在我找到了隐藏在一些评论中的解决方案。我从 git 下载的项目在范围内有一个名为 .editorconfig 的文件,它覆盖了您可以在 Tools 菜单中设置的设置。

寻找一行

indent_style = spaces

并将其更改为

indent_style = tab

这非常令人沮丧 - 我发现即使我正确设置了编辑器配置,VS2019 也会自发地将标签插入到文件中。

我非常喜欢制表符,但在我目前的工作中他们使用空格,所以你必须接受共识。

令他们沮丧的是,我的文件将使用选项卡签入,当然 Git 会对您喋喋不休地谈论更改。

事实证明,根据这个线程:https://developercommunity.visualstudio.com/t/visual-studio-20194-c-insert-spaces-instead-of-tab/847853 Visual Studio 2019 基本上会忽略您的设置,并根据文件的内容决定自发切换到它认为正确的设置。

这显然无法更改:

This is the Adaptive Formatting behavior in Visual Studio: the Editor heuristically determines if the current file should use Tabs or Spaces for indentation.

We don’t currently have an option to disable adaptive white space, but if folks feel that’s important, we can add it.

很多人抱怨过,但后来你得到了“我们在吱吱作响的轮子上工作”的(现在的标准)免责声明。

所以我们只能靠自己了。

因此您可以尝试将其关闭,但 VS2019 仍会以其认为正确的方式维护文件 - 即使文件中只有一个 TAB。

因此,唯一的完整修复,同样直接来自 Microsoft:

Third, if you use .editorconfig in your code repo, we will ALWAYS honor those settings. It’s not a requirement to use .editorconfig, but anyone who is particular interested in maintaining a coding style should know that our of the VS guiding principles is to never believe we’re “smart enough” to override .editorconfig.

我的完整编辑器配置:

[*]
indent_style = space
indent_size = 3
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

注意“end_of_line”...这也解决了 WSL 的问题,否则 Git 可能会将 .sh 文件中的行结尾更改为 CRLF...这不起作用使用 WSL。

除了这个额外的解决方案,设置你的 .gitattributes 文件:

* text=auto eol=lf

*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf
*.cs                  text eol=crlf
*.xaml                text eol=crlf
*.csproj              text eol=crlf
*.njsproj             text eol=crlf
*.pyproj              text eol=crlf
*.sln                 text eol=crlf

一石二鸟