EnvironmentEvent macro doesn't complete

喜欢而已 提交于 2019-12-08 07:40:43

问题


I'm working in Visual Studio 2008 and I would like for Edit > Outlining > Collapse to Definitions to be run whenever I open a file. It would be nice if, after that, all regions were expanded. I tried the code that Kyralessa offered in a comment on The Problem with Code Folding, and that works very nicely as a macro that I have to run manually. I tried to expand this macro to act as an event by placing the following code in the EnvironmentEvents module in the Macro IDE:

Public Sub documentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
    Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
    DTE.SuppressUI = True
    Dim objSelection As TextSelection = DTE.ActiveDocument.Selection
    objSelection.StartOfDocument()
    Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
    Loop
    objSelection.StartOfDocument()
    DTE.SuppressUI = False
End Sub

However, this does not seem to do anything when I open a file from my Solution in VS. To test that the macro was getting run, I put a MsgBox() statement in that subroutine and noticed that code before Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions") ran fine, but nothing seemed to get hit after that line. When I debugged and set a breakpoint within the subroutine, I would hit F10 to continue to the next line and control would leave the subroutine as soon as that ExecuteCommand line ran. Despite this, that line seems to do nothing, i.e. it doesn't collapse the outlining.

I also tried using just DTE.ExecuteCommand("Edit.CollapsetoDefinitions") within the subroutine but with no luck.

This question tries to obtain the same end result as this one, but I'm asking about what I might be doing wrong in my event-handling macro.


回答1:


The problem is that the document is not really active when the event fires. One solution is to use a "fire once" timer to execute the code a short delay after the DocumentOpened event occured:

Dim DocumentOpenedTimer As Timer

Private Sub DocumentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
    DocumentOpenedTimer = New Timer(AddressOf ExpandRegionsCallBack, Nothing, 200, Timeout.Infinite)
End Sub

Private Sub ExpandRegionsCallBack(ByVal state As Object)
    ExpandRegions()
    DocumentOpenedTimer.Dispose()
End Sub

Public Sub ExpandRegions()
    Dim Document As EnvDTE.Document = DTE.ActiveDocument
    If (Document.FullName.EndsWith(".vb") OrElse Document.FullName.EndsWith(".cs")) Then
        If Not DTE.ActiveWindow.Caption.ToUpperInvariant.Contains("design".ToUpperInvariant) Then
            Document.DTE.SuppressUI = True
            Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
            Dim objSelection As TextSelection = Document.Selection
            objSelection.StartOfDocument()
            Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
            Loop
            objSelection.StartOfDocument()
            Document.DTE.SuppressUI = False
        End If
    End If
End Sub

I haven't tested it extensively, so there might be some bugs... Also, I added a check to verify that the active document is a C# or VB source code (not tested with VB though) and that it's not in design mode.
Anyway, hope it works for you...



来源:https://stackoverflow.com/questions/1050944/environmentevent-macro-doesnt-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!