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. ERR_FAIL_COND(!node);
  170. symbol.name = E->key();
  171. symbol.kind = lsp::SymbolKind::Constant;
  172. symbol.deprecated = false;
  173. const int line = LINE_NUMBER_TO_INDEX(E->get().expression->line);
  174. symbol.range.start.line = line;
  175. symbol.range.start.character = E->get().expression->column;
  176. symbol.range.end.line = symbol.range.start.line;
  177. symbol.range.end.character = lines[line].length();
  178. symbol.selectionRange.start.line = symbol.range.start.line;
  179. symbol.documentation = parse_documentation_as_markdown(line);
  180. symbol.uri = uri;
  181. symbol.script_path = path;
  182. symbol.detail = "const " + symbol.name;
  183. if (c.type.kind != GDScriptParser::DataType::UNRESOLVED) {
  184. symbol.detail += ": " + c.type.to_string();
  185. }
  186. String value_text;
  187. if (node->value.get_type() == Variant::OBJECT) {
  188. RES res = node->value;
  189. if (res.is_valid() && !res->get_path().empty()) {
  190. value_text = "preload(\"" + res->get_path() + "\")";
  191. if (symbol.documentation.empty()) {
  192. if (Map<String, ExtendGDScriptParser *>::Element *S = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(res->get_path())) {
  193. symbol.documentation = S->get()->class_symbol.documentation;
  194. }
  195. }
  196. } else {
  197. value_text = JSON::print(node->value);
  198. }
  199. } else {
  200. value_text = JSON::print(node->value);
  201. }
  202. if (!value_text.empty()) {
  203. symbol.detail += " = " + value_text;
  204. }
  205. r_symbol.children.push_back(symbol);
  206. }
  207. for (int i = 0; i < p_class->functions.size(); ++i) {
  208. const GDScriptParser::FunctionNode *func = p_class->functions[i];
  209. lsp::DocumentSymbol symbol;
  210. parse_function_symbol(func, symbol);
  211. r_symbol.children.push_back(symbol);
  212. }
  213. for (int i = 0; i < p_class->static_functions.size(); ++i) {
  214. const GDScriptParser::FunctionNode *func = p_class->static_functions[i];
  215. lsp::DocumentSymbol symbol;
  216. parse_function_symbol(func, symbol);
  217. r_symbol.children.push_back(symbol);
  218. }
  219. for (int i = 0; i < p_class->subclasses.size(); ++i) {
  220. const GDScriptParser::ClassNode *subclass = p_class->subclasses[i];
  221. lsp::DocumentSymbol symbol;
  222. parse_class_symbol(subclass, symbol);
  223. r_symbol.children.push_back(symbol);
  224. }
  225. }
  226. void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol) {
  227. const String uri = get_uri();
  228. r_symbol.name = p_func->name;
  229. r_symbol.kind = lsp::SymbolKind::Function;
  230. r_symbol.detail = "func " + p_func->name + "(";
  231. r_symbol.deprecated = false;
  232. const int line = LINE_NUMBER_TO_INDEX(p_func->line);
  233. r_symbol.range.start.line = line;
  234. r_symbol.range.start.character = p_func->column;
  235. r_symbol.range.end.line = MAX(p_func->body->end_line - 2, p_func->body->line);
  236. r_symbol.range.end.character = lines[r_symbol.range.end.line].length();
  237. r_symbol.selectionRange.start.line = r_symbol.range.start.line;
  238. r_symbol.documentation = parse_documentation_as_markdown(line);
  239. r_symbol.uri = uri;
  240. r_symbol.script_path = path;
  241. String arguments;
  242. for (int i = 0; i < p_func->arguments.size(); i++) {
  243. lsp::DocumentSymbol symbol;
  244. symbol.kind = lsp::SymbolKind::Variable;
  245. symbol.name = p_func->arguments[i];
  246. symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_func->body->line);
  247. symbol.range.start.character = p_func->body->column;
  248. symbol.range.end = symbol.range.start;
  249. symbol.uri = uri;
  250. symbol.script_path = path;
  251. r_symbol.children.push_back(symbol);
  252. if (i > 0) {
  253. arguments += ", ";
  254. }
  255. arguments += String(p_func->arguments[i]);
  256. if (p_func->argument_types[i].kind != GDScriptParser::DataType::UNRESOLVED) {
  257. arguments += ": " + p_func->argument_types[i].to_string();
  258. }
  259. int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size());
  260. if (default_value_idx >= 0) {
  261. const GDScriptParser::ConstantNode *const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(p_func->default_values[default_value_idx]);
  262. if (const_node == NULL) {
  263. const GDScriptParser::OperatorNode *operator_node = dynamic_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[default_value_idx]);
  264. if (operator_node) {
  265. const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(operator_node->next);
  266. }
  267. }
  268. if (const_node) {
  269. String value = JSON::print(const_node->value);
  270. arguments += " = " + value;
  271. }
  272. }
  273. }
  274. r_symbol.detail += arguments + ")";
  275. if (p_func->return_type.kind != GDScriptParser::DataType::UNRESOLVED) {
  276. r_symbol.detail += " -> " + p_func->return_type.to_string();
  277. }
  278. for (const Map<StringName, LocalVarNode *>::Element *E = p_func->body->variables.front(); E; E = E->next()) {
  279. lsp::DocumentSymbol symbol;
  280. const GDScriptParser::LocalVarNode *var = E->value();
  281. symbol.name = E->key();
  282. symbol.kind = lsp::SymbolKind::Variable;
  283. symbol.range.start.line = LINE_NUMBER_TO_INDEX(E->get()->line);
  284. symbol.range.start.character = E->get()->column;
  285. symbol.range.end.line = symbol.range.start.line;
  286. symbol.range.end.character = lines[symbol.range.end.line].length();
  287. symbol.uri = uri;
  288. symbol.script_path = path;
  289. symbol.detail = "var " + symbol.name;
  290. if (var->datatype.kind != GDScriptParser::DataType::UNRESOLVED) {
  291. symbol.detail += ": " + var->datatype.to_string();
  292. }
  293. symbol.documentation = parse_documentation_as_markdown(line);
  294. r_symbol.children.push_back(symbol);
  295. }
  296. }
  297. String ExtendGDScriptParser::marked_documentation(const String &p_bbcode) {
  298. String markdown = p_bbcode.strip_edges();
  299. Vector<String> lines = markdown.split("\n");
  300. bool in_code_block = false;
  301. int code_block_indent = -1;
  302. markdown = "";
  303. for (int i = 0; i < lines.size(); i++) {
  304. String line = lines[i];
  305. int block_start = line.find("[codeblock]");
  306. if (block_start != -1) {
  307. code_block_indent = block_start;
  308. in_code_block = true;
  309. line = "'''gdscript\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\n";
  315. in_code_block = false;
  316. }
  317. if (!in_code_block) {
  318. line = line.strip_edges();
  319. line = line.replace("[code]", "`");
  320. line = line.replace("[/code]", "`");
  321. line = line.replace("[i]", "*");
  322. line = line.replace("[/i]", "*");
  323. line = line.replace("[b]", "**");
  324. line = line.replace("[/b]", "**");
  325. line = line.replace("[u]", "__");
  326. line = line.replace("[/u]", "__");
  327. line = line.replace("[method ", "`");
  328. line = line.replace("[member ", "`");
  329. line = line.replace("[signal ", "`");
  330. line = line.replace("[enum ", "`");
  331. line = line.replace("[constant ", "`");
  332. line = line.replace("[", "`");
  333. line = line.replace("]", "`");
  334. }
  335. if (!in_code_block && i < lines.size() - 1) {
  336. line += "\n\n";
  337. } else if (i < lines.size() - 1) {
  338. line += "\n";
  339. }
  340. markdown += line;
  341. }
  342. return markdown;
  343. }
  344. String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) {
  345. ERR_FAIL_INDEX_V(p_line, lines.size(), String());
  346. List<String> doc_lines;
  347. if (!p_docs_down) { // inline comment
  348. String inline_comment = lines[p_line];
  349. int comment_start = inline_comment.find("#");
  350. if (comment_start != -1) {
  351. inline_comment = inline_comment.substr(comment_start, inline_comment.length()).strip_edges();
  352. if (inline_comment.length() > 1) {
  353. doc_lines.push_back(inline_comment.substr(1, inline_comment.length()));
  354. }
  355. }
  356. }
  357. int step = p_docs_down ? 1 : -1;
  358. int start_line = p_docs_down ? p_line : p_line - 1;
  359. for (int i = start_line; true; i += step) {
  360. if (i < 0 || i >= lines.size()) break;
  361. String line_comment = lines[i].strip_edges(true, false);
  362. if (line_comment.begins_with("#")) {
  363. line_comment = line_comment.substr(1, line_comment.length());
  364. if (p_docs_down) {
  365. doc_lines.push_back(line_comment);
  366. } else {
  367. doc_lines.push_front(line_comment);
  368. }
  369. } else {
  370. break;
  371. }
  372. }
  373. String doc;
  374. for (List<String>::Element *E = doc_lines.front(); E; E = E->next()) {
  375. doc += E->get() + "\n";
  376. }
  377. return doc;
  378. }
  379. String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_cursor) const {
  380. String longthing;
  381. int len = lines.size();
  382. for (int i = 0; i < len; i++) {
  383. if (i == p_cursor.line) {
  384. longthing += lines[i].substr(0, p_cursor.character);
  385. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  386. longthing += lines[i].substr(p_cursor.character, lines[i].size());
  387. } else {
  388. longthing += lines[i];
  389. }
  390. if (i != len - 1)
  391. longthing += "\n";
  392. }
  393. return longthing;
  394. }
  395. String ExtendGDScriptParser::get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol, bool p_func_requred) const {
  396. String longthing;
  397. int len = lines.size();
  398. for (int i = 0; i < len; i++) {
  399. if (i == p_cursor.line) {
  400. String line = lines[i];
  401. String first_part = line.substr(0, p_cursor.character);
  402. String last_part = line.substr(p_cursor.character + 1, lines[i].length());
  403. if (!p_symbol.empty()) {
  404. String left_cursor_text;
  405. for (int c = p_cursor.character - 1; c >= 0; c--) {
  406. left_cursor_text = line.substr(c, p_cursor.character - c);
  407. if (p_symbol.begins_with(left_cursor_text)) {
  408. first_part = line.substr(0, c);
  409. first_part += p_symbol;
  410. break;
  411. }
  412. }
  413. }
  414. longthing += first_part;
  415. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  416. if (p_func_requred) {
  417. longthing += "("; // tell the parser this is a function call
  418. }
  419. longthing += last_part;
  420. } else {
  421. longthing += lines[i];
  422. }
  423. if (i != len - 1)
  424. longthing += "\n";
  425. }
  426. return longthing;
  427. }
  428. String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const {
  429. ERR_FAIL_INDEX_V(p_position.line, lines.size(), "");
  430. String line = lines[p_position.line];
  431. ERR_FAIL_INDEX_V(p_position.character, line.size(), "");
  432. int start_pos = p_position.character;
  433. for (int c = p_position.character; c >= 0; c--) {
  434. start_pos = c;
  435. CharType ch = line[c];
  436. bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
  437. if (!valid_char) {
  438. break;
  439. }
  440. }
  441. int end_pos = p_position.character;
  442. for (int c = p_position.character; c < line.length(); c++) {
  443. CharType ch = line[c];
  444. bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
  445. if (!valid_char) {
  446. break;
  447. }
  448. end_pos = c;
  449. }
  450. if (start_pos < end_pos) {
  451. p_offset.x = start_pos - p_position.character;
  452. p_offset.y = end_pos - p_position.character;
  453. return line.substr(start_pos + 1, end_pos - start_pos);
  454. }
  455. return "";
  456. }
  457. String ExtendGDScriptParser::get_uri() const {
  458. return GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_uri(path);
  459. }
  460. const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) const {
  461. const lsp::DocumentSymbol *ret = NULL;
  462. if (p_line < p_parent.range.start.line) {
  463. return ret;
  464. } else if (p_parent.range.start.line == p_line) {
  465. return &p_parent;
  466. } else {
  467. for (int i = 0; i < p_parent.children.size(); i++) {
  468. ret = search_symbol_defined_at_line(p_line, p_parent.children[i]);
  469. if (ret) {
  470. break;
  471. }
  472. }
  473. }
  474. return ret;
  475. }
  476. String ExtendGDScriptParser::parse_documentation_as_markdown(int p_line, bool p_docs_down) {
  477. return marked_documentation(parse_documentation(p_line, p_docs_down));
  478. }
  479. const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const {
  480. if (p_line <= 0) {
  481. return &class_symbol;
  482. }
  483. return search_symbol_defined_at_line(p_line, class_symbol);
  484. }
  485. const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String &p_name, const String &p_subclass) const {
  486. if (p_subclass.empty()) {
  487. const lsp::DocumentSymbol *const *ptr = members.getptr(p_name);
  488. if (ptr) {
  489. return *ptr;
  490. }
  491. } else {
  492. if (const ClassMembers *_class = inner_classes.getptr(p_subclass)) {
  493. const lsp::DocumentSymbol *const *ptr = _class->getptr(p_name);
  494. if (ptr) {
  495. return *ptr;
  496. }
  497. }
  498. }
  499. return NULL;
  500. }
  501. const Array &ExtendGDScriptParser::get_member_completions() {
  502. if (member_completions.empty()) {
  503. const String *name = members.next(NULL);
  504. while (name) {
  505. const lsp::DocumentSymbol *symbol = members.get(*name);
  506. lsp::CompletionItem item = symbol->make_completion_item();
  507. item.data = JOIN_SYMBOLS(path, *name);
  508. member_completions.push_back(item.to_json());
  509. name = members.next(name);
  510. }
  511. const String *_class = inner_classes.next(NULL);
  512. while (_class) {
  513. const ClassMembers *inner_class = inner_classes.getptr(*_class);
  514. const String *member_name = inner_class->next(NULL);
  515. while (member_name) {
  516. const lsp::DocumentSymbol *symbol = inner_class->get(*member_name);
  517. lsp::CompletionItem item = symbol->make_completion_item();
  518. item.data = JOIN_SYMBOLS(path, JOIN_SYMBOLS(*_class, *member_name));
  519. member_completions.push_back(item.to_json());
  520. member_name = inner_class->next(member_name);
  521. }
  522. _class = inner_classes.next(_class);
  523. }
  524. }
  525. return member_completions;
  526. }
  527. Dictionary ExtendGDScriptParser::dump_function_api(const GDScriptParser::FunctionNode *p_func) const {
  528. Dictionary func;
  529. ERR_FAIL_NULL_V(p_func, func);
  530. func["name"] = p_func->name;
  531. func["return_type"] = p_func->return_type.to_string();
  532. func["rpc_mode"] = p_func->rpc_mode;
  533. Array arguments;
  534. for (int i = 0; i < p_func->arguments.size(); i++) {
  535. Dictionary arg;
  536. arg["name"] = p_func->arguments[i];
  537. arg["type"] = p_func->argument_types[i].to_string();
  538. int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size());
  539. if (default_value_idx >= 0) {
  540. const GDScriptParser::ConstantNode *const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(p_func->default_values[default_value_idx]);
  541. if (const_node == NULL) {
  542. const GDScriptParser::OperatorNode *operator_node = dynamic_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[default_value_idx]);
  543. if (operator_node) {
  544. const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(operator_node->next);
  545. }
  546. }
  547. if (const_node) {
  548. arg["default_value"] = const_node->value;
  549. }
  550. }
  551. arguments.push_back(arg);
  552. }
  553. if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_func->line))) {
  554. func["signature"] = symbol->detail;
  555. func["description"] = symbol->documentation;
  556. }
  557. func["arguments"] = arguments;
  558. return func;
  559. }
  560. Dictionary ExtendGDScriptParser::dump_class_api(const GDScriptParser::ClassNode *p_class) const {
  561. Dictionary class_api;
  562. ERR_FAIL_NULL_V(p_class, class_api);
  563. class_api["name"] = String(p_class->name);
  564. class_api["path"] = path;
  565. Array extends_class;
  566. for (int i = 0; i < p_class->extends_class.size(); i++) {
  567. extends_class.append(String(p_class->extends_class[i]));
  568. }
  569. class_api["extends_class"] = extends_class;
  570. class_api["extends_file"] = String(p_class->extends_file);
  571. class_api["icon"] = String(p_class->icon_path);
  572. if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_class->line))) {
  573. class_api["signature"] = symbol->detail;
  574. class_api["description"] = symbol->documentation;
  575. }
  576. Array subclasses;
  577. for (int i = 0; i < p_class->subclasses.size(); i++) {
  578. subclasses.push_back(dump_class_api(p_class->subclasses[i]));
  579. }
  580. class_api["sub_classes"] = subclasses;
  581. Array constants;
  582. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  583. const GDScriptParser::ClassNode::Constant &c = E->value();
  584. const GDScriptParser::ConstantNode *node = dynamic_cast<const GDScriptParser::ConstantNode *>(c.expression);
  585. ERR_FAIL_COND_V(!node, class_api);
  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. }