diff --git a/src/command_line.cc b/src/command_line.cc index 4ce29e20..7b338de7 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -728,6 +728,18 @@ struct IndexManager { } }; +// Sends indexing progress to client. +void EmitProgress(QueueManager* queue) { + Out_Progress out; + out.params.indexRequestCount = queue->index_request.Size(); + out.params.doIdMapCount = queue->do_id_map.Size(); + out.params.loadPreviousIndexCount = queue->load_previous_index.Size(); + out.params.onIdMappedCount = queue->on_id_mapped.Size(); + out.params.onIndexedCount = queue->on_indexed.Size(); + + IpcManager::instance()->SendOutMessageToClient(IpcId::Cout, out); +} + } // namespace //////////////////////////////////////////////////////////////////////////////// @@ -1131,6 +1143,8 @@ WorkThread::Result IndexMain(Config* config, WorkingFiles* working_files, MultiQueueWaiter* waiter, QueueManager* queue) { + EmitProgress(queue); + // TODO: dispose of index after it is not used for a while. clang::Index index(1, 0); @@ -1174,6 +1188,8 @@ bool QueryDb_ImportMain(Config* config, ImportManager* import_manager, QueueManager* queue, WorkingFiles* working_files) { + EmitProgress(queue); + bool did_work = false; while (true) { diff --git a/src/language_server_api.h b/src/language_server_api.h index 2b5d6785..1e4d3383 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -1654,6 +1654,25 @@ void Reflect(TVisitor& visitor, Out_ShowLogMessage& value) { REFLECT_MEMBER_END(); } +struct Out_Progress : public lsOutMessage { + struct Params { + int indexRequestCount = 0; + int doIdMapCount = 0; + int loadPreviousIndexCount = 0; + int onIdMappedCount = 0; + int onIndexedCount = 0; + }; + std::string method = "$cquery/progress"; + Params params; +}; +MAKE_REFLECT_STRUCT(Out_Progress::Params, + indexRequestCount, + doIdMapCount, + loadPreviousIndexCount, + onIdMappedCount, + onIndexedCount); +MAKE_REFLECT_STRUCT(Out_Progress, jsonrpc, method, params); + struct Out_CquerySetInactiveRegion : public lsOutMessage { struct Params { diff --git a/src/threaded_queue.h b/src/threaded_queue.h index d1465c15..74061c4c 100644 --- a/src/threaded_queue.h +++ b/src/threaded_queue.h @@ -3,14 +3,12 @@ #include #include "work_thread.h" - #include #include #include #include #include - using std::experimental::nullopt; using std::experimental::optional; @@ -66,6 +64,12 @@ struct ThreadedQueue : public BaseThreadQueue { explicit ThreadedQueue(MultiQueueWaiter* waiter) : waiter_(waiter) {} + // Returns the number of elements in the queue. This acquires a lock. + size_t Size() const { + std::lock_guard lock(mutex_); + return priority_.size() + queue_.size(); + } + // Add an element to the front of the queue. void PriorityEnqueue(T&& t) { std::lock_guard lock(mutex_);