Compute CompletionItemKind from Declaration instead of CursorKind

This commit is contained in:
Fangrui Song 2019-02-02 14:14:23 +08:00
parent 2db67f6edb
commit 03f91f0681
2 changed files with 94 additions and 88 deletions

View File

@ -107,12 +107,11 @@ Kind GetKind(const Decl *D, SymbolKind &kind) {
case Decl::LinkageSpec: case Decl::LinkageSpec:
return Kind::Invalid; return Kind::Invalid;
case Decl::Namespace: case Decl::Namespace:
case Decl::NamespaceAlias:
kind = SymbolKind::Namespace; kind = SymbolKind::Namespace;
return Kind::Type; return Kind::Type;
case Decl::NamespaceAlias:
kind = SymbolKind::TypeAlias;
return Kind::Type;
case Decl::ObjCCategory: case Decl::ObjCCategory:
case Decl::ObjCCategoryImpl:
case Decl::ObjCImplementation: case Decl::ObjCImplementation:
case Decl::ObjCInterface: case Decl::ObjCInterface:
case Decl::ObjCProtocol: case Decl::ObjCProtocol:
@ -154,6 +153,9 @@ Kind GetKind(const Decl *D, SymbolKind &kind) {
case Decl::ClassTemplatePartialSpecialization: case Decl::ClassTemplatePartialSpecialization:
kind = SymbolKind::Class; kind = SymbolKind::Class;
return Kind::Type; return Kind::Type;
case Decl::TemplateTypeParm:
kind = SymbolKind::TypeParameter;
return Kind::Type;
case Decl::TypeAlias: case Decl::TypeAlias:
case Decl::Typedef: case Decl::Typedef:
case Decl::UnresolvedUsingTypename: case Decl::UnresolvedUsingTypename:

View File

@ -215,89 +215,96 @@ void FilterCandidates(CompletionList &result, const std::string &complete_text,
finalize(); finalize();
} }
CompletionItemKind GetCompletionKind(CXCursorKind cursor_kind) { CompletionItemKind GetCompletionKind(CodeCompletionContext::Kind K,
switch (cursor_kind) { const CodeCompletionResult &R) {
case CXCursor_UnexposedDecl: switch (R.Kind) {
return CompletionItemKind::Text; case CodeCompletionResult::RK_Declaration: {
const Decl *D = R.Declaration;
case CXCursor_StructDecl: switch (D->getKind()) {
case CXCursor_UnionDecl: case Decl::LinkageSpec:
return CompletionItemKind::Struct; return CompletionItemKind::Keyword;
case CXCursor_ClassDecl: case Decl::Namespace:
return CompletionItemKind::Class; case Decl::NamespaceAlias:
case CXCursor_EnumDecl:
return CompletionItemKind::Enum;
case CXCursor_FieldDecl:
return CompletionItemKind::Field;
case CXCursor_EnumConstantDecl:
return CompletionItemKind::EnumMember;
case CXCursor_FunctionDecl:
return CompletionItemKind::Function;
case CXCursor_VarDecl:
case CXCursor_ParmDecl:
return CompletionItemKind::Variable;
case CXCursor_ObjCInterfaceDecl:
return CompletionItemKind::Interface;
case CXCursor_ObjCInstanceMethodDecl:
case CXCursor_CXXMethod:
case CXCursor_ObjCClassMethodDecl:
return CompletionItemKind::Method;
case CXCursor_FunctionTemplate:
return CompletionItemKind::Function;
case CXCursor_Constructor:
case CXCursor_Destructor:
case CXCursor_ConversionFunction:
return CompletionItemKind::Constructor;
case CXCursor_ObjCIvarDecl:
return CompletionItemKind::Variable;
case CXCursor_ClassTemplate:
case CXCursor_ClassTemplatePartialSpecialization:
case CXCursor_UsingDeclaration:
case CXCursor_TypedefDecl:
case CXCursor_TypeAliasDecl:
case CXCursor_TypeAliasTemplateDecl:
case CXCursor_ObjCCategoryDecl:
case CXCursor_ObjCProtocolDecl:
case CXCursor_ObjCImplementationDecl:
case CXCursor_ObjCCategoryImplDecl:
return CompletionItemKind::Class;
case CXCursor_ObjCPropertyDecl:
return CompletionItemKind::Property;
case CXCursor_MacroInstantiation:
case CXCursor_MacroDefinition:
return CompletionItemKind::Text;
case CXCursor_Namespace:
case CXCursor_NamespaceAlias:
case CXCursor_NamespaceRef:
return CompletionItemKind::Module; return CompletionItemKind::Module;
case Decl::ObjCCategory:
case CXCursor_MemberRef: case Decl::ObjCCategoryImpl:
case CXCursor_TypeRef: case Decl::ObjCImplementation:
case CXCursor_ObjCSuperClassRef: case Decl::ObjCInterface:
case CXCursor_ObjCProtocolRef: case Decl::ObjCProtocol:
case CXCursor_ObjCClassRef: return CompletionItemKind::Interface;
return CompletionItemKind::Reference; case Decl::ObjCMethod:
return CompletionItemKind::Method;
case CXCursor_NotImplemented: case Decl::ObjCProperty:
case CXCursor_OverloadCandidate: return CompletionItemKind::Property;
return CompletionItemKind::Text; case Decl::ClassTemplate:
return CompletionItemKind::Class;
case CXCursor_TemplateTypeParameter: case Decl::FunctionTemplate:
case CXCursor_TemplateTemplateParameter: return CompletionItemKind::Function;
case Decl::TypeAliasTemplate:
return CompletionItemKind::Class;
case Decl::VarTemplate:
if (cast<VarTemplateDecl>(D)->getTemplatedDecl()->isConstexpr())
return CompletionItemKind::Constant;
return CompletionItemKind::Variable;
case Decl::TemplateTemplateParm:
return CompletionItemKind::TypeParameter; return CompletionItemKind::TypeParameter;
case Decl::Enum:
return CompletionItemKind::Enum;
case Decl::CXXRecord:
case Decl::Record:
if (auto *RD = dyn_cast<RecordDecl>(D))
if (RD->getTagKind() == TTK_Struct)
return CompletionItemKind::Struct;
return CompletionItemKind::Class;
case Decl::TemplateTypeParm:
case Decl::TypeAlias:
case Decl::Typedef:
return CompletionItemKind::TypeParameter;
case Decl::Binding:
return CompletionItemKind::Variable;
case Decl::Field:
case Decl::ObjCIvar:
return CompletionItemKind::Field;
case Decl::Function:
return CompletionItemKind::Function;
case Decl::CXXMethod:
return CompletionItemKind::Method;
case Decl::CXXConstructor:
return CompletionItemKind::Constructor;
case Decl::CXXConversion:
case Decl::CXXDestructor:
return CompletionItemKind::Method;
case Decl::Var:
case Decl::Decomposition:
case Decl::ImplicitParam:
case Decl::ParmVar:
case Decl::VarTemplateSpecialization:
case Decl::VarTemplatePartialSpecialization:
if (cast<VarDecl>(D)->isConstexpr())
return CompletionItemKind::Constant;
return CompletionItemKind::Variable;
case Decl::EnumConstant:
return CompletionItemKind::EnumMember;
case Decl::IndirectField:
return CompletionItemKind::Field;
default: default:
LOG_S(WARNING) << "Unhandled completion kind " << cursor_kind; LOG_S(WARNING) << "Unhandled " << int(D->getKind());
return CompletionItemKind::Text; return CompletionItemKind::Text;
} }
break;
}
case CodeCompletionResult::RK_Keyword:
return CompletionItemKind::Keyword;
case CodeCompletionResult::RK_Macro:
return CompletionItemKind::Reference;
case CodeCompletionResult::RK_Pattern:
#if LLVM_VERSION_MAJOR >= 8
if (K == CodeCompletionContext::CCC_IncludedFile)
return CompletionItemKind::File;
#endif
return CompletionItemKind::Snippet;
}
} }
void BuildItem(const CodeCompletionResult &R, const CodeCompletionString &CCS, void BuildItem(const CodeCompletionResult &R, const CodeCompletionString &CCS,
@ -417,15 +424,12 @@ public:
NK == DeclarationName::CXXLiteralOperatorName) NK == DeclarationName::CXXLiteralOperatorName)
continue; continue;
} }
CodeCompletionString *CCS = R.CreateCodeCompletionString( CodeCompletionString *CCS = R.CreateCodeCompletionString(
S, Context, getAllocator(), getCodeCompletionTUInfo(), S, Context, getAllocator(), getCodeCompletionTUInfo(),
includeBriefComments()); includeBriefComments());
CompletionItem ls_item; CompletionItem ls_item;
ls_item.kind = GetCompletionKind(R.CursorKind); ls_item.kind = GetCompletionKind(Context.getKind(), R);
#if LLVM_VERSION_MAJOR >= 8
if (Context.getKind() == CodeCompletionContext::CCC_IncludedFile)
ls_item.kind = CompletionItemKind::File;
#endif
if (const char *brief = CCS->getBriefComment()) if (const char *brief = CCS->getBriefComment())
ls_item.documentation = brief; ls_item.documentation = brief;
ls_item.detail = CCS->getParentContextName().str(); ls_item.detail = CCS->getParentContextName().str();