diff options
Diffstat (limited to 'clang-tools-extra/clangd/index/SymbolCollector.cpp')
| -rw-r--r-- | clang-tools-extra/clangd/index/SymbolCollector.cpp | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp index 291ee592aad..3b9046e74ef 100644 --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -8,8 +8,7 @@ //===----------------------------------------------------------------------===// #include "SymbolCollector.h" -#include "clang/AST/ASTContext.h" -#include "clang/AST/Decl.h" +#include "../CodeCompletionStrings.h" #include "clang/AST/DeclCXX.h" #include "clang/Basic/SourceManager.h" #include "clang/Index/IndexSymbol.h" @@ -56,7 +55,6 @@ std::string makeAbsolutePath(const SourceManager &SM, StringRef Path) { return AbsolutePath.str(); } -// Split a qualified symbol name into scope and unqualified name, e.g. given // "a::b::c", return {"a::b", "c"}. Scope is empty if it doesn't exist. std::pair<llvm::StringRef, llvm::StringRef> splitQualifiedName(llvm::StringRef QName) { @@ -69,6 +67,13 @@ splitQualifiedName(llvm::StringRef QName) { } // namespace +void SymbolCollector::initialize(ASTContext &Ctx) { + ASTCtx = &Ctx; + CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>(); + CompletionTUInfo = + llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator); +} + // Always return true to continue indexing. bool SymbolCollector::handleDeclOccurence( const Decl *D, index::SymbolRoleSet Roles, @@ -79,6 +84,8 @@ bool SymbolCollector::handleDeclOccurence( Roles & static_cast<unsigned>(index::SymbolRole::Definition))) return true; + assert(CompletionAllocator && CompletionTUInfo); + if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) { // FIXME: figure out a way to handle internal linkage symbols (e.g. static // variables, function) defined in the .cc files. Also we skip the symbols @@ -113,6 +120,35 @@ bool SymbolCollector::handleDeclOccurence( S.Name = ScopeAndName.second; S.SymInfo = index::getSymbolInfo(D); S.CanonicalDeclaration = Location; + + // Add completion info. + assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); + CodeCompletionResult SymbolCompletion(ND, 0); + const auto *CCS = SymbolCompletion.CreateCodeCompletionString( + *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator, + *CompletionTUInfo, + /*IncludeBriefComments*/ true); + std::string Label; + std::string SnippetInsertText; + std::string IgnoredLabel; + std::string PlainInsertText; + getLabelAndInsertText(*CCS, &Label, &SnippetInsertText, + /*EnableSnippets=*/true); + getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText, + /*EnableSnippets=*/false); + std::string FilterText = getFilterText(*CCS); + std::string Documentation = getDocumentation(*CCS); + std::string CompletionDetail = getDetail(*CCS); + + S.CompletionFilterText = FilterText; + S.CompletionLabel = Label; + S.CompletionPlainInsertText = PlainInsertText; + S.CompletionSnippetInsertText = SnippetInsertText; + Symbol::Details Detail; + Detail.Documentation = Documentation; + Detail.CompletionDetail = CompletionDetail; + S.Detail = &Detail; + Symbols.insert(S); } |

