views:

630

answers:

4

What I would like to do is have VS2008, when I open a code file, collapse all members of the classes/interfaces in the file by default (including, crucially, any XML documentation and comments).

I do not want to use regions, at all.

I would also like to be able to use the ctrl+m, ctrl+l chord to toggle all member outlining (for example, if everything is collapsed, I would like it to expand all of the members, but not the comments or XML documentation).

Possible? How?

+1  A: 

Yes to part 1.

Unsure about part 2.

To have VS2008 automatically open files in a Collapsed state you'll need to create an addin to run the "Edit.CollapsetoDefinition" when each document opens.

This isn't overly tricky - The difficult parts seems to be the that you have to run the code a few milliseconds after the document is actually opened so you need to use the threed pool to do that.

  1. Create an Addin project for VS2008.
  2. Add this code (see following) to the end of the OnConnection Method of the Connect class.

    switch (connectMode)
    {
        case ext_ConnectMode.ext_cm_UISetup:
        case ext_ConnectMode.ext_cm_Startup:
            //Do nothing OnStartup will be called once IDE is initialised.
            break;
        case ext_ConnectMode.ext_cm_AfterStartup:
            //The addin was started post startup so we need to call its initialisation manually
            InitialiseHandlers();
            break;
    }
  1. Add this method to the Connect class

    private void InitialiseHandlers()
    {
        this._openHandler = new OnOpenHandler(_applicationObject);
    }
  1. Add a call to InitialiseHandlers() to the OnStartupComplete method of the Connect class.

    public void OnStartupComplete(ref Array custom)
    {
        InitialiseHandlers();
    }
  1. Add this class to the project.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using EnvDTE80;
    using EnvDTE;
    using System.Threading;

    namespace Collapser
    {
        internal class OnOpenHandler
        {
            DTE2 _application = null;
            EnvDTE.Events events = null;
            EnvDTE.DocumentEvents docEvents = null;

            internal OnOpenHandler(DTE2 application)
            {
                _application = application;
                events = _application.Events;
                docEvents = events.get_DocumentEvents(null);
                docEvents.DocumentOpened +=new _dispDocumentEvents_DocumentOpenedEventHandler(OnOpenHandler_DocumentOpened);
            }

            void OnOpenHandler_DocumentOpened(EnvDTE.Document document)
            {
                if (_application.Debugger.CurrentMode != dbgDebugMode.dbgBreakMode)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(Collapse));
                }
            }

            private void Collapse(object o)
            {
                System.Threading.Thread.Sleep(150);
                _application.ExecuteCommand("Edit.CollapsetoDefinitions", "");
            }
        }
    }

And now all opened files should be fully collapsed.

Brody
A: 

It would be much easier to use the Visual Studio Macros to do the same thing. Editing the "EnvironmentEvents" Macro file in MyMacros and adding a handler for DocumentEvents.DocumentOpened with :
DTE.ExecuteCommand("Edit.CollapsetoDefinitions")

Could you please clarify how you would do this? I added an answer (http://stackoverflow.com/questions/293806/is-there-a-way-to-specify-outlining-defaults-in-visual-studio-so-that-a-file-open/1050267#1050267) to this question but can't get my macro code to work.
Sarah Vessels
A: 

I had tried working out some Visual Basic code for a macro myself, borrowing from different places, and couldn't get anything to work. So what did I do? Why, I asked a question on StackOverflow of course! It got answered, I added the suggested code to my EnvironmentEvents macro, and now when I open CS files, after about a second, all my definitions are collapsed. :)

Sarah Vessels
A: 

A quick way to collapse all outlining to function-definitions is to press: Contextmenu-button*(next to your right windows button)*, L, O

I use it all the time. If there is a real hotkey for this please tell me :)