简体   繁体   English

如何阻止 VS Code 在保存时完全破坏我的代码?

[英]How do I stop VS Code from totally destroying my code on save?

I've gone through formatting settings and stack overflow posts, I've toggled with a bunch of different settings and can't seem to find an answer to this.我浏览了格式设置和堆栈溢出帖子,切换了一堆不同的设置,但似乎找不到答案。 Nothing in settings.json seems to matter, maybe it's being overwritten somehow? settings.json似乎无关紧要,也许它被以某种方式覆盖了?

My tab spacing is set at 2 spaces and every time I restart my computer, something seems to happen to VS Code where saving a file adds a ton of weird spacing rules and tabs to 4 spaces, as you can see in the gif below that make my code terrible.我的制表符间距设置为 2 个空格,每次我重新启动计算机时,VS Code 似乎会发生一些事情,其中保存文件会添加大量奇怪的间距规则和制表符到 4 个空格,正如您在下面的 gif 中看到的那样我的代码很糟糕。

I don't want to go back to Atom or Sublime but the way VS Code is working for me right now, I can't use it in a professional context.我不想 go 回到 Atom 或 Sublime,但 VS Code 现在为我工作的方式,我不能在专业环境中使用它。 Would love some help getting this figured out.希望得到一些帮助来解决这个问题。

Thanks!谢谢!

(image below is a gif, click it to see the style change on save) (下图为gif,点击查看保存后的风格变化)

在此处输入图像描述

Edit: installed packages in VS Code:编辑:在 VS Code 中安装的包:

在此处输入图像描述

在此处输入图像描述

I found the solution.我找到了解决方案。 It's not Prettier, its "JS-CSS-HTML Formatter" Just uninstall "JS-CSS-HTML Formatter" extension, then reload vscode, and it's done.它不是 Prettier,它的“JS-CSS-HTML Formatter”只需卸载“JS-CSS-HTML Formatter”扩展,然后重新加载 vscode,就可以了。 Also, no need to disable auto-format on save.此外,无需在保存时禁用自动格式化。

its so simple just uninstall Prettier extensions that you have install. 它是如此简单,只需卸载已安装的Prettier扩展。 Press Extension Tab on your left sidebar (shift command X on mac) and choose your Prettier extension and uninstall it. 按下左侧栏中的扩展程序选项卡(在Mac上为shift命令X),然后选择您的Prettier扩展程序并将其卸载。

Looks like prettier formats your code. 看起来您的代码更漂亮 Check your settings . 检查您的设置

Format On Save 保存时格式化

Respects editor.formatOnSave setting. 遵守editor.formatOnSave设置。

You can turn on format-on-save on a per-language basis by scoping the setting: 您可以通过设置以下范围的范围来启用每种语言的保存格式:

 // Set the default "editor.formatOnSave": false, // Enable per-language "[javascript]": { "editor.formatOnSave": true } 

Its probably Prettier that is doing this. 这可能是更漂亮的。

In your settings.json add these two lines: 在您的settings.json添加以下两行:

"editor.formatOnSave": false,

"prettier.disableLanguages": [
    "jsx",
    "js"
],

Or just uninstall prettier 或者只是卸载更漂亮

This comment turns out to have the answer. 这个评论原来是有答案的。 It happens when you have two settings that shouldn't conflict but do for some reason 当您有两个不应冲突但出于某种原因需要进行的设置时,就会发生这种情况

"editor.formatOnSave": false,
"javascript.format.enable": false

The solution was to uninstall vscode-JS-CSS-HTML-formatter which you can do by searching for the author, lonefy , in your packages. 解决方案是卸载vscode-JS-CSS-HTML-formatter ,您可以通过在软件包中搜索作者lonefy来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM