检测方法名称:Visual Studio 2008 Add-in

发布于 2024-12-09 19:04:27 字数 66 浏览 0 评论 0原文

如果光标位于 ac# 文件中函数的左大括号和右大括号之间,我想获取加载项中函数的名称。

有什么想法吗?

I want to get the name of the function in an add-in if cursor is in between the opening and closing brace of a function in a c# file.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2024-12-16 19:04:27

获取EnvDTE中当前文档的TextPoint的CodeElement。
这是一些宏代码:

'Option Strict Off
'Option Explicit Off

Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.ControlChars
Imports System.Windows.Forms
Imports Microsoft.VisualStudio.VCCodeModel
Imports Microsoft.VisualStudio.VCProjectEngine

Public Module Module1

Sub Macro2()
    Dim objTextDocument As EnvDTE.TextDocument
    Dim objCursorTextPoint As EnvDTE.TextPoint

    Try
        objTextDocument = CType(DTE.ActiveDocument.Object, EnvDTE.TextDocument)
        objCursorTextPoint = objTextDocument.Selection.ActivePoint()
        objTextDocument.Selection.LineUp()
        objTextDocument.Selection.SelectLine()
        If objTextDocument.Selection.Text.Contains("*/") Then
            objTextDocument.Selection.LineUp()
            objTextDocument.Selection.LineDown()
            objTextDocument.Selection.NewLine()
            objTextDocument.Selection.LineUp()
            objTextDocument.Selection.Insert("Released", vsInsertFlags.vsInsertFlagsInsertAtStart)
        End If



        MsgBox(objTextDocument.Selection.Text)
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try
End Sub

Sub Macro1()
    Dim objCodeElement As EnvDTE.CodeElement
    Dim objCursorTextPoint As EnvDTE.TextPoint

    Try
        objCursorTextPoint = GetCursorTextPoint()

        If Not (objCursorTextPoint Is Nothing) Then
            For i As Integer = 0 To 39
                objCodeElement = objCursorTextPoint.CodeElement(i)
                If Not objCodeElement Is Nothing Then
                    MsgBox("wow, we have something: " & objCodeElement.FullName)
                End If
            Next i
            If objCodeElement Is Nothing Then
                MsgBox("Visual studio is a joke")
            End If
        End If
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try
End Sub


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.vsCMElementFunction, _
               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


Sub PropertiesExample()
    ' Create and initialize a variable to represent the Documents 
    ' Options page.
    Dim envGenTab As EnvDTE.Properties = _
    DTE.Properties("Environment", "ProjectsAndSolution")
    Dim prop As EnvDTE.Property
    Dim msg As String

    ' Loop through each item in the Documents Options box.
    For Each prop In envGenTab
        msg += ("PROP NAME: " & prop.Name & "   VALUE: " & _
        prop.Value) & vbCr
    Next
    MsgBox(msg)
End Sub

End Module

唉,VCProjects 似乎工作得不太好......

Get the CodeElement of the TextPoint of the current document in EnvDTE.
Here is some macro code:

'Option Strict Off
'Option Explicit Off

Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.ControlChars
Imports System.Windows.Forms
Imports Microsoft.VisualStudio.VCCodeModel
Imports Microsoft.VisualStudio.VCProjectEngine

Public Module Module1

Sub Macro2()
    Dim objTextDocument As EnvDTE.TextDocument
    Dim objCursorTextPoint As EnvDTE.TextPoint

    Try
        objTextDocument = CType(DTE.ActiveDocument.Object, EnvDTE.TextDocument)
        objCursorTextPoint = objTextDocument.Selection.ActivePoint()
        objTextDocument.Selection.LineUp()
        objTextDocument.Selection.SelectLine()
        If objTextDocument.Selection.Text.Contains("*/") Then
            objTextDocument.Selection.LineUp()
            objTextDocument.Selection.LineDown()
            objTextDocument.Selection.NewLine()
            objTextDocument.Selection.LineUp()
            objTextDocument.Selection.Insert("Released", vsInsertFlags.vsInsertFlagsInsertAtStart)
        End If



        MsgBox(objTextDocument.Selection.Text)
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try
End Sub

Sub Macro1()
    Dim objCodeElement As EnvDTE.CodeElement
    Dim objCursorTextPoint As EnvDTE.TextPoint

    Try
        objCursorTextPoint = GetCursorTextPoint()

        If Not (objCursorTextPoint Is Nothing) Then
            For i As Integer = 0 To 39
                objCodeElement = objCursorTextPoint.CodeElement(i)
                If Not objCodeElement Is Nothing Then
                    MsgBox("wow, we have something: " & objCodeElement.FullName)
                End If
            Next i
            If objCodeElement Is Nothing Then
                MsgBox("Visual studio is a joke")
            End If
        End If
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try
End Sub


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.vsCMElementFunction, _
               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


Sub PropertiesExample()
    ' Create and initialize a variable to represent the Documents 
    ' Options page.
    Dim envGenTab As EnvDTE.Properties = _
    DTE.Properties("Environment", "ProjectsAndSolution")
    Dim prop As EnvDTE.Property
    Dim msg As String

    ' Loop through each item in the Documents Options box.
    For Each prop In envGenTab
        msg += ("PROP NAME: " & prop.Name & "   VALUE: " & _
        prop.Value) & vbCr
    Next
    MsgBox(msg)
End Sub

End Module

Alas, VCProjects do not seem to work all that well...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文