i'm looking for a way to write a macro event to parse user's code.
for example when programmer writes some specific code this event will parse the code line and if matches a regular expression will inse开发者_JAVA百科rt some additional codes on active document. how can i get hole line code and which event should i use?You can use TextDocumentKeyPressEvents.AfterKeyPress event. The following macro triggers after user presses any key in text editor. Then it gets the current line. This example tests, if the line contains "hello" text and if so, it shows the line in message box.
Private Sub TextDocumentKeyPressEvents_AfterKeyPress(ByVal Keypress As String, _
ByVal Selection As EnvDTE.TextSelection, ByVal InStatementCompletion As Boolean) _
Handles TextDocumentKeyPressEvents.AfterKeyPress
Try
Dim line As String
Dim aPoint As EditPoint = Selection.ActivePoint.CreateEditPoint
Dim startPoint As EditPoint = aPoint.CreateEditPoint
startPoint.StartOfLine()
Dim endPoint As EditPoint = aPoint.CreateEditPoint
endPoint.EndOfLine()
line = startPoint.GetText(endPoint)
If line.Contains("hello") Then
MsgBox(line)
End If
Catch ex As Exception
End Try
End Sub
To create and apply this macro:
- Go to menu Tools - Macros - Macros IDE...
- In the Macros IDE Class View navigate to MyMacros - {} MyMacros - EnvironmentEvents. Open (double-click) EnvironmentEvents.
- Paste this code inside module (just before End Module line)
There are events that Macros can respond to, however, I do not believe that text entry is one of them, a better approach might be to use snippets - http://msdn.microsoft.com/en-us/library/ms165392(v=VS.100).aspx
They will allow you to define a keyword and then add text into the editor. A good example is the property snippet; In your editor type 'prop' and then press Tab (twice in C#, once in VB) to invoke.
Here is more detailed list of available snippets: http://msdn.microsoft.com/en-us/library/z41h7fat(v=VS.100).aspx
精彩评论