1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include "WorkspaceSymbolRequest.hpp"
#include "LSPEvent.h"
#include "event_notifier.h"
#include "file_logger.h"
namespace
{
FileLogger& operator<<(FileLogger& logger, const std::vector<LSP::SymbolInformation>& symbols)
{
wxString s;
s << "\n[\n";
for(const LSP::SymbolInformation& d : symbols) {
s << " " << d.GetContainerName() << "." << d.GetName() << ",\n";
}
s << "]\n";
logger.Append(s, logger.GetRequestedLogLevel());
return logger;
}
} // namespace
namespace LSP
{
class WorkspaceSymbolParams : public Params
{
wxString m_query;
public:
WorkspaceSymbolParams() {}
virtual ~WorkspaceSymbolParams() {}
virtual void FromJSON(const JSONItem& json) { m_query = json["query"].toString(); }
virtual JSONItem ToJSON(const wxString& name) const
{
JSONItem json = JSONItem::createObject(name);
json.addProperty("query", m_query);
return json;
}
void SetQuery(const wxString& query) { this->m_query = query; }
const wxString& GetQuery() const { return this->m_query; }
};
} // namespace LSP
LSP::WorkspaceSymbolRequest::WorkspaceSymbolRequest(const wxString& query)
{
SetMethod("workspace/symbol");
// set the params
m_params.reset(new WorkspaceSymbolParams());
m_params->As<WorkspaceSymbolParams>()->SetQuery(query);
}
LSP::WorkspaceSymbolRequest::~WorkspaceSymbolRequest() {}
void LSP::WorkspaceSymbolRequest::OnResponse(const LSP::ResponseMessage& response, wxEvtHandler* owner)
{
wxUnusedVar(owner);
auto result = response.Get("result");
if(!result.isOk()) {
clWARNING() << "LSP::WorkspaceSymbolRequest::OnResponse(): invalid 'result' object";
return;
}
if(!result.isArray()) {
clWARNING() << "workspace/symbol: expected array result" << endl;
return;
}
int size = result.arraySize();
if(size == 0) {
LSPEvent symbols_event{ wxEVT_LSP_WORKSPACE_SYMBOLS };
owner->QueueEvent(symbols_event.Clone());
return;
}
LOG_IF_TRACE { clDEBUG1() << result.format() << endl; }
// only SymbolInformation has the `location` property
// fire an event with all the symbols
LSPEvent symbols_event{ wxEVT_LSP_WORKSPACE_SYMBOLS };
auto& symbols = symbols_event.GetSymbolsInformation();
symbols.reserve(size);
for(int i = 0; i < size; ++i) {
SymbolInformation si;
si.FromJSON(result[i]);
symbols.push_back(si);
}
LOG_IF_TRACE { clDEBUG1() << symbols_event.GetSymbolsInformation() << endl; }
EventNotifier::Get()->QueueEvent(symbols_event.Clone());
}
|