godot_lsp.h 49 KB

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