Generally I've no complaints about the speed of Visual Studio, but if I write a macro that writes out about 100 lines of code, it takes 1-2 minutes to complete.
That doesn't sound right. Is there another more convenient way to write code blocks?
For one thing, I noticed it fills the undo buffer as if I'm just typing the text by hand. Could I mimic copy/paste behavior, which is much faster?
Here's a somewhat contrived example I tried to test my case:
Sub WriteManyLines()
DTE.Commands.Raise("{AA61C329-D559-468F-8F0F-4F03896F704D}", 2, Customin, Customout)
DTE.Commands.Raise("{AA61C329-D559-468F-8F0F-4F03896F704D}", 5, Customin, Customout)
Dim sb As New StringBuilder()
For i As Integer = 1 To 100
sb.AppendFormat("public string Method{0:000}() {{ return ""Method{0:000}""; }}", i)
sb.AppendLine()
Next i
DTE.ActiveDocument.Selection.Text = sb.ToString()
End Sub
This API is very deceptive in that it appears that you are replacing the selected text with the actual result of the
StringBuilder
. Or in short, a copy / paste. But what you're really doing is typing the result of theStringBuilder
(this is why you seen the undo buffer filling up with data).This type of behavior is true for much of the editing experience which comes from the DTE namespace. If you're interested in the esoteric details I wrote a blog experience about this general problem some time ago.
In order to fix it though you'll want to abandon DTE and get down into
IVsTextLines
orITextBuffer
(the latter is preferred as it's the newer managed API). To get out of DTE you should be able to execute the followingEdits on an
IVsTextLines
will go directly to the buffer and avoid the overhead of typing.If you want to avoid DTE and COM entirely you can use
IVsEditorAdaptersFactoryService
to map from the COM layer to the new 2010 managed APIs. This interface is typically queried via MEF but I believe you can also useIServiceProvider
(which DTE implements) and do aQueryService
call for it.