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

前端 未结 4 728
南笙
南笙 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条回答
  • 2021-01-02 17:00

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-02 17:07

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

    0 讨论(0)
  • 2021-01-02 17:13

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

    0 讨论(0)
提交回复
热议问题