Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 885
Peter Mounce Avatar asked Nov 16 '08 11:11

Peter Mounce


People also ask

How to collapse everything in Visual Studio?

CTRL + M + O will collapse all.

How to collapse comments in Visual Studio?

Ctrl+M , Ctrl+C collapses all comments (and using / Import statements.) Ctrl+M , Ctrl+D expands all comments (and collapse everything else.)

How to collapse json in Visual Studio?

Ctrl + Shift + [ on Windows and Linux.

How to collapse code in c#?

Like Steve said, CTRL + M plus CTRL + L for collapsing all regions recursively. This one is a toggle, meaning you can do it again to re-open them. Do CTRL + M plus CTRL + O to collapse all regions non-recursively.


1 Answers

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.

like image 115
Brody Avatar answered Sep 30 '22 05:09

Brody