completion: don't reuse cache if the buffer line has changed

Fix emacs-ccls#54
This commit is contained in:
Fangrui Song 2019-09-26 23:25:09 -07:00
parent ff4ee614b9
commit 4711fd36a3
3 changed files with 13 additions and 5 deletions

View File

@ -577,13 +577,14 @@ void MessageHandler::textDocument_completion(CompletionParam &param,
if (!consumer->from_cache) { if (!consumer->from_cache) {
cache.withLock([&]() { cache.withLock([&]() {
cache.path = path; cache.path = path;
cache.line = buffer_line;
cache.position = begin_pos; cache.position = begin_pos;
cache.result = consumer->ls_items; cache.result = consumer->ls_items;
}); });
} }
}; };
if (cache.isCacheValid(path, begin_pos)) { if (cache.isCacheValid(path, buffer_line, begin_pos)) {
CompletionConsumer consumer(cCOpts, true); CompletionConsumer consumer(cCOpts, true);
cache.withLock([&]() { consumer.ls_items = cache.result; }); cache.withLock([&]() { consumer.ls_items = cache.result; });
callback(&consumer); callback(&consumer);

View File

@ -163,6 +163,9 @@ void MessageHandler::textDocument_signatureHelp(
reply.notOpened(path); reply.notOpened(path);
return; 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; std::string filter;
Position end_pos; Position end_pos;
@ -170,7 +173,7 @@ void MessageHandler::textDocument_signatureHelp(
} }
SemaManager::OnComplete callback = SemaManager::OnComplete callback =
[reply, path, begin_pos](CodeCompleteConsumer *optConsumer) { [reply, path, begin_pos, buffer_line](CodeCompleteConsumer *optConsumer) {
if (!optConsumer) if (!optConsumer)
return; return;
auto *consumer = static_cast<SignatureHelpConsumer *>(optConsumer); auto *consumer = static_cast<SignatureHelpConsumer *>(optConsumer);
@ -178,6 +181,7 @@ void MessageHandler::textDocument_signatureHelp(
if (!consumer->from_cache) { if (!consumer->from_cache) {
cache.withLock([&]() { cache.withLock([&]() {
cache.path = path; cache.path = path;
cache.line = buffer_line;
cache.position = begin_pos; cache.position = begin_pos;
cache.result = consumer->ls_sighelp; cache.result = consumer->ls_sighelp;
}); });
@ -188,7 +192,7 @@ void MessageHandler::textDocument_signatureHelp(
cCOpts.IncludeGlobals = false; cCOpts.IncludeGlobals = false;
cCOpts.IncludeMacros = false; cCOpts.IncludeMacros = false;
cCOpts.IncludeBriefComments = true; cCOpts.IncludeBriefComments = true;
if (cache.isCacheValid(path, begin_pos)) { if (cache.isCacheValid(path, buffer_line, begin_pos)) {
SignatureHelpConsumer consumer(cCOpts, true); SignatureHelpConsumer consumer(cCOpts, true);
cache.withLock([&]() { consumer.ls_sighelp = cache.result; }); cache.withLock([&]() { consumer.ls_sighelp = cache.result; });
callback(&consumer); callback(&consumer);

View File

@ -176,6 +176,7 @@ struct SemaManager {
template <typename T> struct CompleteConsumerCache { template <typename T> struct CompleteConsumerCache {
std::mutex mutex; std::mutex mutex;
std::string path; std::string path;
std::string line;
Position position; Position position;
T result; T result;
@ -183,9 +184,11 @@ template <typename T> struct CompleteConsumerCache {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
fn(); 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); std::lock_guard lock(mutex);
return this->path == path && this->position == position; return this->path == path && this->position == position &&
this->line == line;
} }
}; };
} // namespace ccls } // namespace ccls