Skip to content

Commit

Permalink
Drop old completion requests.
Browse files Browse the repository at this point in the history
Also increase xref limit.
  • Loading branch information
jacobdufault committed Feb 22, 2018
1 parent e785d3f commit fae959e
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 23 deletions.
43 changes: 32 additions & 11 deletions src/clang_complete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
// Fetching the completion request blocks until we have a request.
std::unique_ptr<ClangCompleteManager::CompletionRequest> request =
completion_manager->completion_request_.Dequeue();

// Drop older requests if we're not buffering.
while (completion_manager->config_->completion.dropOldRequests &&
!completion_manager->completion_request_.IsEmpty()) {
completion_manager->on_dropped_(request->id);
request = completion_manager->completion_request_.Dequeue();
}

std::string path = request->document.uri.GetPath();

std::shared_ptr<CompletionSession> session =
Expand Down Expand Up @@ -625,25 +633,35 @@ CompletionSession::~CompletionSession() {}
ClangCompleteManager::ParseRequest::ParseRequest(const std::string& path)
: request_time(std::chrono::high_resolution_clock::now()), path(path) {}

ClangCompleteManager::CompletionRequest::CompletionRequest(const lsTextDocumentIdentifier& document,
const bool& emit_diagnostics)
: document(document), emit_diagnostics(emit_diagnostics) {}
ClangCompleteManager::CompletionRequest::CompletionRequest(const lsTextDocumentIdentifier& document,
const lsPosition& position,
const OnComplete& on_complete,
const bool& emit_diagnostics)
: document(document), position(position), on_complete(on_complete), emit_diagnostics(emit_diagnostics) {}
ClangCompleteManager::CompletionRequest::CompletionRequest(
const lsRequestId& id,
const lsTextDocumentIdentifier& document,
bool emit_diagnostics)
: id(id), document(document), emit_diagnostics(emit_diagnostics) {}
ClangCompleteManager::CompletionRequest::CompletionRequest(
const lsRequestId& id,
const lsTextDocumentIdentifier& document,
const lsPosition& position,
const OnComplete& on_complete,
bool emit_diagnostics)
: id(id),
document(document),
position(position),
on_complete(on_complete),
emit_diagnostics(emit_diagnostics) {}

ClangCompleteManager::ClangCompleteManager(Config* config,
Project* project,
WorkingFiles* working_files,
OnDiagnostic on_diagnostic,
OnIndex on_index)
OnIndex on_index,
OnDropped on_dropped)
: config_(config),
project_(project),
working_files_(working_files),
on_diagnostic_(on_diagnostic),
on_index_(on_index),
on_dropped_(on_dropped),
preloaded_sessions_(kMaxPreloadedSessions),
completion_sessions_(kMaxCompletionSessions) {
new std::thread([&]() {
Expand All @@ -660,16 +678,19 @@ ClangCompleteManager::ClangCompleteManager(Config* config,
ClangCompleteManager::~ClangCompleteManager() {}

void ClangCompleteManager::CodeComplete(
const lsRequestId& id,
const lsTextDocumentPositionParams& completion_location,
const OnComplete& on_complete) {
completion_request_.PushBack(MakeUnique<CompletionRequest>(
completion_location.textDocument, completion_location.position,
id, completion_location.textDocument, completion_location.position,
on_complete, false));
}

void ClangCompleteManager::DiagnosticsUpdate(
const lsRequestId& id,
const lsTextDocumentIdentifier& document) {
completion_request_.PushBack(MakeUnique<CompletionRequest>(document, true));
completion_request_.PushBack(
MakeUnique<CompletionRequest>(id, document, true));
}

void ClangCompleteManager::NotifyView(const std::string& filename) {
Expand Down
22 changes: 15 additions & 7 deletions src/clang_complete.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct ClangCompleteManager {
using OnComplete =
std::function<void(const std::vector<lsCompletionItem>& results,
bool is_cached_result)>;
using OnDropped = std::function<void(lsRequestId request_id)>;

struct ParseRequest {
ParseRequest(const std::string& path);
Expand All @@ -55,13 +56,16 @@ struct ClangCompleteManager {
std::string path;
};
struct CompletionRequest {
CompletionRequest(const lsTextDocumentIdentifier& document,
const bool& emit_diagnostics);
CompletionRequest(const lsTextDocumentIdentifier& document,
CompletionRequest(const lsRequestId& id,
const lsTextDocumentIdentifier& document,
bool emit_diagnostics);
CompletionRequest(const lsRequestId& id,
const lsTextDocumentIdentifier& document,
const lsPosition& position,
const OnComplete& on_complete,
const bool& emit_diagnostics);
bool emit_diagnostics);

lsRequestId id;
lsTextDocumentIdentifier document;
optional<lsPosition> position;
OnComplete on_complete; // May be null/empty.
Expand All @@ -72,15 +76,18 @@ struct ClangCompleteManager {
Project* project,
WorkingFiles* working_files,
OnDiagnostic on_diagnostic,
OnIndex on_index);
OnIndex on_index,
OnDropped on_dropped);
~ClangCompleteManager();

// Start a code completion at the given location. |on_complete| will run when
// completion results are available. |on_complete| may run on any thread.
void CodeComplete(const lsTextDocumentPositionParams& completion_location,
void CodeComplete(const lsRequestId& request_id,
const lsTextDocumentPositionParams& completion_location,
const OnComplete& on_complete);
// Request a diagnostics update.
void DiagnosticsUpdate(const lsTextDocumentIdentifier& document);
void DiagnosticsUpdate(const lsRequestId& request_id,
const lsTextDocumentIdentifier& document);

// Notify the completion manager that |filename| has been viewed and we
// should begin preloading completion data.
Expand Down Expand Up @@ -113,6 +120,7 @@ struct ClangCompleteManager {
WorkingFiles* working_files_;
OnDiagnostic on_diagnostic_;
OnIndex on_index_;
OnDropped on_dropped_;

using LruSessionCache = LruCache<std::string, CompletionSession>;

Expand Down
12 changes: 11 additions & 1 deletion src/command_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ bool ShouldDisplayIpcTiming(IpcId id) {
REGISTER_IPC_MESSAGE(Ipc_CancelRequest);

void PrintHelp() {
std::cout << R"help(cquery is a low-latency C/C++/Objective-C language server.
std::cout
<< R"help(cquery is a low-latency C/C++/Objective-C language server.
Mode:
--clang-sanity-check
Expand Down Expand Up @@ -186,6 +187,15 @@ void RunQueryDbThread(const std::string& bin_name,
const std::string& path, const std::vector<std::string>& args) {
IndexWithTuFromCodeCompletion(config, &file_consumer_shared, tu,
unsaved, path, args);
},
[](lsRequestId id) {
if (!std::holds_alternative<std::monostate>(id)) {
Out_Error out;
out.id = id;
out.error.code = lsErrorCodes::InternalError;
out.error.message = "Dropped completion request";
QueueManager::WriteStdout(IpcId::Unknown, out);
}
});

IncludeComplete include_complete(config, &project);
Expand Down
6 changes: 6 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ struct Config {
// items can end up truncated by the UIs.
bool detailedLabel = false;

// On large projects, completion can take a long time. By default if cquery
// receives multiple completion requests while completion is still running
// it will only service the newest request. If this is set to false then all
// completion requests will be serviced.
bool dropOldRequests = true;

// If true, filter and sort completion response. cquery filters and sorts
// completions to try to be nicer to clients that can't handle big numbers
// of completion candidates. This behaviour can be disabled by specifying
Expand Down
8 changes: 5 additions & 3 deletions src/messages/text_document_completion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ struct TextDocumentCompletionHandler : MessageHandler {
std::string character = *request->params.context->triggerCharacter;
int preceding_index = request->params.position.character - 2;

// If the character is '"', '<' or '/', make sure that the line starts with '#'.
// If the character is '"', '<' or '/', make sure that the line starts
// with '#'.
if (character == "\"" || character == "<" || character == "/") {
size_t i = 0;
while (i < buffer_line.size() && isspace(buffer_line[i]))
Expand Down Expand Up @@ -361,15 +362,16 @@ struct TextDocumentCompletionHandler : MessageHandler {
callback(global_code_complete_cache->cached_results_,
true /*is_cached_result*/);
});
clang_complete->CodeComplete(request->params, freshen_global);
clang_complete->CodeComplete(request->id, request->params,
freshen_global);
} else if (non_global_code_complete_cache->IsCacheValid(
request->params)) {
non_global_code_complete_cache->WithLock([&]() {
callback(non_global_code_complete_cache->cached_results_,
true /*is_cached_result*/);
});
} else {
clang_complete->CodeComplete(request->params, callback);
clang_complete->CodeComplete(request->id, request->params, callback);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/messages/text_document_did_change.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct TextDocumentDidChangeHandler
working_files->OnChange(request->params);
clang_complete->NotifyEdit(path);
clang_complete->DiagnosticsUpdate(
std::monostate(),
request->params.textDocument.AsTextDocumentIdentifier());
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/messages/text_document_signature_help.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct TextDocumentSignatureHelpHandler : MessageHandler {
callback(signature_cache->cached_results_, true /*is_cached_result*/);
});
} else {
clang_complete->CodeComplete(params, std::move(callback));
clang_complete->CodeComplete(request->id, params, std::move(callback));
}
}
};
Expand Down

0 comments on commit fae959e

Please sign in to comment.