Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a macro that works fine for C# code. </p> <p>When I tested it on a VB .Net project it didn't work for types in assembly references. Seems like the project code model for VB.Net excludes external types.</p> <p>I include the code here anyway. Maybe someone else knows how to do it for VB</p> <hr> <p>First we need a function that finds the current context</p> <pre><code>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 </code></pre> <p>We also need a function that create all candidate names based on using/import statements </p> <pre><code>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 &amp; "." &amp; className) FindAllCandidates(CType(elem, CodeClass).Parent, className) ElseIf TypeOf elem Is CodeStruct Then mCandidates.Add(CType(elem, CodeStruct).FullName &amp; "." &amp; className) FindAllCandidates(CType(elem, CodeStruct).Parent, className) ElseIf TypeOf elem Is CodeNamespace Then mCandidates.Add(CType(elem, CodeNamespace).FullName &amp; "." &amp; className) For Each ns As String In CType(elem, CodeNamespace).Members.OfType(Of CodeImport) _ .Select(Function(x) x.Namespace) mCandidates.Add(ns &amp; "." &amp; 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 &amp; "." &amp; className) Next End If End Sub </code></pre> <p>And then a function that loops all available items to find one of the candidates</p> <pre><code>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 </code></pre> <p>Two small helper functions</p> <pre><code>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 </code></pre> <p>And then we can write the main function which you can alter depending on how you want to use it.</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload