8/1/11

Visual Studio 2010 VB.NET Macros

'Visual Studio 2010 VB.NET Macros
'www.expressionsoftware.com
'v11.8.1

Imports System
Imports System.Collections.Generic
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100

Public Module ExpressionSoftwareMacros

    Sub PrintFilenamesForOpenFiles()
        Dim output = "macro: PrintFilenamesForOpenFiles" + vbCrLf
        Dim outputFiles As String
        Dim openFiles = GetOpenFiles()

        'openFiles.Sort()  'error: failed to compare two elements in the array
        openFiles.Sort(New EnvDteDocumentComparerClass())

        For Each file In openFiles
            outputFiles += "file: " + file.FullName + vbCrLf
        Next

        output += "date: " + Date.Now + vbCrLf
        output += "count: " + Str(openFiles.Count) + vbCrLf
        output += outputFiles
        Out(output)
    End Sub

    Sub HideLinebreakToBrace()
        Dim output = "macro: HideLinebreakToBrace" + vbCrLf
        Dim count = 0

        'vs pofs regex
        ':b whitespace or tab
        'Dim searchString = ":b*\n:b*\{"  'matches "\b\n{" which cannot be hidden, can use if count is not output, else count incorrect
        Dim searchString = "{:b+\n:b*\{}|{:b*\n:b+\{}"

        DTE.Find.FindWhat = searchString
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument  'search current file only
        DTE.Find.Action = vsFindAction.vsFindActionFind
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr  'regex search
        DTE.SuppressUI = False

        DTE.ActiveDocument.Selection.StartOfDocument()  'set cursor top of file
        While DTE.Find.Execute() <> vsFindResult.vsFindResultNotFound
            count += 1
            DTE.ActiveDocument.Selection.CharLeft(True, 1)  'unselect brace char
            DTE.ExecuteCommand("Edit.HideSelection")
        End While
        DTE.ActiveDocument.Selection.StartOfDocument()

        output += "date: " + Date.Now + vbCrLf
        output += "file: " + DTE.ActiveDocument.FullName + vbCrLf
        output += "count: " + Str(count)
        Out(output)
    End Sub

    Private Function GetOpenFiles() As List(Of EnvDTE.Document)
        Dim openFiles As New List(Of EnvDTE.Document)

        For Each doc As EnvDTE.Document In DTE.Documents
            If doc.ProjectItem IsNot Nothing Then  'todo: combine 
                If doc.ProjectItem.IsOpen() Then
                    openFiles.Add(doc)
                End If
            End If
        Next

        Return openFiles
    End Function

    Private Sub Out(ByVal output)
        GetMacrosOutputWindow().OutputString(output)
    End Sub

    'requires Microsoft Samples Utilities module,
    'copy module into MyMacros project
    Private Function GetMacrosOutputWindow() As OutputWindowPane
        Dim windowTitle = "Expression Software Macros"
        Dim window As Window
        Dim target As Object
        Dim document As EnvDTE.Document

        window = DTE.Windows.Item(Constants.vsWindowKindCommandWindow)
        If DTE.ActiveWindow Is window Then
            target = window.Object
        Else
            target = Utilities.GetOutputWindowPane(windowTitle)
            target.clear()
        End If

        Return target
    End Function
End Module


Public Class EnvDteDocumentComparerClass
    Implements IComparer(Of EnvDTE.Document)

    'compare docs by full filename, for sorting
    Function Compare(ByVal file1 As EnvDTE.Document, _
                     ByVal file2 As EnvDTE.Document) _
                     As Integer Implements IComparer(Of EnvDTE.Document).Compare

        Return file1.FullName.CompareTo(file2.FullName)
    End Function
End Class


Public Class EnvDteDocumentComparableClass
    Implements IComparable(Of EnvDTE.Document)

    'compare docs by full filename, for sorting
    Function CompareTo(ByVal other As EnvDTE.Document) _
                       As Integer Implements IComparable(Of EnvDTE.Document).CompareTo

        Return FullName.CompareTo(other.FullName)
    End Function
End Class


'**************************************************************
'Misc
'DTE.Documents.Count includes closed docs
'DTE.ActiveDocument.FullName
'output += "date: " + Date.Now + vbCrLf
'Microsoft.VisualBasic.Constants.vbCrLf
'DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
'keyboard shortcuts
'  alt + F8    macro explorer
'  alt + F11   macros ide
'**************************************************************

1 comment:

  1. requires the Utilities module from Samples project, add to MyMacros project

    ReplyDelete