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

前端 未结 4 813
南笙
南笙 2021-01-02 16:33

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 documen

4条回答
  •  梦毁少年i
    2021-01-02 17:06

    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.

提交回复
热议问题