Tabs Studio Blog (organizing Visual Studio document tabs)

August 19, 2009

Sorter add-in

Filed under: Uncategorized — Tags: — Sergey Vlasov @ 8:13 am

This is my first add-in for Tabs Studio that automatically sorts tabs in alphabetical order. Download Sorter v1.0.0 with precompiled Sorter.dll and source code. To install Sorter create TabsStudioAddins directory along with the installed TabsStudio.dll and copy Sorter.dll to it. Tabs Studio v1.5.4 is required:

TabsStudioAddins directory along with TabsStudio.dll

TabsStudioAddins directory along with TabsStudio.dll


Sorter add-in installed

Sorter add-in installed

Let’s see how Sorter is implemented. I used Visual Studio 2008 and C#. Project is based on C# class library template. References to EnvDTE and EnvDTE80 were added to use EnvDTE80.DTE2 dte parameter in OnConnection function. PresentationCore, PresentationFramework and WindowsBase were added to work with WPF controls. TabsStudioExt defines all Tabs Studio extensibility interfaces:

Sorter add-in solution structure

Sorter add-in solution structure


You may want to remove my custom post-build command from the project to successfully compile Sorter:
My custom post-build command

My custom post-build command


Whole add-in code is in Sorter.cs file. It is only 44 lines long.

Addin class should implement TabsStudioExt.ITabsStudioAddin interface:

namespace TabsStudioSorter
{
    public class Sorter : TabsStudioExt.ITabsStudioAddin
    {

On startup we hook TabCreated event:

        public void OnConnection(TabsStudioExt.ITabsStudioEngine engine, EnvDTE80.DTE2 dte)
        {
            engine.TabCreated += new TabsStudioExt.TabCreatedEventHandler(OnTabCreated);
        }

On shutdown we do nothing (though unhooking TabCreated event would be nice):

        public void OnDisconnection()
        {
        }

When new tab is created it is added to the end of tab panel. Following code scans existing tabs and finds a place to reinsert new tab in alphabetical order. GetTabNameWithoutPath function is used to compare only file name part of full tab name (p.s. newTabName initialization would be better moved out of foreach loop):

        private void OnTabCreated(object sender, TabsStudioExt.TabCreatedEventArgs e)
        {
            foreach (System.Windows.Controls.TabItem tabItem in e.TabPanel.Children)
            {
                string newTabName = GetTabNameWithoutPath(e.TabItem);
                if (tabItem != e.TabItem && 
                    System.String.Compare(GetTabNameWithoutPath(tabItem), newTabName, true) > 0)
                {
                    e.TabPanel.Children.Remove(e.TabItem);
                    e.TabPanel.Children.Insert(e.TabPanel.Children.IndexOf(tabItem), e.TabItem);
                    break;
                }
            }
        }

Following function returns tab name from TabItem. It uses Tabs Studio controls structure defined in the Specification part of the style documentation:

        private string GetTabName(System.Windows.Controls.TabItem tabItem)
        {
            System.Windows.Controls.DockPanel tabInternals = tabItem.Header as System.Windows.Controls.DockPanel;
            System.Windows.Controls.StackPanel tabNameGroup = tabInternals.Children[0] as System.Windows.Controls.StackPanel;
            System.Windows.Controls.Label tabName = tabNameGroup.Children[0] as System.Windows.Controls.Label;
            return tabName.Content as string;
        }

Following function strips path from tab name. E.g. for App_Code/Class1.cs title it returns Class1.cs:

2 Comments

  1. Looks good but will it be possible to restore the previous tab order when a solution is opened.

    Comment by Charles — August 19, 2009 @ 9:08 am

    • Yes, I plan to save and restore tab positions for solutions, but with a different add-in.

      Comment by svprogramming — August 19, 2009 @ 1:08 pm


RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Blog at WordPress.com.