godot_lsp.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  1. /**************************************************************************/
  2. /* godot_lsp.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/doc_data.h"
  32. #include "core/object/class_db.h"
  33. #include "core/templates/list.h"
  34. namespace LSP {
  35. typedef String DocumentUri;
  36. /** Format BBCode documentation from DocData to markdown */
  37. static String marked_documentation(const String &p_bbcode);
  38. /**
  39. * Text documents are identified using a URI. On the protocol level, URIs are passed as strings.
  40. */
  41. struct TextDocumentIdentifier {
  42. /**
  43. * The text document's URI.
  44. */
  45. DocumentUri uri;
  46. _FORCE_INLINE_ void load(const Dictionary &p_params) {
  47. uri = p_params["uri"];
  48. }
  49. _FORCE_INLINE_ Dictionary to_json() const {
  50. Dictionary dict;
  51. dict["uri"] = uri;
  52. return dict;
  53. }
  54. };
  55. /**
  56. * Position in a text document expressed as zero-based line and zero-based character offset.
  57. * A position is between two characters like an ‘insert’ cursor in a editor.
  58. * Special values like for example -1 to denote the end of a line are not supported.
  59. */
  60. struct Position {
  61. /**
  62. * Line position in a document (zero-based).
  63. */
  64. int line = 0;
  65. /**
  66. * Character offset on a line in a document (zero-based). Assuming that the line is
  67. * represented as a string, the `character` value represents the gap between the
  68. * `character` and `character + 1`.
  69. *
  70. * If the character value is greater than the line length it defaults back to the
  71. * line length.
  72. */
  73. int character = 0;
  74. _FORCE_INLINE_ bool operator==(const Position &p_other) const {
  75. return line == p_other.line && character == p_other.character;
  76. }
  77. String to_string() const {
  78. return vformat("(%d,%d)", line, character);
  79. }
  80. _FORCE_INLINE_ void load(const Dictionary &p_params) {
  81. line = p_params["line"];
  82. character = p_params["character"];
  83. }
  84. _FORCE_INLINE_ Dictionary to_json() const {
  85. Dictionary dict;
  86. dict["line"] = line;
  87. dict["character"] = character;
  88. return dict;
  89. }
  90. };
  91. /**
  92. * A range in a text document expressed as (zero-based) start and end positions.
  93. * A range is comparable to a selection in an editor. Therefore the end position is exclusive.
  94. * If you want to specify a range that contains a line including the line ending character(s) then use an end position denoting the start of the next line.
  95. */
  96. struct Range {
  97. /**
  98. * The range's start position.
  99. */
  100. Position start;
  101. /**
  102. * The range's end position.
  103. */
  104. Position end;
  105. _FORCE_INLINE_ bool operator==(const Range &p_other) const {
  106. return start == p_other.start && end == p_other.end;
  107. }
  108. bool contains(const Position &p_pos) const {
  109. // Inside line range.
  110. if (start.line <= p_pos.line && p_pos.line <= end.line) {
  111. // If on start line: must come after start char.
  112. bool start_ok = p_pos.line == start.line ? start.character <= p_pos.character : true;
  113. // If on end line: must come before end char.
  114. bool end_ok = p_pos.line == end.line ? p_pos.character <= end.character : true;
  115. return start_ok && end_ok;
  116. } else {
  117. return false;
  118. }
  119. }
  120. String to_string() const {
  121. return vformat("[%s:%s]", start.to_string(), end.to_string());
  122. }
  123. _FORCE_INLINE_ void load(const Dictionary &p_params) {
  124. start.load(p_params["start"]);
  125. end.load(p_params["end"]);
  126. }
  127. _FORCE_INLINE_ Dictionary to_json() const {
  128. Dictionary dict;
  129. dict["start"] = start.to_json();
  130. dict["end"] = end.to_json();
  131. return dict;
  132. }
  133. };
  134. /**
  135. * Represents a location inside a resource, such as a line inside a text file.
  136. */
  137. struct Location {
  138. DocumentUri uri;
  139. Range range;
  140. _FORCE_INLINE_ void load(const Dictionary &p_params) {
  141. uri = p_params["uri"];
  142. range.load(p_params["range"]);
  143. }
  144. _FORCE_INLINE_ Dictionary to_json() const {
  145. Dictionary dict;
  146. dict["uri"] = uri;
  147. dict["range"] = range.to_json();
  148. return dict;
  149. }
  150. };
  151. /**
  152. * Represents a link between a source and a target location.
  153. */
  154. struct LocationLink {
  155. /**
  156. * Span of the origin of this link.
  157. *
  158. * Used as the underlined span for mouse interaction. Defaults to the word range at
  159. * the mouse position.
  160. */
  161. Range *originSelectionRange = nullptr;
  162. /**
  163. * The target resource identifier of this link.
  164. */
  165. String targetUri;
  166. /**
  167. * The full target range of this link. If the target for example is a symbol then target range is the
  168. * range enclosing this symbol not including leading/trailing whitespace but everything else
  169. * like comments. This information is typically used to highlight the range in the editor.
  170. */
  171. Range targetRange;
  172. /**
  173. * The range that should be selected and revealed when this link is being followed, e.g the name of a function.
  174. * Must be contained by the `targetRange`. See also `DocumentSymbol#range`
  175. */
  176. Range targetSelectionRange;
  177. };
  178. /**
  179. * A parameter literal used in requests to pass a text document and a position inside that document.
  180. */
  181. struct TextDocumentPositionParams {
  182. /**
  183. * The text document.
  184. */
  185. TextDocumentIdentifier textDocument;
  186. /**
  187. * The position inside the text document.
  188. */
  189. Position position;
  190. _FORCE_INLINE_ void load(const Dictionary &p_params) {
  191. textDocument.load(p_params["textDocument"]);
  192. position.load(p_params["position"]);
  193. }
  194. _FORCE_INLINE_ Dictionary to_json() const {
  195. Dictionary dict;
  196. dict["textDocument"] = textDocument.to_json();
  197. dict["position"] = position.to_json();
  198. return dict;
  199. }
  200. };
  201. struct ReferenceContext {
  202. /**
  203. * Include the declaration of the current symbol.
  204. */
  205. bool includeDeclaration = false;
  206. };
  207. struct ShowMessageParams {
  208. /**
  209. * The message type. See {@link MessageType}.
  210. */
  211. int type;
  212. /**
  213. * The actual message.
  214. */
  215. String message;
  216. _FORCE_INLINE_ Dictionary to_json() const {
  217. Dictionary dict;
  218. dict["type"] = type;
  219. dict["message"] = message;
  220. return dict;
  221. }
  222. };
  223. struct ReferenceParams : TextDocumentPositionParams {
  224. ReferenceContext context;
  225. };
  226. struct DocumentLinkParams {
  227. /**
  228. * The document to provide document links for.
  229. */
  230. TextDocumentIdentifier textDocument;
  231. _FORCE_INLINE_ void load(const Dictionary &p_params) {
  232. textDocument.load(p_params["textDocument"]);
  233. }
  234. };
  235. /**
  236. * A document link is a range in a text document that links to an internal or external resource, like another
  237. * text document or a web site.
  238. */
  239. struct DocumentLink {
  240. /**
  241. * The range this link applies to.
  242. */
  243. Range range;
  244. /**
  245. * The uri this link points to. If missing a resolve request is sent later.
  246. */
  247. DocumentUri target;
  248. Dictionary to_json() const {
  249. Dictionary dict;
  250. dict["range"] = range.to_json();
  251. dict["target"] = target;
  252. return dict;
  253. }
  254. };
  255. /**
  256. * A textual edit applicable to a text document.
  257. */
  258. struct TextEdit {
  259. /**
  260. * The range of the text document to be manipulated. To insert
  261. * text into a document create a range where start === end.
  262. */
  263. Range range;
  264. /**
  265. * The string to be inserted. For delete operations use an
  266. * empty string.
  267. */
  268. String newText;
  269. };
  270. /**
  271. * The edits to be applied.
  272. */
  273. struct WorkspaceEdit {
  274. /**
  275. * Holds changes to existing resources.
  276. */
  277. HashMap<String, Vector<TextEdit>> changes;
  278. _FORCE_INLINE_ void add_edit(const String &uri, const TextEdit &edit) {
  279. if (changes.has(uri)) {
  280. changes[uri].push_back(edit);
  281. } else {
  282. Vector<TextEdit> edits;
  283. edits.push_back(edit);
  284. changes[uri] = edits;
  285. }
  286. }
  287. _FORCE_INLINE_ Dictionary to_json() const {
  288. Dictionary dict;
  289. Dictionary out_changes;
  290. for (const KeyValue<String, Vector<TextEdit>> &E : changes) {
  291. Array edits;
  292. for (int i = 0; i < E.value.size(); ++i) {
  293. Dictionary text_edit;
  294. text_edit["range"] = E.value[i].range.to_json();
  295. text_edit["newText"] = E.value[i].newText;
  296. edits.push_back(text_edit);
  297. }
  298. out_changes[E.key] = edits;
  299. }
  300. dict["changes"] = out_changes;
  301. return dict;
  302. }
  303. _FORCE_INLINE_ void add_change(const String &uri, const int &line, const int &start_character, const int &end_character, const String &new_text) {
  304. TextEdit new_edit;
  305. new_edit.newText = new_text;
  306. new_edit.range.start.line = line;
  307. new_edit.range.start.character = start_character;
  308. new_edit.range.end.line = line;
  309. new_edit.range.end.character = end_character;
  310. if (HashMap<String, Vector<TextEdit>>::Iterator E = changes.find(uri)) {
  311. E->value.push_back(new_edit);
  312. } else {
  313. Vector<TextEdit> edit_list;
  314. edit_list.push_back(new_edit);
  315. changes.insert(uri, edit_list);
  316. }
  317. }
  318. };
  319. /**
  320. * Represents a reference to a command.
  321. * Provides a title which will be used to represent a command in the UI.
  322. * Commands are identified by a string identifier.
  323. * The recommended way to handle commands is to implement their execution on the server side if the client and server provides the corresponding capabilities.
  324. * Alternatively the tool extension code could handle the command. The protocol currently doesn’t specify a set of well-known commands.
  325. */
  326. struct Command {
  327. /**
  328. * Title of the command, like `save`.
  329. */
  330. String title;
  331. /**
  332. * The identifier of the actual command handler.
  333. */
  334. String command;
  335. /**
  336. * Arguments that the command handler should be
  337. * invoked with.
  338. */
  339. Array arguments;
  340. Dictionary to_json() const {
  341. Dictionary dict;
  342. dict["title"] = title;
  343. dict["command"] = command;
  344. if (arguments.size()) {
  345. dict["arguments"] = arguments;
  346. }
  347. return dict;
  348. }
  349. };
  350. // Use namespace instead of enumeration to follow the LSP specifications.
  351. // `LSP::EnumName::EnumValue` is OK but `LSP::EnumValue` is not.
  352. namespace TextDocumentSyncKind {
  353. /**
  354. * Documents should not be synced at all.
  355. */
  356. static const int None = 0;
  357. /**
  358. * Documents are synced by always sending the full content
  359. * of the document.
  360. */
  361. static const int Full = 1;
  362. /**
  363. * Documents are synced by sending the full content on open.
  364. * After that only incremental updates to the document are
  365. * send.
  366. */
  367. static const int Incremental = 2;
  368. }; // namespace TextDocumentSyncKind
  369. namespace MessageType {
  370. /**
  371. * An error message.
  372. */
  373. static const int Error = 1;
  374. /**
  375. * A warning message.
  376. */
  377. static const int Warning = 2;
  378. /**
  379. * An information message.
  380. */
  381. static const int Info = 3;
  382. /**
  383. * A log message.
  384. */
  385. static const int Log = 4;
  386. }; // namespace MessageType
  387. /**
  388. * Completion options.
  389. */
  390. struct CompletionOptions {
  391. /**
  392. * The server provides support to resolve additional
  393. * information for a completion item.
  394. */
  395. bool resolveProvider = true;
  396. /**
  397. * The characters that trigger completion automatically.
  398. */
  399. Vector<String> triggerCharacters;
  400. CompletionOptions() {
  401. triggerCharacters.push_back(".");
  402. triggerCharacters.push_back("$");
  403. triggerCharacters.push_back("'");
  404. triggerCharacters.push_back("\"");
  405. }
  406. Dictionary to_json() const {
  407. Dictionary dict;
  408. dict["resolveProvider"] = resolveProvider;
  409. dict["triggerCharacters"] = triggerCharacters;
  410. return dict;
  411. }
  412. };
  413. /**
  414. * Signature help options.
  415. */
  416. struct SignatureHelpOptions {
  417. /**
  418. * The characters that trigger signature help
  419. * automatically.
  420. */
  421. Vector<String> triggerCharacters;
  422. Dictionary to_json() {
  423. Dictionary dict;
  424. dict["triggerCharacters"] = triggerCharacters;
  425. return dict;
  426. }
  427. };
  428. /**
  429. * Code Lens options.
  430. */
  431. struct CodeLensOptions {
  432. /**
  433. * Code lens has a resolve provider as well.
  434. */
  435. bool resolveProvider = false;
  436. Dictionary to_json() {
  437. Dictionary dict;
  438. dict["resolveProvider"] = resolveProvider;
  439. return dict;
  440. }
  441. };
  442. /**
  443. * Rename options
  444. */
  445. struct RenameOptions {
  446. /**
  447. * Renames should be checked and tested before being executed.
  448. */
  449. bool prepareProvider = true;
  450. Dictionary to_json() {
  451. Dictionary dict;
  452. dict["prepareProvider"] = prepareProvider;
  453. return dict;
  454. }
  455. };
  456. /**
  457. * Document link options.
  458. */
  459. struct DocumentLinkOptions {
  460. /**
  461. * Document links have a resolve provider as well.
  462. */
  463. bool resolveProvider = false;
  464. Dictionary to_json() {
  465. Dictionary dict;
  466. dict["resolveProvider"] = resolveProvider;
  467. return dict;
  468. }
  469. };
  470. /**
  471. * Execute command options.
  472. */
  473. struct ExecuteCommandOptions {
  474. /**
  475. * The commands to be executed on the server
  476. */
  477. Vector<String> commands;
  478. Dictionary to_json() {
  479. Dictionary dict;
  480. dict["commands"] = commands;
  481. return dict;
  482. }
  483. };
  484. /**
  485. * Save options.
  486. */
  487. struct SaveOptions {
  488. /**
  489. * The client is supposed to include the content on save.
  490. */
  491. bool includeText = true;
  492. Dictionary to_json() {
  493. Dictionary dict;
  494. dict["includeText"] = includeText;
  495. return dict;
  496. }
  497. };
  498. /**
  499. * Color provider options.
  500. */
  501. struct ColorProviderOptions {
  502. Dictionary to_json() {
  503. return Dictionary();
  504. }
  505. };
  506. /**
  507. * Folding range provider options.
  508. */
  509. struct FoldingRangeProviderOptions {
  510. Dictionary to_json() {
  511. return Dictionary();
  512. }
  513. };
  514. struct TextDocumentSyncOptions {
  515. /**
  516. * Open and close notifications are sent to the server. If omitted open close notification should not
  517. * be sent.
  518. */
  519. bool openClose = true;
  520. /**
  521. * Change notifications are sent to the server. See TextDocumentSyncKind.None, TextDocumentSyncKind.Full
  522. * and TextDocumentSyncKind.Incremental. If omitted it defaults to TextDocumentSyncKind.None.
  523. */
  524. int change = TextDocumentSyncKind::Full;
  525. /**
  526. * If present will save notifications are sent to the server. If omitted the notification should not be
  527. * sent.
  528. */
  529. bool willSave = false;
  530. /**
  531. * If present will save wait until requests are sent to the server. If omitted the request should not be
  532. * sent.
  533. */
  534. bool willSaveWaitUntil = true;
  535. /**
  536. * If present save notifications are sent to the server. If omitted the notification should not be
  537. * sent.
  538. */
  539. SaveOptions save;
  540. Dictionary to_json() {
  541. Dictionary dict;
  542. dict["willSaveWaitUntil"] = willSaveWaitUntil;
  543. dict["willSave"] = willSave;
  544. dict["openClose"] = openClose;
  545. dict["change"] = change;
  546. dict["save"] = save.to_json();
  547. return dict;
  548. }
  549. };
  550. /**
  551. * Static registration options to be returned in the initialize request.
  552. */
  553. struct StaticRegistrationOptions {
  554. /**
  555. * The id used to register the request. The id can be used to deregister
  556. * the request again. See also Registration#id.
  557. */
  558. String id;
  559. };
  560. /**
  561. * Format document on type options.
  562. */
  563. struct DocumentOnTypeFormattingOptions {
  564. /**
  565. * A character on which formatting should be triggered, like `}`.
  566. */
  567. String firstTriggerCharacter;
  568. /**
  569. * More trigger characters.
  570. */
  571. Vector<String> moreTriggerCharacter;
  572. Dictionary to_json() {
  573. Dictionary dict;
  574. dict["firstTriggerCharacter"] = firstTriggerCharacter;
  575. dict["moreTriggerCharacter"] = moreTriggerCharacter;
  576. return dict;
  577. }
  578. };
  579. struct TextDocumentItem {
  580. /**
  581. * The text document's URI.
  582. */
  583. DocumentUri uri;
  584. /**
  585. * The text document's language identifier.
  586. */
  587. String languageId;
  588. /**
  589. * The version number of this document (it will increase after each
  590. * change, including undo/redo).
  591. */
  592. int version = 0;
  593. /**
  594. * The content of the opened text document.
  595. */
  596. String text;
  597. void load(const Dictionary &p_dict) {
  598. uri = p_dict["uri"];
  599. languageId = p_dict.get("languageId", "");
  600. version = p_dict.get("version", 0);
  601. text = p_dict.get("text", "");
  602. }
  603. Dictionary to_json() const {
  604. Dictionary dict;
  605. dict["uri"] = uri;
  606. dict["languageId"] = languageId;
  607. dict["version"] = version;
  608. dict["text"] = text;
  609. return dict;
  610. }
  611. };
  612. /**
  613. * An event describing a change to a text document.
  614. */
  615. struct TextDocumentContentChangeEvent {
  616. /**
  617. * The new text of the range/document.
  618. */
  619. String text;
  620. void load(const Dictionary &p_params) {
  621. text = p_params["text"];
  622. }
  623. };
  624. // Use namespace instead of enumeration to follow the LSP specifications
  625. namespace DiagnosticSeverity {
  626. /**
  627. * Reports an error.
  628. */
  629. static const int Error = 1;
  630. /**
  631. * Reports a warning.
  632. */
  633. static const int Warning = 2;
  634. /**
  635. * Reports an information.
  636. */
  637. static const int Information = 3;
  638. /**
  639. * Reports a hint.
  640. */
  641. static const int Hint = 4;
  642. }; // namespace DiagnosticSeverity
  643. /**
  644. * Represents a related message and source code location for a diagnostic. This should be
  645. * used to point to code locations that cause or related to a diagnostics, e.g when duplicating
  646. * a symbol in a scope.
  647. */
  648. struct DiagnosticRelatedInformation {
  649. /**
  650. * The location of this related diagnostic information.
  651. */
  652. Location location;
  653. /**
  654. * The message of this related diagnostic information.
  655. */
  656. String message;
  657. Dictionary to_json() const {
  658. Dictionary dict;
  659. dict["location"] = location.to_json();
  660. dict["message"] = message;
  661. return dict;
  662. }
  663. };
  664. /**
  665. * Represents a diagnostic, such as a compiler error or warning.
  666. * Diagnostic objects are only valid in the scope of a resource.
  667. */
  668. struct Diagnostic {
  669. /**
  670. * The range at which the message applies.
  671. */
  672. Range range;
  673. /**
  674. * The diagnostic's severity. Can be omitted. If omitted it is up to the
  675. * client to interpret diagnostics as error, warning, info or hint.
  676. */
  677. int severity = 0;
  678. /**
  679. * The diagnostic's code, which might appear in the user interface.
  680. */
  681. int code = 0;
  682. /**
  683. * A human-readable string describing the source of this
  684. * diagnostic, e.g. 'typescript' or 'super lint'.
  685. */
  686. String source;
  687. /**
  688. * The diagnostic's message.
  689. */
  690. String message;
  691. /**
  692. * An array of related diagnostic information, e.g. when symbol-names within
  693. * a scope collide all definitions can be marked via this property.
  694. */
  695. Vector<DiagnosticRelatedInformation> relatedInformation;
  696. Dictionary to_json() const {
  697. Dictionary dict;
  698. dict["range"] = range.to_json();
  699. dict["code"] = code;
  700. dict["severity"] = severity;
  701. dict["message"] = message;
  702. dict["source"] = source;
  703. if (!relatedInformation.is_empty()) {
  704. Array arr;
  705. arr.resize(relatedInformation.size());
  706. for (int i = 0; i < relatedInformation.size(); i++) {
  707. arr[i] = relatedInformation[i].to_json();
  708. }
  709. dict["relatedInformation"] = arr;
  710. }
  711. return dict;
  712. }
  713. };
  714. // Use namespace instead of enumeration to follow the LSP specifications
  715. /**
  716. * Describes the content type that a client supports in various
  717. * result literals like `Hover`, `ParameterInfo` or `CompletionItem`.
  718. *
  719. * Please note that `MarkupKinds` must not start with a `$`. This kinds
  720. * are reserved for internal usage.
  721. */
  722. namespace MarkupKind {
  723. static const String PlainText = "plaintext";
  724. static const String Markdown = "markdown";
  725. }; // namespace MarkupKind
  726. /**
  727. * A `MarkupContent` literal represents a string value which content is interpreted base on its
  728. * kind flag. Currently the protocol supports `plaintext` and `markdown` as markup kinds.
  729. *
  730. * If the kind is `markdown` then the value can contain fenced code blocks like in GitHub issues.
  731. * See https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting
  732. *
  733. * Here is an example how such a string can be constructed using JavaScript / TypeScript:
  734. * ```typescript
  735. * let markdown: MarkdownContent = {
  736. * kind: MarkupKind.Markdown,
  737. * value: [
  738. * '# Header',
  739. * 'Some text',
  740. * '```typescript',
  741. * 'someCode();',
  742. * '```'
  743. * ].join('\n')
  744. * };
  745. * ```
  746. *
  747. * *Please Note* that clients might sanitize the return markdown. A client could decide to
  748. * remove HTML from the markdown to avoid script execution.
  749. */
  750. struct MarkupContent {
  751. /**
  752. * The type of the Markup.
  753. */
  754. String kind;
  755. /**
  756. * The content itself.
  757. */
  758. String value;
  759. MarkupContent() {
  760. kind = MarkupKind::Markdown;
  761. }
  762. MarkupContent(const String &p_value) {
  763. value = p_value;
  764. kind = MarkupKind::Markdown;
  765. }
  766. Dictionary to_json() const {
  767. Dictionary dict;
  768. dict["kind"] = kind;
  769. dict["value"] = value;
  770. return dict;
  771. }
  772. };
  773. // Use namespace instead of enumeration to follow the LSP specifications
  774. // `LSP::EnumName::EnumValue` is OK but `LSP::EnumValue` is not.
  775. // And here C++ compilers are unhappy with our enumeration name like `Color`, `File`, `RefCounted` etc.
  776. /**
  777. * The kind of a completion entry.
  778. */
  779. namespace CompletionItemKind {
  780. static const int Text = 1;
  781. static const int Method = 2;
  782. static const int Function = 3;
  783. static const int Constructor = 4;
  784. static const int Field = 5;
  785. static const int Variable = 6;
  786. static const int Class = 7;
  787. static const int Interface = 8;
  788. static const int Module = 9;
  789. static const int Property = 10;
  790. static const int Unit = 11;
  791. static const int Value = 12;
  792. static const int Enum = 13;
  793. static const int Keyword = 14;
  794. static const int Snippet = 15;
  795. static const int Color = 16;
  796. static const int File = 17;
  797. static const int RefCounted = 18;
  798. static const int Folder = 19;
  799. static const int EnumMember = 20;
  800. static const int Constant = 21;
  801. static const int Struct = 22;
  802. static const int Event = 23;
  803. static const int Operator = 24;
  804. static const int TypeParameter = 25;
  805. }; // namespace CompletionItemKind
  806. // Use namespace instead of enumeration to follow the LSP specifications.
  807. /**
  808. * Defines whether the insert text in a completion item should be interpreted as
  809. * plain text or a snippet.
  810. */
  811. namespace InsertTextFormat {
  812. /**
  813. * The primary text to be inserted is treated as a plain string.
  814. */
  815. static const int PlainText = 1;
  816. /**
  817. * The primary text to be inserted is treated as a snippet.
  818. *
  819. * A snippet can define tab stops and placeholders with `$1`, `$2`
  820. * and `${3:foo}`. `$0` defines the final tab stop, it defaults to
  821. * the end of the snippet. Placeholders with equal identifiers are linked,
  822. * that is typing in one will update others too.
  823. */
  824. static const int Snippet = 2;
  825. }; // namespace InsertTextFormat
  826. struct CompletionItem {
  827. /**
  828. * The label of this completion item. By default
  829. * also the text that is inserted when selecting
  830. * this completion.
  831. */
  832. String label;
  833. /**
  834. * The kind of this completion item. Based of the kind
  835. * an icon is chosen by the editor. The standardized set
  836. * of available values is defined in `CompletionItemKind`.
  837. */
  838. int kind = 0;
  839. /**
  840. * A human-readable string with additional information
  841. * about this item, like type or symbol information.
  842. */
  843. String detail;
  844. /**
  845. * A human-readable string that represents a doc-comment.
  846. */
  847. MarkupContent documentation;
  848. /**
  849. * Indicates if this item is deprecated.
  850. */
  851. bool deprecated = false;
  852. /**
  853. * Select this item when showing.
  854. *
  855. * *Note* that only one completion item can be selected and that the
  856. * tool / client decides which item that is. The rule is that the *first*
  857. * item of those that match best is selected.
  858. */
  859. bool preselect = false;
  860. /**
  861. * A string that should be used when comparing this item
  862. * with other items. When omitted the label is used
  863. * as the filter text for this item.
  864. */
  865. String sortText;
  866. /**
  867. * A string that should be used when filtering a set of
  868. * completion items. When omitted the label is used as the
  869. * filter text for this item.
  870. */
  871. String filterText;
  872. /**
  873. * A string that should be inserted into a document when selecting
  874. * this completion. When omitted the label is used as the insert text
  875. * for this item.
  876. *
  877. * The `insertText` is subject to interpretation by the client side.
  878. * Some tools might not take the string literally. For example
  879. * VS Code when code complete is requested in this example
  880. * `con<cursor position>` and a completion item with an `insertText` of
  881. * `console` is provided it will only insert `sole`. Therefore it is
  882. * recommended to use `textEdit` instead since it avoids additional client
  883. * side interpretation.
  884. */
  885. String insertText;
  886. /**
  887. * The format of the insert text. The format applies to both the `insertText` property
  888. * and the `newText` property of a provided `textEdit`.
  889. */
  890. int insertTextFormat = 0;
  891. /**
  892. * An edit which is applied to a document when selecting this completion. When an edit is provided the value of
  893. * `insertText` is ignored.
  894. *
  895. * *Note:* The range of the edit must be a single line range and it must contain the position at which completion
  896. * has been requested.
  897. */
  898. TextEdit textEdit;
  899. /**
  900. * An optional array of additional text edits that are applied when
  901. * selecting this completion. Edits must not overlap (including the same insert position)
  902. * with the main edit nor with themselves.
  903. *
  904. * Additional text edits should be used to change text unrelated to the current cursor position
  905. * (for example adding an import statement at the top of the file if the completion item will
  906. * insert an unqualified type).
  907. */
  908. Vector<TextEdit> additionalTextEdits;
  909. /**
  910. * An optional set of characters that when pressed while this completion is active will accept it first and
  911. * then type that character. *Note* that all commit characters should have `length=1` and that superfluous
  912. * characters will be ignored.
  913. */
  914. Vector<String> commitCharacters;
  915. /**
  916. * An optional command that is executed *after* inserting this completion. *Note* that
  917. * additional modifications to the current document should be described with the
  918. * additionalTextEdits-property.
  919. */
  920. Command command;
  921. /**
  922. * A data entry field that is preserved on a completion item between
  923. * a completion and a completion resolve request.
  924. */
  925. Variant data;
  926. _FORCE_INLINE_ Dictionary to_json(bool resolved = false) const {
  927. Dictionary dict;
  928. dict["label"] = label;
  929. dict["kind"] = kind;
  930. dict["data"] = data;
  931. if (!insertText.is_empty()) {
  932. dict["insertText"] = insertText;
  933. }
  934. if (resolved) {
  935. dict["detail"] = detail;
  936. dict["documentation"] = documentation.to_json();
  937. dict["deprecated"] = deprecated;
  938. dict["preselect"] = preselect;
  939. if (!sortText.is_empty()) {
  940. dict["sortText"] = sortText;
  941. }
  942. if (!filterText.is_empty()) {
  943. dict["filterText"] = filterText;
  944. }
  945. if (commitCharacters.size()) {
  946. dict["commitCharacters"] = commitCharacters;
  947. }
  948. if (!command.command.is_empty()) {
  949. dict["command"] = command.to_json();
  950. }
  951. }
  952. return dict;
  953. }
  954. void load(const Dictionary &p_dict) {
  955. if (p_dict.has("label")) {
  956. label = p_dict["label"];
  957. }
  958. if (p_dict.has("kind")) {
  959. kind = p_dict["kind"];
  960. }
  961. if (p_dict.has("detail")) {
  962. detail = p_dict["detail"];
  963. }
  964. if (p_dict.has("documentation")) {
  965. Variant doc = p_dict["documentation"];
  966. if (doc.is_string()) {
  967. documentation.value = doc;
  968. } else if (doc.get_type() == Variant::DICTIONARY) {
  969. Dictionary v = doc;
  970. documentation.value = v["value"];
  971. }
  972. }
  973. if (p_dict.has("deprecated")) {
  974. deprecated = p_dict["deprecated"];
  975. }
  976. if (p_dict.has("preselect")) {
  977. preselect = p_dict["preselect"];
  978. }
  979. if (p_dict.has("sortText")) {
  980. sortText = p_dict["sortText"];
  981. }
  982. if (p_dict.has("filterText")) {
  983. filterText = p_dict["filterText"];
  984. }
  985. if (p_dict.has("insertText")) {
  986. insertText = p_dict["insertText"];
  987. }
  988. if (p_dict.has("data")) {
  989. data = p_dict["data"];
  990. }
  991. }
  992. };
  993. /**
  994. * Represents a collection of [completion items](#CompletionItem) to be presented
  995. * in the editor.
  996. */
  997. struct CompletionList {
  998. /**
  999. * This list it not complete. Further typing should result in recomputing
  1000. * this list.
  1001. */
  1002. bool isIncomplete = false;
  1003. /**
  1004. * The completion items.
  1005. */
  1006. Vector<CompletionItem> items;
  1007. };
  1008. // Use namespace instead of enumeration to follow the LSP specifications
  1009. // `LSP::EnumName::EnumValue` is OK but `LSP::EnumValue` is not
  1010. // And here C++ compilers are unhappy with our enumeration name like `String`, `Array`, `Object` etc
  1011. /**
  1012. * A symbol kind.
  1013. */
  1014. namespace SymbolKind {
  1015. static const int File = 1;
  1016. static const int Module = 2;
  1017. static const int Namespace = 3;
  1018. static const int Package = 4;
  1019. static const int Class = 5;
  1020. static const int Method = 6;
  1021. static const int Property = 7;
  1022. static const int Field = 8;
  1023. static const int Constructor = 9;
  1024. static const int Enum = 10;
  1025. static const int Interface = 11;
  1026. static const int Function = 12;
  1027. static const int Variable = 13;
  1028. static const int Constant = 14;
  1029. static const int String = 15;
  1030. static const int Number = 16;
  1031. static const int Boolean = 17;
  1032. static const int Array = 18;
  1033. static const int Object = 19;
  1034. static const int Key = 20;
  1035. static const int Null = 21;
  1036. static const int EnumMember = 22;
  1037. static const int Struct = 23;
  1038. static const int Event = 24;
  1039. static const int Operator = 25;
  1040. static const int TypeParameter = 26;
  1041. }; // namespace SymbolKind
  1042. /**
  1043. * Represents programming constructs like variables, classes, interfaces etc. that appear in a document. Document symbols can be
  1044. * hierarchical and they have two ranges: one that encloses its definition and one that points to its most interesting range,
  1045. * e.g. the range of an identifier.
  1046. */
  1047. struct DocumentSymbol {
  1048. /**
  1049. * The name of this symbol. Will be displayed in the user interface and therefore must not be
  1050. * an empty string or a string only consisting of white spaces.
  1051. */
  1052. String name;
  1053. /**
  1054. * More detail for this symbol, e.g the signature of a function.
  1055. */
  1056. String detail;
  1057. /**
  1058. * Documentation for this symbol.
  1059. */
  1060. String documentation;
  1061. /**
  1062. * Class name for the native symbols.
  1063. */
  1064. String native_class;
  1065. /**
  1066. * The kind of this symbol.
  1067. */
  1068. int kind = SymbolKind::File;
  1069. /**
  1070. * Indicates if this symbol is deprecated.
  1071. */
  1072. bool deprecated = false;
  1073. /**
  1074. * If `true`: Symbol is local to script and cannot be accessed somewhere else.
  1075. *
  1076. * For example: local variable inside a `func`.
  1077. */
  1078. bool local = false;
  1079. /**
  1080. * The range enclosing this symbol not including leading/trailing whitespace but everything else
  1081. * like comments. This information is typically used to determine if the clients cursor is
  1082. * inside the symbol to reveal in the symbol in the UI.
  1083. */
  1084. Range range;
  1085. /**
  1086. * The range that should be selected and revealed when this symbol is being picked, e.g the name of a function.
  1087. * Must be contained by the `range`.
  1088. */
  1089. Range selectionRange;
  1090. DocumentUri uri;
  1091. String script_path;
  1092. /**
  1093. * Children of this symbol, e.g. properties of a class.
  1094. */
  1095. Vector<DocumentSymbol> children;
  1096. Dictionary to_json(bool with_doc = false) const {
  1097. Dictionary dict;
  1098. dict["name"] = name;
  1099. dict["detail"] = detail;
  1100. dict["kind"] = kind;
  1101. dict["deprecated"] = deprecated;
  1102. dict["range"] = range.to_json();
  1103. dict["selectionRange"] = selectionRange.to_json();
  1104. if (with_doc) {
  1105. dict["documentation"] = documentation;
  1106. dict["native_class"] = native_class;
  1107. }
  1108. if (!children.is_empty()) {
  1109. Array arr;
  1110. for (int i = 0; i < children.size(); i++) {
  1111. if (children[i].local) {
  1112. continue;
  1113. }
  1114. arr.push_back(children[i].to_json(with_doc));
  1115. }
  1116. if (!children.is_empty()) {
  1117. dict["children"] = arr;
  1118. }
  1119. }
  1120. return dict;
  1121. }
  1122. _FORCE_INLINE_ MarkupContent render() const {
  1123. MarkupContent markdown;
  1124. if (detail.length()) {
  1125. markdown.value = "\t" + detail + "\n\n";
  1126. }
  1127. if (documentation.length()) {
  1128. markdown.value += marked_documentation(documentation) + "\n\n";
  1129. }
  1130. if (script_path.length()) {
  1131. markdown.value += "Defined in [" + script_path + "](" + uri + ")";
  1132. }
  1133. return markdown;
  1134. }
  1135. _FORCE_INLINE_ CompletionItem make_completion_item(bool resolved = false) const {
  1136. LSP::CompletionItem item;
  1137. item.label = name;
  1138. if (resolved) {
  1139. item.documentation = render();
  1140. }
  1141. switch (kind) {
  1142. case LSP::SymbolKind::Enum:
  1143. item.kind = LSP::CompletionItemKind::Enum;
  1144. break;
  1145. case LSP::SymbolKind::Class:
  1146. item.kind = LSP::CompletionItemKind::Class;
  1147. break;
  1148. case LSP::SymbolKind::Property:
  1149. item.kind = LSP::CompletionItemKind::Property;
  1150. break;
  1151. case LSP::SymbolKind::Method:
  1152. case LSP::SymbolKind::Function:
  1153. item.kind = LSP::CompletionItemKind::Method;
  1154. break;
  1155. case LSP::SymbolKind::Event:
  1156. item.kind = LSP::CompletionItemKind::Event;
  1157. break;
  1158. case LSP::SymbolKind::Constant:
  1159. item.kind = LSP::CompletionItemKind::Constant;
  1160. break;
  1161. case LSP::SymbolKind::Variable:
  1162. item.kind = LSP::CompletionItemKind::Variable;
  1163. break;
  1164. case LSP::SymbolKind::File:
  1165. item.kind = LSP::CompletionItemKind::File;
  1166. break;
  1167. default:
  1168. item.kind = LSP::CompletionItemKind::Text;
  1169. break;
  1170. }
  1171. return item;
  1172. }
  1173. };
  1174. struct ApplyWorkspaceEditParams {
  1175. WorkspaceEdit edit;
  1176. Dictionary to_json() {
  1177. Dictionary dict;
  1178. dict["edit"] = edit.to_json();
  1179. return dict;
  1180. }
  1181. };
  1182. struct NativeSymbolInspectParams {
  1183. String native_class;
  1184. String symbol_name;
  1185. void load(const Dictionary &p_params) {
  1186. native_class = p_params["native_class"];
  1187. symbol_name = p_params["symbol_name"];
  1188. }
  1189. };
  1190. /**
  1191. * Enum of known range kinds
  1192. */
  1193. namespace FoldingRangeKind {
  1194. /**
  1195. * Folding range for a comment
  1196. */
  1197. static const String Comment = "comment";
  1198. /**
  1199. * Folding range for a imports or includes
  1200. */
  1201. static const String Imports = "imports";
  1202. /**
  1203. * Folding range for a region (e.g. `#region`)
  1204. */
  1205. static const String Region = "region";
  1206. } // namespace FoldingRangeKind
  1207. /**
  1208. * Represents a folding range.
  1209. */
  1210. struct FoldingRange {
  1211. /**
  1212. * The zero-based line number from where the folded range starts.
  1213. */
  1214. int startLine = 0;
  1215. /**
  1216. * The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line.
  1217. */
  1218. int startCharacter = 0;
  1219. /**
  1220. * The zero-based line number where the folded range ends.
  1221. */
  1222. int endLine = 0;
  1223. /**
  1224. * The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line.
  1225. */
  1226. int endCharacter = 0;
  1227. /**
  1228. * Describes the kind of the folding range such as `comment' or 'region'. The kind
  1229. * is used to categorize folding ranges and used by commands like 'Fold all comments'. See
  1230. * [FoldingRangeKind](#FoldingRangeKind) for an enumeration of standardized kinds.
  1231. */
  1232. String kind = FoldingRangeKind::Region;
  1233. _FORCE_INLINE_ Dictionary to_json() const {
  1234. Dictionary dict;
  1235. dict["startLine"] = startLine;
  1236. dict["startCharacter"] = startCharacter;
  1237. dict["endLine"] = endLine;
  1238. dict["endCharacter"] = endCharacter;
  1239. return dict;
  1240. }
  1241. };
  1242. // Use namespace instead of enumeration to follow the LSP specifications
  1243. /**
  1244. * How a completion was triggered
  1245. */
  1246. namespace CompletionTriggerKind {
  1247. /**
  1248. * Completion was triggered by typing an identifier (24x7 code
  1249. * complete), manual invocation (e.g Ctrl+Space) or via API.
  1250. */
  1251. static const int Invoked = 1;
  1252. /**
  1253. * Completion was triggered by a trigger character specified by
  1254. * the `triggerCharacters` properties of the `CompletionRegistrationOptions`.
  1255. */
  1256. static const int TriggerCharacter = 2;
  1257. /**
  1258. * Completion was re-triggered as the current completion list is incomplete.
  1259. */
  1260. static const int TriggerForIncompleteCompletions = 3;
  1261. } // namespace CompletionTriggerKind
  1262. /**
  1263. * Contains additional information about the context in which a completion request is triggered.
  1264. */
  1265. struct CompletionContext {
  1266. /**
  1267. * How the completion was triggered.
  1268. */
  1269. int triggerKind = CompletionTriggerKind::TriggerCharacter;
  1270. /**
  1271. * The trigger character (a single character) that has trigger code complete.
  1272. * Is undefined if `triggerKind !== CompletionTriggerKind.TriggerCharacter`
  1273. */
  1274. String triggerCharacter;
  1275. void load(const Dictionary &p_params) {
  1276. triggerKind = int(p_params["triggerKind"]);
  1277. triggerCharacter = p_params.get("triggerCharacter", "");
  1278. }
  1279. };
  1280. struct CompletionParams : public TextDocumentPositionParams {
  1281. /**
  1282. * The completion context. This is only available if the client specifies
  1283. * to send this using `ClientCapabilities.textDocument.completion.contextSupport === true`
  1284. */
  1285. CompletionContext context;
  1286. void load(const Dictionary &p_params) {
  1287. TextDocumentPositionParams::load(p_params);
  1288. context.load(p_params["context"]);
  1289. }
  1290. Dictionary to_json() {
  1291. Dictionary ctx;
  1292. ctx["triggerCharacter"] = context.triggerCharacter;
  1293. ctx["triggerKind"] = context.triggerKind;
  1294. Dictionary dict;
  1295. dict = TextDocumentPositionParams::to_json();
  1296. dict["context"] = ctx;
  1297. return dict;
  1298. }
  1299. };
  1300. /**
  1301. * The result of a hover request.
  1302. */
  1303. struct Hover {
  1304. /**
  1305. * The hover's content
  1306. */
  1307. MarkupContent contents;
  1308. /**
  1309. * An optional range is a range inside a text document
  1310. * that is used to visualize a hover, e.g. by changing the background color.
  1311. */
  1312. Range range;
  1313. _FORCE_INLINE_ Dictionary to_json() const {
  1314. Dictionary dict;
  1315. dict["range"] = range.to_json();
  1316. dict["contents"] = contents.to_json();
  1317. return dict;
  1318. }
  1319. };
  1320. /**
  1321. * Represents a parameter of a callable-signature. A parameter can
  1322. * have a label and a doc-comment.
  1323. */
  1324. struct ParameterInformation {
  1325. /**
  1326. * The label of this parameter information.
  1327. *
  1328. * Either a string or an inclusive start and exclusive end offsets within its containing
  1329. * signature label. (see SignatureInformation.label). The offsets are based on a UTF-16
  1330. * string representation as `Position` and `Range` does.
  1331. *
  1332. * *Note*: a label of type string should be a substring of its containing signature label.
  1333. * Its intended use case is to highlight the parameter label part in the `SignatureInformation.label`.
  1334. */
  1335. String label;
  1336. /**
  1337. * The human-readable doc-comment of this parameter. Will be shown
  1338. * in the UI but can be omitted.
  1339. */
  1340. MarkupContent documentation;
  1341. Dictionary to_json() const {
  1342. Dictionary dict;
  1343. dict["label"] = label;
  1344. dict["documentation"] = documentation.to_json();
  1345. return dict;
  1346. }
  1347. };
  1348. /**
  1349. * Represents the signature of something callable. A signature
  1350. * can have a label, like a function-name, a doc-comment, and
  1351. * a set of parameters.
  1352. */
  1353. struct SignatureInformation {
  1354. /**
  1355. * The label of this signature. Will be shown in
  1356. * the UI.
  1357. */
  1358. String label;
  1359. /**
  1360. * The human-readable doc-comment of this signature. Will be shown
  1361. * in the UI but can be omitted.
  1362. */
  1363. MarkupContent documentation;
  1364. /**
  1365. * The parameters of this signature.
  1366. */
  1367. Vector<ParameterInformation> parameters;
  1368. Dictionary to_json() const {
  1369. Dictionary dict;
  1370. dict["label"] = label;
  1371. dict["documentation"] = documentation.to_json();
  1372. Array args;
  1373. for (int i = 0; i < parameters.size(); i++) {
  1374. args.push_back(parameters[i].to_json());
  1375. }
  1376. dict["parameters"] = args;
  1377. return dict;
  1378. }
  1379. };
  1380. /**
  1381. * Signature help represents the signature of something
  1382. * callable. There can be multiple signature but only one
  1383. * active and only one active parameter.
  1384. */
  1385. struct SignatureHelp {
  1386. /**
  1387. * One or more signatures.
  1388. */
  1389. Vector<SignatureInformation> signatures;
  1390. /**
  1391. * The active signature. If omitted or the value lies outside the
  1392. * range of `signatures` the value defaults to zero or is ignored if
  1393. * `signatures.length === 0`. Whenever possible implementers should
  1394. * make an active decision about the active signature and shouldn't
  1395. * rely on a default value.
  1396. * In future version of the protocol this property might become
  1397. * mandatory to better express this.
  1398. */
  1399. int activeSignature = 0;
  1400. /**
  1401. * The active parameter of the active signature. If omitted or the value
  1402. * lies outside the range of `signatures[activeSignature].parameters`
  1403. * defaults to 0 if the active signature has parameters. If
  1404. * the active signature has no parameters it is ignored.
  1405. * In future version of the protocol this property might become
  1406. * mandatory to better express the active parameter if the
  1407. * active signature does have any.
  1408. */
  1409. int activeParameter = 0;
  1410. Dictionary to_json() const {
  1411. Dictionary dict;
  1412. Array sigs;
  1413. for (int i = 0; i < signatures.size(); i++) {
  1414. sigs.push_back(signatures[i].to_json());
  1415. }
  1416. dict["signatures"] = sigs;
  1417. dict["activeSignature"] = activeSignature;
  1418. dict["activeParameter"] = activeParameter;
  1419. return dict;
  1420. }
  1421. };
  1422. /**
  1423. * A pattern to describe in which file operation requests or notifications
  1424. * the server is interested in.
  1425. */
  1426. struct FileOperationPattern {
  1427. /**
  1428. * The glob pattern to match.
  1429. */
  1430. String glob = "**/*.gd";
  1431. /**
  1432. * Whether to match `file`s or `folder`s with this pattern.
  1433. *
  1434. * Matches both if undefined.
  1435. */
  1436. String matches = "file";
  1437. Dictionary to_json() const {
  1438. Dictionary dict;
  1439. dict["glob"] = glob;
  1440. dict["matches"] = matches;
  1441. return dict;
  1442. }
  1443. };
  1444. /**
  1445. * A filter to describe in which file operation requests or notifications
  1446. * the server is interested in.
  1447. */
  1448. struct FileOperationFilter {
  1449. /**
  1450. * The actual file operation pattern.
  1451. */
  1452. FileOperationPattern pattern;
  1453. Dictionary to_json() const {
  1454. Dictionary dict;
  1455. dict["pattern"] = pattern.to_json();
  1456. return dict;
  1457. }
  1458. };
  1459. /**
  1460. * The options to register for file operations.
  1461. */
  1462. struct FileOperationRegistrationOptions {
  1463. /**
  1464. * The actual filters.
  1465. */
  1466. Vector<FileOperationFilter> filters;
  1467. FileOperationRegistrationOptions() {
  1468. filters.push_back(FileOperationFilter());
  1469. }
  1470. Dictionary to_json() const {
  1471. Dictionary dict;
  1472. Array filts;
  1473. for (int i = 0; i < filters.size(); i++) {
  1474. filts.push_back(filters[i].to_json());
  1475. }
  1476. dict["filters"] = filts;
  1477. return dict;
  1478. }
  1479. };
  1480. /**
  1481. * The server is interested in file notifications/requests.
  1482. */
  1483. struct FileOperations {
  1484. /**
  1485. * The server is interested in receiving didDeleteFiles file notifications.
  1486. */
  1487. FileOperationRegistrationOptions didDelete;
  1488. Dictionary to_json() const {
  1489. Dictionary dict;
  1490. dict["didDelete"] = didDelete.to_json();
  1491. return dict;
  1492. }
  1493. };
  1494. /**
  1495. * Workspace specific server capabilities
  1496. */
  1497. struct Workspace {
  1498. /**
  1499. * The server is interested in file notifications/requests.
  1500. */
  1501. FileOperations fileOperations;
  1502. Dictionary to_json() const {
  1503. Dictionary dict;
  1504. dict["fileOperations"] = fileOperations.to_json();
  1505. return dict;
  1506. }
  1507. };
  1508. struct ServerCapabilities {
  1509. /**
  1510. * Defines how text documents are synced. Is either a detailed structure defining each notification or
  1511. * for backwards compatibility the TextDocumentSyncKind number. If omitted it defaults to `TextDocumentSyncKind.None`.
  1512. */
  1513. TextDocumentSyncOptions textDocumentSync;
  1514. /**
  1515. * The server provides hover support.
  1516. */
  1517. bool hoverProvider = true;
  1518. /**
  1519. * The server provides completion support.
  1520. */
  1521. CompletionOptions completionProvider;
  1522. /**
  1523. * The server provides signature help support.
  1524. */
  1525. SignatureHelpOptions signatureHelpProvider;
  1526. /**
  1527. * The server provides goto definition support.
  1528. */
  1529. bool definitionProvider = true;
  1530. /**
  1531. * The server provides Goto Type Definition support.
  1532. *
  1533. * Since 3.6.0
  1534. */
  1535. bool typeDefinitionProvider = false;
  1536. /**
  1537. * The server provides Goto Implementation support.
  1538. *
  1539. * Since 3.6.0
  1540. */
  1541. bool implementationProvider = false;
  1542. /**
  1543. * The server provides find references support.
  1544. */
  1545. bool referencesProvider = true;
  1546. /**
  1547. * The server provides document highlight support.
  1548. */
  1549. bool documentHighlightProvider = false;
  1550. /**
  1551. * The server provides document symbol support.
  1552. */
  1553. bool documentSymbolProvider = true;
  1554. /**
  1555. * The server provides workspace symbol support.
  1556. */
  1557. bool workspaceSymbolProvider = false;
  1558. /**
  1559. * The server supports workspace folder.
  1560. */
  1561. Workspace workspace;
  1562. /**
  1563. * The server provides code actions. The `CodeActionOptions` return type is only
  1564. * valid if the client signals code action literal support via the property
  1565. * `textDocument.codeAction.codeActionLiteralSupport`.
  1566. */
  1567. bool codeActionProvider = false;
  1568. /**
  1569. * The server provides code lens.
  1570. */
  1571. CodeLensOptions codeLensProvider;
  1572. /**
  1573. * The server provides document formatting.
  1574. */
  1575. bool documentFormattingProvider = false;
  1576. /**
  1577. * The server provides document range formatting.
  1578. */
  1579. bool documentRangeFormattingProvider = false;
  1580. /**
  1581. * The server provides document formatting on typing.
  1582. */
  1583. DocumentOnTypeFormattingOptions documentOnTypeFormattingProvider;
  1584. /**
  1585. * The server provides rename support. RenameOptions may only be
  1586. * specified if the client states that it supports
  1587. * `prepareSupport` in its initial `initialize` request.
  1588. */
  1589. RenameOptions renameProvider;
  1590. /**
  1591. * The server provides document link support.
  1592. */
  1593. DocumentLinkOptions documentLinkProvider;
  1594. /**
  1595. * The server provides color provider support.
  1596. *
  1597. * Since 3.6.0
  1598. */
  1599. ColorProviderOptions colorProvider;
  1600. /**
  1601. * The server provides folding provider support.
  1602. *
  1603. * Since 3.10.0
  1604. */
  1605. FoldingRangeProviderOptions foldingRangeProvider;
  1606. /**
  1607. * The server provides go to declaration support.
  1608. *
  1609. * Since 3.14.0
  1610. */
  1611. bool declarationProvider = true;
  1612. /**
  1613. * The server provides execute command support.
  1614. */
  1615. ExecuteCommandOptions executeCommandProvider;
  1616. _FORCE_INLINE_ Dictionary to_json() {
  1617. Dictionary dict;
  1618. dict["textDocumentSync"] = textDocumentSync.to_json();
  1619. dict["completionProvider"] = completionProvider.to_json();
  1620. signatureHelpProvider.triggerCharacters.push_back(",");
  1621. signatureHelpProvider.triggerCharacters.push_back("(");
  1622. dict["signatureHelpProvider"] = signatureHelpProvider.to_json();
  1623. //dict["codeLensProvider"] = codeLensProvider.to_json();
  1624. dict["documentOnTypeFormattingProvider"] = documentOnTypeFormattingProvider.to_json();
  1625. dict["renameProvider"] = renameProvider.to_json();
  1626. dict["documentLinkProvider"] = documentLinkProvider.to_json();
  1627. dict["colorProvider"] = false; // colorProvider.to_json();
  1628. dict["foldingRangeProvider"] = false; //foldingRangeProvider.to_json();
  1629. dict["executeCommandProvider"] = executeCommandProvider.to_json();
  1630. dict["hoverProvider"] = hoverProvider;
  1631. dict["definitionProvider"] = definitionProvider;
  1632. dict["typeDefinitionProvider"] = typeDefinitionProvider;
  1633. dict["implementationProvider"] = implementationProvider;
  1634. dict["referencesProvider"] = referencesProvider;
  1635. dict["documentHighlightProvider"] = documentHighlightProvider;
  1636. dict["documentSymbolProvider"] = documentSymbolProvider;
  1637. dict["workspaceSymbolProvider"] = workspaceSymbolProvider;
  1638. dict["workspace"] = workspace.to_json();
  1639. dict["codeActionProvider"] = codeActionProvider;
  1640. dict["documentFormattingProvider"] = documentFormattingProvider;
  1641. dict["documentRangeFormattingProvider"] = documentRangeFormattingProvider;
  1642. dict["declarationProvider"] = declarationProvider;
  1643. return dict;
  1644. }
  1645. };
  1646. struct InitializeResult {
  1647. /**
  1648. * The capabilities the language server provides.
  1649. */
  1650. ServerCapabilities capabilities;
  1651. _FORCE_INLINE_ Dictionary to_json() {
  1652. Dictionary dict;
  1653. dict["capabilities"] = capabilities.to_json();
  1654. return dict;
  1655. }
  1656. };
  1657. struct GodotNativeClassInfo {
  1658. String name;
  1659. const DocData::ClassDoc *class_doc = nullptr;
  1660. const ClassDB::ClassInfo *class_info = nullptr;
  1661. Dictionary to_json() const {
  1662. Dictionary dict;
  1663. dict["name"] = name;
  1664. dict["inherits"] = class_doc->inherits;
  1665. return dict;
  1666. }
  1667. };
  1668. /** Features not included in the standard lsp specifications */
  1669. struct GodotCapabilities {
  1670. /**
  1671. * Native class list
  1672. */
  1673. List<GodotNativeClassInfo> native_classes;
  1674. Dictionary to_json() const {
  1675. Dictionary dict;
  1676. Array classes;
  1677. for (const GodotNativeClassInfo &native_class : native_classes) {
  1678. classes.push_back(native_class.to_json());
  1679. }
  1680. dict["native_classes"] = classes;
  1681. return dict;
  1682. }
  1683. };
  1684. /** Format BBCode documentation from DocData to markdown */
  1685. static String marked_documentation(const String &p_bbcode) {
  1686. String markdown = p_bbcode.strip_edges();
  1687. Vector<String> lines = markdown.split("\n");
  1688. bool in_code_block = false;
  1689. int code_block_indent = -1;
  1690. markdown = "";
  1691. for (int i = 0; i < lines.size(); i++) {
  1692. String line = lines[i];
  1693. int block_start = line.find("[codeblock]");
  1694. if (block_start != -1) {
  1695. code_block_indent = block_start;
  1696. in_code_block = true;
  1697. line = "\n";
  1698. } else if (in_code_block) {
  1699. line = "\t" + line.substr(code_block_indent);
  1700. }
  1701. if (in_code_block && line.contains("[/codeblock]")) {
  1702. line = "\n";
  1703. in_code_block = false;
  1704. }
  1705. if (!in_code_block) {
  1706. line = line.strip_edges();
  1707. line = line.replace("[code]", "`");
  1708. line = line.replace("[/code]", "`");
  1709. line = line.replace("[i]", "*");
  1710. line = line.replace("[/i]", "*");
  1711. line = line.replace("[b]", "**");
  1712. line = line.replace("[/b]", "**");
  1713. line = line.replace("[u]", "__");
  1714. line = line.replace("[/u]", "__");
  1715. line = line.replace("[method ", "`");
  1716. line = line.replace("[member ", "`");
  1717. line = line.replace("[signal ", "`");
  1718. line = line.replace("[enum ", "`");
  1719. line = line.replace("[constant ", "`");
  1720. line = line.replace_chars("[]", '`');
  1721. }
  1722. if (!in_code_block && i < lines.size() - 1) {
  1723. line += "\n\n";
  1724. } else if (i < lines.size() - 1) {
  1725. line += "\n";
  1726. }
  1727. markdown += line;
  1728. }
  1729. return markdown;
  1730. }
  1731. } // namespace LSP