gdscript_extend_parser.cpp 31 KB

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