gdscript_extend_parser.cpp 31 KB

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