gdscript_extend_parser.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*************************************************************************/
  2. /* gdscript_extend_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include "gdscript_extend_parser.h"
  31. #include "../gdscript.h"
  32. #include "core/io/json.h"
  33. #include "gdscript_language_protocol.h"
  34. #include "gdscript_workspace.h"
  35. void ExtendGDScriptParser::update_diagnostics() {
  36. diagnostics.clear();
  37. if (has_error()) {
  38. lsp::Diagnostic diagnostic;
  39. diagnostic.severity = lsp::DiagnosticSeverity::Error;
  40. diagnostic.message = get_error();
  41. diagnostic.source = "gdscript";
  42. diagnostic.code = -1;
  43. lsp::Range range;
  44. lsp::Position pos;
  45. int line = LINE_NUMBER_TO_INDEX(get_error_line());
  46. const String &line_text = get_lines()[line];
  47. pos.line = line;
  48. pos.character = line_text.length() - line_text.strip_edges(true, false).length();
  49. range.start = pos;
  50. range.end = range.start;
  51. range.end.character = line_text.strip_edges(false).length();
  52. diagnostic.range = range;
  53. diagnostics.push_back(diagnostic);
  54. }
  55. const List<GDScriptWarning> &warnings = get_warnings();
  56. for (const List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) {
  57. const GDScriptWarning &warning = E->get();
  58. lsp::Diagnostic diagnostic;
  59. diagnostic.severity = lsp::DiagnosticSeverity::Warning;
  60. diagnostic.message = warning.get_message();
  61. diagnostic.source = "gdscript";
  62. diagnostic.code = warning.code;
  63. lsp::Range range;
  64. lsp::Position pos;
  65. int line = LINE_NUMBER_TO_INDEX(warning.line);
  66. const String &line_text = get_lines()[line];
  67. pos.line = line;
  68. pos.character = line_text.length() - line_text.strip_edges(true, false).length();
  69. range.start = pos;
  70. range.end = pos;
  71. range.end.character = line_text.strip_edges(false).length();
  72. diagnostic.range = range;
  73. diagnostics.push_back(diagnostic);
  74. }
  75. }
  76. void ExtendGDScriptParser::update_symbols() {
  77. members.clear();
  78. const GDScriptParser::Node *head = get_parse_tree();
  79. if (const GDScriptParser::ClassNode *gdclass = dynamic_cast<const GDScriptParser::ClassNode *>(head)) {
  80. parse_class_symbol(gdclass, class_symbol);
  81. for (int i = 0; i < class_symbol.children.size(); i++) {
  82. const lsp::DocumentSymbol &symbol = class_symbol.children[i];
  83. members.set(symbol.name, &symbol);
  84. // cache level one inner classes
  85. if (symbol.kind == lsp::SymbolKind::Class) {
  86. ClassMembers inner_class;
  87. for (int j = 0; j < symbol.children.size(); j++) {
  88. const lsp::DocumentSymbol &s = symbol.children[j];
  89. inner_class.set(s.name, &s);
  90. }
  91. inner_classes.set(symbol.name, inner_class);
  92. }
  93. }
  94. }
  95. }
  96. void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol) {
  97. const String uri = get_uri();
  98. r_symbol.uri = uri;
  99. r_symbol.script_path = path;
  100. r_symbol.children.clear();
  101. r_symbol.name = p_class->name;
  102. if (r_symbol.name.empty())
  103. r_symbol.name = path.get_file();
  104. r_symbol.kind = lsp::SymbolKind::Class;
  105. r_symbol.deprecated = false;
  106. r_symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_class->line);
  107. r_symbol.range.start.character = p_class->column;
  108. r_symbol.range.end.line = LINE_NUMBER_TO_INDEX(p_class->end_line);
  109. r_symbol.selectionRange.start.line = r_symbol.range.start.line;
  110. r_symbol.detail = "class " + r_symbol.name;
  111. bool is_root_class = &r_symbol == &class_symbol;
  112. r_symbol.documentation = parse_documentation_as_markdown(is_root_class ? 0 : LINE_NUMBER_TO_INDEX(p_class->line), is_root_class);
  113. for (int i = 0; i < p_class->variables.size(); ++i) {
  114. const GDScriptParser::ClassNode::Member &m = p_class->variables[i];
  115. lsp::DocumentSymbol symbol;
  116. symbol.name = m.identifier;
  117. symbol.kind = lsp::SymbolKind::Variable;
  118. symbol.deprecated = false;
  119. const int line = LINE_NUMBER_TO_INDEX(m.line);
  120. symbol.range.start.line = line;
  121. symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length();
  122. symbol.range.end.line = line;
  123. symbol.range.end.character = lines[line].length();
  124. symbol.selectionRange.start.line = symbol.range.start.line;
  125. if (m._export.type != Variant::NIL) {
  126. symbol.detail += "export ";
  127. }
  128. symbol.detail += "var " + m.identifier;
  129. if (m.data_type.kind != GDScriptParser::DataType::UNRESOLVED) {
  130. symbol.detail += ": " + m.data_type.to_string();
  131. }
  132. if (m.default_value.get_type() != Variant::NIL) {
  133. symbol.detail += " = " + JSON::print(m.default_value);
  134. }
  135. symbol.documentation = parse_documentation_as_markdown(line);
  136. symbol.uri = uri;
  137. symbol.script_path = path;
  138. r_symbol.children.push_back(symbol);
  139. }
  140. for (int i = 0; i < p_class->_signals.size(); ++i) {
  141. const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i];
  142. lsp::DocumentSymbol symbol;
  143. symbol.name = signal.name;
  144. symbol.kind = lsp::SymbolKind::Event;
  145. symbol.deprecated = false;
  146. const int line = LINE_NUMBER_TO_INDEX(signal.line);
  147. symbol.range.start.line = line;
  148. symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length();
  149. symbol.range.end.line = symbol.range.start.line;
  150. symbol.range.end.character = lines[line].length();
  151. symbol.selectionRange.start.line = symbol.range.start.line;
  152. symbol.documentation = parse_documentation_as_markdown(line);
  153. symbol.uri = uri;
  154. symbol.script_path = path;
  155. symbol.detail = "signal " + signal.name + "(";
  156. for (int j = 0; j < signal.arguments.size(); j++) {
  157. if (j > 0) {
  158. symbol.detail += ", ";
  159. }
  160. symbol.detail += signal.arguments[j];
  161. }
  162. symbol.detail += ")";
  163. r_symbol.children.push_back(symbol);
  164. }
  165. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  166. lsp::DocumentSymbol symbol;
  167. const GDScriptParser::ClassNode::Constant &c = E->value();
  168. const GDScriptParser::ConstantNode *node = dynamic_cast<const GDScriptParser::ConstantNode *>(c.expression);
  169. symbol.name = E->key();
  170. symbol.kind = lsp::SymbolKind::Constant;
  171. symbol.deprecated = false;
  172. const int line = LINE_NUMBER_TO_INDEX(E->get().expression->line);
  173. symbol.range.start.line = line;
  174. symbol.range.start.character = E->get().expression->column;
  175. symbol.range.end.line = symbol.range.start.line;
  176. symbol.range.end.character = lines[line].length();
  177. symbol.selectionRange.start.line = symbol.range.start.line;
  178. symbol.documentation = parse_documentation_as_markdown(line);
  179. symbol.uri = uri;
  180. symbol.script_path = path;
  181. symbol.detail = "const " + symbol.name;
  182. if (c.type.kind != GDScriptParser::DataType::UNRESOLVED) {
  183. symbol.detail += ": " + c.type.to_string();
  184. }
  185. String value_text;
  186. if (node->value.get_type() == Variant::OBJECT) {
  187. RES res = node->value;
  188. if (res.is_valid() && !res->get_path().empty()) {
  189. value_text = "preload(\"" + res->get_path() + "\")";
  190. if (symbol.documentation.empty()) {
  191. if (Map<String, ExtendGDScriptParser *>::Element *S = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(res->get_path())) {
  192. symbol.documentation = S->get()->class_symbol.documentation;
  193. }
  194. }
  195. } else {
  196. value_text = JSON::print(node->value);
  197. }
  198. } else {
  199. value_text = JSON::print(node->value);
  200. }
  201. if (!value_text.empty()) {
  202. symbol.detail += " = " + value_text;
  203. }
  204. r_symbol.children.push_back(symbol);
  205. }
  206. for (int i = 0; i < p_class->functions.size(); ++i) {
  207. const GDScriptParser::FunctionNode *func = p_class->functions[i];
  208. lsp::DocumentSymbol symbol;
  209. parse_function_symbol(func, symbol);
  210. r_symbol.children.push_back(symbol);
  211. }
  212. for (int i = 0; i < p_class->static_functions.size(); ++i) {
  213. const GDScriptParser::FunctionNode *func = p_class->static_functions[i];
  214. lsp::DocumentSymbol symbol;
  215. parse_function_symbol(func, symbol);
  216. r_symbol.children.push_back(symbol);
  217. }
  218. for (int i = 0; i < p_class->subclasses.size(); ++i) {
  219. const GDScriptParser::ClassNode *subclass = p_class->subclasses[i];
  220. lsp::DocumentSymbol symbol;
  221. parse_class_symbol(subclass, symbol);
  222. r_symbol.children.push_back(symbol);
  223. }
  224. }
  225. void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol) {
  226. const String uri = get_uri();
  227. r_symbol.name = p_func->name;
  228. r_symbol.kind = lsp::SymbolKind::Function;
  229. r_symbol.detail = "func " + p_func->name + "(";
  230. r_symbol.deprecated = false;
  231. const int line = LINE_NUMBER_TO_INDEX(p_func->line);
  232. r_symbol.range.start.line = line;
  233. r_symbol.range.start.character = p_func->column;
  234. r_symbol.range.end.line = MAX(p_func->body->end_line - 2, p_func->body->line);
  235. r_symbol.range.end.character = lines[r_symbol.range.end.line].length();
  236. r_symbol.selectionRange.start.line = r_symbol.range.start.line;
  237. r_symbol.documentation = parse_documentation_as_markdown(line);
  238. r_symbol.uri = uri;
  239. r_symbol.script_path = path;
  240. String arguments;
  241. for (int i = 0; i < p_func->arguments.size(); i++) {
  242. lsp::DocumentSymbol symbol;
  243. symbol.kind = lsp::SymbolKind::Variable;
  244. symbol.name = p_func->arguments[i];
  245. symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_func->body->line);
  246. symbol.range.start.character = p_func->body->column;
  247. symbol.range.end = symbol.range.start;
  248. symbol.uri = uri;
  249. symbol.script_path = path;
  250. r_symbol.children.push_back(symbol);
  251. if (i > 0) {
  252. arguments += ", ";
  253. }
  254. arguments += String(p_func->arguments[i]);
  255. if (p_func->argument_types[i].kind != GDScriptParser::DataType::UNRESOLVED) {
  256. arguments += ": " + p_func->argument_types[i].to_string();
  257. }
  258. int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size());
  259. if (default_value_idx >= 0) {
  260. const GDScriptParser::ConstantNode *const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(p_func->default_values[default_value_idx]);
  261. if (const_node == NULL) {
  262. const GDScriptParser::OperatorNode *operator_node = dynamic_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[default_value_idx]);
  263. if (operator_node) {
  264. const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(operator_node->next);
  265. }
  266. }
  267. if (const_node) {
  268. String value = JSON::print(const_node->value);
  269. arguments += " = " + value;
  270. }
  271. }
  272. }
  273. r_symbol.detail += arguments + ")";
  274. if (p_func->return_type.kind != GDScriptParser::DataType::UNRESOLVED) {
  275. r_symbol.detail += " -> " + p_func->return_type.to_string();
  276. }
  277. for (const Map<StringName, LocalVarNode *>::Element *E = p_func->body->variables.front(); E; E = E->next()) {
  278. lsp::DocumentSymbol symbol;
  279. const GDScriptParser::LocalVarNode *var = E->value();
  280. symbol.name = E->key();
  281. symbol.kind = lsp::SymbolKind::Variable;
  282. symbol.range.start.line = LINE_NUMBER_TO_INDEX(E->get()->line);
  283. symbol.range.start.character = E->get()->column;
  284. symbol.range.end.line = symbol.range.start.line;
  285. symbol.range.end.character = lines[symbol.range.end.line].length();
  286. symbol.uri = uri;
  287. symbol.script_path = path;
  288. symbol.detail = "var " + symbol.name;
  289. if (var->datatype.kind != GDScriptParser::DataType::UNRESOLVED) {
  290. symbol.detail += ": " + var->datatype.to_string();
  291. }
  292. symbol.documentation = parse_documentation_as_markdown(line);
  293. r_symbol.children.push_back(symbol);
  294. }
  295. }
  296. String ExtendGDScriptParser::marked_documentation(const String &p_bbcode) {
  297. String markdown = p_bbcode.strip_edges();
  298. Vector<String> lines = markdown.split("\n");
  299. bool in_code_block = false;
  300. int code_block_indent = -1;
  301. markdown = "";
  302. for (int i = 0; i < lines.size(); i++) {
  303. String line = lines[i];
  304. int block_start = line.find("[codeblock]");
  305. if (block_start != -1) {
  306. code_block_indent = block_start;
  307. in_code_block = true;
  308. line = "'''gdscript";
  309. line = "\n";
  310. } else if (in_code_block) {
  311. line = "\t" + line.substr(code_block_indent, line.length());
  312. }
  313. if (in_code_block && line.find("[/codeblock]") != -1) {
  314. line = "'''\n";
  315. line = "\n";
  316. in_code_block = false;
  317. }
  318. if (!in_code_block) {
  319. line = line.strip_edges();
  320. line = line.replace("[code]", "`");
  321. line = line.replace("[/code]", "`");
  322. line = line.replace("[i]", "*");
  323. line = line.replace("[/i]", "*");
  324. line = line.replace("[b]", "**");
  325. line = line.replace("[/b]", "**");
  326. line = line.replace("[u]", "__");
  327. line = line.replace("[/u]", "__");
  328. line = line.replace("[method ", "`");
  329. line = line.replace("[member ", "`");
  330. line = line.replace("[signal ", "`");
  331. line = line.replace("[enum ", "`");
  332. line = line.replace("[constant ", "`");
  333. line = line.replace("[", "`");
  334. line = line.replace("]", "`");
  335. }
  336. if (!in_code_block && i < lines.size() - 1) {
  337. line += "\n\n";
  338. } else if (i < lines.size() - 1) {
  339. line += "\n";
  340. }
  341. markdown += line;
  342. }
  343. return markdown;
  344. }
  345. String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) {
  346. ERR_FAIL_INDEX_V(p_line, lines.size(), String());
  347. List<String> doc_lines;
  348. if (!p_docs_down) { // inline comment
  349. String inline_comment = lines[p_line];
  350. int comment_start = inline_comment.find("#");
  351. if (comment_start != -1) {
  352. inline_comment = inline_comment.substr(comment_start, inline_comment.length()).strip_edges();
  353. if (inline_comment.length() > 1) {
  354. doc_lines.push_back(inline_comment.substr(1, inline_comment.length()));
  355. }
  356. }
  357. }
  358. int step = p_docs_down ? 1 : -1;
  359. int start_line = p_docs_down ? p_line : p_line - 1;
  360. for (int i = start_line; true; i += step) {
  361. if (i < 0 || i >= lines.size()) break;
  362. String line_comment = lines[i].strip_edges(true, false);
  363. if (line_comment.begins_with("#")) {
  364. line_comment = line_comment.substr(1, line_comment.length());
  365. if (p_docs_down) {
  366. doc_lines.push_back(line_comment);
  367. } else {
  368. doc_lines.push_front(line_comment);
  369. }
  370. } else {
  371. break;
  372. }
  373. }
  374. String doc;
  375. for (List<String>::Element *E = doc_lines.front(); E; E = E->next()) {
  376. doc += E->get() + "\n";
  377. }
  378. return doc;
  379. }
  380. String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_cursor) const {
  381. String longthing;
  382. int len = lines.size();
  383. for (int i = 0; i < len; i++) {
  384. if (i == p_cursor.line) {
  385. longthing += lines[i].substr(0, p_cursor.character);
  386. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  387. longthing += lines[i].substr(p_cursor.character, lines[i].size());
  388. } else {
  389. longthing += lines[i];
  390. }
  391. if (i != len - 1)
  392. longthing += "\n";
  393. }
  394. return longthing;
  395. }
  396. String ExtendGDScriptParser::get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol, bool p_func_requred) const {
  397. String longthing;
  398. int len = lines.size();
  399. for (int i = 0; i < len; i++) {
  400. if (i == p_cursor.line) {
  401. String line = lines[i];
  402. String first_part = line.substr(0, p_cursor.character);
  403. String last_part = line.substr(p_cursor.character + 1, lines[i].length());
  404. if (!p_symbol.empty()) {
  405. String left_cursor_text;
  406. for (int c = p_cursor.character - 1; c >= 0; c--) {
  407. left_cursor_text = line.substr(c, p_cursor.character - c);
  408. if (p_symbol.begins_with(left_cursor_text)) {
  409. first_part = line.substr(0, c);
  410. first_part += p_symbol;
  411. break;
  412. }
  413. }
  414. }
  415. longthing += first_part;
  416. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  417. if (p_func_requred) {
  418. longthing += "("; // tell the parser this is a function call
  419. }
  420. longthing += last_part;
  421. } else {
  422. longthing += lines[i];
  423. }
  424. if (i != len - 1)
  425. longthing += "\n";
  426. }
  427. return longthing;
  428. }
  429. String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const {
  430. ERR_FAIL_INDEX_V(p_position.line, lines.size(), "");
  431. String line = lines[p_position.line];
  432. ERR_FAIL_INDEX_V(p_position.character, line.size(), "");
  433. int start_pos = p_position.character;
  434. for (int c = p_position.character; c >= 0; c--) {
  435. start_pos = c;
  436. CharType ch = line[c];
  437. bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
  438. if (!valid_char) {
  439. break;
  440. }
  441. }
  442. int end_pos = p_position.character;
  443. for (int c = p_position.character; c < line.length(); c++) {
  444. CharType ch = line[c];
  445. bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
  446. if (!valid_char) {
  447. break;
  448. }
  449. end_pos = c;
  450. }
  451. if (start_pos < end_pos) {
  452. p_offset.x = start_pos - p_position.character;
  453. p_offset.y = end_pos - p_position.character;
  454. return line.substr(start_pos + 1, end_pos - start_pos);
  455. }
  456. return "";
  457. }
  458. String ExtendGDScriptParser::get_uri() const {
  459. return GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_uri(path);
  460. }
  461. const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) const {
  462. const lsp::DocumentSymbol *ret = NULL;
  463. if (p_line < p_parent.range.start.line) {
  464. return ret;
  465. } else if (p_parent.range.start.line == p_line) {
  466. return &p_parent;
  467. } else {
  468. for (int i = 0; i < p_parent.children.size(); i++) {
  469. ret = search_symbol_defined_at_line(p_line, p_parent.children[i]);
  470. if (ret) {
  471. break;
  472. }
  473. }
  474. }
  475. return ret;
  476. }
  477. String ExtendGDScriptParser::parse_documentation_as_markdown(int p_line, bool p_docs_down) {
  478. return marked_documentation(parse_documentation(p_line, p_docs_down));
  479. }
  480. const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const {
  481. if (p_line <= 0) {
  482. return &class_symbol;
  483. }
  484. return search_symbol_defined_at_line(p_line, class_symbol);
  485. }
  486. const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String &p_name, const String &p_subclass) const {
  487. if (p_subclass.empty()) {
  488. const lsp::DocumentSymbol *const *ptr = members.getptr(p_name);
  489. if (ptr) {
  490. return *ptr;
  491. }
  492. } else {
  493. if (const ClassMembers *_class = inner_classes.getptr(p_subclass)) {
  494. const lsp::DocumentSymbol *const *ptr = _class->getptr(p_name);
  495. if (ptr) {
  496. return *ptr;
  497. }
  498. }
  499. }
  500. return NULL;
  501. }
  502. const Array &ExtendGDScriptParser::get_member_completions() {
  503. if (member_completions.empty()) {
  504. const String *name = members.next(NULL);
  505. while (name) {
  506. const lsp::DocumentSymbol *symbol = members.get(*name);
  507. lsp::CompletionItem item = symbol->make_completion_item();
  508. item.data = JOIN_SYMBOLS(path, *name);
  509. member_completions.push_back(item.to_json());
  510. name = members.next(name);
  511. }
  512. const String *_class = inner_classes.next(NULL);
  513. while (_class) {
  514. const ClassMembers *inner_class = inner_classes.getptr(*_class);
  515. const String *member_name = inner_class->next(NULL);
  516. while (member_name) {
  517. const lsp::DocumentSymbol *symbol = inner_class->get(*member_name);
  518. lsp::CompletionItem item = symbol->make_completion_item();
  519. item.data = JOIN_SYMBOLS(path, JOIN_SYMBOLS(*_class, *member_name));
  520. member_completions.push_back(item.to_json());
  521. member_name = inner_class->next(member_name);
  522. }
  523. _class = inner_classes.next(_class);
  524. }
  525. }
  526. return member_completions;
  527. }
  528. Dictionary ExtendGDScriptParser::dump_function_api(const GDScriptParser::FunctionNode *p_func) const {
  529. Dictionary func;
  530. ERR_FAIL_NULL_V(p_func, func);
  531. func["name"] = p_func->name;
  532. func["return_type"] = p_func->return_type.to_string();
  533. func["rpc_mode"] = p_func->rpc_mode;
  534. Array arguments;
  535. for (int i = 0; i < p_func->arguments.size(); i++) {
  536. Dictionary arg;
  537. arg["name"] = p_func->arguments[i];
  538. arg["type"] = p_func->argument_types[i].to_string();
  539. int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size());
  540. if (default_value_idx >= 0) {
  541. const GDScriptParser::ConstantNode *const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(p_func->default_values[default_value_idx]);
  542. if (const_node == NULL) {
  543. const GDScriptParser::OperatorNode *operator_node = dynamic_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[default_value_idx]);
  544. if (operator_node) {
  545. const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(operator_node->next);
  546. }
  547. }
  548. if (const_node) {
  549. arg["default_value"] = const_node->value;
  550. }
  551. }
  552. arguments.push_back(arg);
  553. }
  554. if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_func->line))) {
  555. func["signature"] = symbol->detail;
  556. func["description"] = symbol->documentation;
  557. }
  558. func["arguments"] = arguments;
  559. return func;
  560. }
  561. Dictionary ExtendGDScriptParser::dump_class_api(const GDScriptParser::ClassNode *p_class) const {
  562. Dictionary class_api;
  563. ERR_FAIL_NULL_V(p_class, class_api);
  564. class_api["name"] = String(p_class->name);
  565. class_api["path"] = path;
  566. Array extends_class;
  567. for (int i = 0; i < p_class->extends_class.size(); i++) {
  568. extends_class.append(String(p_class->extends_class[i]));
  569. }
  570. class_api["extends_class"] = extends_class;
  571. class_api["extends_file"] = String(p_class->extends_file);
  572. class_api["icon"] = String(p_class->icon_path);
  573. if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_class->line))) {
  574. class_api["signature"] = symbol->detail;
  575. class_api["description"] = symbol->documentation;
  576. }
  577. Array subclasses;
  578. for (int i = 0; i < p_class->subclasses.size(); i++) {
  579. subclasses.push_back(dump_class_api(p_class->subclasses[i]));
  580. }
  581. class_api["sub_classes"] = subclasses;
  582. Array constants;
  583. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  584. const GDScriptParser::ClassNode::Constant &c = E->value();
  585. const GDScriptParser::ConstantNode *node = dynamic_cast<const GDScriptParser::ConstantNode *>(c.expression);
  586. Dictionary api;
  587. api["name"] = E->key();
  588. api["value"] = node->value;
  589. api["data_type"] = node->datatype.to_string();
  590. if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(node->line))) {
  591. api["signature"] = symbol->detail;
  592. api["description"] = symbol->documentation;
  593. }
  594. constants.push_back(api);
  595. }
  596. class_api["constants"] = constants;
  597. Array members;
  598. for (int i = 0; i < p_class->variables.size(); ++i) {
  599. const GDScriptParser::ClassNode::Member &m = p_class->variables[i];
  600. Dictionary api;
  601. api["name"] = m.identifier;
  602. api["data_type"] = m.data_type.to_string();
  603. api["default_value"] = m.default_value;
  604. api["setter"] = String(m.setter);
  605. api["getter"] = String(m.getter);
  606. api["export"] = m._export.type != Variant::NIL;
  607. if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line))) {
  608. api["signature"] = symbol->detail;
  609. api["description"] = symbol->documentation;
  610. }
  611. members.push_back(api);
  612. }
  613. class_api["members"] = members;
  614. Array signals;
  615. for (int i = 0; i < p_class->_signals.size(); ++i) {
  616. const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i];
  617. Dictionary api;
  618. api["name"] = signal.name;
  619. Array args;
  620. for (int j = 0; j < signal.arguments.size(); j++) {
  621. args.append(signal.arguments[j]);
  622. }
  623. api["arguments"] = args;
  624. if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(signal.line))) {
  625. api["signature"] = symbol->detail;
  626. api["description"] = symbol->documentation;
  627. }
  628. signals.push_back(api);
  629. }
  630. class_api["signals"] = signals;
  631. Array methods;
  632. for (int i = 0; i < p_class->functions.size(); ++i) {
  633. methods.append(dump_function_api(p_class->functions[i]));
  634. }
  635. class_api["methods"] = methods;
  636. Array static_functions;
  637. for (int i = 0; i < p_class->static_functions.size(); ++i) {
  638. static_functions.append(dump_function_api(p_class->functions[i]));
  639. }
  640. class_api["static_functions"] = static_functions;
  641. return class_api;
  642. }
  643. Dictionary ExtendGDScriptParser::generate_api() const {
  644. Dictionary api;
  645. const GDScriptParser::Node *head = get_parse_tree();
  646. if (const GDScriptParser::ClassNode *gdclass = dynamic_cast<const GDScriptParser::ClassNode *>(head)) {
  647. api = dump_class_api(gdclass);
  648. }
  649. return api;
  650. }
  651. Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) {
  652. path = p_path;
  653. lines = p_code.split("\n");
  654. Error err = GDScriptParser::parse(p_code, p_path.get_base_dir(), false, p_path, false, NULL, false);
  655. update_diagnostics();
  656. update_symbols();
  657. return err;
  658. }