![]() |
||||
This article explains how to get the code element (class, property, procedure, etc.) at the position of the cursor in a code window. More Information To get the TextPoint object at the cursor position you can use: CType(DTE.ActiveDocument, EnvDTE.TextDocument).Selection.ActivePoint The following macro illustrates this approach: Sub GetCodeElementAtCursor()
Dim objCodeElement As EnvDTE.CodeElement
Dim objCursorTextPoint As EnvDTE.TextPoint
Try
objCursorTextPoint = GetCursorTextPoint()
If Not (objCursorTextPoint Is Nothing) Then
' Get the class at the cursor
objCodeElement = GetCodeElementAtTextPoint(vsCMElement.vsCMElementClass, _
DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElements, objCursorTextPoint)
End If
If objCodeElement Is Nothing Then
MessageBox.Show("No class found at the cursor!")
Else
MessageBox.Show("Class at the cursor: " & objCodeElement.FullName)
End If
Catch ex As System.Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Function GetCursorTextPoint() As EnvDTE.TextPoint
Dim objTextDocument As EnvDTE.TextDocument
Dim objCursorTextPoint As EnvDTE.TextPoint
Try
objTextDocument = CType(DTE.ActiveDocument.Object, EnvDTE.TextDocument)
objCursorTextPoint = objTextDocument.Selection.ActivePoint()
Catch ex As System.Exception
End Try
Return objCursorTextPoint
End Function
Private Function GetCodeElementAtTextPoint(ByVal eRequestedCodeElementKind As EnvDTE.vsCMElement, _
ByVal colCodeElements As EnvDTE.CodeElements, ByVal objTextPoint As EnvDTE.TextPoint) _
As EnvDTE.CodeElement
Dim objCodeElement As EnvDTE.CodeElement
Dim objResultCodeElement As EnvDTE.CodeElement
Dim colCodeElementMembers As EnvDTE.CodeElements
Dim objMemberCodeElement As EnvDTE.CodeElement
If Not (colCodeElements Is Nothing) Then
For Each objCodeElement In colCodeElements
If objCodeElement.StartPoint.GreaterThan(objTextPoint) Then
' The code element starts beyond the point
ElseIf objCodeElement.EndPoint.LessThan(objTextPoint) Then
' The code element ends before the point
Else ' The code element contains the point
If objCodeElement.Kind = eRequestedCodeElementKind Then
' Found
objResultCodeElement = objCodeElement
End If
' We enter in recursion, just in case there is an inner code element that also
' satisfies the conditions, for example, if we are searching a namespace or a class
colCodeElementMembers = GetCodeElementMembers(objCodeElement)
objMemberCodeElement = GetCodeElementAtTextPoint(eRequestedCodeElementKind, _
colCodeElementMembers, objTextPoint)
If Not (objMemberCodeElement Is Nothing) Then
' A nested code element also satisfies the conditions
objResultCodeElement = objMemberCodeElement
End If
Exit For
End If
Next
End If
Return objResultCodeElement
End Function
Private Function GetCodeElementMembers(ByVal objCodeElement As CodeElement) As EnvDTE.CodeElements
Dim colCodeElements As EnvDTE.CodeElements
If TypeOf objCodeElement Is EnvDTE.CodeNamespace Then
colCodeElements = CType(objCodeElement, EnvDTE.CodeNamespace).Members
ElseIf TypeOf objCodeElement Is EnvDTE.CodeType Then
colCodeElements = CType(objCodeElement, EnvDTE.CodeType).Members
ElseIf TypeOf objCodeElement Is EnvDTE.CodeFunction Then
colCodeElements = DirectCast(objCodeElement, EnvDTE.CodeFunction).Parameters
End If
Return colCodeElements
End Function
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|
| Copyright © 2000-2013 MZTools Software. All Rights Reserved. Legal Notice |