From 09f00dcf69fd87d50e93eb5218f20ebdd8ec7a61 Mon Sep 17 00:00:00 2001 From: Kirill Bobyrev Date: Mon, 10 Sep 2018 11:51:05 +0000 Subject: [PATCH] [clangd] Implement FuzzyFindRequest JSON (de)serialization JSON (de)serialization of `FuzzyFindRequest` might be useful for both D51090 and D51628. Also, this allows precise logging of the fuzzy find requests. Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D51852 llvm-svn: 341802 --- clang-tools-extra/clangd/CodeComplete.cpp | 3 +-- clang-tools-extra/clangd/index/Index.cpp | 27 +++++++++++++++++++++++++++ clang-tools-extra/clangd/index/Index.h | 6 +++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index 354a277..1e632dc 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -1381,8 +1381,7 @@ private: Req.Scopes = QueryScopes; // FIXME: we should send multiple weighted paths here. Req.ProximityPaths.push_back(FileName); - vlog("Code complete: fuzzyFind(\"{0}\", scopes=[{1}])", Req.Query, - llvm::join(Req.Scopes.begin(), Req.Scopes.end(), ",")); + vlog("Code complete: fuzzyFind({0:2})", toJSON(Req)); if (SpecFuzzyFind) SpecFuzzyFind->NewReq = Req; diff --git a/clang-tools-extra/clangd/index/Index.cpp b/clang-tools-extra/clangd/index/Index.cpp index a01c49f..013ba5c 100644 --- a/clang-tools-extra/clangd/index/Index.cpp +++ b/clang-tools-extra/clangd/index/Index.cpp @@ -175,6 +175,33 @@ std::shared_ptr SwapIndex::snapshot() const { return Index; } +bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request) { + json::ObjectMapper O(Parameters); + llvm::Optional MaxCandidateCount; + bool OK = + O && O.map("Query", Request.Query) && O.map("Scopes", Request.Scopes) && + O.map("RestrictForCodeCompletion", Request.RestrictForCodeCompletion) && + O.map("ProximityPaths", Request.ProximityPaths) && + O.map("MaxCandidateCount", MaxCandidateCount); + if (MaxCandidateCount) + Request.MaxCandidateCount = MaxCandidateCount.getValue(); + return OK; +} + +llvm::json::Value toJSON(const FuzzyFindRequest &Request) { + auto Result = json::Object{ + {"Query", Request.Query}, + {"Scopes", json::Array{Request.Scopes}}, + {"RestrictForCodeCompletion", Request.RestrictForCodeCompletion}, + {"ProximityPaths", json::Array{Request.ProximityPaths}}, + }; + // A huge limit means no limit, leave it out. + if (Request.MaxCandidateCount <= std::numeric_limits::max()) + Result["MaxCandidateCount"] = + static_cast(Request.MaxCandidateCount); + return Result; +} + bool SwapIndex::fuzzyFind(const FuzzyFindRequest &R, llvm::function_ref CB) const { return snapshot()->fuzzyFind(R, CB); diff --git a/clang-tools-extra/clangd/index/Index.h b/clang-tools-extra/clangd/index/Index.h index 12727ef..b531d1e 100644 --- a/clang-tools-extra/clangd/index/Index.h +++ b/clang-tools-extra/clangd/index/Index.h @@ -19,8 +19,10 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/JSON.h" #include "llvm/Support/StringSaver.h" #include +#include #include #include #include @@ -435,7 +437,7 @@ struct FuzzyFindRequest { std::vector Scopes; /// \brief The number of top candidates to return. The index may choose to /// return more than this, e.g. if it doesn't know which candidates are best. - size_t MaxCandidateCount = UINT_MAX; + size_t MaxCandidateCount = std::numeric_limits::max(); /// If set to true, only symbols for completion support will be considered. bool RestrictForCodeCompletion = false; /// Contextually relevant files (e.g. the file we're code-completing in). @@ -450,6 +452,8 @@ struct FuzzyFindRequest { } bool operator!=(const FuzzyFindRequest &Req) const { return !(*this == Req); } }; +bool fromJSON(const llvm::json::Value &Value, FuzzyFindRequest &Request); +llvm::json::Value toJSON(const FuzzyFindRequest &Request); struct LookupRequest { llvm::DenseSet IDs; -- 2.7.4