Is there a way to specify outlining defaults in Visual Studio 2008 so that a file opens up with members collapsed by default?

强颜欢笑 提交于 2019-11-30 13:59:13

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.

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

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

Sarah Vessels

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!