VisualStudio,每个解决方案缩进设置

我正在做一些不同的事情,有些使用制表符,有些使用2个空格缩进,另一些用户使用4个空格缩进等。

在 VisualStudio 中设置此选项的选项是 Tools-> Options-> Text Editor-> < language >-> Tabs

是否有办法在每个解决方案的基础上覆盖这些设置?

24238 次浏览

Here is one (admittedly hacky) way to achieve what you are looking for:

1) create a macro that changes the indentation (source)

 Sub Set-Indent(indent As integer)
Dim props As EnvDTE.Properties = DTE.Properties("TextEditor", "C/C++")
Dim ts As EnvDTE.Property = props.Item("TabSize")
Dim ins As EnvDTE.Property = props.Item("IndentSize")
ts.Value = indent
ins.Value = indent
End Sub

2) Hook that up with your solution loading: In the macro explorer, choose EnvironmentEvents, select SolutionEvents in the first drop-down, Opened in the second. You now have a macro that will trigger every time you open a solution. You just need to map your solutions to the required indentation.

You can set the Insert Tabs/Spaces setting with props.Item("InsertTabs") = bool

Check this out: https://github.com/jamesfoster/VS-Whitespace-Macros

UPDATE: VS 2017 supports EditorConfig natively: https://blogs.msdn.microsoft.com/dotnet/2016/12/15/code-style-configuration-in-the-vs2017-rc-update/

In VS 2010 and above, there's an extension that sets the indentation based on .editorconfig file in the solution/project root:

http://visualstudiogallery.msdn.microsoft.com/c8bccfe2-650c-4b42-bc5c-845e21f96328

There's also similar extension for Visual Studio Code.

UPDATE: seems like indentation is not managed by Resharper, so this answer does not actually address the specific question. However, it does apply in general terms; i.e. "Visual Studio, per solution settings" in case anyone stumbles here looking for that.


If you are using Resharper, you can save formatting settings (really, any settings) for just the solution. You can further specify if just for you (i.e. not committed to source-control) or shared solution-specific settings (i.e. commit to source-control).

Resharper > Options > Code Editing > C# (or whatever language) > Formatting Style > Braces Layout

Then at the bottom of the window, under Save To choose "Solution XYZ personal" (or shared, depending on your purpose).

This creates an xml file YOURSOLUTION.sln.DotSettings.user with values like:

<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue">NEXT_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CASE_BLOCK_BRACES/@EntryValue">NEXT_LINE</s:String>

Resharper Settings - Save for Solution Only

From the VS extension EditorConfig (http://visualstudiogallery.msdn.microsoft.com/c8bccfe2-650c-4b42-bc5c-845e21f96328) website:

The EditorConfig project defines a simple file format for configuring common text-editor options such as indentation sizes. These configuration files are designed to sit alongside a project's source code, allowing text editors to use the right options on a file-by-file basis. The EditorConfig project provides plugins for many common text editors, making the format fully cross-platform.

(emphasis mine)

Full Disclosure: I have not been able to personally test it (because ReSharper takes over).

So, the accepted solution says "You just need to map your solutions to the required indentation", which is great, except how do you do that? After wasting a lot of time trying to figure that out, I found a method I prefer here. This method loads any number of exported settings that can be different for every solution. Settings can include anything, from indents to colors or even window layout (I think).

  • In Visual Studio 2008, open Tools > Macros > Macro explorer
  • Double click MyMacros > Module1 (if you don't have Module1, right click MyMacros and choose 'New module...' to create it).
  • In the macro explorer window, double click 'EnvironmentEvents' on the left.
  • In the upper-left dropdown, choose 'SolutionEvents'.
  • In the upper-right dropdown, choose 'Opened'.
  • Code to handle the SolutionEvents.Opened event is automatically added. Change that code to the following:

    Private Sub SolutionEvents_Opened() Handles SolutionEvents.Opened
    Dim item As ProjectItem = DTE.Solution.FindProjectItem("solution.vssettings")
    If Not item Is Nothing Then
    'MsgBox("Uncomment this to see a message when settings are loaded")
    Dim name = item.FileNames(1)
    DTE.ExecuteCommand("Tools.ImportandExportSettings", "/import:""" & name & """")
    End If
    End Sub
    
  • Save the macro.

  • Use Tools > Options to change your UI to have the tab settings and whatever else you want set.
  • Tools > Import and Export Settings... > Export selected environment settings > Next
  • To just export tab settings, uncheck 'All Settings' then check Options > Text Editor > C/C++
  • Click Next, then type 'solution.vssettings' as 'What do you want to name your settings file?' and save the file to wherever you have a solution you want to use those settings.
  • Drag solution.vssettings to any location in the Solution Explorer window.
  • Quit Visual Studio and next time you open a solution containing solution.vssettings, it should load those settings.

If the macro doesn't run, it could be because of a MS security patch. Based on this, add the following string:

<AllowDComReflection enabled="true"/>

Below the <runtime> tag in the following three files:

"C:\Program Files (x86)\Common Files\Microsoft Shared\VSA\9.0\VsaEnv\vsmsvr.exe.config"
"C:\Program Files (x86)\Common Files\Microsoft Shared\VSA\9.0\VsaEnv\vsaenv.exe.config"
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe.config"

Be sure to start the editor you use to edit those files with Administrator privileges (ie right click Notepad and choose 'Run as Administrator').

I also found a plug-in that's supposed to do the same thing as the macro suggested above but for some reason it didn't do anything after I ran its installer.

There's also another Add-in called "Rebracer" now... link is here: http://visualstudiogallery.msdn.microsoft.com/410e9b9f-65f3-4495-b68e-15567e543c58

clang-format provides quite sophisticated options for formatting your source code.

The Visual Studio plugin is quite rudimentary, i.e. it does not run automatically, but it does its job well (when manually invoked, that is).

http://llvm.org/builds/

VS2017 RC added support for .editorconfig format.