Skip to content

Commit 4711fd3

Browse files
committed
completion: don't reuse cache if the buffer line has changed
Fix emacs-ccls#54
1 parent ff4ee61 commit 4711fd3

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/messages/textDocument_completion.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,14 @@ void MessageHandler::textDocument_completion(CompletionParam &param,
577577
if (!consumer->from_cache) {
578578
cache.withLock([&]() {
579579
cache.path = path;
580+
cache.line = buffer_line;
580581
cache.position = begin_pos;
581582
cache.result = consumer->ls_items;
582583
});
583584
}
584585
};
585586

586-
if (cache.isCacheValid(path, begin_pos)) {
587+
if (cache.isCacheValid(path, buffer_line, begin_pos)) {
587588
CompletionConsumer consumer(cCOpts, true);
588589
cache.withLock([&]() { consumer.ls_items = cache.result; });
589590
callback(&consumer);

src/messages/textDocument_signatureHelp.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,25 @@ void MessageHandler::textDocument_signatureHelp(
163163
reply.notOpened(path);
164164
return;
165165
}
166+
std::string buffer_line;
167+
if (param.position.line >= 0 && param.position.line < wf->buffer_lines.size())
168+
buffer_line = wf->buffer_lines[param.position.line];
166169
{
167170
std::string filter;
168171
Position end_pos;
169172
begin_pos = wf->getCompletionPosition(param.position, &filter, &end_pos);
170173
}
171174

172175
SemaManager::OnComplete callback =
173-
[reply, path, begin_pos](CodeCompleteConsumer *optConsumer) {
176+
[reply, path, begin_pos, buffer_line](CodeCompleteConsumer *optConsumer) {
174177
if (!optConsumer)
175178
return;
176179
auto *consumer = static_cast<SignatureHelpConsumer *>(optConsumer);
177180
reply(consumer->ls_sighelp);
178181
if (!consumer->from_cache) {
179182
cache.withLock([&]() {
180183
cache.path = path;
184+
cache.line = buffer_line;
181185
cache.position = begin_pos;
182186
cache.result = consumer->ls_sighelp;
183187
});
@@ -188,7 +192,7 @@ void MessageHandler::textDocument_signatureHelp(
188192
cCOpts.IncludeGlobals = false;
189193
cCOpts.IncludeMacros = false;
190194
cCOpts.IncludeBriefComments = true;
191-
if (cache.isCacheValid(path, begin_pos)) {
195+
if (cache.isCacheValid(path, buffer_line, begin_pos)) {
192196
SignatureHelpConsumer consumer(cCOpts, true);
193197
cache.withLock([&]() { consumer.ls_sighelp = cache.result; });
194198
callback(&consumer);

src/sema_manager.hh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,19 @@ struct SemaManager {
176176
template <typename T> struct CompleteConsumerCache {
177177
std::mutex mutex;
178178
std::string path;
179+
std::string line;
179180
Position position;
180181
T result;
181182

182183
template <typename Fn> void withLock(Fn &&fn) {
183184
std::lock_guard lock(mutex);
184185
fn();
185186
}
186-
bool isCacheValid(const std::string path, Position position) {
187+
bool isCacheValid(const std::string &path, const std::string &line,
188+
Position position) {
187189
std::lock_guard lock(mutex);
188-
return this->path == path && this->position == position;
190+
return this->path == path && this->position == position &&
191+
this->line == line;
189192
}
190193
};
191194
} // namespace ccls

0 commit comments

Comments
 (0)