From 639bf8a43168195fc78cfcd13a1fd884599ec86d Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sun, 21 May 2017 22:54:27 -0700 Subject: [PATCH] Show error message when regex parsing fails. --- src/command_line.cc | 49 +-------------------------------------------- src/ipc_manager.cc | 40 ++++++++++++++++++++++++++++++++++++ src/ipc_manager.h | 31 ++++++++++++++++++++++++++++ src/match.cc | 10 +++++++-- 4 files changed, 80 insertions(+), 50 deletions(-) create mode 100644 src/ipc_manager.cc create mode 100644 src/ipc_manager.h diff --git a/src/command_line.cc b/src/command_line.cc index 7abb14bb..443462c2 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -4,6 +4,7 @@ #include "file_consumer.h" #include "match.h" #include "include_completion.h" +#include "ipc_manager.h" #include "indexer.h" #include "query.h" #include "language_server_api.h" @@ -107,54 +108,6 @@ struct CodeCompleteCache { -struct IpcManager { -static IpcManager* instance_; -static IpcManager* instance() { - return instance_; -} -static void CreateInstance(MultiQueueWaiter* waiter) { - instance_ = new IpcManager(waiter); -} - -std::unique_ptr>> threaded_queue_for_client_; -std::unique_ptr>> threaded_queue_for_server_; - -enum class Destination { - Client, Server -}; - -ThreadedQueue>* GetThreadedQueue(Destination destination) { - return destination == Destination::Client ? threaded_queue_for_client_.get() : threaded_queue_for_server_.get(); -} - -void SendOutMessageToClient(IpcId id, lsBaseOutMessage& response) { - std::ostringstream sstream; - response.Write(sstream); - - auto out = MakeUnique(); - out->content = sstream.str(); - out->original_ipc_id = id; - GetThreadedQueue(Destination::Client)->Enqueue(std::move(out)); - } - - void SendMessage(Destination destination, std::unique_ptr message) { - GetThreadedQueue(destination)->Enqueue(std::move(message)); - } - - std::vector> GetMessages(Destination destination) { - return GetThreadedQueue(destination)->DequeueAll(); - } - - private: - IpcManager(MultiQueueWaiter* waiter) { - threaded_queue_for_client_ = MakeUnique>>(waiter); - threaded_queue_for_server_ = MakeUnique>>(waiter); - } -}; - -IpcManager* IpcManager::instance_ = nullptr; - - diff --git a/src/ipc_manager.cc b/src/ipc_manager.cc new file mode 100644 index 00000000..5a872af7 --- /dev/null +++ b/src/ipc_manager.cc @@ -0,0 +1,40 @@ +#include "ipc_manager.h" + +IpcManager* IpcManager::instance_ = nullptr; + +// static +IpcManager* IpcManager::instance() { + return instance_; +} + +// static +void IpcManager::CreateInstance(MultiQueueWaiter* waiter) { + instance_ = new IpcManager(waiter); +} + +ThreadedQueue>* IpcManager::GetThreadedQueue(Destination destination) { + return destination == Destination::Client ? threaded_queue_for_client_.get() : threaded_queue_for_server_.get(); +} + +void IpcManager::SendOutMessageToClient(IpcId id, lsBaseOutMessage& response) { + std::ostringstream sstream; + response.Write(sstream); + + auto out = MakeUnique(); + out->content = sstream.str(); + out->original_ipc_id = id; + GetThreadedQueue(Destination::Client)->Enqueue(std::move(out)); +} + +void IpcManager::SendMessage(Destination destination, std::unique_ptr message) { + GetThreadedQueue(destination)->Enqueue(std::move(message)); +} + +std::vector> IpcManager::GetMessages(Destination destination) { + return GetThreadedQueue(destination)->DequeueAll(); +} + +IpcManager::IpcManager(MultiQueueWaiter* waiter) { + threaded_queue_for_client_ = MakeUnique>>(waiter); + threaded_queue_for_server_ = MakeUnique>>(waiter); +} \ No newline at end of file diff --git a/src/ipc_manager.h b/src/ipc_manager.h new file mode 100644 index 00000000..8f089030 --- /dev/null +++ b/src/ipc_manager.h @@ -0,0 +1,31 @@ +#pragma once + +#include "ipc.h" +#include "language_server_api.h" +#include "threaded_queue.h" + +#include + +struct IpcManager { + static IpcManager* instance_; + static IpcManager* instance(); + static void CreateInstance(MultiQueueWaiter* waiter); + + std::unique_ptr>> threaded_queue_for_client_; + std::unique_ptr>> threaded_queue_for_server_; + + enum class Destination { + Client, Server + }; + + ThreadedQueue>* GetThreadedQueue(Destination destination); + + void SendOutMessageToClient(IpcId id, lsBaseOutMessage& response); + + void SendMessage(Destination destination, std::unique_ptr message); + + std::vector> GetMessages(Destination destination); + +private: + IpcManager(MultiQueueWaiter* waiter); +}; diff --git a/src/match.cc b/src/match.cc index 0213285e..7ea202bf 100644 --- a/src/match.cc +++ b/src/match.cc @@ -1,5 +1,8 @@ #include "match.h" +#include "ipc_manager.h" +#include "language_server_api.h" + #include #include @@ -27,8 +30,11 @@ optional Matcher::Create(const std::string& search) { return m; } catch (std::exception e) { - // TODO/FIXME: show a warning message, we need to access IpcManager so we can print it safely to stdout. - std::cerr << "Building matcher for " << search << " failed; " << e.what() << std::endl; + Out_ShowLogMessage out; + out.display_type = Out_ShowLogMessage::DisplayType::Show; + out.params.type = lsMessageType::Error; + out.params.message = "cquery: Parsing EMCAScript regex \"" + search + "\" failed; " + e.what(); + IpcManager::instance()->SendOutMessageToClient(IpcId::Cout, out); return nullopt; } }