Visual Studio, for adjusting the indentation of the solution

I work on several different things, and some use tabs, some use 2 spaces for indentation, others 4 places for indentation, etc.

The ability to install this in Visual Studio in Tools โ†’ Options โ†’ Text Editor โ†’ <language> โ†’ Tabs

Is there a way to override these settings based on each solution?

+70
indentation visual-studio-2008
Jul 31 '09 at 11:44
source share
9 answers

Here is one (admittedly hacker) way to get 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) Connect to downloading your solution: In the macro explorer, select EnvironmentEvents , select SolutionEvents in the first drop-down list, Opened in the second. Now you have a macro that will run every time you open the solution. You just need to match your decisions with the required indentation.

+14
Jul 31 '09 at 12:27
source share

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

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

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

There is also an extension for Visual Studio code .

+70
Jul 12 2018-12-12T00:
source share

There is also another "Rebracer" add-on ... link here: http://visualstudiogallery.msdn.microsoft.com/410e9b9f-65f3-4495-b68e-15567e543c58

+6
Aug 6 '14 at 16:27
source share

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

The EditorConfig project defines a simple file format for customizing common text editor options , such as indent sizes . These configuration files are intended to be placed next to the source of the project code, allowing text editors to use the correct parameters in the file behind the base file. The EditorConfig project provides plugins for large, common text editors, making the format completely cross-platform.

(my accent)

Full disclosure: I could not personally verify it (because ReSharper takes over).

+3
Sep 23 '13 at 14:22
source share

VS2017 RC added support for .editorconfig format.

+3
Nov 28 '16 at 11:32
source share

You can set the Insert tabs / spaces parameter with props.Item("InsertTabs") = bool

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

0
May 4 '12 at 10:18
source share

UPDATE: it seems that the indentation is not controlled by Resharper, so this answer does not actually address the specific question. However, it is used in general terms; that is, "Visual Studio, for each solution parameter" in case someone stumbles here looking for it.




If you use Resharper , you can save the formatting settings (indeed, any settings) just for the solution. You can additionally indicate whether it is only for you (i.e., not transferred to the initial state) or general parameters that depend on the particular solution (i.e., Lock control source).

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

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

This creates the xml file YOURSOLUTION.sln.DotSettings.user with values, for example:

 <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

0
Jul 09 '13 at 14:21
source share

So, the decision says: โ€œYou just need to match your decisions with the required indentation,โ€ which is great, except for how you do it? Having spent a lot of time understanding this, I found the method that I prefer here . This method loads any number of exported parameters, which may be different for each solution. Settings can include anything from padding to colors or even window layout (I think).

  • In Visual Studio 2008, go to Tools> Macros> Macro Explorer
  • Double-click MyMacros> Module1 (if you do not have module 1, right-click MyMacros and select "Create Module ..." to create it).
  • In the macro explorer window, double-click "EnvironmentEvents" on the left.
  • In the upper left drop-down menu, select "SolutionEvents".
  • From the drop-down list in the upper right corner, select "Opened".
  • Code to handle the SolutionEvents.Opened event is automatically added. Change this 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 macro.

  • Use "Tools"> "Options" to change your interface to have tab settings and everything you want to set.
  • Tools> Import and Export Settings ...> Export Selected Environment Settings> Next
  • To simply export tab settings, uncheck the "All settings" box, then select "Options"> "Text editor"> "C / C ++"
  • Click "Next", then enter "solution.vssettings" as "What do you want to call your settings file?" and save the file where you have the solution you want to use.
  • Drag the .vssettings solution anywhere in the Solution Explorer window.
  • Close Visual Studio and the next time you open the solution containing solution.vssettings, it should load these settings.

If the macro does not run, it may be due to an MS security fix. Based on this, add the following line:

 <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 run the editor that you use to edit these files with administrator rights (for example, right-click Notepad and select "Run as administrator").

I also found a plugin that was supposed to do the same thing as the macro suggested above, but for some reason it did not do this after I ran its installer.

0
May 23 '14 at 21:02
source share

clang-format provides fairly sophisticated source code formatting options.

The Visual Studio plugin is rather rudimentary, i.e. it does not start automatically, but it does its job well (with a manual call).

http://llvm.org/builds/

0
Jul 08 '16 at 8:59
source share



All Articles