user960341
user960341

Reputation: 21

How can you sort tabs in Visual Studio 2008 by file type?

I have been trying (without much luck) to write a macro for Visual Studio 2008 that will sort opened .cpp and .h files into two tab groups, placing headers on the left tab group and .cpp files on the right. I would love to have this feature because of the amount of time I spend moving my tabs around so that I can see both parts of the class that I am working on. I know that there is a non-free add-on for visual studio that allows tab managing, but it conflicts with an add-on that I need to use for work, making a macro my best option so far.

I am sure that this could be applied to other layouts and sorting needs as well if I can get it working. My thinking was to make the sorting happen automatically for me each time I open a document window, so I created a visual basic macro in the environment events section of the Visual Studio Macro IDE. Below is the code that I have so far:

Public Sub keepHeaderAndCxxInDifferentTabs() Handles WindowEvents.WindowCreated
     Dim openedFile As String
     openedFile = ActiveWindow.Document.FullName

     If openedFile.Contains(".h") Then
         ' if the file being opened is a header file make sure it appears on the left
         If DTE.ActiveDocument.ActiveWindow.Left > 600 Then
             DTE.ExecuteCommand("Window.MovetoNextTabGroup")
         End If
     ElseIf openedFile.Contains(".cpp") Then
         ' if the file being opened is a cpp file make sure it appears on the right
         If DTE.ActiveDocument.ActiveWindow.Left < 250 Then
             DTE.ExecuteCommand("Window.MovetoNextTabGroup")
         End If
     Else
         ' if the file being opened is anything else make sure it appears on the right
         If DTE.ActiveDocument.ActiveWindow.Left < 250 Then
             DTE.ExecuteCommand("Window.MovetoNextTabGroup")
         End If
     End If
 End Sub

Sadly this macro currently does not do anything. My best guess was that I could use the 'Left' property of the active window to figure out which tab group the new window appeared in and then move the window to the next tab group if it is not where I want it to be. I have tried a range of different values for the 'Left' property but so far none of my tabs move.

Does anyone know what I did wrong or have any suggestions? Thank you in advance for your time.

Upvotes: 1

Views: 504

Answers (1)

user960341
user960341

Reputation: 21

I found a way to do what I wanted using the function below. As long as I have my two vertical tabs setup when I run the macro it puts all headers in the left tab group and all other files in the right tab group. This can be further extended so that when I open any files using any other macros I write it sorts them as well by making a call to it after the macro has run. Sadly this does not work automatically, I am having problems getting it to actually perform the sorting whenever a specific event is triggered (using the environment events section).

'=====================================================================
' Sorts all opened documents putting headers into the left tab group
' and everything else into the right tab group
'===================================================================== 
Public Sub SortFilesInTabs()
    For i = 1 To DTE.Windows.Count Step 1
        If DTE.Windows.Item(i).Document IsNot Nothing Then
            If DTE.Windows.Item(i).Document.FullName.Contains(".h") Then
                ' if the file is a header file make sure it appears on the left
                If DTE.Windows.Item(i).Document.ActiveWindow.Left > 600 Then
                    WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to left")
                    DTE.Windows.Item(i).Document.Activate()
                    DTE.ExecuteCommand("Window.MovetoPreviousTabGroup")
                End If
            ElseIf DTE.Windows.Item(i).Document.FullName.Contains(".cpp") Then
                ' if the file is a cpp file make sure it appears on the right
                If DTE.Windows.Item(i).Document.ActiveWindow.Left < 250 Then
                    WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to right")
                    DTE.Windows.Item(i).Document.Activate()
                    DTE.ExecuteCommand("Window.MovetoNextTabGroup")
                End If
            ElseIf DTE.Windows.Item(i).Document.FullName.Length > 0 Then
                ' if the file is any other valid document then make sure it appears on the right
                If DTE.Windows.Item(i).Document.ActiveWindow.Left < 250 Then
                    WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to right")
                    DTE.Windows.Item(i).Document.Activate()
                    DTE.ExecuteCommand("Window.MovetoNextTabGroup")
                End If
            End If
        End If
    Next i
End Sub

If anyone can improve this further pl

Upvotes: 1

Related Questions