Debian Codegraph @

CodeLite/LSP/ResponseMessage.cpp

raw
1 #include "ResponseMessage.h"
2 #include "StringUtils.h"
3 #include <wx/tokenzr.h>
4
5 LSP::ResponseMessage::ResponseMessage(std::unique_ptr<JSON>&& json)
6 {
7 // a valid JSON-RPC response
8 m_json = std::move(json);
9 FromJSON(m_json->toElement());
10 }
11
12 LSP::ResponseMessage::~ResponseMessage() {}
13
14 std::string LSP::ResponseMessage::ToString() const
15 {
16 if(!m_json || !m_json->isOk()) {
17 return "";
18 }
19 return StringUtils::ToStdString(m_json->toElement().format(false));
20 }
21
22 // we dont really serialise response messages
23 JSONItem LSP::ResponseMessage::ToJSON(const wxString& name) const { return JSONItem(nullptr); }
24
25 void LSP::ResponseMessage::FromJSON(const JSONItem& json)
26 {
27 Message::FromJSON(json);
28 m_id = json.namedObject("id").toInt();
29 }
30
31 bool LSP::ResponseMessage::Has(const wxString& property) const
32 {
33 return m_json && m_json->toElement().hasNamedObject(property);
34 }
35
36 JSONItem LSP::ResponseMessage::Get(const wxString& property) const
37 {
38 if(!Has(property)) {
39 return JSONItem(nullptr);
40 }
41 return m_json->toElement().namedObject(property);
42 }
43
44 std::vector<LSP::Diagnostic> LSP::ResponseMessage::GetDiagnostics() const
45 {
46 JSONItem params = Get("params");
47 if(!params.isOk()) {
48 return {};
49 }
50
51 std::vector<LSP::Diagnostic> res;
52 JSONItem arrDiags = params.namedObject("diagnostics");
53 int size = arrDiags.arraySize();
54 for(int i = 0; i < size; ++i) {
55 LSP::Diagnostic d;
56 d.FromJSON(arrDiags.arrayItem(i));
57 res.push_back(d);
58 }
59 return res;
60 }
61
62 wxString LSP::ResponseMessage::GetDiagnosticsUri() const
63 {
64 JSONItem params = Get("params");
65 if(!params.isOk()) {
66 return "";
67 }
68 return params.namedObject("uri").toString();
69 }
70
71 bool LSP::ResponseMessage::IsErrorResponse() const { return Has("error"); }