gdscript_extend_parser.cpp 29 KB

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