gdscript_extend_parser.cpp 27 KB

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