From 617cdd89dea3d3ae3708ed14c72166f0fb89836a Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 23 Oct 2018 20:48:12 -0700 Subject: [PATCH] Implement textDocument/foldingRange --- CMakeLists.txt | 1 + src/messages/initialize.cc | 4 +- src/messages/textDocument_foldingRange.cc | 61 +++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/messages/textDocument_foldingRange.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c526d72..a16583e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -220,6 +220,7 @@ target_sources(ccls PRIVATE src/messages/textDocument_completion.cc src/messages/textDocument_definition.cc src/messages/textDocument_did.cc + src/messages/textDocument_foldingRange.cc src/messages/textDocument_formatting.cc src/messages/textDocument_documentHighlight.cc src/messages/textDocument_documentSymbol.cc diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index c9aa4ce2..0836b436 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -160,6 +160,7 @@ struct lsServerCapabilities { bool renameProvider = true; // The server provides document link support. lsDocumentLinkOptions documentLinkProvider; + bool foldingRangeProvider = true; // The server provides execute command support. struct ExecuteCommandOptions { std::vector commands{std::string(ccls_xref)}; @@ -184,7 +185,8 @@ MAKE_REFLECT_STRUCT(lsServerCapabilities, textDocumentSync, hoverProvider, codeLensProvider, documentFormattingProvider, documentRangeFormattingProvider, documentOnTypeFormattingProvider, renameProvider, - documentLinkProvider, executeCommandProvider, workspace); + documentLinkProvider, foldingRangeProvider, + executeCommandProvider, workspace); // Workspace specific client capabilities. struct lsWorkspaceClientCapabilites { diff --git a/src/messages/textDocument_foldingRange.cc b/src/messages/textDocument_foldingRange.cc new file mode 100644 index 00000000..31563695 --- /dev/null +++ b/src/messages/textDocument_foldingRange.cc @@ -0,0 +1,61 @@ +// Copyright 2017-2018 ccls Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "message_handler.h" +#include "pipeline.hh" +#include "project.h" +#include "query_utils.h" +#include "working_files.h" + +namespace ccls { +namespace { +MethodType foldingRange = "textDocument/foldingRange"; + +struct In_textDocumentFoldingRange : public RequestMessage { + MethodType GetMethodType() const override { return foldingRange; } + struct Params { + lsTextDocumentIdentifier textDocument; + } params; +}; +MAKE_REFLECT_STRUCT(In_textDocumentFoldingRange::Params, textDocument); +MAKE_REFLECT_STRUCT(In_textDocumentFoldingRange, id, params); +REGISTER_IN_MESSAGE(In_textDocumentFoldingRange); + +struct FoldingRange { + int startLine, startCharacter, endLine, endCharacter; + std::string kind = "region"; +}; +MAKE_REFLECT_STRUCT(FoldingRange, startLine, startCharacter, endLine, endCharacter, kind); + +struct Handler_textDocumentFoldingRange + : BaseMessageHandler { + MethodType GetMethodType() const override { return foldingRange; } + void Run(In_textDocumentFoldingRange *request) override { + QueryFile *file; + if (!FindFileOrFail(db, project, request->id, + request->params.textDocument.uri.GetPath(), &file, + nullptr)) + return; + WorkingFile *wfile = + working_files->GetFileByFilename(file->def->path); + if (!wfile) + return; + std::vector result; + std::optional ls_range; + + for (auto [sym, refcnt] : file->symbol2refcnt) + if (refcnt > 0 && sym.extent.Valid() && + (sym.kind == SymbolKind::Func || sym.kind == SymbolKind::Type) && + (ls_range = GetLsRange(wfile, sym.extent))) { + FoldingRange &fold = result.emplace_back(); + fold.startLine = ls_range->start.line; + fold.startCharacter = ls_range->start.character; + fold.endLine = ls_range->end.line; + fold.endCharacter = ls_range->end.character; + } + pipeline::Reply(request->id, result); + } +}; +REGISTER_MESSAGE_HANDLER(Handler_textDocumentFoldingRange); +} // namespace +} // namespace ccls