Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Had something similar for adding profiling code using Macros in VS, here's the code (this also groups everything under a single "undo" command and lists all of the changes in its own output window)</p> <pre><code>Imports System Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Public Module Module1 Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane Dim window As Window Dim outputWindow As OutputWindow Dim outputWindowPane As OutputWindowPane window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) If show Then window.Visible = True outputWindow = window.Object Try outputWindowPane = outputWindow.OutputWindowPanes.Item(Name) Catch e As System.Exception outputWindowPane = outputWindow.OutputWindowPanes.Add(Name) End Try outputWindowPane.Activate() Return outputWindowPane End Function Const ToInsert As String = "/* Inserted text :D */" Sub AddProfilingToFunction(ByVal func As CodeFunction2) Dim editPoint As EditPoint2 = func.StartPoint.CreateEditPoint() While editPoint.GetText(1) &lt;&gt; "{" editPoint.CharRight() End While editPoint.CharRight() editPoint.InsertNewLine(1) Dim insertStartLine As Integer = editPoint.Line Dim insertStartChar As Integer = editPoint.LineCharOffset editPoint.Insert(ToInsert) GetOutputWindowPane("Macro Inserted Code").OutputString( _ editPoint.Parent.Parent.FullName &amp; _ "(" &amp; insertStartLine &amp; "," &amp; insertStartChar &amp; _ ") : Inserted Code """ &amp; ToInsert &amp; """" &amp; vbCrLf) End Sub Sub AddProfilingToProject(ByVal proj As Project) If Not proj.CodeModel() Is Nothing Then Dim EventTitle As String = "Add Profiling to project '" &amp; proj.Name &amp; "'" GetOutputWindowPane("Macro Inserted Code").OutputString("Add Profiling to project '" &amp; proj.Name &amp; "'" &amp; vbCrLf) DTE.UndoContext.Open(EventTitle) Try Dim allNames As String = "" For i As Integer = 1 To proj.CodeModel().CodeElements.Count() If proj.CodeModel().CodeElements.Item(i).Kind = vsCMElement.vsCMElementFunction Then AddProfilingToFunction(proj.CodeModel().CodeElements.Item(i)) End If Next Finally DTE.UndoContext.Close() End Try GetOutputWindowPane("Macro Inserted Code").OutputString(vbCrLf) End If End Sub Sub AddProfilingToSolution() GetOutputWindowPane("Macro Inserted Code").Clear() If Not DTE.Solution Is Nothing And DTE.Solution.IsOpen() Then For i As Integer = 1 To DTE.Solution.Projects.Count() AddProfilingToProject(DTE.Solution.Projects.Item(i)) Next End If End Sub End Module </code></pre> <p>P.S Remember to change the "Const ToInsert As String = ..." to the code you actually want to be inserted</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload