代码之家  ›  专栏  ›  技术社区  ›  default

Visual studio宏-入门|将定义从.cpp复制到.h

  •  1
  • default  · 技术社区  · 16 年前

    Foo::Foo(int aParameter, int bParameter){
        //
    }
    
    int Foo::someMethod(char aCharacter) const {
        return 0;  
    }
    

    从.cpp文件可以是:

    class Foo {
        Foo(int aParameter, int bParameter);
    
        int someMethod(char aCharacter) const;
    };
    

    如果有任何人拥有针对visualstudio.Net的优秀教程或文档的知识(可能也涵盖了上述问题),我可能也会接受这个答案

    3 回复  |  直到 14 年前
        1
  •  1
  •   Hans Passant    16 年前

    不,这是几乎不可能得到100%可靠。必须编写一个C++语言解析器,以便能够准确地从声明中提升类名,并处理诸如默认参数值和缺少参数名等复杂度。编写C++解析器在任何语言中都很难,但你会特别发现VBA缺乏。

    退房 Visual Assist . 他们的网站现在坏了(哦哦),不能给你一个准确的链接。

        2
  •  0
  •   Default    15 年前

    它既不漂亮也不快,但确实管用。
    使用录制宏功能,浏览对象浏览器以获取调用,然后将其拼接在一起。
    我的visual basic知识有限,所以 . 例如,当我试图定位 class <name> { 行。有什么建议可以加速吗?

    Imports EnvDTE
    
    Public Module Module1
        Function getFileName(ByRef str As String) As String
            If (str = "main.cpp") Then
                Return ""
            Else
                Dim pos As Integer
                pos = InStr(str, ".cpp")
                If (pos = 0) Then
                    ' not a .cpp file.'
                    Return ""
                Else
                    Dim header As String
                    header = Left(str, pos - 1)
                    Return header
                End If
            End If
            Return ""
        End Function
    
        Function getParts(ByRef str As String, ByRef returnvalue As String, ByRef classname As String, ByRef identifier As String, ByRef arguments As String) As String
            ' common function looks like:'
            '   <return_value> <classname>::<identifier>([arguments])[{]'
            '                 ^divider    ^colonposition            ^parenthesisEnd'
            '                  ^classnamepos            ^parenthesisStart'
            ''
            ' exceptions: ctor and dtor'
            '   <classname>::[~]<classname>([arguments])[{]'
    
            Dim colonposition As Integer
            Dim classnameposition As Integer
            Dim divider As Integer
            Dim divider2 As Integer
            Dim parenthesisStart As Integer
            Dim parenthesisEnd As Integer
    
            colonposition = InStr(str, "::")
            parenthesisStart = InStr(str, "(")
            parenthesisEnd = InStr(str, ")")
            If (colonposition = 0 Or parenthesisStart = 0 Or parenthesisEnd = 0) Then
                Return "Not a function line" ' no colons or parenthesis found? maybe not a function line'
            End If
    
            divider = InStr(str, " ")
    
            ' do we have a ctor/dtor?'
            If (divider > colonposition Or divider = 0) Then
    
                Return "constructor or destructor"
            End If
            ' this might be a function'
            While True
                divider2 = InStr(divider + 1, str, " ")
                If (divider2 > colonposition) Then
                    Exit While
                End If
                divider = divider2
            End While
            ' now we have the full return value in 0 -> divider-1'
            returnvalue = Left(str, divider - 1)
            ' and the classname as well'
            classname = Left(Right(str, str.Length - divider), colonposition - divider - 1)
            'indentifier is right after the :: and before the parenthesis'
            identifier = Left(Right(str, str.Length - colonposition - 1), parenthesisStart - colonposition - 2)
            ' and not to mention the arguments between the parenthesis'
            arguments = Left(Right(str, str.Length - parenthesisStart), parenthesisEnd - parenthesisStart - 1)
    
    
            Return "Success"
        End Function
    
        Sub getDefinition()
            Dim sourcefile As String
            Dim filename As String
            Dim header As String
            Dim returnvalue As String
            Dim classname As String
            Dim identifier As String
            Dim arguments As String
    
            Dim str As String
            Dim line As String
            Dim pos As Integer
    
    
            sourcefile = DTE.ActiveDocument.Name
            ' get the filename for the current file (without the extension)'
            filename = getFileName(sourcefile)
            If (filename.Length = 0) Then
                MsgBox("Im not in a .cpp file", , "GetDefinition")
                Return
            End If
    
            ' get the current line'
            DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
            DTE.ActiveDocument.Selection.EndOfLine(True)
            line = DTE.ActiveDocument.Selection.Text()
            ' now interpret the line'
            str = getParts(line, returnvalue, classname, identifier, arguments)
            If (Not str = "Success") Then
                MsgBox(str, , "GetDefinition")
                Exit Sub
            End If
            ' the str should be put into the header file as of:'
            ' class <classname>[:<base>][{]'
            ' [{]'
            '     <--- somewhere here'
            ' }'
    
            ' attach the header ending'
            header = filename + ".h"
            ' activate the header file'
            DTE.Windows.Item(header).Activate()
            DTE.ActiveDocument.Selection.StartOfDocument()
    
    
            While (True)
                DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
                DTE.ActiveDocument.Selection.EndOfLine(True)
                line = DTE.ActiveDocument.Selection.Text()
                pos = InStr(line, classname)
                If (Not pos = 0) Then
                    Exit While
                End If
                DTE.ActiveDocument.Selection.LineDown(False, 1)
            End While
            ' found the class definition'
            While (True)
                pos = InStr(line, "{")
                If (Not pos = 0) Then
                    Exit While
                End If
                DTE.ActiveDocument.Selection.LineDown(False, 1)
            End While
            DTE.ActiveDocument.Selection.EndOfLine(False)
            DTE.ActiveDocument.Selection.NewLine()
    
            DTE.ActiveDocument.Selection.Text = returnvalue & " " & identifier & "(" & arguments & ");"
    
    
        End Sub
    End Module
    
        3
  •  0
  •   default    15 年前

    Automation and Extensibility for Visual Studio ,它涵盖了我正在尝试做的事情,通过对象浏览器和录制宏功能提供了一些帮助。