Author: Haojian Wu Date: 2022-09-05T15:55:29+02:00 New Revision: 16987998e6a0ad62efd4e387c18b0b9b5ef51947
URL: https://github.com/llvm/llvm-project/commit/16987998e6a0ad62efd4e387c18b0b9b5ef51947 DIFF: https://github.com/llvm/llvm-project/commit/16987998e6a0ad62efd4e387c18b0b9b5ef51947.diff LOG: [clangd] Fix LineFoldingOnly flag is not propagated correctly to ClangdServer. The Opts.LineFoldingOnly must be set before the clangdServer construction, otherwise this flag is always false when using clangd in VSCode. Differential Revision: https://reviews.llvm.org/D133299 Added: clang-tools-extra/clangd/test/folding-range.test Modified: clang-tools-extra/clangd/ClangdLSPServer.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp index 449ac9e3a85bd..c5238e426d692 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -466,32 +466,6 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params, if (Server) return Reply(llvm::make_error<LSPError>("server already initialized", ErrorCode::InvalidRequest)); - if (Opts.UseDirBasedCDB) { - DirectoryBasedGlobalCompilationDatabase::Options CDBOpts(TFS); - if (const auto &Dir = Params.initializationOptions.compilationDatabasePath) - CDBOpts.CompileCommandsDir = Dir; - CDBOpts.ContextProvider = Opts.ContextProvider; - BaseCDB = - std::make_unique<DirectoryBasedGlobalCompilationDatabase>(CDBOpts); - BaseCDB = getQueryDriverDatabase(llvm::makeArrayRef(Opts.QueryDriverGlobs), - std::move(BaseCDB)); - } - auto Mangler = CommandMangler::detect(); - if (Opts.ResourceDir) - Mangler.ResourceDir = *Opts.ResourceDir; - CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags, - tooling::ArgumentsAdjuster(std::move(Mangler))); - { - // Switch caller's context with LSPServer's background context. Since we - // rather want to propagate information from LSPServer's context into the - // Server, CDB, etc. - WithContext MainContext(BackgroundContext.clone()); - llvm::Optional<WithContextValue> WithOffsetEncoding; - if (Opts.Encoding) - WithOffsetEncoding.emplace(kCurrentOffsetEncoding, *Opts.Encoding); - Server.emplace(*CDB, TFS, Opts, - static_cast<ClangdServer::Callbacks *>(this)); - } Opts.CodeComplete.EnableSnippets = Params.capabilities.CompletionSnippets; Opts.CodeComplete.IncludeFixIts = Params.capabilities.CompletionFixes; @@ -521,6 +495,33 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params, BackgroundIndexSkipCreate = Params.capabilities.ImplicitProgressCreation; Opts.ImplicitCancellation = !Params.capabilities.CancelsStaleRequests; + if (Opts.UseDirBasedCDB) { + DirectoryBasedGlobalCompilationDatabase::Options CDBOpts(TFS); + if (const auto &Dir = Params.initializationOptions.compilationDatabasePath) + CDBOpts.CompileCommandsDir = Dir; + CDBOpts.ContextProvider = Opts.ContextProvider; + BaseCDB = + std::make_unique<DirectoryBasedGlobalCompilationDatabase>(CDBOpts); + BaseCDB = getQueryDriverDatabase(llvm::makeArrayRef(Opts.QueryDriverGlobs), + std::move(BaseCDB)); + } + auto Mangler = CommandMangler::detect(); + if (Opts.ResourceDir) + Mangler.ResourceDir = *Opts.ResourceDir; + CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags, + tooling::ArgumentsAdjuster(std::move(Mangler))); + { + // Switch caller's context with LSPServer's background context. Since we + // rather want to propagate information from LSPServer's context into the + // Server, CDB, etc. + WithContext MainContext(BackgroundContext.clone()); + llvm::Optional<WithContextValue> WithOffsetEncoding; + if (Opts.Encoding) + WithOffsetEncoding.emplace(kCurrentOffsetEncoding, *Opts.Encoding); + Server.emplace(*CDB, TFS, Opts, + static_cast<ClangdServer::Callbacks *>(this)); + } + llvm::json::Object ServerCaps{ {"textDocumentSync", llvm::json::Object{ diff --git a/clang-tools-extra/clangd/test/folding-range.test b/clang-tools-extra/clangd/test/folding-range.test new file mode 100644 index 0000000000000..758980d2061d2 --- /dev/null +++ b/clang-tools-extra/clangd/test/folding-range.test @@ -0,0 +1,24 @@ +# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s +void f() { + +} +--- +{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument": {"foldingRange": {"lineFoldingOnly": true}}},"trace":"off"}} +--- +{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"languageId":"cpp","text":"void f() {\n\n}\n","uri":"test:///foo.cpp","version":1}}} +--- +{"id":1,"jsonrpc":"2.0","method":"textDocument/foldingRange","params":{"textDocument":{"uri":"test:///foo.cpp"}}} +# CHECK: "id": 1, +# CHECK-NEXT: "jsonrpc": "2.0", +# CHECK-NEXT: "result": [ +# CHECK-NEXT: { +# CHECK-NEXT: "endLine": 1, +# CHECK-NEXT: "kind": "region", +# CHECK-NEXT: "startCharacter": 10, +# CHECK-NEXT: "startLine": 0 +# CHECK-NEXT: } +# CHECK-NEXT: ] +--- +{"jsonrpc":"2.0","id":5,"method":"shutdown"} +--- +{"jsonrpc":"2.0","method":"exit"} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits