【问题标题】:VSCode autoformat going to 2 spaces instead of 4VSCode 自动格式化到 2 个空格而不是 4 个
【发布时间】:2017-05-23 07:57:52
【问题描述】:

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

Editor.tabsize 也显示 4。

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

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

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

【问题讨论】:

    标签: visual-studio-code


    【解决方案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 代码扩展程序

    【讨论】:

      【解决方案2】:

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

      前往

      File->Preferences->Settings
      

      您可以在搜索框中找到两者(查找tab sizedetect indentation),如下图所示(请参阅文本“让我们玩”)

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

      tab size => 设置为 4

      怎么做?需要可视化吗?在这里,

      右击下图并在新标签页中打开看清楚

      【讨论】:

        【解决方案3】:

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

        // 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
        

        【讨论】:

        • how to do it? 答案非常好,请麻烦添加一些指南,它完美地讲述了what to do,但每个人都不会得到how to do it
        • 我做到了 - 仍然没有工作。有什么想法吗?
        • 我非常讨厌这个 IDE。现在两个小时我无法解决这个问题......我用 4 个空格缩进文件,当我点击自动格式化快捷方式时,它又回到 2......我不知道到底发生了什么......
        • 我认为这是来自 vs code 的错误
        • @LachezarRaychev - 您可能安装了 Prettier 等扩展程序,它覆盖了 VSCode 的内置设置。找到该扩展的配置并在那里配置您的缩进设置。
        猜你喜欢
        • 2016-08-29
        • 1970-01-01
        • 2018-08-16
        • 1970-01-01
        • 1970-01-01
        • 2022-07-12
        • 2023-04-10
        • 2019-01-29
        相关资源
        最近更新 更多