7

我想要做的是 VS2008,当我打开一个代码文件时,默认情况下折叠文件中类/接口的所有成员(包括任何 XML 文档和注释)。

我根本不想使用区域。

我还希望能够使用 ctrl+m、ctrl+l 和弦来切换所有成员大纲(例如,如果所有内容都折叠了,我希望它展开所有成员,而不是评论或 XML 文档)。

可能的?如何?

4

4 回答 4

5

是的,第 1 部分。

不确定第 2 部分。

要让 VS2008 自动打开处于折叠状态的文件,您需要创建一个插件以在每个文档打开时运行“Edit.CollapsetoDefinition”。

这并不过分棘手 - 困难的部分似乎是您必须在文档实际打开几毫秒后运行代码,因此您需要使用 threed 池来执行此操作。

  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;
    }
  1. 将此方法添加到 Connect 类

    private void InitialiseHandlers()
    {
        this._openHandler = new OnOpenHandler(_applicationObject);
    }
  1. 将对 InitialiseHandlers() 的调用添加到 Connect 类的 OnStartupComplete 方法。

    public void OnStartupComplete(ref Array custom)
    {
        InitialiseHandlers();
    }
  1. 将此类添加到项目中。

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

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

于 2008-11-17T20:55:51.783 回答
0

使用 Visual Studio 宏来做同样的事情会容易得多。在 MyMacros 中编辑“EnvironmentEvents”宏文件并为 DocumentEvents.DocumentOpened 添加一个处理程序:
DTE.ExecuteCommand("Edit.CollapsetoDefinitions")

于 2009-03-18T18:55:15.953 回答
0

我曾尝试自己为宏编写一些 Visual Basic 代码,从不同的地方借用,但无法使任何工作。那我做了什么?为什么,我当然在 StackOverflow 上问了一个问题!它得到了回答,我将建议的代码添加到我的EnvironmentEvents宏中,现在当我打开 CS 文件时,大约一秒钟后,我所有的定义都被折叠了。:)

于 2009-06-26T17:25:39.660 回答
0

将所有大纲折叠到函数定义的一种快速方法是按下: Contextmenu-button*(在右侧窗口按钮旁边)*、L、O

我用它所有的时间。如果有一个真正的热键请告诉我:)

于 2009-06-26T17:43:22.103 回答