How do I add Debug Breakpoints to lines displayed

2020-02-03 16:25发布

问题:

In Visual Studio 2005-2015 it is possible to find all lines containing certain references and display them in a "Find Results" window.

Now that these result lines are displayed, is there any keyboard shortcut that would allow adding debug breakpoints to all of them?

回答1:

This answer does not work for Visual Studio 2015 or later. A more recent answer can be found here.

You can do this fairly easily with a Visual Studio macro. Within Visual Studio, hit Alt-F11 to open the Macro IDE and add a new module by right-clicking on MyMacros and selecting Add|Add Module...

Paste the following in the source editor:

Imports System
Imports System.IO
Imports System.Text.RegularExpressions
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module CustomMacros
    Sub BreakpointFindResults()
        Dim findResultsWindow As Window = DTE.Windows.Item(Constants.vsWindowKindFindResults1)

        Dim selection As TextSelection
        selection = findResultsWindow.Selection
        selection.SelectAll()

        Dim findResultsReader As New StringReader(selection.Text)
        Dim findResult As String = findResultsReader.ReadLine()

        Dim findResultRegex As New Regex("(?<Path>.*?)\((?<LineNumber>\d+)\):")

        While Not findResult Is Nothing
            Dim findResultMatch As Match = findResultRegex.Match(findResult)

            If findResultMatch.Success Then
                Dim path As String = findResultMatch.Groups.Item("Path").Value
                Dim lineNumber As Integer = Integer.Parse(findResultMatch.Groups.Item("LineNumber").Value)

                Try
                    DTE.Debugger.Breakpoints.Add("", path, lineNumber)
                Catch ex As Exception
                    ' breakpoints can't be added everywhere
                End Try
            End If

            findResult = findResultsReader.ReadLine()
        End While
    End Sub
End Module

This example uses the results in the "Find Results 1" window; you might want to create an individual shortcut for each result window.

You can create a keyboard shortcut by going to Tools|Options... and selecting Keyboard under the Environment section in the navigation on the left. Select your macro and assign any shortcut you like.

You can also add your macro to a menu or toolbar by going to Tools|Customize... and selecting the Macros section in the navigation on the left. Once you locate your macro in the list, you can drag it to any menu or toolbar, where it its text or icon can be customized to whatever you want.



回答2:

I needed something similar to disable all breakpoints and place a breakpoint on every "Catch ex as Exception". However, I expanded this a little so it will place a breakpoint at every occurance of the string you have selected. All you need to do with this is highlight the string you want to have a breakpoint on and run the macro.

 Sub BreakPointAtString()

    Try
        DTE.ExecuteCommand("Debug.DisableAllBreakpoints")
    Catch ex As Exception

    End Try

    Dim tsSelection As String = DTE.ActiveDocument.Selection.text
    DTE.ActiveDocument.Selection.selectall()
    Dim AllText As String = DTE.ActiveDocument.Selection.Text

    Dim findResultsReader As New StringReader(AllText)
    Dim findResult As String = findResultsReader.ReadLine()
    Dim lineNum As Integer = 1

    Do Until findResultsReader.Peek = -1
        lineNum += 1
        findResult = findResultsReader.ReadLine()
        If Trim(findResult) = Trim(tsSelection) Then
            DTE.ActiveDocument.Selection.GotoLine(lineNum)
            DTE.ExecuteCommand("Debug.ToggleBreakpoint")
        End If
    Loop

End Sub

Hope it works for you :)



回答3:

If you can search for the word exactly, you can use a pair of keyboard shortcuts to do it quickly.

Tools -> Options -> Enviroment -> Keyboard

  • Edit.GoToFindResults1NextLocation
  • EditorContextMenus.CodeWindow.Breakpoint.InsertBreakpoint

Assign them to Control+Alt+F11 and F10 and you can go through all the results very quickly. I haven't found a shortcut for going to the next reference however.



回答4:

Paul, thanks a lot, but I have the following error (message box), may be I need to restart my PC:

Error
---------------------------
Error HRESULT E_FAIL has been returned from a call to a COM component.
---------------------------
OK   
---------------------------

I would propose the following solution that's very simple but it works for me

Sub BreakPointsFromSearch()
    Dim n As Integer = InputBox("Enter the number of search results")

    For i = 1 To n
        DTE.ExecuteCommand("Edit.GoToNextLocation")
        DTE.ExecuteCommand("Debug.ToggleBreakpoint")            
    Next
End Sub