gdscript_extend_parser.cpp 26 KB

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