| 1 |
#include "LSP/basic_types.h" |
| 2 |
|
| 3 |
#include "JSON.h" |
| 4 |
#include "file_logger.h" |
| 5 |
|
| 6 |
#include <wx/filesys.h> |
| 7 |
|
| 8 |
//===------------------------------------------------ |
| 9 |
// Request |
| 10 |
//===------------------------------------------------ |
| 11 |
namespace LSP |
| 12 |
{ |
| 13 |
|
| 14 |
//===---------------------------------------------------------------------------------- |
| 15 |
// TextDocumentIdentifier |
| 16 |
//===---------------------------------------------------------------------------------- |
| 17 |
void TextDocumentIdentifier::FromJSON(const JSONItem& json) |
| 18 |
{ |
| 19 |
URI::FromString(json.namedObject("uri").toString(), &m_filename); |
| 20 |
} |
| 21 |
|
| 22 |
JSONItem TextDocumentIdentifier::ToJSON(const wxString& name) const |
| 23 |
{ |
| 24 |
JSONItem json = JSONItem::createObject(name); |
| 25 |
json.addProperty("uri", GetPathAsURI()); |
| 26 |
return json; |
| 27 |
} |
| 28 |
|
| 29 |
//===---------------------------------------------------------------------------------- |
| 30 |
// VersionedTextDocumentIdentifier |
| 31 |
//===---------------------------------------------------------------------------------- |
| 32 |
void VersionedTextDocumentIdentifier::FromJSON(const JSONItem& json) |
| 33 |
{ |
| 34 |
TextDocumentIdentifier::FromJSON(json); |
| 35 |
m_version = json.namedObject("version").toInt(m_version); |
| 36 |
} |
| 37 |
|
| 38 |
JSONItem VersionedTextDocumentIdentifier::ToJSON(const wxString& name) const |
| 39 |
{ |
| 40 |
JSONItem json = TextDocumentIdentifier::ToJSON(name); |
| 41 |
json.addProperty("version", m_version); |
| 42 |
return json; |
| 43 |
} |
| 44 |
|
| 45 |
//===---------------------------------------------------------------------------------- |
| 46 |
// Position |
| 47 |
//===---------------------------------------------------------------------------------- |
| 48 |
void Position::FromJSON(const JSONItem& json) |
| 49 |
{ |
| 50 |
m_line = json.namedObject("line").toInt(wxNOT_FOUND); |
| 51 |
m_character = json.namedObject("character").toInt(wxNOT_FOUND); |
| 52 |
} |
| 53 |
|
| 54 |
JSONItem Position::ToJSON(const wxString& name) const |
| 55 |
{ |
| 56 |
JSONItem json = JSONItem::createObject(name); |
| 57 |
json.addProperty("line", m_line); |
| 58 |
json.addProperty("character", m_character); |
| 59 |
return json; |
| 60 |
} |
| 61 |
|
| 62 |
//===---------------------------------------------------------------------------------- |
| 63 |
// TextDocumentItem |
| 64 |
//===---------------------------------------------------------------------------------- |
| 65 |
void TextDocumentItem::FromJSON(const JSONItem& json) |
| 66 |
{ |
| 67 |
URI::FromString(json.namedObject("uri").toString(), &m_uri); |
| 68 |
|
| 69 |
m_languageId = json.namedObject("languageId").toString(); |
| 70 |
m_version = json.namedObject("version").toInt(); |
| 71 |
m_text = json.namedObject("text").toString(); |
| 72 |
} |
| 73 |
|
| 74 |
JSONItem TextDocumentItem::ToJSON(const wxString& name) const |
| 75 |
{ |
| 76 |
JSONItem json = JSONItem::createObject(name); |
| 77 |
json.addProperty("uri", GetPathAsURI()) |
| 78 |
.addProperty("languageId", GetLanguageId()) |
| 79 |
.addProperty("version", GetVersion()) |
| 80 |
.addProperty("text", GetText()); |
| 81 |
return json; |
| 82 |
} |
| 83 |
//===---------------------------------------------------------------------------------- |
| 84 |
// TextDocumentContentChangeEvent |
| 85 |
//===---------------------------------------------------------------------------------- |
| 86 |
void TextDocumentContentChangeEvent::FromJSON(const JSONItem& json) { m_text = json.namedObject("text").toString(); } |
| 87 |
|
| 88 |
JSONItem TextDocumentContentChangeEvent::ToJSON(const wxString& name) const |
| 89 |
{ |
| 90 |
JSONItem json = JSONItem::createObject(name); |
| 91 |
json.addProperty("text", m_text); |
| 92 |
return json; |
| 93 |
} |
| 94 |
|
| 95 |
void Range::FromJSON(const JSONItem& json) |
| 96 |
{ |
| 97 |
m_start.FromJSON(json.namedObject("start")); |
| 98 |
m_end.FromJSON(json.namedObject("end")); |
| 99 |
} |
| 100 |
|
| 101 |
JSONItem Range::ToJSON(const wxString& name) const |
| 102 |
{ |
| 103 |
JSONItem json = JSONItem::createObject(name); |
| 104 |
json.append(m_start.ToJSON("start")); |
| 105 |
json.append(m_start.ToJSON("end")); |
| 106 |
return json; |
| 107 |
} |
| 108 |
|
| 109 |
void Location::FromJSON(const JSONItem& json) |
| 110 |
{ |
| 111 |
URI::FromString(json.namedObject("uri").toString(), &m_uri); |
| 112 |
m_range.FromJSON(json.namedObject("range")); |
| 113 |
m_pattern = json["pattern"].toString(); |
| 114 |
m_name = json["name"].toString(); |
| 115 |
} |
| 116 |
|
| 117 |
JSONItem Location::ToJSON(const wxString& name) const |
| 118 |
{ |
| 119 |
JSONItem json = JSONItem::createObject(name); |
| 120 |
json.addProperty("uri", GetPathAsURI()); |
| 121 |
json.append(m_range.ToJSON("range")); |
| 122 |
json.addProperty("pattern", m_pattern); |
| 123 |
json.addProperty("name", m_name); |
| 124 |
return json; |
| 125 |
} |
| 126 |
|
| 127 |
void TextEdit::FromJSON(const JSONItem& json) |
| 128 |
{ |
| 129 |
m_range.FromJSON(json.namedObject("range")); |
| 130 |
m_newText = json.namedObject("newText").toString(); |
| 131 |
} |
| 132 |
|
| 133 |
JSONItem TextEdit::ToJSON(const wxString& name) const |
| 134 |
{ |
| 135 |
JSONItem json = JSONItem::createObject(name); |
| 136 |
json.addProperty("newText", m_newText); |
| 137 |
json.append(m_range.ToJSON("range")); |
| 138 |
return json; |
| 139 |
} |
| 140 |
|
| 141 |
void ParameterInformation::FromJSON(const JSONItem& json) |
| 142 |
{ |
| 143 |
m_label = json.namedObject("label").toString(); |
| 144 |
m_documentation = json.namedObject("documentation").toString(); |
| 145 |
} |
| 146 |
|
| 147 |
JSONItem ParameterInformation::ToJSON(const wxString& name) const |
| 148 |
{ |
| 149 |
JSONItem json = JSONItem::createObject(name); |
| 150 |
json.addProperty("label", m_label); |
| 151 |
json.addProperty("documentation", m_documentation); |
| 152 |
return json; |
| 153 |
} |
| 154 |
|
| 155 |
void SignatureInformation::FromJSON(const JSONItem& json) |
| 156 |
{ |
| 157 |
m_label = json.namedObject("label").toString(); |
| 158 |
m_documentation = json.namedObject("documentation").toString(); |
| 159 |
m_parameters.clear(); |
| 160 |
if(json.hasNamedObject("parameters")) { |
| 161 |
JSONItem parameters = json.namedObject("parameters"); |
| 162 |
const int size = parameters.arraySize(); |
| 163 |
if(size > 0) { |
| 164 |
m_parameters.reserve(size); |
| 165 |
for(int i = 0; i < size; ++i) { |
| 166 |
ParameterInformation p; |
| 167 |
p.FromJSON(parameters.arrayItem(i)); |
| 168 |
m_parameters.push_back(p); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
JSONItem SignatureInformation::ToJSON(const wxString& name) const |
| 175 |
{ |
| 176 |
JSONItem json = JSONItem::createObject(name); |
| 177 |
json.addProperty("label", m_label); |
| 178 |
json.addProperty("documentation", m_documentation); |
| 179 |
if(!m_parameters.empty()) { |
| 180 |
JSONItem params = JSONItem::createArray("parameters"); |
| 181 |
json.append(params); |
| 182 |
for(size_t i = 0; i < m_parameters.size(); ++i) { |
| 183 |
params.append(m_parameters.at(i).ToJSON("")); |
| 184 |
} |
| 185 |
} |
| 186 |
return json; |
| 187 |
} |
| 188 |
|
| 189 |
void SignatureHelp::FromJSON(const JSONItem& json) |
| 190 |
{ |
| 191 |
// Read the signatures |
| 192 |
m_signatures.clear(); |
| 193 |
JSONItem signatures = json.namedObject("signatures"); |
| 194 |
const int count = signatures.arraySize(); |
| 195 |
for(int i = 0; i < count; ++i) { |
| 196 |
SignatureInformation si; |
| 197 |
si.FromJSON(signatures.arrayItem(i)); |
| 198 |
m_signatures.push_back(si); |
| 199 |
} |
| 200 |
|
| 201 |
m_activeSignature = json.namedObject("activeSignature").toInt(0); |
| 202 |
m_activeParameter = json.namedObject("activeParameter").toInt(0); |
| 203 |
} |
| 204 |
|
| 205 |
JSONItem SignatureHelp::ToJSON(const wxString& name) const |
| 206 |
{ |
| 207 |
JSONItem json = JSONItem::createObject(name); |
| 208 |
JSONItem signatures = JSONItem::createArray("signatures"); |
| 209 |
json.append(signatures); |
| 210 |
for(const SignatureInformation& si : m_signatures) { |
| 211 |
signatures.arrayAppend(si.ToJSON("")); |
| 212 |
} |
| 213 |
json.addProperty("activeSignature", m_activeSignature); |
| 214 |
json.addProperty("activeParameter", m_activeParameter); |
| 215 |
return json; |
| 216 |
} |
| 217 |
|
| 218 |
void MarkupContent::FromJSON(const JSONItem& json) |
| 219 |
{ |
| 220 |
m_kind = json.namedObject("kind").toString(); |
| 221 |
m_value = json.namedObject("value").toString(); |
| 222 |
} |
| 223 |
|
| 224 |
JSONItem MarkupContent::ToJSON(const wxString& name) const |
| 225 |
{ |
| 226 |
JSONItem json = JSONItem::createObject(name); |
| 227 |
json.addProperty("kind", m_kind); |
| 228 |
json.addProperty("value", m_value); |
| 229 |
return json; |
| 230 |
} |
| 231 |
|
| 232 |
void Hover::FromJSON(const JSONItem& json) |
| 233 |
{ |
| 234 |
m_contents.FromJSON(json.namedObject("contents")); |
| 235 |
m_range.FromJSON(json.namedObject("range")); |
| 236 |
} |
| 237 |
|
| 238 |
JSONItem Hover::ToJSON(const wxString& name) const |
| 239 |
{ |
| 240 |
JSONItem json = JSONItem::createObject(name); |
| 241 |
json.append(m_contents.ToJSON("contents")); |
| 242 |
json.append(m_range.ToJSON("range")); |
| 243 |
return json; |
| 244 |
} |
| 245 |
|
| 246 |
///===------------------------------------------------------------------------ |
| 247 |
/// Diagnostic |
| 248 |
///===------------------------------------------------------------------------ |
| 249 |
void Diagnostic::FromJSON(const JSONItem& json) |
| 250 |
{ |
| 251 |
m_range.FromJSON(json.namedObject("range")); |
| 252 |
m_message = json.namedObject("message").toString(); |
| 253 |
} |
| 254 |
|
| 255 |
JSONItem Diagnostic::ToJSON(const wxString& name) const |
| 256 |
{ |
| 257 |
JSONItem json = JSONItem::createObject(name); |
| 258 |
json.append(m_range.ToJSON("range")); |
| 259 |
json.addProperty("message", GetMessage()); |
| 260 |
return json; |
| 261 |
} |
| 262 |
|
| 263 |
TextDocumentContentChangeEvent& TextDocumentContentChangeEvent::SetText(const wxString& text) |
| 264 |
{ |
| 265 |
this->m_text.clear(); |
| 266 |
if(!text.empty()) { |
| 267 |
this->m_text.reserve(text.length() + 1); // for the null |
| 268 |
this->m_text.append(text); |
| 269 |
} |
| 270 |
return *this; |
| 271 |
} |
| 272 |
//===---------------------------------------------------------------------------------- |
| 273 |
// DocumentSymbol |
| 274 |
//===---------------------------------------------------------------------------------- |
| 275 |
void DocumentSymbol::FromJSON(const JSONItem& json) |
| 276 |
{ |
| 277 |
name = json["name"].toString(); |
| 278 |
detail = json["detail"].toString(); |
| 279 |
kind = (eSymbolKind)json["kind"].toInt(0); |
| 280 |
range.FromJSON(json["range"]); |
| 281 |
selectionRange.FromJSON(json["selectionRange"]); |
| 282 |
|
| 283 |
// read the children |
| 284 |
auto jsonChildren = json["children"]; |
| 285 |
int size = jsonChildren.arraySize(); |
| 286 |
children.clear(); |
| 287 |
children.reserve(size); |
| 288 |
for(int i = 0; i < size; ++i) { |
| 289 |
auto child = jsonChildren[i]; |
| 290 |
DocumentSymbol ds; |
| 291 |
ds.FromJSON(child); |
| 292 |
children.push_back(ds); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
JSONItem DocumentSymbol::ToJSON(const wxString& name) const |
| 297 |
{ |
| 298 |
wxASSERT_MSG(false, "DocumentSymbol::ToJSON(): is not implemented"); |
| 299 |
return JSONItem(nullptr); |
| 300 |
} |
| 301 |
|
| 302 |
//===---------------------------------------------------------------------------------- |
| 303 |
// DocumentSymbol |
| 304 |
//===---------------------------------------------------------------------------------- |
| 305 |
void SymbolInformation::FromJSON(const JSONItem& json) |
| 306 |
{ |
| 307 |
name = json["name"].toString(); |
| 308 |
containerName = json["containerName"].toString(); |
| 309 |
kind = (eSymbolKind)json["kind"].toInt(0); |
| 310 |
location.FromJSON(json["location"]); |
| 311 |
} |
| 312 |
|
| 313 |
JSONItem SymbolInformation::ToJSON(const wxString& name) const |
| 314 |
{ |
| 315 |
JSONItem json = JSONItem::createObject(name); |
| 316 |
json.addProperty("kind", (int)kind); |
| 317 |
json.addProperty("containerName", containerName); |
| 318 |
json.append(location.ToJSON("location")); |
| 319 |
json.addProperty("name", this->name); |
| 320 |
return json; |
| 321 |
} |
| 322 |
|
| 323 |
const wxString& URI::GetPath() const { return m_path; } |
| 324 |
const wxString& URI::GetUrl() const { return m_uri; } |
| 325 |
void URI::FromString(const wxString& str, URI* uri) |
| 326 |
{ |
| 327 |
uri->m_path = FileUtils::FilePathFromURI(str); |
| 328 |
uri->m_uri = FileUtils::FilePathToURI(str); |
| 329 |
} |
| 330 |
}; // namespace LSP |