From 31280bde3cb86ab77064dbf31d023637f4790b61 Mon Sep 17 00:00:00 2001 From: xaizek Date: Sun, 16 Jul 2017 23:18:02 +0300 Subject: [PATCH] Add g:ClangListSymbols() to list symbols --- plugin/clang/cindex.py | 60 ++++++++++++++++++++++++++++++++++++++- plugin/clang_complete.vim | 5 ++++ plugin/libclang.py | 25 ++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/plugin/clang/cindex.py b/plugin/clang/cindex.py index 4e6e3c5b..871cabd9 100644 --- a/plugin/clang/cindex.py +++ b/plugin/clang/cindex.py @@ -205,6 +205,11 @@ def from_offset(tu, file, offset): """ return conf.lib.clang_getLocationForOffset(tu, file, offset) + @property + def isFromMainFile(self): + """Get the file represented by this source location.""" + return conf.lib.clang_Location_isFromMainFile(self) + @property def file(self): """Get the file represented by this source location.""" @@ -732,10 +737,14 @@ def __repr__(self): # A reference to a labeled statement. CursorKind.LABEL_REF = CursorKind(48) -# A reference toa a set of overloaded functions or function templates +# A reference to a set of overloaded functions or function templates # that has not yet been resolved to a specific function or function template. CursorKind.OVERLOADED_DECL_REF = CursorKind(49) +# A reference to a variable that occurs in some non-expression +# context, e.g., a C++ lambda capture list. +CursorKind.VARIABLE_REF = CursorKind(50) + ### # Invalid/Error Kinds @@ -919,6 +928,26 @@ def __repr__(self): # pack. CursorKind.SIZE_OF_PACK_EXPR = CursorKind(143) +# Represents a C++ lambda expression that produces a local function +# object. +# +# \code +# void abssort(float *x, unsigned N) { +# std::sort(x, x + N, +# [](float a, float b) { +# return std::abs(a) < std::abs(b); +# }); +# } +# \endcode +CursorKind.LAMBDA_EXPR = CursorKind(144) + +# Objective-c Boolean Literal. +CursorKind.OBJ_BOOL_LITERAL_EXPR = CursorKind(145) + +# Represents the "self" expression in a ObjC method. +CursorKind.OBJ_SELF_EXPR = CursorKind(146) + + # A statement whose specific kind is not exposed via this interface. # # Unexposed statements have the same operations as any other kind of statement; @@ -1010,6 +1039,9 @@ def __repr__(self): # Windows Structured Exception Handling's finally statement. CursorKind.SEH_FINALLY_STMT = CursorKind(228) +# A MS inline assembly statement extension. +CursorKind.MS_ASM_STMT = CursorKind(229) + # The null statement. CursorKind.NULL_STMT = CursorKind(230) @@ -1039,6 +1071,20 @@ def __repr__(self): CursorKind.CXX_OVERRIDE_ATTR = CursorKind(405) CursorKind.ANNOTATE_ATTR = CursorKind(406) CursorKind.ASM_LABEL_ATTR = CursorKind(407) +CursorKind.PACKED_ATTR = CursorKind(408) +CursorKind.PURE_ATTR = CursorKind(409) +CursorKind.CONST_ATTR = CursorKind(410) +CursorKind.NODUPLICATE_ATTR = CursorKind(411) +CursorKind.CUDACONSTANT_ATTR = CursorKind(412) +CursorKind.CUDADEVICE_ATTR = CursorKind(413) +CursorKind.CUDAGLOBAL_ATTR = CursorKind(414) +CursorKind.CUDAHOST_ATTR = CursorKind(415) +CursorKind.CUDASHARED_ATTR = CursorKind(416) + +CursorKind.VISIBILITY_ATTR = CursorKind(417) + +CursorKind.DLLEXPORT_ATTR = CursorKind(418) +CursorKind.DLLIMPORT_ATTR = CursorKind(419) ### # Preprocessing @@ -1047,6 +1093,14 @@ def __repr__(self): CursorKind.MACRO_INSTANTIATION = CursorKind(502) CursorKind.INCLUSION_DIRECTIVE = CursorKind(503) +### +# Extra declaration + +# A module import declaration. +CursorKind.MODULE_IMPORT_DECL = CursorKind(600) +# A type alias template declaration +CursorKind.TYPE_ALIAS_TEMPLATE_DECL = CursorKind(601) + ### Cursors ### class Cursor(Structure): @@ -3027,6 +3081,10 @@ def cursor(self): [Type], bool), + ("clang_Location_isFromMainFile", + [SourceLocation], + bool), + ("clang_parseTranslationUnit", [Index, c_char_p, c_void_p, c_int, c_void_p, c_int, c_int], c_object_p), diff --git a/plugin/clang_complete.vim b/plugin/clang_complete.vim index 5302329c..cf90a36c 100644 --- a/plugin/clang_complete.vim +++ b/plugin/clang_complete.vim @@ -417,6 +417,11 @@ function! s:DoPeriodicQuickFix() call s:ClangQuickFix() endfunction +function! g:ClangListSymbols() + execute s:py_cmd "vim.command('let l:list = ' + json.dumps(listSymbols()))" + return l:list +endfunction + function! s:ClangQuickFix() " Clear the bad spell, the user may have corrected them. syntax clear SpellBad diff --git a/plugin/libclang.py b/plugin/libclang.py index 5f9ed450..4f8c51a4 100644 --- a/plugin/libclang.py +++ b/plugin/libclang.py @@ -356,6 +356,31 @@ def getCompileParams(fileName): return { 'args' : args, 'cwd' : params['cwd'] } +def listSymbols(): + global debug + debug = int(vim.eval("g:clang_debug")) == 1 + params = getCompileParams(vim.current.buffer.name) + timer = CodeCompleteTimer(debug, vim.current.buffer.name, -1, -1, params) + + list = [] + def listSymbols(cursor): + for c in cursor.get_children(): + if c.location.file is None or \ + c.location.file.name.startswith('/usr/include') or \ + not c.location.isFromMainFile or \ + not c.kind.is_declaration(): + continue + list.append(c.spelling) + listSymbols(c) + + with libclangLock: + tu = getCurrentTranslationUnit(params['args'], getCurrentFile(), + vim.current.buffer.name, timer) + listSymbols(tu.cursor) + timer.finish() + + return list + def updateCurrentDiagnostics(): global debug debug = int(vim.eval("g:clang_debug")) == 1