From: Kadir Cetinkaya Date: Thu, 27 Sep 2018 17:13:07 +0000 (+0000) Subject: Introduce completionItemKind capability support. X-Git-Tag: llvmorg-8.0.0-rc1~7758 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=133d46f9a7c5dcbcda72fa09202d1d6cf0b7f79b;p=platform%2Fupstream%2Fllvm.git Introduce completionItemKind capability support. Reviewers: sammccall Reviewed By: sammccall Subscribers: ilya-biryukov, ioeric, jkorous, arphaman, cfe-commits Differential Revision: https://reviews.llvm.org/D52616 llvm-svn: 343237 --- diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp index a4ebf57..c6ce3ef9 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -70,6 +70,14 @@ SymbolKindBitset defaultSymbolKinds() { return Defaults; } +CompletionItemKindBitset defaultCompletionItemKinds() { + CompletionItemKindBitset Defaults; + for (size_t I = CompletionItemKindMin; + I <= static_cast(CompletionItemKind::Reference); ++I) + Defaults.set(I); + return Defaults; +} + } // namespace void ClangdLSPServer::onInitialize(InitializeParams &Params) { @@ -89,13 +97,20 @@ void ClangdLSPServer::onInitialize(InitializeParams &Params) { Params.capabilities.textDocument.publishDiagnostics.categorySupport; if (Params.capabilities.workspace && Params.capabilities.workspace->symbol && - Params.capabilities.workspace->symbol->symbolKind) { + Params.capabilities.workspace->symbol->symbolKind && + Params.capabilities.workspace->symbol->symbolKind->valueSet) { for (SymbolKind Kind : *Params.capabilities.workspace->symbol->symbolKind->valueSet) { SupportedSymbolKinds.set(static_cast(Kind)); } } + if (Params.capabilities.textDocument.completion.completionItemKind && + Params.capabilities.textDocument.completion.completionItemKind->valueSet) + for (CompletionItemKind Kind : *Params.capabilities.textDocument.completion + .completionItemKind->valueSet) + SupportedCompletionItemKinds.set(static_cast(Kind)); + reply(json::Object{ {{"capabilities", json::Object{ @@ -347,8 +362,12 @@ void ClangdLSPServer::onCompletion(TextDocumentPositionParams &Params) { return replyError(List.takeError()); CompletionList LSPList; LSPList.isIncomplete = List->HasMore; - for (const auto &R : List->Completions) - LSPList.items.push_back(R.render(CCOpts)); + for (const auto &R : List->Completions) { + CompletionItem C = R.render(CCOpts); + C.kind = adjustKindToCapability( + C.kind, SupportedCompletionItemKinds); + LSPList.items.push_back(std::move(C)); + } return reply(std::move(LSPList)); }); } @@ -459,6 +478,7 @@ ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, : CompilationDB::makeDirectoryBased( std::move(CompileCommandsDir))), CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()), + SupportedCompletionItemKinds(defaultCompletionItemKinds()), Server(new ClangdServer(CDB.getCDB(), FSProvider, /*DiagConsumer=*/*this, Opts)) {} diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h b/clang-tools-extra/clangd/ClangdLSPServer.h index f39fdb6..f77b24b 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.h +++ b/clang-tools-extra/clangd/ClangdLSPServer.h @@ -164,6 +164,8 @@ private: ClangdDiagnosticOptions DiagOpts; /// The supported kinds of the client. SymbolKindBitset SupportedSymbolKinds; + /// The supported completion item kinds of the client. + CompletionItemKindBitset SupportedCompletionItemKinds; // Store of the current versions of the open documents. DraftStore DraftMgr; diff --git a/clang-tools-extra/clangd/Protocol.cpp b/clang-tools-extra/clangd/Protocol.cpp index e6ffe66..7a137eb 100644 --- a/clang-tools-extra/clangd/Protocol.cpp +++ b/clang-tools-extra/clangd/Protocol.cpp @@ -496,6 +496,57 @@ json::Value toJSON(const Hover &H) { return std::move(Result); } +bool fromJSON(const json::Value &E, CompletionItemKind &Out) { + if (auto T = E.getAsInteger()) { + if (*T < static_cast(CompletionItemKind::Text) || + *T > static_cast(CompletionItemKind::TypeParameter)) + return false; + Out = static_cast(*T); + return true; + } + return false; +} + +CompletionItemKind +adjustKindToCapability(CompletionItemKind Kind, + CompletionItemKindBitset &supportedCompletionItemKinds) { + auto KindVal = static_cast(Kind); + if (KindVal >= CompletionItemKindMin && + KindVal <= supportedCompletionItemKinds.size() && + supportedCompletionItemKinds[KindVal]) + return Kind; + + switch (Kind) { + // Provide some fall backs for common kinds that are close enough. + case CompletionItemKind::Folder: + return CompletionItemKind::File; + case CompletionItemKind::EnumMember: + return CompletionItemKind::Enum; + case CompletionItemKind::Struct: + return CompletionItemKind::Class; + default: + return CompletionItemKind::Text; + } +} + +bool fromJSON(const json::Value &E, std::vector &Out) { + if (auto *A = E.getAsArray()) { + Out.clear(); + for (size_t I = 0; I < A->size(); ++I) { + CompletionItemKind KindOut; + if (fromJSON((*A)[I], KindOut)) + Out.push_back(KindOut); + } + return true; + } + return false; +} + +bool fromJSON(const json::Value &Params, CompletionItemKindCapabilities &R) { + json::ObjectMapper O(Params); + return O && O.map("valueSet", R.valueSet); +} + json::Value toJSON(const CompletionItem &CI) { assert(!CI.label.empty() && "completion item label is required"); json::Object Result{{"label", CI.label}}; diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h index 5e77e8b..22da2e3 100644 --- a/clang-tools-extra/clangd/Protocol.h +++ b/clang-tools-extra/clangd/Protocol.h @@ -233,13 +233,65 @@ struct CompletionItemClientCapabilities { }; bool fromJSON(const llvm::json::Value &, CompletionItemClientCapabilities &); +/// The kind of a completion entry. +enum class CompletionItemKind { + Missing = 0, + Text = 1, + Method = 2, + Function = 3, + Constructor = 4, + Field = 5, + Variable = 6, + Class = 7, + Interface = 8, + Module = 9, + Property = 10, + Unit = 11, + Value = 12, + Enum = 13, + Keyword = 14, + Snippet = 15, + Color = 16, + File = 17, + Reference = 18, + Folder = 19, + EnumMember = 20, + Constant = 21, + Struct = 22, + Event = 23, + Operator = 24, + TypeParameter = 25, +}; +bool fromJSON(const llvm::json::Value &, CompletionItemKind &); + +struct CompletionItemKindCapabilities { + /// The CompletionItemKinds that the client supports. If not set, the client + /// only supports <= CompletionItemKind::Reference and will not fall back to a + /// valid default value. + llvm::Optional> valueSet; +}; +// Discards unknown CompletionItemKinds. +bool fromJSON(const llvm::json::Value &, std::vector &); +bool fromJSON(const llvm::json::Value &, CompletionItemKindCapabilities &); + +constexpr auto CompletionItemKindMin = + static_cast(CompletionItemKind::Text); +constexpr auto CompletionItemKindMax = + static_cast(CompletionItemKind::TypeParameter); +using CompletionItemKindBitset = std::bitset; +CompletionItemKind +adjustKindToCapability(CompletionItemKind Kind, + CompletionItemKindBitset &supportedCompletionItemKinds); + struct CompletionClientCapabilities { /// Whether completion supports dynamic registration. bool dynamicRegistration = false; /// The client supports the following `CompletionItem` specific capabilities. CompletionItemClientCapabilities completionItem; - // NOTE: not used by clangd at the moment. - // llvm::Optional completionItemKind; + /// The CompletionItemKinds that the client supports. If not set, the client + /// only supports <= CompletionItemKind::Reference and will not fall back to a + /// valid default value. + llvm::Optional completionItemKind; /// The client supports to send additional context information for a /// `textDocument/completion` request. @@ -305,6 +357,7 @@ struct SymbolKindCapabilities { /// value. llvm::Optional> valueSet; }; +// Discards unknown SymbolKinds. bool fromJSON(const llvm::json::Value &, std::vector &); bool fromJSON(const llvm::json::Value &, SymbolKindCapabilities &); SymbolKind adjustKindToCapability(SymbolKind Kind, @@ -683,36 +736,6 @@ struct Hover { }; llvm::json::Value toJSON(const Hover &H); -/// The kind of a completion entry. -enum class CompletionItemKind { - Missing = 0, - Text = 1, - Method = 2, - Function = 3, - Constructor = 4, - Field = 5, - Variable = 6, - Class = 7, - Interface = 8, - Module = 9, - Property = 10, - Unit = 11, - Value = 12, - Enum = 13, - Keyword = 14, - Snippet = 15, - Color = 16, - File = 17, - Reference = 18, - Folder = 19, - EnumMember = 20, - Constant = 21, - Struct = 22, - Event = 23, - Operator = 24, - TypeParameter = 25, -}; - /// Defines whether the insert text in a completion item should be interpreted /// as plain text or a snippet. enum class InsertTextFormat {