如何解决Visual Studio 2012中缺少宏的问题

6

我所在的组织有极其严格和死板的规则,我们的代码必须遵守这些规则才能获得认证和授权。过去十年左右,我们开发了近百个VS宏,用于格式化代码、生成注释块、强制执行样式规则等。

我们的宏不是你录制鼠标动作的那种类型;它们都依赖于EnvDTE * VS自动化对象。随着VS 2012放弃宏,我们不知道是否能够升级,而不对团队产生重大影响。

我知道微软正在朝着VS Add-ins的方向发展,我愿意探索这条路线,但我在如何让VS Add-In与Visual Studio中的活动代码文件交互方面遇到了问题,无法找到代码示例或文档。

例如,下面是一个我们经常使用的宏,它将我们的Try包装器设计模式应用于所有能够抛出未处理异常的方法。

''' <summary>
'''  Wraps active method in Try* access wrappers.
''' </summary>
Sub InsertSingleMethodTryWrappers()
    Dim textSelection As TextSelection
    Dim codeElement As CodeElement

    textSelection = DTE.ActiveWindow.Selection

    DTE.UndoContext.Open("Generate Try Wrappers")  'Allow for single Undo operation to rollback all changes

    Try
        codeElement = textSelection.ActivePoint.CodeElement(vsCMElement.vsCMElementFunction)
        If Not (codeElement Is Nothing) Then
            Dim textSelection2 As TextSelection
            Dim codeFunction As CodeFunction
            'Dim codeFunction2 As CodeFunction2
            Dim editPoint As EditPoint
            Dim codeParameter As CodeParameter
            Dim parameters As CodeElements
            Dim codeElement2 As CodeElement
            Dim isVirtual As Boolean = False
            Dim strVirtual As String = String.Empty
            Dim strTypeName As String = String.Empty

            '' Cast the codeElement to codeFunction object
            codeFunction = codeElement

            '' Move cursor to the start of the method
            textSelection.MoveToPoint(codeFunction.GetStartPoint(vsCMPart.vsCMPartHeader))

            '' Should be able to use codeFunction.Kind.ToString to retrieve the function type
            '' vsCMFunctionVirtual if the method is virtual but there is a bug in the API
            '' that returns vsCMFunctionFunction even if the function is virtual (C# parsing bug?)
            ''
            '' vsCMFunction Type
            '' http://msdn.microsoft.com/en-us/library/envdte.vscmfunction(v=vs.80).aspx
            ''
            '' This frustrating bug means that we have to parse the header to determine if virtual
            textSelection.EndOfLine(True)
            If (textSelection.Text.IndexOf("virtual") > 0) Then
                isVirtual = True
                strVirtual = " virtual"
            End If
            textSelection.StartOfLine()

            '' Try not to screw up comments and attributes
            editPoint = GetNoneCommentOrAttribHeaderEditPoint(textSelection)
            If editPoint Is Nothing Then
                MsgBox("Could not find a line above the method that isn't a comment or attribute", _
                       MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
                Exit Sub
            End If

            '' Create an EditPoint to inject Try* methods
            'editPoint = textSelection.TopPoint.CreateEditPoint()
            'editPoint.LineUp()  'Move up 1 line
            editPoint.EndOfLine()   'Go to end of line above signature
            editPoint.Insert(Environment.NewLine)   'Insert blank line for cleanliness
            editPoint.Insert(Environment.NewLine)   'Insert blank line for cleanliness
            editPoint.LineUp()  'Move up 1 line

            parameters = codeFunction.Parameters

            Dim strAccess As String : strAccess = GetAccessModifierString(codeFunction.Access) 'Access Modifier
            Dim strName As String : strName = codeElement.Name  'Member Name
            Dim strType As String : strType = codeFunction.Type.AsString    'Type Name

            '' Get the un-qualified object name
            If (strType.IndexOf(".") > 0) Then
                Dim arrType() As String = strType.Split(".")
                strTypeName = arrType(arrType.Length - 1)
            Else
                strTypeName = strType
            End If

            ''' Create parameter type/name arrayList
            Dim arrParams As System.Collections.ArrayList
            arrParams = New System.Collections.ArrayList()

            For Each codeElement2 In parameters
                codeParameter = codeElement2
                arrParams.Add(codeParameter.Type.AsString.Trim & " " & codeParameter.Name.Trim & ", ")
            Next

            Dim strParams As String
            Dim strParamNames As String

            '' Capture a string with parameter names and types and one just of names
            For Each strParam As String In arrParams
                strParams += strParam
                strParamNames += strParam.Split(" ")(1)
            Next

            '' Trim excess comma for members of type void
            If strType = "void" Then
                If Not String.IsNullOrEmpty(strParams) Then
                    If strParams.TrimEnd.EndsWith(",") Then
                        strParams = strParams.TrimEnd()
                        strParams = strParams.Remove(strParams.Length - 1, 1)
                    End If
                End If
            End If

            '' -- Try* swallow methods --
            '' we don't care what the exception is, we just want to know success or failure
            Dim strTrySwallowSignature As String
            Dim strTrySwallowBody As String
            Dim strTryOutParams As String
            Dim strOutDef As String
            Dim strOutSig As String

            '' Members of type 'void' get no out parameters
            If Not strType = "void" Then
                strTryOutParams = "out " & strTypeName & " outObjType"
                strOutDef = "outObjType = null;"
                strOutSig = " out outObjType,"
            End If

            strTrySwallowSignature = vbTab & vbTab & strAccess & strVirtual & " bool Try" & strName & "(" & strParams & strTryOutParams & ")"
            strTrySwallowBody = vbCrLf & vbTab & vbTab & "{" _
                              & vbCrLf & vbTab & vbTab & vbTab & "Exception exception;" _
                              & vbCrLf & vbTab & vbTab & vbTab & strOutDef _
                              & vbCrLf & vbTab & vbTab & vbTab & "return Try" & strName & "(" & strParamNames & strOutSig & " out exception);" _
                              & vbCrLf & vbTab & vbTab & "}"


            '' -- Try* re-throw methods --
            '' We want to know success or failure as well as the exception if it failed
            Dim strTryReThrowSignature As String
            Dim strTryReThrowBody As String

            '' Members of type 'void' only get out exception parameter
            If Not strType = "void" Then
                strTryOutParams = "out " & strTypeName & " outObjType, out Exception exception"
                'strOutDef = "outObjType = new " & strTypeName & "();"
                strOutDef = "outObjType = null;"
            Else
                strTryOutParams = "out Exception exception"
            End If

            strTryReThrowSignature = vbTab & vbTab & strAccess & strVirtual & " bool Try" & strName & "(" & strParams & strTryOutParams & ")"
            strTryReThrowBody = vbCrLf & vbTab & vbTab & "{" _
                              & vbCrLf & vbTab & vbTab & vbTab & "bool result = false;" _
                              & vbCrLf & vbTab & vbTab & vbTab & "exception = null;" _
                              & vbCrLf & vbTab & vbTab & vbTab & strOutDef _
                              & vbCrLf & vbTab & vbTab & vbTab & "try" _
                              & vbCrLf & vbTab & vbTab & vbTab & "{" _
                              & vbCrLf & vbTab & vbTab & vbTab & vbTab & "// insert code here " _
                              & vbCrLf & vbTab & vbTab & vbTab & vbTab & "//result = true; " _
                              & vbCrLf & vbTab & vbTab & vbTab & vbTab & "throw new NotImplementedException();" _
                              & vbCrLf & vbTab & vbTab & vbTab & "}" _
                              & vbCrLf & vbTab & vbTab & vbTab & "catch (Exception e)" _
                              & vbCrLf & vbTab & vbTab & vbTab & "{" _
                              & vbCrLf & vbTab & vbTab & vbTab & vbTab & "exception = e;" _
                              & vbCrLf & vbTab & vbTab & vbTab & "}" _
                              & vbCrLf & vbTab & vbTab & vbTab & "return result;" _
                              & vbCrLf & vbTab & vbTab & "}"

            editPoint.Insert(strTrySwallowSignature)
            editPoint.Insert(strTrySwallowBody)
            editPoint.Insert(vbCrLf & vbCrLf)
            editPoint.Insert(strTryReThrowSignature)
            editPoint.Insert(strTryReThrowBody)
            editPoint.Insert(vbCrLf)

        End If
    Catch Ex As Exception
        MsgBox(Ex.Message)
    Finally
        DTE.UndoContext.Close()
    End Try
End Sub

请问有没有人能指导我如何使用EnvDTE* 或其他可用于2012的对象模型来操纵活动/打开的代码文件,作为VS 2012附加组件?


问题已解决... 宏对象模型是VS对象模型的一部分,可以直接在插件中使用。 - Neal Bailey
1个回答

4

如何开始在vs2012中编写“宏”?有链接或类似的东西吗? - LosManos
2
@LosManos 请查看 https://dev59.com/mWct5IYBdhLWcg3wh9xK - Dominik Grabiec

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接