Archive

Posts Tagged ‘macro’

Visual Studio, Attach debugger to w3wp process through macro

October 12th, 2011 No comments

Here is a short macro that will attach the debugger to the first w3wp.exe process it can find in the process list. (Script assumes a w3wp.exe process is running. Expect error messages if it doesn’t ;))

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
 
Public Module Famvdploeg
    'Attach the debugger to a w3wp.exe process
    Sub AttachDebugger()
        Try
            'Get your hostname
            Dim hostname As String = System.Net.Dns.GetHostName
            'Load the debugger interface
            Dim debugger As EnvDTE80.Debugger2 = DTE.Debugger
            'Get a local transport, (this is the "Default")
            Dim trans As EnvDTE80.Transport = debugger.Transports.Item("Default")
 
            'Attach to the first w3wp.exe command which we find
            Dim dbgeng(3) As EnvDTE80.Engine
            dbgeng(0) = trans.Engines.Item("T-SQL")
            dbgeng(1) = trans.Engines.Item("T-SQL")
            dbgeng(2) = trans.Engines.Item("Managed")
            Dim process As EnvDTE80.Process2 = debugger.GetProcesses(trans, hostname).Item("w3wp.exe")
            process.Attach2(dbgeng)
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
 
    End Sub
End Module

Removing trailing spaces in Visual Studio

November 30th, 2010 4 comments

Trailing spaces or tabs is something I dislike. Using the combination CTRL+R, CTRL+W you can show the whitespace distribution in VS. But screening the lines one by one is a tedious job. That’s why we are going to use regular expressions. Regular expressions in VS are a bit of an odd duck as the syntax is not nice and Perly.

You can read more about regular expressions in Visual Studio here.

Putting this together we get the following:

Find what: ^{.@}:b+$
Replace with: \1

This will seach those nasty trailing whitespaces and remove them.
{.@} -> Match everything, non-greedy. This is our capturing group.
:b+$ -> Match one or more spaces or tabs before the end of the line.

Optionally:
You can add the following macro to Visual Studio (and bind some keystroke):

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
 
Public Module Famvdploeg
    'Remove trailing whitespace from document
    Sub RemoveTrailingWhitespace()
        DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, "( |\t)+$", vsFindOptions.vsFindOptionsRegularExpression, String.Empty, vsFindTarget.vsFindTargetCurrentDocument, , , )
    End Sub
End Module

Visual Studio copy selection macro

April 15th, 2010 4 comments

I use the copy line / copy selection under cursor in NetBeans a lot. So I created this macro for use in Visual Studio that provides this functionality. If no line is selected the current line where the cursor is placed will be copied.
When a selection is made the selection will be copied and pasted below the selection. Afterwards the selection will be remade so you can repeat the action immediately.

Below is the VB snippet.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
 
Public Module Famvdploeg
    ' Copy the current line or selection
    Sub CopySelection()
        Dim selection As String
        Dim lines As Array
 
        ' Select current line if nothing is selected
        If String.IsNullOrEmpty(DTE.ActiveDocument.Selection.Text) Then
            DTE.ActiveDocument.Selection.StartOfLine(0)
            DTE.ActiveDocument.Selection.EndOfLine(True)
        End If
 
        ' Store some variables
        selection = DTE.ActiveDocument.Selection.Text
        lines = Split(selection, vbCrLf)
 
        ' Perform the copy+paste action depending on how selection is made
        If String.IsNullOrEmpty(lines(lines.Length - 1)) Then
            DTE.ActiveDocument.Selection.Copy()
            DTE.ActiveDocument.Selection.EndOfLine(0)
            DTE.ActiveDocument.Selection.StartOfLine(0)
            DTE.ActiveDocument.Selection.Paste()
        Else            
            DTE.ActiveDocument.Selection.Copy()
            DTE.ActiveDocument.Selection.EndOfLine()
            DTE.ActiveDocument.Selection.NewLine()
            ' Sanitize any auto text insertion
            DTE.ActiveDocument.Selection.StartOfLine(0)
            DTE.ActiveDocument.Selection.EndOfLine(True)
            If Not String.IsNullOrEmpty(DTE.ActiveDocument.Selection.Text()) Then
                DTE.ActiveDocument.Selection.Delete()
            End If
            DTE.ActiveDocument.Selection.StartOfLine(0)
            DTE.ActiveDocument.Selection.Paste()
        End If
 
        If lines.Length > 1 Then
            ' ReSelect the text so we can repeat the action immediately
            DTE.ActiveDocument.Selection.LineUp(False, lines.Length - 1)
            DTE.ActiveDocument.Selection.StartOfLine(0)
            DTE.ActiveDocument.Selection.LineDown(True, lines.Length - 1)
            If Not String.IsNullOrEmpty(lines(lines.Length - 1)) Then
                DTE.ActiveDocument.Selection.EndOfLine(True)
            End If
        Else
            DTE.ActiveDocument.Selection.StartOfLine(0)
        End If
    End Sub
End Module