gdscript_extend_parser.cpp 25 KB

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