From aa9668a8fc8b21ff08984f94dfbdce4dbbe9db06 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 26 Sep 2019 23:25:09 -0700 Subject: [PATCH] completion: don't reuse cache if the buffer line has changed Fix emacs-ccls#54 --- src/messages/textDocument_completion.cc | 3 ++- src/messages/textDocument_signatureHelp.cc | 8 ++++++-- src/sema_manager.hh | 8 ++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/messages/textDocument_completion.cc b/src/messages/textDocument_completion.cc index 1fcfbd4a..5178e459 100644 --- a/src/messages/textDocument_completion.cc +++ b/src/messages/textDocument_completion.cc @@ -565,13 +565,14 @@ void MessageHandler::textDocument_completion(CompletionParam ¶m, if (!consumer->from_cache) { cache.withLock([&]() { cache.path = path; + cache.line = buffer_line; cache.position = begin_pos; cache.result = consumer->ls_items; }); } }; - if (cache.isCacheValid(path, begin_pos)) { + if (cache.isCacheValid(path, buffer_line, begin_pos)) { CompletionConsumer consumer(ccOpts, true); cache.withLock([&]() { consumer.ls_items = cache.result; }); callback(&consumer); diff --git a/src/messages/textDocument_signatureHelp.cc b/src/messages/textDocument_signatureHelp.cc index bffdcf36..e5d5c0d7 100644 --- a/src/messages/textDocument_signatureHelp.cc +++ b/src/messages/textDocument_signatureHelp.cc @@ -151,6 +151,9 @@ void MessageHandler::textDocument_signatureHelp( reply.notOpened(path); return; } + std::string buffer_line; + if (param.position.line >= 0 && param.position.line < wf->buffer_lines.size()) + buffer_line = wf->buffer_lines[param.position.line]; { std::string filter; Position end_pos; @@ -158,7 +161,7 @@ void MessageHandler::textDocument_signatureHelp( } SemaManager::OnComplete callback = - [reply, path, begin_pos](CodeCompleteConsumer *optConsumer) { + [reply, path, begin_pos, buffer_line](CodeCompleteConsumer *optConsumer) { if (!optConsumer) return; auto *consumer = static_cast(optConsumer); @@ -166,6 +169,7 @@ void MessageHandler::textDocument_signatureHelp( if (!consumer->from_cache) { cache.withLock([&]() { cache.path = path; + cache.line = buffer_line; cache.position = begin_pos; cache.result = consumer->ls_sighelp; }); @@ -176,7 +180,7 @@ void MessageHandler::textDocument_signatureHelp( ccOpts.IncludeGlobals = false; ccOpts.IncludeMacros = false; ccOpts.IncludeBriefComments = true; - if (cache.isCacheValid(path, begin_pos)) { + if (cache.isCacheValid(path, buffer_line, begin_pos)) { SignatureHelpConsumer consumer(ccOpts, true); cache.withLock([&]() { consumer.ls_sighelp = cache.result; }); callback(&consumer); diff --git a/src/sema_manager.hh b/src/sema_manager.hh index 2d3a969d..426ab7bb 100644 --- a/src/sema_manager.hh +++ b/src/sema_manager.hh @@ -164,6 +164,7 @@ struct SemaManager { template struct CompleteConsumerCache { std::mutex mutex; std::string path; + std::string line; Position position; T result; @@ -171,9 +172,12 @@ template struct CompleteConsumerCache { std::lock_guard lock(mutex); fn(); } - bool isCacheValid(const std::string path, Position position) { + bool isCacheValid(const std::string &path, const std::string &line, + Position position) { std::lock_guard lock(mutex); - return this->path == path && this->position == position; + return this->position == position && this->path == path && + this->line.compare(0, position.character, line, 0, + position.character) == 0; } }; } // namespace ccls