visual-studio “Tabify”Visual Studio 解决方案中的所有文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3035566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:06:24  来源:igfitidea点击:

"Tabify" all files in Visual Studio solution?

visual-studiovisual-studio-2010tabs

提问by Borek Bernard

There's a "tabify" command in

有一个“tabify”命令

Edit > Advanced > Tabify Selected Lines

编辑 > 高级 > Tabify 选定的行

(and the Power Tools 2010 also provide this functionality on a per-file basis) but is there a way to do this for all code files in a solution?

(并且 Power Tools 2010 也基于每个文件提供此功能)但是有没有办法对解决方案中的所有代码文件执行此操作?

ReSharper has a Clean Up command but the only half-suitable option I found there is to run formatting on all files which does more than I want (I don't want to run a complete formatting, just tabifying).

ReSharper 有一个清理命令,但我发现唯一一个半合适的选项是对所有文件运行格式化,这比我想要的更多(我不想运行完整的格式化,只是制表)。

回答by Pete Stens?nes

If you have added the Microsoft Productivity Power toolsextension (which if you haven't I would recommned) it adds an option to tabify files. This does not apply across all files in a solution, but it's prompted for when editing each file, on a per file basis. Not quite what you're after but a help.

如果您添加了Microsoft Productivity Power 工具扩展(如果您没有,我会推荐)它会添加一个选项来标记文件。这并不适用于解决方案中的所有文件,但在编辑每个文件时会根据每个文件提示输入。不是你想要的,而是一个帮助。

Also you might try setting your IDE editor settings to use tabs, then do menu-edit-advanced-format document (CTRL+E,D). This will replace groups of tab length spaces with a tab, and that should be scriptable for all files in the solution via a macro.

您也可以尝试将 IDE 编辑器设置设置为使用选项卡,然后执行菜单编辑高级格式文档(CTRL+E,D)。这将用制表符替换制表符长度空格组,并且应该可以通过宏为解决方案中的所有文件编写脚本。

回答by Rami A.

The request contains links to IDE macros that can do the job:
http://blogs.msdn.com/b/kevinpilchbisson/archive/2004/05/17/133371.aspx
http://web.archive.org/web/20090217094033/http://chriseargle.com/post/Format-Solution.aspx

该请求包含指向可以完成这项工作的 IDE 宏的链接:
http: //blogs.msdn.com/b/kevinpilchbisson/archive/2004/05/17/133371.aspx
http://web.archive.org/web/ 20090217094033/http://chriseargle.com/post/Format-Solution.aspx

Here is sample code for a Visual Studio macro that automatically formats all *.cs, *.h, *.cpp, and *.hpp files in an open solution, which includes converting spaces to tabs (depending on your Tab settings in Tools > Options > Text Editor > specific language or "All Languages" > Tabs):

以下是 Visual Studio 宏的示例代码,该宏在打开的解决方案中自动格式化所有 *.cs、*.h、*.cpp 和 *.hpp 文件,其中包括将空格转换为制表符(取决于您在工具 >选项 > 文本编辑器 > 特定语言或“所有语言” > 标签):

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module ConvertTabsToSpaces

    Public Sub FormatSolution()
        Dim sol As Solution = DTE.Solution
        For i As Integer = 1 To sol.Projects.Count
            FormatProject(sol.Projects.Item(i))
        Next
    End Sub

    Private Sub FormatProject(ByVal proj As Project)
        If Not proj.ProjectItems Is Nothing Then
            For i As Integer = 1 To proj.ProjectItems.Count
                FormatProjectItem(proj.ProjectItems.Item(i))
            Next
        End If
    End Sub

    Private Sub FormatProjectItem(ByVal projectItem As ProjectItem)
        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
            If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                window.Activate()
                projectItem.Document.DTE.ExecuteCommand("Edit.FormatDocument")
                window.Close(vsSaveChanges.vsSaveChangesYes)
            ElseIf ((projectItem.Name.LastIndexOf(".cpp") = projectItem.Name.Length - 4) OrElse (projectItem.Name.LastIndexOf(".hpp") = projectItem.Name.Length - 4) OrElse (projectItem.Name.LastIndexOf(".h") = projectItem.Name.Length - 2)) Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                window.Activate()
                projectItem.Document.DTE.ExecuteCommand("Edit.SelectAll")
                projectItem.Document.DTE.ExecuteCommand("Edit.FormatSelection")
                window.Close(vsSaveChanges.vsSaveChangesYes)
            End If
        End If

        'Be sure to format all of the ProjectItems.
        If Not projectItem.ProjectItems Is Nothing Then
            For i As Integer = 1 To projectItem.ProjectItems.Count
                FormatProjectItem(projectItem.ProjectItems.Item(i))
            Next
        End If

        'Format the SubProject if it exists.
        If Not projectItem.SubProject Is Nothing Then
            FormatProject(projectItem.SubProject)
        End If
    End Sub

End Module

Instructions (Visual Studio 2005, but similar for newer versions):

说明(Visual Studio 2005,但新版本类似):

  1. Launch Visual Studio
  2. Tools > Macros > Macros IDE...
  3. Right-click MyMacros > Add > Add New Item...
  4. Select Module
  5. Enter "ConvertSpacesToTabs" without quotes in the Name field
  6. Click Add
  7. Replace the contents of the new module with the code above
  8. Click Save
  9. Close the Macros IDE
  10. Tools > Macros > Macro Explorer
  11. Expand MyMacros > ConvertSpacesToTabs
  12. Double-click on FormatSolution
  13. Wait for the macro to finish
  1. 启动 Visual Studio
  2. 工具 > 宏 > 宏 IDE...
  3. 右键单击 MyMacros > 添加 > 添加新项...
  4. 选择模块
  5. 在名称字段中输入不带引号的“ConvertSpacesToTabs”
  6. 点击添加
  7. 用上面的代码替换新模块的内容
  8. 点击保存
  9. 关闭宏 IDE
  10. 工具 > 宏 > 宏资源管理器
  11. 展开 MyMacros > ConvertSpacesToTabs
  12. 双击 FormatSolution
  13. 等待宏完成

Edit
I updated the code to also support *.h, *.cpp, and *.hpp files using code from Siegmund Frenzel here: https://stackoverflow.com/a/14766393/90287

编辑
我使用 Siegmund Frenzel 的代码更新了代码以支持 *.h、*.cpp 和 *.hpp 文件:https://stackoverflow.com/a/14766393/90287

回答by brokenisfixed

as far as I know what "Tabify" does is this - it only replaces " " (4 spaces) with a tab, it does not change the formatting or anything else.

据我所知,“Tabify”的作用是——它只用一个制表符替换“”(4 个空格),它不会改变格式或其他任何东西。

Although I would suggest using document formatting, the "tabification" could easily be done via a custom application which would mimic the same action on all the files that you want.

虽然我建议使用文档格式,但“制表”可以通过自定义应用程序轻松完成,该应用程序可以对您想要的所有文件进行相同的操作。

Hope this helps!

希望这可以帮助!

回答by jswolf19

For vs2010, you can use the following find and replace (this example is for tabs to 4 spaces).

对于vs2010,可以使用下面的查找替换(这个例子是制表符到4个空格)。

In the find box, enter: ^{ *}(^{space *}tab)

在查找框中输入:^{ *}^{空间*}选项卡)

In the replace box, enter \1(\1space space space space)

在替换框中,输入\1\1空格空格空格空格)

Check the condition box and set to regular expressions. Newer versions of vs use different regular expression syntax, but the same should be doable.

选中条件框并设置为正则表达式。较新版本的 vs 使用不同的正则表达式语法,但相同的应该是可行的。

UpdateThis worked by executing once for vb files, but required multiple passes for a resx file, so you may have to execute multiple times depending on the file type...

更新这是通过对 vb 文件执行一次来工作的,但需要多次通过 resx 文件,因此您可能必须根据文件类型执行多次...