簡體   English   中英

VSCode格式被覆蓋

[英]VSCode formatting being overrided

我正在使用Angular進行開發,經過將近一年的完美格式化,我的vscode才剛剛起步。 更具體地說,當我單擊“格式文檔”時。 它縮進2個空格而不是4個空格。如何解決此問題,或檢查什么覆蓋了我的設置?

我的用戶和工作區設置(它們相同):

{
  "editor.fontSize": 16,
  "editor.tabSize": 4,
  "editor.insertSpaces": true,
  "editor.wordWrap": "off",
  "workbench.iconTheme": "material-icon-theme",
  "editor.detectIndentation": true,
  "prettier.bracketSpacing": true,
  "prettier.eslintIntegration": true,
  "prettier.singleQuote": true,
  "prettier.tabWidth": 4,
  "prettier.useTabs": false,
}

我的擴展程序列表:

Angular Essentials (注意更漂亮是一個依賴項,並且已安裝所有其他dep)

支架對 着色器語言-vscode-javascript-angular2

編輯:

同樣,它在我的編輯器底部顯示縮進2個空格,當我單擊它並單擊4個空格時,它表示已配置的選項卡大小。 如果不是默認文件,為什么不為每個文件設置呢?

我相信這是因為Angular Essentials Prettier插件默認縮進2,因此您必須為Prettier創建一個單獨的配置才能正常工作。

看到這個github帖子

看來這可能是兩件事的結果:

EditorConfig

首先,注釋中提到的.editorconfig文件是Angular生成的單獨文件(特別是與VSCode不相關)。 它可作為任何編輯者閱讀和服從的指南,允許開發人員以相同的樣式處理同一個項目,但可以選擇多個IDE /編輯器。 它看起來應該像這樣:

# Editor configuration, see https://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false

正如評論中已經指出的那樣,可以在https://editorconfig.org上找到更多信息。

在您的情況下,您需要將indent_size = 2行更改為indent_size = 4

角度CLI

如果這樣做不能解決問題,也可能是Angular CLI正在生成具有2個空格的文件,而VSCode正在根據"editor.detectIndentation": true設置檢測到該縮進。 這已經是一段時間的熱門話題 ,應該可以解決這個問題 ,但是根據這個評論似乎沒有幫助。

我使用VSCode中的“ 語言特定設置”修復了此問題。 我選擇將制表符的大小強制設置為4,並對Angular生成的所有文件類型禁用縮進檢測。 您可以將其放在VSCode用戶設置中,或按照以下說明進行操作 ,以獲取“正確”的路由。

{
    "[typescript]": {
        "editor.tabSize": 4,
        "editor.detectIndentation": false
    },
    "[javascript]": {
        "editor.tabSize": 4,
        "editor.detectIndentation": false
    },
    "[html]": {
        "editor.tabSize": 4,
        "editor.detectIndentation": false
    },
    "[json]": {
        "editor.tabSize": 4,
        "editor.detectIndentation": false
    },
    "[jsonc]": {
        "editor.tabSize": 4,
        "editor.detectIndentation": false
    }
}

添加"editor.formatOnSave": true或只是在任何地方都按Ctrl + Shift + I (格式化文檔)即可。

"editor.formatOnPaste": true如果經常粘貼2個空格的示例代碼,則"editor.formatOnPaste": true可能也很有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM