gdscript_extend_parser.cpp 26 KB

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