这是一个适用于 C# 代码的宏。
当我在 VB .Net 项目上对其进行测试时,它不适用于程序集引用中的类型。似乎 VB.Net 的项目代码模型不包括外部类型。
无论如何,我在此处包含代码。也许其他人知道如何为 VB 做到这一点
首先我们需要一个查找当前上下文的函数
Private Function FindCodeElement(ByVal caretPosition As TextPoint, ByVal elems As CodeElements) As CodeElement
If elems Is Nothing Then Return Nothing
Return elems.Cast(Of CodeElement) _
.Where(Function(x) x.StartPoint.LessThan(caretPosition) AndAlso _
x.EndPoint.GreaterThan(caretPosition)) _
.Select(Function(x) If(FindCodeElement(caretPosition, GetMembers(x)), x)) _
.FirstOrDefault()
End Function
我们还需要一个基于 using/import 语句创建所有候选名称的函数
Private Sub FindAllCandidates(ByVal elem As Object, ByVal className As String)
If TypeOf elem Is CodeFunction Then
FindAllCandidates(CType(elem, CodeFunction).Parent, className)
ElseIf TypeOf elem Is CodeClass Then
mCandidates.Add(CType(elem, CodeClass).FullName & "." & className)
FindAllCandidates(CType(elem, CodeClass).Parent, className)
ElseIf TypeOf elem Is CodeStruct Then
mCandidates.Add(CType(elem, CodeStruct).FullName & "." & className)
FindAllCandidates(CType(elem, CodeStruct).Parent, className)
ElseIf TypeOf elem Is CodeNamespace Then
mCandidates.Add(CType(elem, CodeNamespace).FullName & "." & className)
For Each ns As String In CType(elem, CodeNamespace).Members.OfType(Of CodeImport) _
.Select(Function(x) x.Namespace)
mCandidates.Add(ns & "." & className)
Next
FindAllCandidates(CType(elem, CodeNamespace).Parent, className)
ElseIf TypeOf elem Is FileCodeModel Then
For Each ns As String In CType(elem, FileCodeModel).CodeElements.OfType(Of CodeImport) _
.Select(Function(x) x.Namespace)
mCandidates.Add(ns & "." & className)
Next
End If
End Sub
然后是一个循环所有可用项目以找到候选对象之一的函数
Private Function FindClassInCodeElements(ByVal elems As CodeElements) As CodeElement
If elems Is Nothing Then Return Nothing
For Each elem As CodeElement In elems
If IsClassType(elem) Then
If mCandidates.Contains(elem.FullName) Then Return elem
ElseIf TypeOf elem Is CodeNamespace Then
For Each candidate As String In mCandidates
If candidate.StartsWith(elem.FullName) Then
Dim found As CodeElement = FindClassInCodeElements(GetMembers(elem))
If found IsNot Nothing Then Return found
Exit For
End If
Next
End If
Next
Return Nothing
End Function
两个小辅助函数
Private Function IsClassType(ByVal elem As CodeElement) As Boolean
Return TypeOf elem Is CodeClass OrElse TypeOf elem Is CodeStruct OrElse TypeOf elem Is CodeInterface
End Function
Private Function GetMembers(ByVal elem As CodeElement) As CodeElements
If TypeOf elem Is CodeClass Then
Return CType(elem, CodeClass).Members
ElseIf TypeOf elem Is CodeNamespace Then
Return CType(elem, CodeNamespace).Members
ElseIf TypeOf elem Is CodeStruct Then
Return CType(elem, CodeStruct).Members
ElseIf TypeOf elem Is CodeInterface Then
Return CType(elem, CodeInterface).Members
End If
Return Nothing
End Function
然后我们可以编写 main 函数,您可以根据自己的使用方式对其进行更改。
Dim mCandidates As New HashSet(Of String)
Sub ExpandFullNameOfSelection()
Dim selection As EnvDTE.TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
' Assume type is selected
Dim className As String = selection.Text
' Find current context
Dim currentFunction As CodeElement = FindCodeElement(selection.ActivePoint, DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElements)
mCandidates.Clear()
FindAllCandidates(currentFunction, className)
Dim classType As CodeElement = DTE.Solution.Projects.Cast(Of Project) _
.Where(Function(x) x.CodeModel IsNot Nothing) _
.Select(Function(x) FindClassInCodeElements(x.CodeModel.CodeElements)) _
.FirstOrDefault(Function(x) x IsNot Nothing)
If classType IsNot Nothing Then
selection.Text = classType.FullName ' replace with full name
End If
End Function