コード例 #1
0
ファイル: clang_indexer.py プロジェクト: FooBarrior/yavide
    def __find_all_references(self, id, args):
        start = time.clock()
        references = ()
        tunit = self.parser.parse(str(args[0]), str(args[0]))
        if tunit:
            cursor = self.parser.get_cursor(tunit, int(args[1]), int(args[2]))
            if cursor:
                # TODO In order to make find-all-references work on edited (and not yet saved) files,
                #      we would need to manipulate directly with USR.
                #      In case of edited files, USR contains a name of a temporary file we serialized
                #      the contents in and therefore will not match the USR in the database (which in
                #      contrast contains an original filename).
                usr = cursor.referenced.get_usr() if cursor.referenced else cursor.get_usr()
                ast_node_id = self.parser.get_ast_node_id(cursor)
                if ast_node_id in [ASTNodeId.getFunctionId(), ASTNodeId.getMethodId()]:
                    references = self.symbol_db.get_by_id(usr).fetchall()
                elif ast_node_id in [ASTNodeId.getClassId(), ASTNodeId.getStructId(), ASTNodeId.getEnumId(), ASTNodeId.getEnumValueId(), ASTNodeId.getUnionId(), ASTNodeId.getTypedefId()]:
                    references = self.symbol_db.get_by_id(usr).fetchall()
                elif ast_node_id in [ASTNodeId.getLocalVariableId(), ASTNodeId.getFunctionParameterId(), ASTNodeId.getFieldId()]:
                    references = self.symbol_db.get_by_id(usr).fetchall()
                elif ast_node_id in [ASTNodeId.getMacroDefinitionId(), ASTNodeId.getMacroInstantiationId()]:
                    references = self.symbol_db.get_by_id(usr).fetchall()
                else:
                    pass
            logging.info("Find-all-references operation of '{0}', [{1}, {2}], '{3}' took {4}".format(cursor.displayname, cursor.location.line, cursor.location.column, tunit.spelling, time.clock() - start))

        if self.callback:
            self.callback(id, [args, references])

        logging.info("\n{0}".format('\n'.join(str(ref) for ref in references)))
コード例 #2
0
 def visitor(ast_node, ast_parent_node, client_data):
     if ast_node.location.file and ast_node.location.file.name == tunit.spelling:  # we're only interested in symbols from associated translation unit
         ast_node_id = client_data.clang_parser.get_ast_node_id(ast_node)
         if ast_node_id != ASTNodeId.getUnsupportedId():
             highlight_rule = VimSyntaxGenerator.__tag_id_to_vim_syntax_group(ast_node_id) + " " + client_data.clang_parser.get_ast_node_name(ast_node)
             client_data.vim_syntax_element.append(
                 "call matchaddpos('" +
                 str(VimSyntaxGenerator.__tag_id_to_vim_syntax_group(ast_node_id)) +
                 "', [[" +
                 str(client_data.clang_parser.get_ast_node_line(ast_node)) +
                 ", " +
                 str(client_data.clang_parser.get_ast_node_column(ast_node)) +
                 ", " +
                 str(len(client_data.clang_parser.get_ast_node_name(ast_node))) +
                 "]], -1)" +
                 "\n"
             )
         else:
             logging.debug("Unsupported token id: [{0}, {1}]: {2} '{3}'".format(
                     ast_node.location.line, ast_node.location.column,
                     ast_node.kind, client_data.clang_parser.get_ast_node_name(ast_node)
                 )
             )
         return ChildVisitResult.RECURSE.value  # If we are positioned in TU of interest, then we'll traverse through all descendants
     return ChildVisitResult.CONTINUE.value  # Otherwise, we'll skip to the next sibling
コード例 #3
0
ファイル: syntax_generator.py プロジェクト: zhiminWu/yavide
    def __call__(self, clang_parser, args):
        start = time.clock()

        # Build Vim syntax highlight rules
        vim_syntax_element = ['call clearmatches()\n']
        ast_node_list = clang_parser.get_ast_node_list()
        for ast_node in ast_node_list:
            ast_node_id = clang_parser.get_ast_node_id(ast_node)
            if ast_node_id != ASTNodeId.getUnsupportedId():
                highlight_rule = self.__tag_id_to_vim_syntax_group(ast_node_id) + " " + clang_parser.get_ast_node_name(ast_node)
                vim_syntax_element.append(
                    "call matchaddpos('" +
                    str(self.__tag_id_to_vim_syntax_group(ast_node_id)) +
                    "', [[" +
                    str(clang_parser.get_ast_node_line(ast_node)) +
                    ", " +
                    str(clang_parser.get_ast_node_column(ast_node)) +
                    ", " +
                    str(len(clang_parser.get_ast_node_name(ast_node))) +
                    "]], -1)" +
                    "\n"
                )
            else:
                logging.debug("Unsupported token id: [{0}, {1}]: {2} '{3}'".format(
                        ast_node.location.line, ast_node.location.column, 
                        ast_node.kind, clang_parser.get_ast_node_name(ast_node)
                    )
                )

        # Write Vim syntax file
        vim_syntax_file = open(self.output_syntax_file, "w", 0)
        vim_syntax_file.writelines(vim_syntax_element)
        time_elapsed = time.clock() - start

        # Apply newly generated syntax rules
        YavideUtils.call_vim_remote_function(self.yavide_instance, "Y_SrcCodeHighlighter_Apply('" + clang_parser.filename + "'" + ", '" + self.output_syntax_file + "')")

        # Write some debug information
        clang_parser.dump_ast_nodes()

        # Log how long generating Vim syntax file took
        logging.info("Vim syntax generator for '{0}' took {1}.".format(clang_parser.filename, time_elapsed))
コード例 #4
0
ファイル: ctags_parser.py プロジェクト: FooBarrior/yavide
 def to_token_id(kind):
     if (kind == "namespace"):
         return ASTNodeId.getNamespaceId()
     if (kind == "class"):
         return ASTNodeId.getClassId()
     if (kind == "struct"):
         return ASTNodeId.getStructId()
     if (kind == "enum"):
         return ASTNodeId.getEnumId()
     if (kind == "enumerator"):
         return ASTNodeId.getEnumValueId()
     if (kind == "union"):
         return ASTNodeId.getUnionId()
     if (kind == "member"):
         return ASTNodeId.getClassStructUnionMemberId()
     if (kind == "local"):
         return ASTNodeId.getLocalVariableId()
     if (kind == "variable"):
         return ASTNodeId.getVariableDefinitionId()
     if (kind == "prototype"):
         return ASTNodeId.getFunctionPrototypeId()
     if (kind == "function"):
         return ASTNodeId.getFunctionDefinitionId()
     if (kind == "macro"):
         return ASTNodeId.getMacroId()
     if (kind == "typedef"):
         return ASTNodeId.getTypedefId()
     if (kind == "externvar"):
         return ASTNodeId.getExternFwdDeclarationId()
コード例 #5
0
 def to_token_id(kind):
     if (kind == "namespace"):
         return ASTNodeId.getNamespaceId()
     if (kind == "class"):
         return ASTNodeId.getClassId()
     if (kind == "struct"):
         return ASTNodeId.getStructId()
     if (kind == "enum"):
         return ASTNodeId.getEnumId()
     if (kind == "enumerator"):
         return ASTNodeId.getEnumValueId()
     if (kind == "union"):
         return ASTNodeId.getUnionId()
     if (kind == "member"):
         return ASTNodeId.getClassStructUnionMemberId()
     if (kind == "local"):
         return ASTNodeId.getLocalVariableId()
     if (kind == "variable"):
         return ASTNodeId.getVariableDefinitionId()
     if (kind == "prototype"):
         return ASTNodeId.getFunctionPrototypeId()
     if (kind == "function"):
         return ASTNodeId.getFunctionDefinitionId()
     if (kind == "macro"):
         return ASTNodeId.getMacroId()
     if (kind == "typedef"):
         return ASTNodeId.getTypedefId()
     if (kind == "externvar"):
         return ASTNodeId.getExternFwdDeclarationId()
コード例 #6
0
ファイル: clang_parser.py プロジェクト: xizhuanhe/yavide
 def to_ast_node_id(kind):
     if (kind == clang.cindex.CursorKind.NAMESPACE):
         return ASTNodeId.getNamespaceId()
     if (kind in [clang.cindex.CursorKind.CLASS_DECL, clang.cindex.CursorKind.CLASS_TEMPLATE, clang.cindex.CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION]):
         return ASTNodeId.getClassId()
     if (kind == clang.cindex.CursorKind.STRUCT_DECL):
         return ASTNodeId.getStructId()
     if (kind == clang.cindex.CursorKind.ENUM_DECL):
         return ASTNodeId.getEnumId()
     if (kind == clang.cindex.CursorKind.ENUM_CONSTANT_DECL):
         return ASTNodeId.getEnumValueId()
     if (kind == clang.cindex.CursorKind.UNION_DECL):
         return ASTNodeId.getUnionId()
     if (kind == clang.cindex.CursorKind.FIELD_DECL):
         return ASTNodeId.getFieldId()
     if (kind == clang.cindex.CursorKind.VAR_DECL):
         return ASTNodeId.getLocalVariableId()
     if (kind in [clang.cindex.CursorKind.FUNCTION_DECL, clang.cindex.CursorKind.FUNCTION_TEMPLATE]):
         return ASTNodeId.getFunctionId()
     if (kind in [clang.cindex.CursorKind.CXX_METHOD, clang.cindex.CursorKind.CONSTRUCTOR, clang.cindex.CursorKind.DESTRUCTOR]):
         return ASTNodeId.getMethodId()
     if (kind == clang.cindex.CursorKind.PARM_DECL):
         return ASTNodeId.getFunctionParameterId()
     if (kind == clang.cindex.CursorKind.TEMPLATE_TYPE_PARAMETER):
         return ASTNodeId.getTemplateTypeParameterId()
     if (kind == clang.cindex.CursorKind.TEMPLATE_NON_TYPE_PARAMETER):
         return ASTNodeId.getTemplateNonTypeParameterId()
     if (kind == clang.cindex.CursorKind.TEMPLATE_TEMPLATE_PARAMETER):
         return ASTNodeId.getTemplateTemplateParameterId()
     if (kind == clang.cindex.CursorKind.MACRO_DEFINITION):
         return ASTNodeId.getMacroDefinitionId()
     if (kind == clang.cindex.CursorKind.MACRO_INSTANTIATION):
         return ASTNodeId.getMacroInstantiationId()
     if (kind in [clang.cindex.CursorKind.TYPEDEF_DECL, clang.cindex.CursorKind.TYPE_ALIAS_DECL]):
         return ASTNodeId.getTypedefId()
     if (kind == clang.cindex.CursorKind.NAMESPACE_ALIAS):
         return ASTNodeId.getNamespaceAliasId()
     if (kind == clang.cindex.CursorKind.USING_DIRECTIVE):
         return ASTNodeId.getUsingDirectiveId()
     if (kind == clang.cindex.CursorKind.USING_DECLARATION):
         return ASTNodeId.getUsingDeclarationId()
     return ASTNodeId.getUnsupportedId()
コード例 #7
0
 def __tag_id_to_vim_syntax_group(tag_identifier):
     if tag_identifier == ASTNodeId.getNamespaceId():
         return "yavideCppNamespace"
     if tag_identifier == ASTNodeId.getNamespaceAliasId():
         return "yavideCppNamespaceAlias"
     if tag_identifier == ASTNodeId.getClassId():
         return "yavideCppClass"
     if tag_identifier == ASTNodeId.getStructId():
         return "yavideCppStructure"
     if tag_identifier == ASTNodeId.getEnumId():
         return "yavideCppEnum"
     if tag_identifier == ASTNodeId.getEnumValueId():
         return "yavideCppEnumValue"
     if tag_identifier == ASTNodeId.getUnionId():
         return "yavideCppUnion"
     if tag_identifier == ASTNodeId.getFieldId():
         return "yavideCppField"
     if tag_identifier == ASTNodeId.getLocalVariableId():
         return "yavideCppLocalVariable"
     if tag_identifier == ASTNodeId.getFunctionId():
         return "yavideCppFunction"
     if tag_identifier == ASTNodeId.getMethodId():
         return "yavideCppMethod"
     if tag_identifier == ASTNodeId.getFunctionParameterId():
         return "yavideCppFunctionParameter"
     if tag_identifier == ASTNodeId.getTemplateTypeParameterId():
         return "yavideCppTemplateTypeParameter"
     if tag_identifier == ASTNodeId.getTemplateNonTypeParameterId():
         return "yavideCppTemplateNonTypeParameter"
     if tag_identifier == ASTNodeId.getTemplateTemplateParameterId():
         return "yavideCppTemplateTemplateParameter"
     if tag_identifier == ASTNodeId.getMacroDefinitionId():
         return "yavideCppMacroDefinition"
     if tag_identifier == ASTNodeId.getMacroInstantiationId():
         return "yavideCppMacroInstantiation"
     if tag_identifier == ASTNodeId.getTypedefId():
         return "yavideCppTypedef"
     if tag_identifier == ASTNodeId.getUsingDirectiveId():
         return "yavideCppUsingDirective"
     if tag_identifier == ASTNodeId.getUsingDeclarationId():
         return "yavideCppUsingDeclaration"
コード例 #8
0
ファイル: clang_indexer.py プロジェクト: FooBarrior/yavide
 def visitor(ast_node, ast_parent_node, parser):
     ast_node_location = ast_node.location
     ast_node_tunit_spelling = ast_node.translation_unit.spelling
     if (ast_node_location.file and ast_node_location.file.name == ast_node_tunit_spelling):  # we are not interested in symbols which got into this TU via includes
         id = parser.get_ast_node_id(ast_node)
         usr = ast_node.referenced.get_usr() if ast_node.referenced else ast_node.get_usr()
         line = int(parser.get_ast_node_line(ast_node))
         column = int(parser.get_ast_node_column(ast_node))
         if id in [
             ASTNodeId.getClassId(), ASTNodeId.getStructId(), ASTNodeId.getEnumId(), ASTNodeId.getEnumValueId(), # handle user-defined types
             ASTNodeId.getUnionId(), ASTNodeId.getTypedefId(), ASTNodeId.getUsingDeclarationId(),
             ASTNodeId.getFunctionId(), ASTNodeId.getMethodId(),                                                 # handle functions and methods
             ASTNodeId.getLocalVariableId(), ASTNodeId.getFunctionParameterId(), ASTNodeId.getFieldId(),         # handle local/function variables and member variables
             ASTNodeId.getMacroDefinitionId(), ASTNodeId.getMacroInstantiationId()                               # handle macros
         ]:
             symbol_db.insert_single(
                 get_basename(root_directory, ast_node_tunit_spelling),
                 line,
                 column,
                 usr,
                 extract_cursor_context(ast_node_tunit_spelling, line),
                 ast_node.referenced._kind_id if ast_node.referenced else ast_node._kind_id,
                 ast_node.is_definition()
             )
         else:
             pass
         return ChildVisitResult.RECURSE.value  # If we are positioned in TU of interest, then we'll traverse through all descendants
     return ChildVisitResult.CONTINUE.value  # Otherwise, we'll skip to the next sibling
コード例 #9
0
ファイル: clang_indexer.py プロジェクト: xizhuanhe/yavide
 def visitor(ast_node, ast_parent_node, parser):
     ast_node_location = ast_node.location
     ast_node_tunit_spelling = ast_node.translation_unit.spelling
     if (
             ast_node_location.file
             and ast_node_location.file.name == ast_node_tunit_spelling
     ):  # we are not interested in symbols which got into this TU via includes
         id = parser.get_ast_node_id(ast_node)
         usr = ast_node.referenced.get_usr(
         ) if ast_node.referenced else ast_node.get_usr()
         line = int(parser.get_ast_node_line(ast_node))
         column = int(parser.get_ast_node_column(ast_node))
         if id in [
                 ASTNodeId.getClassId(),
                 ASTNodeId.getStructId(),
                 ASTNodeId.getEnumId(),
                 ASTNodeId.getEnumValueId(),  # handle user-defined types
                 ASTNodeId.getUnionId(),
                 ASTNodeId.getTypedefId(),
                 ASTNodeId.getUsingDeclarationId(),
                 ASTNodeId.getFunctionId(),
                 ASTNodeId.getMethodId(),  # handle functions and methods
                 ASTNodeId.getLocalVariableId(),
                 ASTNodeId.getFunctionParameterId(),
                 ASTNodeId.getFieldId(
                 ),  # handle local/function variables and member variables
                 ASTNodeId.getMacroDefinitionId(),
                 ASTNodeId.getMacroInstantiationId()  # handle macros
         ]:
             symbol_db.insert_single(
                 get_basename(root_directory,
                              ast_node_tunit_spelling), line, column, usr,
                 extract_cursor_context(ast_node_tunit_spelling, line),
                 ast_node.referenced._kind_id
                 if ast_node.referenced else ast_node._kind_id,
                 ast_node.is_definition())
         else:
             pass
         return ChildVisitResult.RECURSE.value  # If we are positioned in TU of interest, then we'll traverse through all descendants
     return ChildVisitResult.CONTINUE.value  # Otherwise, we'll skip to the next sibling
コード例 #10
0
ファイル: clang_indexer.py プロジェクト: xizhuanhe/yavide
    def __find_all_references(self, id, args):
        start = time.clock()
        references = ()
        tunit = self.parser.parse(str(args[0]), str(args[0]))
        if tunit:
            cursor = self.parser.get_cursor(tunit, int(args[1]), int(args[2]))
            if cursor:
                # TODO In order to make find-all-references work on edited (and not yet saved) files,
                #      we would need to manipulate directly with USR.
                #      In case of edited files, USR contains a name of a temporary file we serialized
                #      the contents in and therefore will not match the USR in the database (which in
                #      contrast contains an original filename).
                usr = cursor.referenced.get_usr(
                ) if cursor.referenced else cursor.get_usr()
                ast_node_id = self.parser.get_ast_node_id(cursor)
                if ast_node_id in [
                        ASTNodeId.getFunctionId(),
                        ASTNodeId.getMethodId()
                ]:
                    references = self.symbol_db.get_by_id(usr).fetchall()
                elif ast_node_id in [
                        ASTNodeId.getClassId(),
                        ASTNodeId.getStructId(),
                        ASTNodeId.getEnumId(),
                        ASTNodeId.getEnumValueId(),
                        ASTNodeId.getUnionId(),
                        ASTNodeId.getTypedefId()
                ]:
                    references = self.symbol_db.get_by_id(usr).fetchall()
                elif ast_node_id in [
                        ASTNodeId.getLocalVariableId(),
                        ASTNodeId.getFunctionParameterId(),
                        ASTNodeId.getFieldId()
                ]:
                    references = self.symbol_db.get_by_id(usr).fetchall()
                elif ast_node_id in [
                        ASTNodeId.getMacroDefinitionId(),
                        ASTNodeId.getMacroInstantiationId()
                ]:
                    references = self.symbol_db.get_by_id(usr).fetchall()
                else:
                    pass
            logging.info(
                "Find-all-references operation of '{0}', [{1}, {2}], '{3}' took {4}"
                .format(cursor.displayname, cursor.location.line,
                        cursor.location.column, tunit.spelling,
                        time.clock() - start))

        if self.callback:
            self.callback(id, [args, references])

        logging.info("\n{0}".format('\n'.join(str(ref) for ref in references)))