簡體   English   中英

在 VS2022 中,您可以設置一個解決方案范圍的編輯器配置,並覆蓋項目特定的編輯器配置嗎?

[英]In VS2022 can you setup a solution wide editorconfig, and and project specific editorconfig that override?

在 Visual Studio 2022 中,您能否設置一個解決方案范圍的 .editorconfig 文件,然后投影特定的 .editorconfig 文件來覆蓋或添加到解決方案范圍的文件中?

就我而言,我希望為單元測試項目設置不同的設置,和/或在遺留項目上禁用遺留方法警告。

Visual Studio 2022 支持嗎?

是的, .editorconfig文件的設置是分層的,其中子.editorconfig文件中的設置添加或覆蓋父.editorconfig文件中的設置。

甚至可以通過在子文件夾中包含root = true來停止 inheritance 從子文件夾中的某個級別開始.editorconfig文件。

文檔

當您將.editorconfig文件添加到文件層次結構中的文件夾時,其設置將應用於該級別及以下級別的所有適用文件。

要覆蓋部分或全部 EditorConfig 設置,請在您希望應用這些覆蓋設置的文件層次結構級別添加一個.editorconfig文件。 新的 EditorConfig 文件設置適用於同一級別的文件和任何子目錄。

如果您想要覆蓋某些但不是全部設置,請僅在.editorconfig文件中指定這些設置。 僅覆蓋您在較低級別文件中明確列出的那些屬性。 來自更高級別.editorconfig文件的其他設置繼續適用。

這樣的文件夾和文件層次結構可能如下所示。

\ YourSolution
  - YourSolution.sln
  - .editorconfig
    ...      
  \ Src      
    \ Project1
      ...    
    \ Project2    
      ...
    \ Project3
      - .editorconfig    
      ...            
  \ Tests      
    - .editorconfig        
    \ Tests1
      ...          
    \ Tests2
      - .editorconfig

.editorconfig文件也可以位於所有 Visual Studio 解決方案的公共父文件夾中。

\ YourSolutions
  - .editorconfig
  \ YourSolution1
    - YourSolution1.sln  
    ...
  \ YourSolution2
    - YourSolution2.sln
    ...
    

pfx 的回答是使用 .editorconfig 文件的一個很好的概述——如上所述,它們是基於目錄的層次結構,因此優先級總是暗示屬性與文件的定義有多接近(不能有兩個 .editorconfig 文件同等優先)。

作為補充答案,因為您標記了 C#,我還會提到.globalconfig 文件 其中 .editorconfig 文件適用於目錄的內容,.globalconfig 文件用於根據項目應用分析器規則(或作為 NuGet 包的一部分)。 例如,如果您有從項目文件夾外部鏈接到項目的文件,則該項目的 .editorconfig 設置不會應用於這些文件,但 .globalconfig 設置

全局配置文件可以隱式繼承或顯式包含在每個項目的基礎上(如果命名不是“.globalconfig”)。 請參閱https://learn.microsoft.com/en-us/do.net/fundamentals/code-analysis/configuration-files#naming 如果您的項目結構如下所示,這會有所幫助:

\ YourSolution
  - YourSolution.sln
  - .editorconfig
  - Directory.Build.props
    ...      
  \ Src      
    \ Feature1
      \src
        - Feature1.csproj
        ...
      \test
        - Feature1.Test.csproj
        ...
    \ Feature2
      \src
        - Feature2.csproj
        ...
      \test
        - Feature2.Test.csproj
        ...

在這種情況下,您可以在 Directory.Build.props 文件中定義一些內容以僅應用於測試項目,例如

<PropertyGroup Condition="$(MSBuildProjectName.EndsWith('.Test'))">
  <GlobalAnalyzerConfigFiles Include="AnalyzerSettingsForTestProjects.globalconfig" />
</PropertyGroup>

Globalconfig 文件可能相互沖突或與適用於相同文件的 .editorconfig 文件沖突。 Globalconfig 文件可以指定一個global_level屬性來指定它們的相對優先級。 .editorconfig 將勝過.globalconfig。 沖突解決規則列於此處: https://learn.microsoft.com/en-us/do.net/fundamentals/code-analysis/configuration-files#precedence

暫無
暫無

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

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