VS Code Model events
Users of the Code Model may be alerted of significant changes of code by listening for events with a CodeModelEvents object. The CodeModelEvents object is in EnvDTE80, you get it by casting DTE.Events to EnvDTE80.Events2, e.g. in VB, it is like CType(DTE.Events, EnvDTE80.Events2).CodeModelEvents. These events will be fired as the languages rebuild the code model after edits have been made. The timing and ordering of these events is not guaranteed to be consistent across different languages. This object provides three events: ElementAdded, ElementChanged, and ElementDeleted.
ElementAdded event
This event is raised when a CodeElement object has been created. The new object is passed into the event handler. Although the object containing the new element is changed by the addition, no events are raised by the parent object. For example, if a parameter is added to a function the ElementAdded event will be raised for the new CodeParameter object. No events will be raised for the CodeFunction object that contains it.
ElementChanged event
This event is raised when a CodeElement object has been changed. Only one ElementChanged event is made for any given change in the code. The "most local" object would raise the event. For example, if a function's name is changed, the ElementChanged event would be fired for that CodeFunction object only. (There would not be an event raised for the containing CodeClass object.)
ElementDeleted event
This event is raised when a CodeElement object is deleted. It is only raised for the outermost element that was removed. For example, if the user deletes an entire class, the only ElementRemoved event will be only for the CodeClass object.
You can use following macros to play around with Code Model events by watching the output from OutputWindowPane "CodeModel Events Output" in the Output Window.
Dim owpane As OutputWindowPane
Public WithEvents myCMEvents As EnvDTE80.CodeModelEvents
Private Sub myCMEvents_ElementAdded(ByVal Element As EnvDTE.CodeElement) Handles myCMEvents.ElementAdded
owpane.OutputString("myCMEvents_ElementAdded - Element Name = " & Element.Name & "; Kind = " & Element.Kind.ToString() & vbCrLf)
End Sub
Private Sub myCMEvents_ElementChanged(ByVal Element As EnvDTE.CodeElement, ByVal Change As EnvDTE80.vsCMChangeKind) Handles myCMEvents.ElementChanged
owpane.OutputString("myCMEvents_ElementChanged - Element Name = " & Element.Name & "; Kind = " & Element.Kind.ToString() & " ChangeKind = " & Change.ToString() & vbCrLf)
End Sub
Private Sub myCMEvents_ElementDeleted(ByVal Parent As Object, ByVal Element As EnvDTE.CodeElement) Handles myCMEvents.ElementDeleted
Dim cc As CodeElement = CType(Parent, CodeElement)
owpane.OutputString("myCMEvents_ElementDeleted - Element Name = " & Element.Name & "; Kind = " & Element.Kind.ToString() & " Parent = " & Parent.ToString() & vbCrLf)
End Sub
Sub tryCodeModelEvents()
owpane = GetOutputWindowPane("CodeModel Events Output")
owpane.OutputString("CodeModel Events Testing starts:" & vbCrLf)
Try
myCMEvents = CType(DTE.Events, EnvDTE80.Events2).CodeModelEvents
Catch ex As System.Exception
owpane.OutputString(ex.ToString())
End Try
End Sub
Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
If show Then win.Visible = True
Dim ow As OutputWindow = win.Object
Dim owpane As OutputWindowPane
Try
owpane = ow.OutputWindowPanes.Item(Name)
Catch e As System.Exception
owpane = ow.OutputWindowPanes.Add(Name)
End Try
owpane.Activate()
Return owpane
End Function