diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b447d76..fca0ff41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -221,10 +221,7 @@ target_sources(ccls PRIVATE src/messages/textDocument_codeLens.cc src/messages/textDocument_completion.cc src/messages/textDocument_definition.cc - src/messages/textDocument_didChange.cc - src/messages/textDocument_didClose.cc - src/messages/textDocument_didOpen.cc - src/messages/textDocument_didSave.cc + src/messages/textDocument_did.cc src/messages/textDocument_documentHighlight.cc src/messages/textDocument_documentSymbol.cc src/messages/textDocument_hover.cc diff --git a/src/messages/textDocument_did.cc b/src/messages/textDocument_did.cc new file mode 100644 index 00000000..5a069ee2 --- /dev/null +++ b/src/messages/textDocument_did.cc @@ -0,0 +1,165 @@ +/* Copyright 2017-2018 ccls Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include "clang_complete.hh" +#include "include_complete.h" +#include "message_handler.h" +#include "pipeline.hh" +#include "project.h" +#include "working_files.h" +using namespace ccls; + +namespace { +MethodType didChange = "textDocument/didChange"; +MethodType didClose = "textDocument/didClose"; +MethodType didOpen = "textDocument/didOpen"; +MethodType didSave = "textDocument/didSave"; + +struct In_TextDocumentDidChange : public NotificationInMessage { + MethodType GetMethodType() const override { return didChange; } + lsTextDocumentDidChangeParams params; +}; +MAKE_REFLECT_STRUCT(In_TextDocumentDidChange, params); +REGISTER_IN_MESSAGE(In_TextDocumentDidChange); + +struct Handler_TextDocumentDidChange + : BaseMessageHandler { + MethodType GetMethodType() const override { return didChange; } + + void Run(In_TextDocumentDidChange *request) override { + const auto ¶ms = request->params; + std::string path = params.textDocument.uri.GetPath(); + working_files->OnChange(params); + if (g_config->index.onChange) + pipeline::Index(path, {}, IndexMode::OnChange); + clang_complete->NotifyView(path); + if (g_config->diagnostics.onChange >= 0) + clang_complete->DiagnosticsUpdate(path, g_config->diagnostics.onChange); + } +}; +REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidChange); + +struct In_TextDocumentDidClose : public NotificationInMessage { + MethodType GetMethodType() const override { return didClose; } + struct Params { + lsTextDocumentIdentifier textDocument; + } params; +}; +MAKE_REFLECT_STRUCT(In_TextDocumentDidClose::Params, textDocument); +MAKE_REFLECT_STRUCT(In_TextDocumentDidClose, params); +REGISTER_IN_MESSAGE(In_TextDocumentDidClose); + +struct Handler_TextDocumentDidClose + : BaseMessageHandler { + MethodType GetMethodType() const override { return didClose; } + + void Run(In_TextDocumentDidClose *request) override { + std::string path = request->params.textDocument.uri.GetPath(); + + // Clear any diagnostics for the file. + Out_TextDocumentPublishDiagnostics out; + out.params.uri = request->params.textDocument.uri; + pipeline::WriteStdout(didClose, out); + + // Remove internal state. + working_files->OnClose(request->params.textDocument); + clang_complete->OnClose(path); + } +}; +REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidClose); + +struct In_TextDocumentDidOpen : public NotificationInMessage { + MethodType GetMethodType() const override { return didOpen; } + + struct Params { + lsTextDocumentItem textDocument; + + // ccls extension + // If specified (e.g. ["clang++", "-DM", "a.cc"]), it overrides the project + // entry (e.g. loaded from compile_commands.json or .ccls). + std::vector args; + } params; +}; +MAKE_REFLECT_STRUCT(In_TextDocumentDidOpen::Params, textDocument, args); +MAKE_REFLECT_STRUCT(In_TextDocumentDidOpen, params); +REGISTER_IN_MESSAGE(In_TextDocumentDidOpen); + +struct Handler_TextDocumentDidOpen + : BaseMessageHandler { + MethodType GetMethodType() const override { return didOpen; } + + void Run(In_TextDocumentDidOpen *request) override { + // NOTE: This function blocks code lens. If it starts taking a long time + // we will need to find a way to unblock the code lens request. + const auto ¶ms = request->params; + const std::string &path = params.textDocument.uri.GetPath(); + + WorkingFile *working_file = working_files->OnOpen(params.textDocument); + if (std::optional cached_file_contents = + pipeline::LoadIndexedContent(path)) + working_file->SetIndexContent(*cached_file_contents); + + QueryFile *file = nullptr; + FindFileOrFail(db, project, std::nullopt, path, &file); + if (file && file->def) { + EmitSkippedRanges(working_file, file->def->skipped_ranges); + EmitSemanticHighlighting(db, working_file, file); + } + + include_complete->AddFile(working_file->filename); + std::vector args; + for (const std::string &arg : params.args) + args.push_back(Intern(arg)); + if (args.size()) + project->SetArgsForFile(args, path); + + // Submit new index request if it is not a header file. + if (SourceFileLanguage(path) != LanguageId::Unknown) + pipeline::Index(path, args, IndexMode::Normal); + + clang_complete->NotifyView(path); + } +}; +REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidOpen); + +struct In_TextDocumentDidSave : public NotificationInMessage { + MethodType GetMethodType() const override { return didSave; } + + struct Params { + // The document that was saved. + lsTextDocumentIdentifier textDocument; + + // Optional the content when saved. Depends on the includeText value + // when the save notifcation was requested. + // std::string text; + } params; +}; +MAKE_REFLECT_STRUCT(In_TextDocumentDidSave::Params, textDocument); +MAKE_REFLECT_STRUCT(In_TextDocumentDidSave, params); +REGISTER_IN_MESSAGE(In_TextDocumentDidSave); + +struct Handler_TextDocumentDidSave + : BaseMessageHandler { + MethodType GetMethodType() const override { return didSave; } + + void Run(In_TextDocumentDidSave *request) override { + const auto ¶ms = request->params; + const std::string &path = params.textDocument.uri.GetPath(); + pipeline::Index(path, {}, IndexMode::Normal); + clang_complete->NotifySave(path); + } +}; +REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidSave); +} // namespace diff --git a/src/messages/textDocument_didChange.cc b/src/messages/textDocument_didChange.cc deleted file mode 100644 index a91665f6..00000000 --- a/src/messages/textDocument_didChange.cc +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2017-2018 ccls Authors -// SPDX-License-Identifier: Apache-2.0 - -#include "clang_complete.hh" -#include "message_handler.h" -#include "pipeline.hh" -#include "project.h" -#include "working_files.h" -using namespace ccls; - -namespace { -MethodType kMethodType = "textDocument/didChange"; - -struct In_TextDocumentDidChange : public NotificationInMessage { - MethodType GetMethodType() const override { return kMethodType; } - lsTextDocumentDidChangeParams params; -}; - -MAKE_REFLECT_STRUCT(In_TextDocumentDidChange, params); -REGISTER_IN_MESSAGE(In_TextDocumentDidChange); - -struct Handler_TextDocumentDidChange - : BaseMessageHandler { - MethodType GetMethodType() const override { return kMethodType; } - - void Run(In_TextDocumentDidChange *request) override { - const auto ¶ms = request->params; - std::string path = params.textDocument.uri.GetPath(); - working_files->OnChange(params); - if (g_config->index.onChange) - pipeline::Index(path, {}, IndexMode::OnChange); - clang_complete->NotifyView(path); - if (g_config->diagnostics.onChange >= 0) - clang_complete->DiagnosticsUpdate(path, g_config->diagnostics.onChange); - } -}; -REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidChange); -} // namespace diff --git a/src/messages/textDocument_didClose.cc b/src/messages/textDocument_didClose.cc deleted file mode 100644 index 4baa8cda..00000000 --- a/src/messages/textDocument_didClose.cc +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2017-2018 ccls Authors -// SPDX-License-Identifier: Apache-2.0 - -#include "clang_complete.hh" -#include "message_handler.h" -#include "pipeline.hh" -#include "working_files.h" -using namespace ccls; - -namespace { -MethodType kMethodType = "textDocument/didClose"; - -struct In_TextDocumentDidClose : public NotificationInMessage { - MethodType GetMethodType() const override { return kMethodType; } - struct Params { - lsTextDocumentIdentifier textDocument; - } params; -}; -MAKE_REFLECT_STRUCT(In_TextDocumentDidClose::Params, textDocument); -MAKE_REFLECT_STRUCT(In_TextDocumentDidClose, params); -REGISTER_IN_MESSAGE(In_TextDocumentDidClose); - -struct Handler_TextDocumentDidClose - : BaseMessageHandler { - MethodType GetMethodType() const override { return kMethodType; } - - void Run(In_TextDocumentDidClose *request) override { - std::string path = request->params.textDocument.uri.GetPath(); - - // Clear any diagnostics for the file. - Out_TextDocumentPublishDiagnostics out; - out.params.uri = request->params.textDocument.uri; - pipeline::WriteStdout(kMethodType, out); - - // Remove internal state. - working_files->OnClose(request->params.textDocument); - clang_complete->OnClose(path); - } -}; -REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidClose); -} // namespace diff --git a/src/messages/textDocument_didOpen.cc b/src/messages/textDocument_didOpen.cc deleted file mode 100644 index 794e82f9..00000000 --- a/src/messages/textDocument_didOpen.cc +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2017-2018 ccls Authors -// SPDX-License-Identifier: Apache-2.0 - -#include "clang_complete.hh" -#include "include_complete.h" -#include "message_handler.h" -#include "pipeline.hh" -#include "project.h" -#include "working_files.h" -using namespace ccls; - -namespace { -MethodType kMethodType = "textDocument/didOpen"; - -struct In_TextDocumentDidOpen : public NotificationInMessage { - MethodType GetMethodType() const override { return kMethodType; } - - struct Params { - lsTextDocumentItem textDocument; - - // ccls extension - // If specified (e.g. ["clang++", "-DM", "a.cc"]), it overrides the project - // entry (e.g. loaded from compile_commands.json or .ccls). - std::vector args; - } params; -}; -MAKE_REFLECT_STRUCT(In_TextDocumentDidOpen::Params, textDocument, args); -MAKE_REFLECT_STRUCT(In_TextDocumentDidOpen, params); -REGISTER_IN_MESSAGE(In_TextDocumentDidOpen); - -struct Handler_TextDocumentDidOpen - : BaseMessageHandler { - MethodType GetMethodType() const override { return kMethodType; } - - void Run(In_TextDocumentDidOpen *request) override { - // NOTE: This function blocks code lens. If it starts taking a long time - // we will need to find a way to unblock the code lens request. - const auto ¶ms = request->params; - const std::string &path = params.textDocument.uri.GetPath(); - - WorkingFile *working_file = working_files->OnOpen(params.textDocument); - if (std::optional cached_file_contents = - pipeline::LoadIndexedContent(path)) - working_file->SetIndexContent(*cached_file_contents); - - QueryFile *file = nullptr; - FindFileOrFail(db, project, std::nullopt, path, &file); - if (file && file->def) { - EmitSkippedRanges(working_file, file->def->skipped_ranges); - EmitSemanticHighlighting(db, working_file, file); - } - - include_complete->AddFile(working_file->filename); - std::vector args; - for (const std::string &arg : params.args) - args.push_back(Intern(arg)); - if (args.size()) - project->SetArgsForFile(args, path); - - // Submit new index request if it is not a header file. - if (SourceFileLanguage(path) != LanguageId::Unknown) - pipeline::Index(path, args, IndexMode::Normal); - - clang_complete->NotifyView(path); - } -}; -REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidOpen); -} // namespace diff --git a/src/messages/textDocument_didSave.cc b/src/messages/textDocument_didSave.cc deleted file mode 100644 index 1846e047..00000000 --- a/src/messages/textDocument_didSave.cc +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2017-2018 ccls Authors -// SPDX-License-Identifier: Apache-2.0 - -#include "clang_complete.hh" -#include "message_handler.h" -#include "pipeline.hh" -using namespace ccls; - -namespace { -MethodType kMethodType = "textDocument/didSave"; - -struct In_TextDocumentDidSave : public NotificationInMessage { - MethodType GetMethodType() const override { return kMethodType; } - - struct Params { - // The document that was saved. - lsTextDocumentIdentifier textDocument; - - // Optional the content when saved. Depends on the includeText value - // when the save notifcation was requested. - // std::string text; - } params; -}; -MAKE_REFLECT_STRUCT(In_TextDocumentDidSave::Params, textDocument); -MAKE_REFLECT_STRUCT(In_TextDocumentDidSave, params); -REGISTER_IN_MESSAGE(In_TextDocumentDidSave); - -struct Handler_TextDocumentDidSave - : BaseMessageHandler { - MethodType GetMethodType() const override { return kMethodType; } - - void Run(In_TextDocumentDidSave *request) override { - const auto ¶ms = request->params; - const std::string &path = params.textDocument.uri.GetPath(); - pipeline::Index(path, {}, IndexMode::Normal); - clang_complete->NotifySave(path); - } -}; -REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidSave); -} // namespace