Visual Studio,每个解决方案缩进设置

发布于 2024-07-29 11:26:28 字数 183 浏览 5 评论 0 原文

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

在 Visual Studio 中设置此选项的选项位于“工具”->“选项”->“文本编辑器”中-><语言>->选项卡

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

I'm working on a couple of different things and some use tabs, some use 2 spaces for indents, another users 4 spaces for indents etc.

The option to set this in Visual Studio is in Tools->Options->Text Editor-><language>->Tabs

Is there some way to override these settings on a per solution bases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

美人骨 2024-08-05 11:26:28

更新:VS 2017 原生支持 EditorConfig: https://blogs.msdn.microsoft.com/dotnet/2016/12/15/code-style-configuration-in-the-vs2017-rc-update/

在 VS 2010 和上面,有一个扩展根据解决方案/项目根目录中的 .editorconfig 文件设置缩进:

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

还有类似的 Visual Studio Code 扩展

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.

通知家属抬走 2024-08-05 11:26:28

这是实现您正在寻找的目标的一种(诚然是黑客)方法:

1)创建一个更改缩进的宏(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) 将其与您的解决方案加载挂钩:
在宏资源管理器中,选择
EnvironmentEvents,在第一个下拉列表中选择 SolutionEvents,在第二个下拉列表中选择 Opened
现在,您有了一个每次打开解决方案时都会触发的宏。
您只需将解决方案映射到所需的缩进即可。

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.

偏爱自由 2024-08-05 11:26:28

现在还有另一个名为“Rebracer”的加载项...链接位于:http://visualstudiogallery.msdn.microsoft.com/410e9b9f-65f3-4495-b68e-15567e543c58

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

深海里的那抹蓝 2024-08-05 11:26:28

从 VS 扩展 EditorConfig (http://visualstudiogallery.msdn.microsoft.com /c8bccfe2-650c-4b42-bc5c-845e21f96328)网站:

EditorConfig 项目定义了用于配置的简单文件格式
常见的文本编辑器选项例如缩进大小。 这些
配置文件被设计为与项目源代码并存
代码,允许文本编辑器在逐个文件上使用正确的选项
基础。 EditorConfig项目提供了许多常见文本的插件
编辑器,使格式完全跨平台。

(强调我的)

全面披露:我无法亲自测试它(因为 ReSharper 接管)。

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).

冷了相思 2024-08-05 11:26:28

VS2017 RC 添加了对 .editorconfig 格式的支持。

VS2017 RC added support for .editorconfig format.

风尘浪孓 2024-08-05 11:26:28

您可以使用 props.Item("InsertTabs") = bool 设置插入制表符/空格设置,

请查看:https://github.com/jamesfoster/VS-Whitespace-Macros

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

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

咋地 2024-08-05 11:26:28

更新:似乎缩进不是由Resharper管理的,所以这个答案实际上并没有解决具体问题。 然而,它确实适用于一般术语; 即“Visual Studio,每个解决方案设置”,以防有人在这里偶然发现它。


如果您使用 Resharper,您可以保存格式设置(实际上是任何设置)以获取解决方案。 您可以进一步指定是否只适合您(即不致力于源代码控制)或共享特定于解决方案的设置(即提交源代码控制)。

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

然后在窗口底部的保存到下选择“解决方案 XYZ 个人”(或共享,具体取决于您的目的)。

这将创建一个 xml 文件 YOURSOLUTION.sln.DotSettings.user,其值如下:

<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仅解决方案

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

又爬满兰若 2024-08-05 11:26:28

因此,公认的解决方案说“您只需将解决方案映射到所需的缩进”,这很好,但您如何做到这一点呢? 在浪费了大量时间试图弄清楚之后,我找到了一种我更喜欢的方法 这里。 此方法加载任意数量的导出设置,这些设置对于每个解决方案可能有所不同。 设置可以包括任何内容,从缩进到颜色甚至窗口布局(我认为)。

  • 在 Visual Studio 2008 中,打开“工具”>“工具”。 宏> 宏资源管理器
  • 双击 MyMacros > Module1(如果没有 Module1,请右键单击 MyMacros 并选择“新模块...”来创建它)。
  • 在宏资源管理器窗口中,双击左侧的“EnvironmentEvents”。
  • 在左上角的下拉列表中,选择“SolutionEvents”。
  • 在右上角的下拉列表中,选择“已打开”。
  • 自动添加处理 SolutionEvents.Opened 事件的代码。 将该代码更改为以下内容:

    私有子 SolutionEvents_Opened() 处理 SolutionEvents.Opened 
          暗淡项目 As ProjectItem = DTE.Solution.FindProjectItem("solution.vssettings") 
          如果不是项目什么都不是那么 
              'MsgBox(“取消注释此选项可在加载设置时查看消息”) 
              暗淡名称 = item.FileNames(1) 
              DTE.ExecuteCommand("Tools.ImportandExportSettings", "/import:""" & name & """") 
          万一 
      结束子 
      
  • 保存宏。

  • 使用工具> 用于更改用户界面以进行选项卡设置以及您想要设置的任何其他内容的选项。
  • 工具> 导入和导出设置...>> 导出选定的环境设置> 接下来
  • 要仅导出选项卡设置,请取消选中“所有设置”,然后选中“选项”>“选项卡”。 文本编辑器> C/C++
  • 单击“下一步”,然后键入“solution.vssettings”作为“您想将设置文件命名为什么?” 并将文件保存到您有想要使用这些设置的解决方案的任何位置。
  • 将 Solution.vssettings 拖到“解决方案资源管理器”窗口中的任意位置。
  • 退出 Visual Studio,下次打开包含 Solution.vssettings 的解决方案时,它应该加载这些设置。

如果宏未运行,可能是由于 MS 安全补丁的原因。 基于 this,添加以下字符串:

<AllowDComReflection enabled="true"/>

下面 标记以下三个文件:

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

请确保以管理员权限启动用于编辑这些文件的编辑器(即右键单击“记事本”并选择“以管理员身份运行”)。

我还发现 一个插件,它应该执行与上面建议的宏相同的操作,但由于某种原因,在我运行其安装程序后它没有执行任何操作。

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.

花心好男孩 2024-08-05 11:26:28

clang-format 提供了相当复杂的选项来格式化源代码。

Visual Studio 插件非常初级,即它不会自动运行,但它可以很好地完成其工作(即手动调用时)。

http://llvm.org/builds/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文