Skip to content

Commit

Permalink
completion: don't reuse cache if the buffer line has changed
Browse files Browse the repository at this point in the history
Fix emacs-ccls#54
  • Loading branch information
MaskRay committed Sep 27, 2019
1 parent 0c6be47 commit aa9668a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/messages/textDocument_completion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -565,13 +565,14 @@ void MessageHandler::textDocument_completion(CompletionParam &param,
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);
Expand Down
8 changes: 6 additions & 2 deletions src/messages/textDocument_signatureHelp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,25 @@ 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;
begin_pos = wf->getCompletionPosition(param.position, &filter, &end_pos);
}

SemaManager::OnComplete callback =
[reply, path, begin_pos](CodeCompleteConsumer *optConsumer) {
[reply, path, begin_pos, buffer_line](CodeCompleteConsumer *optConsumer) {
if (!optConsumer)
return;
auto *consumer = static_cast<SignatureHelpConsumer *>(optConsumer);
reply(consumer->ls_sighelp);
if (!consumer->from_cache) {
cache.withLock([&]() {
cache.path = path;
cache.line = buffer_line;
cache.position = begin_pos;
cache.result = consumer->ls_sighelp;
});
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/sema_manager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,20 @@ struct SemaManager {
template <typename T> struct CompleteConsumerCache {
std::mutex mutex;
std::string path;
std::string line;
Position position;
T result;

template <typename Fn> void withLock(Fn &&fn) {
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

0 comments on commit aa9668a

Please sign in to comment.