If I was to have:
/// <summary>
/// My summary
/// </summary>
/// <param name='args'></param>
on the clipboard pasted it on top of the method static void Main(string[] args)
, it will look like:
class Program
{
/// <summary>
/// This is my summary
/// </summary>
/// <param name='args'></param>
static void Main(string[] args)
{
}
}
Note: The text I had on the clipboard had no indentation (4 white spaces on the left). When I pasted it, Visual Studio was able to figure out that it needed indentation.
I would like to do the same thing with a macro. I do not want to use the clipboard as I have the text I want to insert in a variable (myText
). I have something like:
Sub TemporaryMacro()
Dim myText As String = "/// <summary>" _
& vbCrLf & "/// My summary" _
& vbCrLf & "/// </summary>" _
& vbCrLf & "/// <param name='args'></param>"
DTE.ActiveDocument.Selection.Text = myText
End Sub
When I run that macro I end up with:
class Program
{
/// <summary>
/// <summary>
/// /// My summary
/// /// </summary>
/// /// <paramref name=" name='args'></param>"/></summary>
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
}
}
Note: I do get a different result.
I have also tried:
Public Module RecordingModule
Sub TemporaryMacro()
Dim myText As String = "/// <summary>" _
& vbCrLf & "/// My summary" _
& vbCrLf & "/// </summary>" _
& vbCrLf & "/// <param name='args'></param>"
DTE.ActiveDocument.Selection.Insert(myText)
End Sub
End Module
which results in:
class Program
{
/// <summary>
/// My summary
/// </summary>
/// <param name='args'></param>
static void Main(string[] args)
{
}
}
I know I can place myText
on the clipboard and then paste it. That does not make sense though. How can I achieve the same behavior as if I where pasting myText
without having it to place it on the clipboard?
Finally found the solution:
Dim myText As String = "/// <summary>" _
& vbCrLf & "/// My summary" _
& vbCrLf & "/// </summary>" _
& vbCrLf & "/// <param name='args'></param>"
DTE.ActiveDocument.Selection.Insert(myText)
' how many characters where inserted?
' Trying myText.Length gives incorrect results because visual studio counts \r\n as one character so we
' use the regex \r\n|. to try to find first \r\n as one match and any other character as another match
Dim insertLength As Integer = System.Text.RegularExpressions.Regex.Matches(myText, "(?>\r\n|.)").Count
' select the text that was just inserted
DTE.ActiveDocument.Selection.CharLeft(True, insertLength)
' here comes the magic!
DTE.ActiveDocument.Selection.SmartFormat()
I am making this edit to ask why this answer reserved down votes. If you refer to the question title it asks: Visual studio macro to insert text like when pasting
when you paste text it maters where the cursor is located! I do not have to search for static void Main(string[] args)
also I up voted the other answer cause I appreciate the help. If I would have known the answer earlier I would have turn this question into a bounty.
Lastly the other solution does not work correclty... run that macro when I have comments at the top of the page and it does not work. It is helpful that's why I up voted!