Search code examples
visual-studiodebuggingbreakpoints

How do I add Debug Breakpoints to lines displayed in a "Find Results" window in Visual Studio


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?


Solution

  • 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.