有没有一种方法可以在Visual Studio 2008中指定轮廓默认值,以便文件默认情况下折叠成员?

7
我希望在打开代码文件时,VS2008默认折叠文件中所有类/接口成员(包括XML文档和注释),而不使用区域。同时,我想使用ctrl+m, ctrl+l 快捷键来切换所有成员的折叠状态(例如,如果所有内容都被折叠了,我想展开所有成员,但不展开注释或XML文档)。是否可行?如何实现?
4个回答

5

第一部分可以确定。

对于第二部分,我不太确定。

如果想要在VS2008中自动以折叠状态打开文件,你需要创建一个插件,在每个文档打开时运行“Edit.CollapsetoDefinition”命令。

这并不是特别难,困难的部分似乎在于你需要在文档实际打开后几毫秒运行代码,因此需要使用线程池来完成。

  1. 为VS2008创建一个插件项目。
  2. 将以下代码添加到Connect类的OnConnection方法的末尾。

    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;
    }

请将以下方法添加到Connect类中:

    private void InitialiseHandlers()
    {
        this._openHandler = new OnOpenHandler(_applicationObject);
    }
  1. 在Connect类的OnStartupComplete方法中添加调用InitialiseHandlers()的语句。

    public void OnStartupComplete(ref Array custom)
    {
        InitialiseHandlers();
    }

将此类添加到项目中。

    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", "");
            }
        }
    }

现在所有打开的文件都应该完全折叠。

0

我曾经尝试自己编写一些 Visual Basic 代码来制作宏,从不同的地方借鉴,但是什么都做不出来。那我该怎么办呢?当然是在 StackOverflow 上提问了!问题得到了解答,我将建议的代码添加到我的 EnvironmentEvents 宏中,现在当我打开 CS 文件时,大约一秒钟后,所有的定义都被折叠了。 :)


0
一个快速折叠所有函数定义的方法是按下:

上下文菜单按钮*(位于右侧 Windows 按钮旁边)*, L, O

我经常使用它。如果有真正的热键,请告诉我 :)


0

使用Visual Studio宏来完成相同的操作会更容易。在MyMacros中编辑“EnvironmentEvents”宏文件,并添加一个处理程序DocumentEvents.DocumentOpened:
DTE.ExecuteCommand("Edit.CollapsetoDefinitions")


请问您能否澄清一下您会如何做到这一点?我已经在这个问题上添加了一个答案(https://dev59.com/ukXRa4cB1Zd3GeqPuslA#1050267),但是我的宏代码无法工作。 - Sarah Vessels

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接