godot_lsp.h 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  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["languageId"];
  600. version = p_dict["version"];
  601. text = p_dict["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. If range and rangeLength are omitted
  614. * the new text is considered to be the full content of the document.
  615. */
  616. struct TextDocumentContentChangeEvent {
  617. /**
  618. * The range of the document that changed.
  619. */
  620. Range range;
  621. /**
  622. * The length of the range that got replaced.
  623. */
  624. int rangeLength = 0;
  625. /**
  626. * The new text of the range/document.
  627. */
  628. String text;
  629. void load(const Dictionary &p_params) {
  630. text = p_params["text"];
  631. rangeLength = p_params["rangeLength"];
  632. range.load(p_params["range"]);
  633. }
  634. };
  635. // Use namespace instead of enumeration to follow the LSP specifications
  636. namespace DiagnosticSeverity {
  637. /**
  638. * Reports an error.
  639. */
  640. static const int Error = 1;
  641. /**
  642. * Reports a warning.
  643. */
  644. static const int Warning = 2;
  645. /**
  646. * Reports an information.
  647. */
  648. static const int Information = 3;
  649. /**
  650. * Reports a hint.
  651. */
  652. static const int Hint = 4;
  653. }; // namespace DiagnosticSeverity
  654. /**
  655. * Represents a related message and source code location for a diagnostic. This should be
  656. * used to point to code locations that cause or related to a diagnostics, e.g when duplicating
  657. * a symbol in a scope.
  658. */
  659. struct DiagnosticRelatedInformation {
  660. /**
  661. * The location of this related diagnostic information.
  662. */
  663. Location location;
  664. /**
  665. * The message of this related diagnostic information.
  666. */
  667. String message;
  668. Dictionary to_json() const {
  669. Dictionary dict;
  670. dict["location"] = location.to_json();
  671. dict["message"] = message;
  672. return dict;
  673. }
  674. };
  675. /**
  676. * Represents a diagnostic, such as a compiler error or warning.
  677. * Diagnostic objects are only valid in the scope of a resource.
  678. */
  679. struct Diagnostic {
  680. /**
  681. * The range at which the message applies.
  682. */
  683. Range range;
  684. /**
  685. * The diagnostic's severity. Can be omitted. If omitted it is up to the
  686. * client to interpret diagnostics as error, warning, info or hint.
  687. */
  688. int severity = 0;
  689. /**
  690. * The diagnostic's code, which might appear in the user interface.
  691. */
  692. int code = 0;
  693. /**
  694. * A human-readable string describing the source of this
  695. * diagnostic, e.g. 'typescript' or 'super lint'.
  696. */
  697. String source;
  698. /**
  699. * The diagnostic's message.
  700. */
  701. String message;
  702. /**
  703. * An array of related diagnostic information, e.g. when symbol-names within
  704. * a scope collide all definitions can be marked via this property.
  705. */
  706. Vector<DiagnosticRelatedInformation> relatedInformation;
  707. Dictionary to_json() const {
  708. Dictionary dict;
  709. dict["range"] = range.to_json();
  710. dict["code"] = code;
  711. dict["severity"] = severity;
  712. dict["message"] = message;
  713. dict["source"] = source;
  714. if (!relatedInformation.is_empty()) {
  715. Array arr;
  716. arr.resize(relatedInformation.size());
  717. for (int i = 0; i < relatedInformation.size(); i++) {
  718. arr[i] = relatedInformation[i].to_json();
  719. }
  720. dict["relatedInformation"] = arr;
  721. }
  722. return dict;
  723. }
  724. };
  725. // Use namespace instead of enumeration to follow the LSP specifications
  726. /**
  727. * Describes the content type that a client supports in various
  728. * result literals like `Hover`, `ParameterInfo` or `CompletionItem`.
  729. *
  730. * Please note that `MarkupKinds` must not start with a `$`. This kinds
  731. * are reserved for internal usage.
  732. */
  733. namespace MarkupKind {
  734. static const String PlainText = "plaintext";
  735. static const String Markdown = "markdown";
  736. }; // namespace MarkupKind
  737. /**
  738. * A `MarkupContent` literal represents a string value which content is interpreted base on its
  739. * kind flag. Currently the protocol supports `plaintext` and `markdown` as markup kinds.
  740. *
  741. * If the kind is `markdown` then the value can contain fenced code blocks like in GitHub issues.
  742. * See https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting
  743. *
  744. * Here is an example how such a string can be constructed using JavaScript / TypeScript:
  745. * ```typescript
  746. * let markdown: MarkdownContent = {
  747. * kind: MarkupKind.Markdown,
  748. * value: [
  749. * '# Header',
  750. * 'Some text',
  751. * '```typescript',
  752. * 'someCode();',
  753. * '```'
  754. * ].join('\n')
  755. * };
  756. * ```
  757. *
  758. * *Please Note* that clients might sanitize the return markdown. A client could decide to
  759. * remove HTML from the markdown to avoid script execution.
  760. */
  761. struct MarkupContent {
  762. /**
  763. * The type of the Markup.
  764. */
  765. String kind;
  766. /**
  767. * The content itself.
  768. */
  769. String value;
  770. MarkupContent() {
  771. kind = MarkupKind::Markdown;
  772. }
  773. MarkupContent(const String &p_value) {
  774. value = p_value;
  775. kind = MarkupKind::Markdown;
  776. }
  777. Dictionary to_json() const {
  778. Dictionary dict;
  779. dict["kind"] = kind;
  780. dict["value"] = value;
  781. return dict;
  782. }
  783. };
  784. // Use namespace instead of enumeration to follow the LSP specifications
  785. // `LSP::EnumName::EnumValue` is OK but `LSP::EnumValue` is not.
  786. // And here C++ compilers are unhappy with our enumeration name like `Color`, `File`, `RefCounted` etc.
  787. /**
  788. * The kind of a completion entry.
  789. */
  790. namespace CompletionItemKind {
  791. static const int Text = 1;
  792. static const int Method = 2;
  793. static const int Function = 3;
  794. static const int Constructor = 4;
  795. static const int Field = 5;
  796. static const int Variable = 6;
  797. static const int Class = 7;
  798. static const int Interface = 8;
  799. static const int Module = 9;
  800. static const int Property = 10;
  801. static const int Unit = 11;
  802. static const int Value = 12;
  803. static const int Enum = 13;
  804. static const int Keyword = 14;
  805. static const int Snippet = 15;
  806. static const int Color = 16;
  807. static const int File = 17;
  808. static const int RefCounted = 18;
  809. static const int Folder = 19;
  810. static const int EnumMember = 20;
  811. static const int Constant = 21;
  812. static const int Struct = 22;
  813. static const int Event = 23;
  814. static const int Operator = 24;
  815. static const int TypeParameter = 25;
  816. }; // namespace CompletionItemKind
  817. // Use namespace instead of enumeration to follow the LSP specifications.
  818. /**
  819. * Defines whether the insert text in a completion item should be interpreted as
  820. * plain text or a snippet.
  821. */
  822. namespace InsertTextFormat {
  823. /**
  824. * The primary text to be inserted is treated as a plain string.
  825. */
  826. static const int PlainText = 1;
  827. /**
  828. * The primary text to be inserted is treated as a snippet.
  829. *
  830. * A snippet can define tab stops and placeholders with `$1`, `$2`
  831. * and `${3:foo}`. `$0` defines the final tab stop, it defaults to
  832. * the end of the snippet. Placeholders with equal identifiers are linked,
  833. * that is typing in one will update others too.
  834. */
  835. static const int Snippet = 2;
  836. }; // namespace InsertTextFormat
  837. struct CompletionItem {
  838. /**
  839. * The label of this completion item. By default
  840. * also the text that is inserted when selecting
  841. * this completion.
  842. */
  843. String label;
  844. /**
  845. * The kind of this completion item. Based of the kind
  846. * an icon is chosen by the editor. The standardized set
  847. * of available values is defined in `CompletionItemKind`.
  848. */
  849. int kind = 0;
  850. /**
  851. * A human-readable string with additional information
  852. * about this item, like type or symbol information.
  853. */
  854. String detail;
  855. /**
  856. * A human-readable string that represents a doc-comment.
  857. */
  858. MarkupContent documentation;
  859. /**
  860. * Indicates if this item is deprecated.
  861. */
  862. bool deprecated = false;
  863. /**
  864. * Select this item when showing.
  865. *
  866. * *Note* that only one completion item can be selected and that the
  867. * tool / client decides which item that is. The rule is that the *first*
  868. * item of those that match best is selected.
  869. */
  870. bool preselect = false;
  871. /**
  872. * A string that should be used when comparing this item
  873. * with other items. When omitted the label is used
  874. * as the filter text for this item.
  875. */
  876. String sortText;
  877. /**
  878. * A string that should be used when filtering a set of
  879. * completion items. When omitted the label is used as the
  880. * filter text for this item.
  881. */
  882. String filterText;
  883. /**
  884. * A string that should be inserted into a document when selecting
  885. * this completion. When omitted the label is used as the insert text
  886. * for this item.
  887. *
  888. * The `insertText` is subject to interpretation by the client side.
  889. * Some tools might not take the string literally. For example
  890. * VS Code when code complete is requested in this example
  891. * `con<cursor position>` and a completion item with an `insertText` of
  892. * `console` is provided it will only insert `sole`. Therefore it is
  893. * recommended to use `textEdit` instead since it avoids additional client
  894. * side interpretation.
  895. */
  896. String insertText;
  897. /**
  898. * The format of the insert text. The format applies to both the `insertText` property
  899. * and the `newText` property of a provided `textEdit`.
  900. */
  901. int insertTextFormat = 0;
  902. /**
  903. * An edit which is applied to a document when selecting this completion. When an edit is provided the value of
  904. * `insertText` is ignored.
  905. *
  906. * *Note:* The range of the edit must be a single line range and it must contain the position at which completion
  907. * has been requested.
  908. */
  909. TextEdit textEdit;
  910. /**
  911. * An optional array of additional text edits that are applied when
  912. * selecting this completion. Edits must not overlap (including the same insert position)
  913. * with the main edit nor with themselves.
  914. *
  915. * Additional text edits should be used to change text unrelated to the current cursor position
  916. * (for example adding an import statement at the top of the file if the completion item will
  917. * insert an unqualified type).
  918. */
  919. Vector<TextEdit> additionalTextEdits;
  920. /**
  921. * An optional set of characters that when pressed while this completion is active will accept it first and
  922. * then type that character. *Note* that all commit characters should have `length=1` and that superfluous
  923. * characters will be ignored.
  924. */
  925. Vector<String> commitCharacters;
  926. /**
  927. * An optional command that is executed *after* inserting this completion. *Note* that
  928. * additional modifications to the current document should be described with the
  929. * additionalTextEdits-property.
  930. */
  931. Command command;
  932. /**
  933. * A data entry field that is preserved on a completion item between
  934. * a completion and a completion resolve request.
  935. */
  936. Variant data;
  937. _FORCE_INLINE_ Dictionary to_json(bool resolved = false) const {
  938. Dictionary dict;
  939. dict["label"] = label;
  940. dict["kind"] = kind;
  941. dict["data"] = data;
  942. if (!insertText.is_empty()) {
  943. dict["insertText"] = insertText;
  944. }
  945. if (resolved) {
  946. dict["detail"] = detail;
  947. dict["documentation"] = documentation.to_json();
  948. dict["deprecated"] = deprecated;
  949. dict["preselect"] = preselect;
  950. if (!sortText.is_empty()) {
  951. dict["sortText"] = sortText;
  952. }
  953. if (!filterText.is_empty()) {
  954. dict["filterText"] = filterText;
  955. }
  956. if (commitCharacters.size()) {
  957. dict["commitCharacters"] = commitCharacters;
  958. }
  959. if (!command.command.is_empty()) {
  960. dict["command"] = command.to_json();
  961. }
  962. }
  963. return dict;
  964. }
  965. void load(const Dictionary &p_dict) {
  966. if (p_dict.has("label")) {
  967. label = p_dict["label"];
  968. }
  969. if (p_dict.has("kind")) {
  970. kind = p_dict["kind"];
  971. }
  972. if (p_dict.has("detail")) {
  973. detail = p_dict["detail"];
  974. }
  975. if (p_dict.has("documentation")) {
  976. Variant doc = p_dict["documentation"];
  977. if (doc.is_string()) {
  978. documentation.value = doc;
  979. } else if (doc.get_type() == Variant::DICTIONARY) {
  980. Dictionary v = doc;
  981. documentation.value = v["value"];
  982. }
  983. }
  984. if (p_dict.has("deprecated")) {
  985. deprecated = p_dict["deprecated"];
  986. }
  987. if (p_dict.has("preselect")) {
  988. preselect = p_dict["preselect"];
  989. }
  990. if (p_dict.has("sortText")) {
  991. sortText = p_dict["sortText"];
  992. }
  993. if (p_dict.has("filterText")) {
  994. filterText = p_dict["filterText"];
  995. }
  996. if (p_dict.has("insertText")) {
  997. insertText = p_dict["insertText"];
  998. }
  999. if (p_dict.has("data")) {
  1000. data = p_dict["data"];
  1001. }
  1002. }
  1003. };
  1004. /**
  1005. * Represents a collection of [completion items](#CompletionItem) to be presented
  1006. * in the editor.
  1007. */
  1008. struct CompletionList {
  1009. /**
  1010. * This list it not complete. Further typing should result in recomputing
  1011. * this list.
  1012. */
  1013. bool isIncomplete = false;
  1014. /**
  1015. * The completion items.
  1016. */
  1017. Vector<CompletionItem> items;
  1018. };
  1019. // Use namespace instead of enumeration to follow the LSP specifications
  1020. // `LSP::EnumName::EnumValue` is OK but `LSP::EnumValue` is not
  1021. // And here C++ compilers are unhappy with our enumeration name like `String`, `Array`, `Object` etc
  1022. /**
  1023. * A symbol kind.
  1024. */
  1025. namespace SymbolKind {
  1026. static const int File = 1;
  1027. static const int Module = 2;
  1028. static const int Namespace = 3;
  1029. static const int Package = 4;
  1030. static const int Class = 5;
  1031. static const int Method = 6;
  1032. static const int Property = 7;
  1033. static const int Field = 8;
  1034. static const int Constructor = 9;
  1035. static const int Enum = 10;
  1036. static const int Interface = 11;
  1037. static const int Function = 12;
  1038. static const int Variable = 13;
  1039. static const int Constant = 14;
  1040. static const int String = 15;
  1041. static const int Number = 16;
  1042. static const int Boolean = 17;
  1043. static const int Array = 18;
  1044. static const int Object = 19;
  1045. static const int Key = 20;
  1046. static const int Null = 21;
  1047. static const int EnumMember = 22;
  1048. static const int Struct = 23;
  1049. static const int Event = 24;
  1050. static const int Operator = 25;
  1051. static const int TypeParameter = 26;
  1052. }; // namespace SymbolKind
  1053. /**
  1054. * Represents programming constructs like variables, classes, interfaces etc. that appear in a document. Document symbols can be
  1055. * hierarchical and they have two ranges: one that encloses its definition and one that points to its most interesting range,
  1056. * e.g. the range of an identifier.
  1057. */
  1058. struct DocumentSymbol {
  1059. /**
  1060. * The name of this symbol. Will be displayed in the user interface and therefore must not be
  1061. * an empty string or a string only consisting of white spaces.
  1062. */
  1063. String name;
  1064. /**
  1065. * More detail for this symbol, e.g the signature of a function.
  1066. */
  1067. String detail;
  1068. /**
  1069. * Documentation for this symbol.
  1070. */
  1071. String documentation;
  1072. /**
  1073. * Class name for the native symbols.
  1074. */
  1075. String native_class;
  1076. /**
  1077. * The kind of this symbol.
  1078. */
  1079. int kind = SymbolKind::File;
  1080. /**
  1081. * Indicates if this symbol is deprecated.
  1082. */
  1083. bool deprecated = false;
  1084. /**
  1085. * If `true`: Symbol is local to script and cannot be accessed somewhere else.
  1086. *
  1087. * For example: local variable inside a `func`.
  1088. */
  1089. bool local = false;
  1090. /**
  1091. * The range enclosing this symbol not including leading/trailing whitespace but everything else
  1092. * like comments. This information is typically used to determine if the clients cursor is
  1093. * inside the symbol to reveal in the symbol in the UI.
  1094. */
  1095. Range range;
  1096. /**
  1097. * The range that should be selected and revealed when this symbol is being picked, e.g the name of a function.
  1098. * Must be contained by the `range`.
  1099. */
  1100. Range selectionRange;
  1101. DocumentUri uri;
  1102. String script_path;
  1103. /**
  1104. * Children of this symbol, e.g. properties of a class.
  1105. */
  1106. Vector<DocumentSymbol> children;
  1107. Dictionary to_json(bool with_doc = false) const {
  1108. Dictionary dict;
  1109. dict["name"] = name;
  1110. dict["detail"] = detail;
  1111. dict["kind"] = kind;
  1112. dict["deprecated"] = deprecated;
  1113. dict["range"] = range.to_json();
  1114. dict["selectionRange"] = selectionRange.to_json();
  1115. if (with_doc) {
  1116. dict["documentation"] = documentation;
  1117. dict["native_class"] = native_class;
  1118. }
  1119. if (!children.is_empty()) {
  1120. Array arr;
  1121. for (int i = 0; i < children.size(); i++) {
  1122. if (children[i].local) {
  1123. continue;
  1124. }
  1125. arr.push_back(children[i].to_json(with_doc));
  1126. }
  1127. if (!children.is_empty()) {
  1128. dict["children"] = arr;
  1129. }
  1130. }
  1131. return dict;
  1132. }
  1133. _FORCE_INLINE_ MarkupContent render() const {
  1134. MarkupContent markdown;
  1135. if (detail.length()) {
  1136. markdown.value = "\t" + detail + "\n\n";
  1137. }
  1138. if (documentation.length()) {
  1139. markdown.value += marked_documentation(documentation) + "\n\n";
  1140. }
  1141. if (script_path.length()) {
  1142. markdown.value += "Defined in [" + script_path + "](" + uri + ")";
  1143. }
  1144. return markdown;
  1145. }
  1146. _FORCE_INLINE_ CompletionItem make_completion_item(bool resolved = false) const {
  1147. LSP::CompletionItem item;
  1148. item.label = name;
  1149. if (resolved) {
  1150. item.documentation = render();
  1151. }
  1152. switch (kind) {
  1153. case LSP::SymbolKind::Enum:
  1154. item.kind = LSP::CompletionItemKind::Enum;
  1155. break;
  1156. case LSP::SymbolKind::Class:
  1157. item.kind = LSP::CompletionItemKind::Class;
  1158. break;
  1159. case LSP::SymbolKind::Property:
  1160. item.kind = LSP::CompletionItemKind::Property;
  1161. break;
  1162. case LSP::SymbolKind::Method:
  1163. case LSP::SymbolKind::Function:
  1164. item.kind = LSP::CompletionItemKind::Method;
  1165. break;
  1166. case LSP::SymbolKind::Event:
  1167. item.kind = LSP::CompletionItemKind::Event;
  1168. break;
  1169. case LSP::SymbolKind::Constant:
  1170. item.kind = LSP::CompletionItemKind::Constant;
  1171. break;
  1172. case LSP::SymbolKind::Variable:
  1173. item.kind = LSP::CompletionItemKind::Variable;
  1174. break;
  1175. case LSP::SymbolKind::File:
  1176. item.kind = LSP::CompletionItemKind::File;
  1177. break;
  1178. default:
  1179. item.kind = LSP::CompletionItemKind::Text;
  1180. break;
  1181. }
  1182. return item;
  1183. }
  1184. };
  1185. struct ApplyWorkspaceEditParams {
  1186. WorkspaceEdit edit;
  1187. Dictionary to_json() {
  1188. Dictionary dict;
  1189. dict["edit"] = edit.to_json();
  1190. return dict;
  1191. }
  1192. };
  1193. struct NativeSymbolInspectParams {
  1194. String native_class;
  1195. String symbol_name;
  1196. void load(const Dictionary &p_params) {
  1197. native_class = p_params["native_class"];
  1198. symbol_name = p_params["symbol_name"];
  1199. }
  1200. };
  1201. /**
  1202. * Enum of known range kinds
  1203. */
  1204. namespace FoldingRangeKind {
  1205. /**
  1206. * Folding range for a comment
  1207. */
  1208. static const String Comment = "comment";
  1209. /**
  1210. * Folding range for a imports or includes
  1211. */
  1212. static const String Imports = "imports";
  1213. /**
  1214. * Folding range for a region (e.g. `#region`)
  1215. */
  1216. static const String Region = "region";
  1217. } // namespace FoldingRangeKind
  1218. /**
  1219. * Represents a folding range.
  1220. */
  1221. struct FoldingRange {
  1222. /**
  1223. * The zero-based line number from where the folded range starts.
  1224. */
  1225. int startLine = 0;
  1226. /**
  1227. * The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line.
  1228. */
  1229. int startCharacter = 0;
  1230. /**
  1231. * The zero-based line number where the folded range ends.
  1232. */
  1233. int endLine = 0;
  1234. /**
  1235. * The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line.
  1236. */
  1237. int endCharacter = 0;
  1238. /**
  1239. * Describes the kind of the folding range such as `comment' or 'region'. The kind
  1240. * is used to categorize folding ranges and used by commands like 'Fold all comments'. See
  1241. * [FoldingRangeKind](#FoldingRangeKind) for an enumeration of standardized kinds.
  1242. */
  1243. String kind = FoldingRangeKind::Region;
  1244. _FORCE_INLINE_ Dictionary to_json() const {
  1245. Dictionary dict;
  1246. dict["startLine"] = startLine;
  1247. dict["startCharacter"] = startCharacter;
  1248. dict["endLine"] = endLine;
  1249. dict["endCharacter"] = endCharacter;
  1250. return dict;
  1251. }
  1252. };
  1253. // Use namespace instead of enumeration to follow the LSP specifications
  1254. /**
  1255. * How a completion was triggered
  1256. */
  1257. namespace CompletionTriggerKind {
  1258. /**
  1259. * Completion was triggered by typing an identifier (24x7 code
  1260. * complete), manual invocation (e.g Ctrl+Space) or via API.
  1261. */
  1262. static const int Invoked = 1;
  1263. /**
  1264. * Completion was triggered by a trigger character specified by
  1265. * the `triggerCharacters` properties of the `CompletionRegistrationOptions`.
  1266. */
  1267. static const int TriggerCharacter = 2;
  1268. /**
  1269. * Completion was re-triggered as the current completion list is incomplete.
  1270. */
  1271. static const int TriggerForIncompleteCompletions = 3;
  1272. } // namespace CompletionTriggerKind
  1273. /**
  1274. * Contains additional information about the context in which a completion request is triggered.
  1275. */
  1276. struct CompletionContext {
  1277. /**
  1278. * How the completion was triggered.
  1279. */
  1280. int triggerKind = CompletionTriggerKind::TriggerCharacter;
  1281. /**
  1282. * The trigger character (a single character) that has trigger code complete.
  1283. * Is undefined if `triggerKind !== CompletionTriggerKind.TriggerCharacter`
  1284. */
  1285. String triggerCharacter;
  1286. void load(const Dictionary &p_params) {
  1287. triggerKind = int(p_params["triggerKind"]);
  1288. triggerCharacter = p_params["triggerCharacter"];
  1289. }
  1290. };
  1291. struct CompletionParams : public TextDocumentPositionParams {
  1292. /**
  1293. * The completion context. This is only available if the client specifies
  1294. * to send this using `ClientCapabilities.textDocument.completion.contextSupport === true`
  1295. */
  1296. CompletionContext context;
  1297. void load(const Dictionary &p_params) {
  1298. TextDocumentPositionParams::load(p_params);
  1299. context.load(p_params["context"]);
  1300. }
  1301. Dictionary to_json() {
  1302. Dictionary ctx;
  1303. ctx["triggerCharacter"] = context.triggerCharacter;
  1304. ctx["triggerKind"] = context.triggerKind;
  1305. Dictionary dict;
  1306. dict = TextDocumentPositionParams::to_json();
  1307. dict["context"] = ctx;
  1308. return dict;
  1309. }
  1310. };
  1311. /**
  1312. * The result of a hover request.
  1313. */
  1314. struct Hover {
  1315. /**
  1316. * The hover's content
  1317. */
  1318. MarkupContent contents;
  1319. /**
  1320. * An optional range is a range inside a text document
  1321. * that is used to visualize a hover, e.g. by changing the background color.
  1322. */
  1323. Range range;
  1324. _FORCE_INLINE_ Dictionary to_json() const {
  1325. Dictionary dict;
  1326. dict["range"] = range.to_json();
  1327. dict["contents"] = contents.to_json();
  1328. return dict;
  1329. }
  1330. };
  1331. /**
  1332. * Represents a parameter of a callable-signature. A parameter can
  1333. * have a label and a doc-comment.
  1334. */
  1335. struct ParameterInformation {
  1336. /**
  1337. * The label of this parameter information.
  1338. *
  1339. * Either a string or an inclusive start and exclusive end offsets within its containing
  1340. * signature label. (see SignatureInformation.label). The offsets are based on a UTF-16
  1341. * string representation as `Position` and `Range` does.
  1342. *
  1343. * *Note*: a label of type string should be a substring of its containing signature label.
  1344. * Its intended use case is to highlight the parameter label part in the `SignatureInformation.label`.
  1345. */
  1346. String label;
  1347. /**
  1348. * The human-readable doc-comment of this parameter. Will be shown
  1349. * in the UI but can be omitted.
  1350. */
  1351. MarkupContent documentation;
  1352. Dictionary to_json() const {
  1353. Dictionary dict;
  1354. dict["label"] = label;
  1355. dict["documentation"] = documentation.to_json();
  1356. return dict;
  1357. }
  1358. };
  1359. /**
  1360. * Represents the signature of something callable. A signature
  1361. * can have a label, like a function-name, a doc-comment, and
  1362. * a set of parameters.
  1363. */
  1364. struct SignatureInformation {
  1365. /**
  1366. * The label of this signature. Will be shown in
  1367. * the UI.
  1368. */
  1369. String label;
  1370. /**
  1371. * The human-readable doc-comment of this signature. Will be shown
  1372. * in the UI but can be omitted.
  1373. */
  1374. MarkupContent documentation;
  1375. /**
  1376. * The parameters of this signature.
  1377. */
  1378. Vector<ParameterInformation> parameters;
  1379. Dictionary to_json() const {
  1380. Dictionary dict;
  1381. dict["label"] = label;
  1382. dict["documentation"] = documentation.to_json();
  1383. Array args;
  1384. for (int i = 0; i < parameters.size(); i++) {
  1385. args.push_back(parameters[i].to_json());
  1386. }
  1387. dict["parameters"] = args;
  1388. return dict;
  1389. }
  1390. };
  1391. /**
  1392. * Signature help represents the signature of something
  1393. * callable. There can be multiple signature but only one
  1394. * active and only one active parameter.
  1395. */
  1396. struct SignatureHelp {
  1397. /**
  1398. * One or more signatures.
  1399. */
  1400. Vector<SignatureInformation> signatures;
  1401. /**
  1402. * The active signature. If omitted or the value lies outside the
  1403. * range of `signatures` the value defaults to zero or is ignored if
  1404. * `signatures.length === 0`. Whenever possible implementers should
  1405. * make an active decision about the active signature and shouldn't
  1406. * rely on a default value.
  1407. * In future version of the protocol this property might become
  1408. * mandatory to better express this.
  1409. */
  1410. int activeSignature = 0;
  1411. /**
  1412. * The active parameter of the active signature. If omitted or the value
  1413. * lies outside the range of `signatures[activeSignature].parameters`
  1414. * defaults to 0 if the active signature has parameters. If
  1415. * the active signature has no parameters it is ignored.
  1416. * In future version of the protocol this property might become
  1417. * mandatory to better express the active parameter if the
  1418. * active signature does have any.
  1419. */
  1420. int activeParameter = 0;
  1421. Dictionary to_json() const {
  1422. Dictionary dict;
  1423. Array sigs;
  1424. for (int i = 0; i < signatures.size(); i++) {
  1425. sigs.push_back(signatures[i].to_json());
  1426. }
  1427. dict["signatures"] = sigs;
  1428. dict["activeSignature"] = activeSignature;
  1429. dict["activeParameter"] = activeParameter;
  1430. return dict;
  1431. }
  1432. };
  1433. /**
  1434. * A pattern to describe in which file operation requests or notifications
  1435. * the server is interested in.
  1436. */
  1437. struct FileOperationPattern {
  1438. /**
  1439. * The glob pattern to match.
  1440. */
  1441. String glob = "**/*.gd";
  1442. /**
  1443. * Whether to match `file`s or `folder`s with this pattern.
  1444. *
  1445. * Matches both if undefined.
  1446. */
  1447. String matches = "file";
  1448. Dictionary to_json() const {
  1449. Dictionary dict;
  1450. dict["glob"] = glob;
  1451. dict["matches"] = matches;
  1452. return dict;
  1453. }
  1454. };
  1455. /**
  1456. * A filter to describe in which file operation requests or notifications
  1457. * the server is interested in.
  1458. */
  1459. struct FileOperationFilter {
  1460. /**
  1461. * The actual file operation pattern.
  1462. */
  1463. FileOperationPattern pattern;
  1464. Dictionary to_json() const {
  1465. Dictionary dict;
  1466. dict["pattern"] = pattern.to_json();
  1467. return dict;
  1468. }
  1469. };
  1470. /**
  1471. * The options to register for file operations.
  1472. */
  1473. struct FileOperationRegistrationOptions {
  1474. /**
  1475. * The actual filters.
  1476. */
  1477. Vector<FileOperationFilter> filters;
  1478. FileOperationRegistrationOptions() {
  1479. filters.push_back(FileOperationFilter());
  1480. }
  1481. Dictionary to_json() const {
  1482. Dictionary dict;
  1483. Array filts;
  1484. for (int i = 0; i < filters.size(); i++) {
  1485. filts.push_back(filters[i].to_json());
  1486. }
  1487. dict["filters"] = filts;
  1488. return dict;
  1489. }
  1490. };
  1491. /**
  1492. * The server is interested in file notifications/requests.
  1493. */
  1494. struct FileOperations {
  1495. /**
  1496. * The server is interested in receiving didDeleteFiles file notifications.
  1497. */
  1498. FileOperationRegistrationOptions didDelete;
  1499. Dictionary to_json() const {
  1500. Dictionary dict;
  1501. dict["didDelete"] = didDelete.to_json();
  1502. return dict;
  1503. }
  1504. };
  1505. /**
  1506. * Workspace specific server capabilities
  1507. */
  1508. struct Workspace {
  1509. /**
  1510. * The server is interested in file notifications/requests.
  1511. */
  1512. FileOperations fileOperations;
  1513. Dictionary to_json() const {
  1514. Dictionary dict;
  1515. dict["fileOperations"] = fileOperations.to_json();
  1516. return dict;
  1517. }
  1518. };
  1519. struct ServerCapabilities {
  1520. /**
  1521. * Defines how text documents are synced. Is either a detailed structure defining each notification or
  1522. * for backwards compatibility the TextDocumentSyncKind number. If omitted it defaults to `TextDocumentSyncKind.None`.
  1523. */
  1524. TextDocumentSyncOptions textDocumentSync;
  1525. /**
  1526. * The server provides hover support.
  1527. */
  1528. bool hoverProvider = true;
  1529. /**
  1530. * The server provides completion support.
  1531. */
  1532. CompletionOptions completionProvider;
  1533. /**
  1534. * The server provides signature help support.
  1535. */
  1536. SignatureHelpOptions signatureHelpProvider;
  1537. /**
  1538. * The server provides goto definition support.
  1539. */
  1540. bool definitionProvider = true;
  1541. /**
  1542. * The server provides Goto Type Definition support.
  1543. *
  1544. * Since 3.6.0
  1545. */
  1546. bool typeDefinitionProvider = false;
  1547. /**
  1548. * The server provides Goto Implementation support.
  1549. *
  1550. * Since 3.6.0
  1551. */
  1552. bool implementationProvider = false;
  1553. /**
  1554. * The server provides find references support.
  1555. */
  1556. bool referencesProvider = true;
  1557. /**
  1558. * The server provides document highlight support.
  1559. */
  1560. bool documentHighlightProvider = false;
  1561. /**
  1562. * The server provides document symbol support.
  1563. */
  1564. bool documentSymbolProvider = true;
  1565. /**
  1566. * The server provides workspace symbol support.
  1567. */
  1568. bool workspaceSymbolProvider = false;
  1569. /**
  1570. * The server supports workspace folder.
  1571. */
  1572. Workspace workspace;
  1573. /**
  1574. * The server provides code actions. The `CodeActionOptions` return type is only
  1575. * valid if the client signals code action literal support via the property
  1576. * `textDocument.codeAction.codeActionLiteralSupport`.
  1577. */
  1578. bool codeActionProvider = false;
  1579. /**
  1580. * The server provides code lens.
  1581. */
  1582. CodeLensOptions codeLensProvider;
  1583. /**
  1584. * The server provides document formatting.
  1585. */
  1586. bool documentFormattingProvider = false;
  1587. /**
  1588. * The server provides document range formatting.
  1589. */
  1590. bool documentRangeFormattingProvider = false;
  1591. /**
  1592. * The server provides document formatting on typing.
  1593. */
  1594. DocumentOnTypeFormattingOptions documentOnTypeFormattingProvider;
  1595. /**
  1596. * The server provides rename support. RenameOptions may only be
  1597. * specified if the client states that it supports
  1598. * `prepareSupport` in its initial `initialize` request.
  1599. */
  1600. RenameOptions renameProvider;
  1601. /**
  1602. * The server provides document link support.
  1603. */
  1604. DocumentLinkOptions documentLinkProvider;
  1605. /**
  1606. * The server provides color provider support.
  1607. *
  1608. * Since 3.6.0
  1609. */
  1610. ColorProviderOptions colorProvider;
  1611. /**
  1612. * The server provides folding provider support.
  1613. *
  1614. * Since 3.10.0
  1615. */
  1616. FoldingRangeProviderOptions foldingRangeProvider;
  1617. /**
  1618. * The server provides go to declaration support.
  1619. *
  1620. * Since 3.14.0
  1621. */
  1622. bool declarationProvider = true;
  1623. /**
  1624. * The server provides execute command support.
  1625. */
  1626. ExecuteCommandOptions executeCommandProvider;
  1627. _FORCE_INLINE_ Dictionary to_json() {
  1628. Dictionary dict;
  1629. dict["textDocumentSync"] = textDocumentSync.to_json();
  1630. dict["completionProvider"] = completionProvider.to_json();
  1631. signatureHelpProvider.triggerCharacters.push_back(",");
  1632. signatureHelpProvider.triggerCharacters.push_back("(");
  1633. dict["signatureHelpProvider"] = signatureHelpProvider.to_json();
  1634. //dict["codeLensProvider"] = codeLensProvider.to_json();
  1635. dict["documentOnTypeFormattingProvider"] = documentOnTypeFormattingProvider.to_json();
  1636. dict["renameProvider"] = renameProvider.to_json();
  1637. dict["documentLinkProvider"] = documentLinkProvider.to_json();
  1638. dict["colorProvider"] = false; // colorProvider.to_json();
  1639. dict["foldingRangeProvider"] = false; //foldingRangeProvider.to_json();
  1640. dict["executeCommandProvider"] = executeCommandProvider.to_json();
  1641. dict["hoverProvider"] = hoverProvider;
  1642. dict["definitionProvider"] = definitionProvider;
  1643. dict["typeDefinitionProvider"] = typeDefinitionProvider;
  1644. dict["implementationProvider"] = implementationProvider;
  1645. dict["referencesProvider"] = referencesProvider;
  1646. dict["documentHighlightProvider"] = documentHighlightProvider;
  1647. dict["documentSymbolProvider"] = documentSymbolProvider;
  1648. dict["workspaceSymbolProvider"] = workspaceSymbolProvider;
  1649. dict["workspace"] = workspace.to_json();
  1650. dict["codeActionProvider"] = codeActionProvider;
  1651. dict["documentFormattingProvider"] = documentFormattingProvider;
  1652. dict["documentRangeFormattingProvider"] = documentRangeFormattingProvider;
  1653. dict["declarationProvider"] = declarationProvider;
  1654. return dict;
  1655. }
  1656. };
  1657. struct InitializeResult {
  1658. /**
  1659. * The capabilities the language server provides.
  1660. */
  1661. ServerCapabilities capabilities;
  1662. _FORCE_INLINE_ Dictionary to_json() {
  1663. Dictionary dict;
  1664. dict["capabilities"] = capabilities.to_json();
  1665. return dict;
  1666. }
  1667. };
  1668. struct GodotNativeClassInfo {
  1669. String name;
  1670. const DocData::ClassDoc *class_doc = nullptr;
  1671. const ClassDB::ClassInfo *class_info = nullptr;
  1672. Dictionary to_json() const {
  1673. Dictionary dict;
  1674. dict["name"] = name;
  1675. dict["inherits"] = class_doc->inherits;
  1676. return dict;
  1677. }
  1678. };
  1679. /** Features not included in the standard lsp specifications */
  1680. struct GodotCapabilities {
  1681. /**
  1682. * Native class list
  1683. */
  1684. List<GodotNativeClassInfo> native_classes;
  1685. Dictionary to_json() const {
  1686. Dictionary dict;
  1687. Array classes;
  1688. for (const GodotNativeClassInfo &native_class : native_classes) {
  1689. classes.push_back(native_class.to_json());
  1690. }
  1691. dict["native_classes"] = classes;
  1692. return dict;
  1693. }
  1694. };
  1695. /** Format BBCode documentation from DocData to markdown */
  1696. static String marked_documentation(const String &p_bbcode) {
  1697. String markdown = p_bbcode.strip_edges();
  1698. Vector<String> lines = markdown.split("\n");
  1699. bool in_code_block = false;
  1700. int code_block_indent = -1;
  1701. markdown = "";
  1702. for (int i = 0; i < lines.size(); i++) {
  1703. String line = lines[i];
  1704. int block_start = line.find("[codeblock]");
  1705. if (block_start != -1) {
  1706. code_block_indent = block_start;
  1707. in_code_block = true;
  1708. line = "\n";
  1709. } else if (in_code_block) {
  1710. line = "\t" + line.substr(code_block_indent);
  1711. }
  1712. if (in_code_block && line.contains("[/codeblock]")) {
  1713. line = "\n";
  1714. in_code_block = false;
  1715. }
  1716. if (!in_code_block) {
  1717. line = line.strip_edges();
  1718. line = line.replace("[code]", "`");
  1719. line = line.replace("[/code]", "`");
  1720. line = line.replace("[i]", "*");
  1721. line = line.replace("[/i]", "*");
  1722. line = line.replace("[b]", "**");
  1723. line = line.replace("[/b]", "**");
  1724. line = line.replace("[u]", "__");
  1725. line = line.replace("[/u]", "__");
  1726. line = line.replace("[method ", "`");
  1727. line = line.replace("[member ", "`");
  1728. line = line.replace("[signal ", "`");
  1729. line = line.replace("[enum ", "`");
  1730. line = line.replace("[constant ", "`");
  1731. line = line.replace_chars("[]", '`');
  1732. }
  1733. if (!in_code_block && i < lines.size() - 1) {
  1734. line += "\n\n";
  1735. } else if (i < lines.size() - 1) {
  1736. line += "\n";
  1737. }
  1738. markdown += line;
  1739. }
  1740. return markdown;
  1741. }
  1742. } // namespace LSP