gdscript_extend_parser.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_extend_parser.h"
  31. #include "../gdscript.h"
  32. #include "core/io/json.h"
  33. #include "gdscript_language_protocol.h"
  34. #include "gdscript_workspace.h"
  35. void ExtendGDScriptParser::update_diagnostics() {
  36. diagnostics.clear();
  37. if (has_error()) {
  38. lsp::Diagnostic diagnostic;
  39. diagnostic.severity = lsp::DiagnosticSeverity::Error;
  40. diagnostic.message = get_error();
  41. diagnostic.source = "gdscript";
  42. diagnostic.code = -1;
  43. lsp::Range range;
  44. lsp::Position pos;
  45. int line = LINE_NUMBER_TO_INDEX(get_error_line());
  46. const String &line_text = get_lines()[line];
  47. pos.line = line;
  48. pos.character = line_text.length() - line_text.strip_edges(true, false).length();
  49. range.start = pos;
  50. range.end = range.start;
  51. range.end.character = line_text.strip_edges(false).length();
  52. diagnostic.range = range;
  53. diagnostics.push_back(diagnostic);
  54. }
  55. const List<GDScriptWarning> &warnings = get_warnings();
  56. for (const List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) {
  57. const GDScriptWarning &warning = E->get();
  58. lsp::Diagnostic diagnostic;
  59. diagnostic.severity = lsp::DiagnosticSeverity::Warning;
  60. diagnostic.message = warning.get_message();
  61. diagnostic.source = "gdscript";
  62. diagnostic.code = warning.code;
  63. lsp::Range range;
  64. lsp::Position pos;
  65. int line = LINE_NUMBER_TO_INDEX(warning.line);
  66. const String &line_text = get_lines()[line];
  67. pos.line = line;
  68. pos.character = line_text.length() - line_text.strip_edges(true, false).length();
  69. range.start = pos;
  70. range.end = pos;
  71. range.end.character = line_text.strip_edges(false).length();
  72. diagnostic.range = range;
  73. diagnostics.push_back(diagnostic);
  74. }
  75. }
  76. void ExtendGDScriptParser::update_symbols() {
  77. const GDScriptParser::Node *head = get_parse_tree();
  78. if (const GDScriptParser::ClassNode *gdclass = dynamic_cast<const GDScriptParser::ClassNode *>(head)) {
  79. parse_class_symbol(gdclass, class_symbol);
  80. }
  81. }
  82. void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol) {
  83. const String uri = get_uri();
  84. r_symbol.uri = uri;
  85. r_symbol.script_path = path;
  86. r_symbol.children.clear();
  87. r_symbol.name = p_class->name;
  88. if (r_symbol.name.empty())
  89. r_symbol.name = path.get_file();
  90. r_symbol.kind = lsp::SymbolKind::Class;
  91. r_symbol.deprecated = false;
  92. r_symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_class->line);
  93. r_symbol.range.start.character = p_class->column;
  94. r_symbol.range.end.line = LINE_NUMBER_TO_INDEX(p_class->end_line);
  95. r_symbol.selectionRange.start.line = r_symbol.range.start.line;
  96. r_symbol.detail = "class " + r_symbol.name;
  97. r_symbol.documentation = parse_documentation(LINE_NUMBER_TO_INDEX(p_class->line));
  98. for (int i = 0; i < p_class->variables.size(); ++i) {
  99. const GDScriptParser::ClassNode::Member &m = p_class->variables[i];
  100. lsp::DocumentSymbol symbol;
  101. symbol.name = m.identifier;
  102. symbol.kind = lsp::SymbolKind::Variable;
  103. symbol.deprecated = false;
  104. const int line = LINE_NUMBER_TO_INDEX(m.line);
  105. symbol.range.start.line = line;
  106. symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length();
  107. symbol.range.end.line = line;
  108. symbol.range.end.character = lines[line].length();
  109. symbol.selectionRange.start.line = symbol.range.start.line;
  110. symbol.detail = "var " + m.identifier;
  111. if (m.data_type.kind != GDScriptParser::DataType::UNRESOLVED) {
  112. symbol.detail += ": " + m.data_type.to_string();
  113. }
  114. symbol.detail += " = " + String(m.default_value);
  115. symbol.documentation = parse_documentation(line);
  116. symbol.uri = uri;
  117. symbol.script_path = path;
  118. r_symbol.children.push_back(symbol);
  119. }
  120. for (int i = 0; i < p_class->_signals.size(); ++i) {
  121. const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i];
  122. lsp::DocumentSymbol symbol;
  123. symbol.name = signal.name;
  124. symbol.kind = lsp::SymbolKind::Event;
  125. symbol.deprecated = false;
  126. const int line = LINE_NUMBER_TO_INDEX(signal.line);
  127. symbol.range.start.line = line;
  128. symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length();
  129. symbol.range.end.line = symbol.range.start.line;
  130. symbol.range.end.character = lines[line].length();
  131. symbol.selectionRange.start.line = symbol.range.start.line;
  132. symbol.documentation = parse_documentation(line);
  133. symbol.uri = uri;
  134. symbol.script_path = path;
  135. symbol.detail = "signal " + signal.name + "(";
  136. for (int j = 0; j < signal.arguments.size(); j++) {
  137. if (j > 0) {
  138. symbol.detail += ", ";
  139. }
  140. symbol.detail += signal.arguments[j];
  141. }
  142. symbol.detail += ")";
  143. r_symbol.children.push_back(symbol);
  144. }
  145. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  146. lsp::DocumentSymbol symbol;
  147. const GDScriptParser::ClassNode::Constant &c = E->value();
  148. const GDScriptParser::ConstantNode *node = dynamic_cast<const GDScriptParser::ConstantNode *>(c.expression);
  149. symbol.name = E->key();
  150. symbol.kind = lsp::SymbolKind::Constant;
  151. symbol.deprecated = false;
  152. const int line = LINE_NUMBER_TO_INDEX(E->get().expression->line);
  153. symbol.range.start.line = line;
  154. symbol.range.start.character = E->get().expression->column;
  155. symbol.range.end.line = symbol.range.start.line;
  156. symbol.range.end.character = lines[line].length();
  157. symbol.selectionRange.start.line = symbol.range.start.line;
  158. symbol.documentation = parse_documentation(line);
  159. symbol.uri = uri;
  160. symbol.script_path = path;
  161. symbol.detail = "const " + symbol.name;
  162. if (c.type.kind != GDScriptParser::DataType::UNRESOLVED) {
  163. symbol.detail += ": " + c.type.to_string();
  164. }
  165. symbol.detail += " = " + String(node->value);
  166. r_symbol.children.push_back(symbol);
  167. }
  168. for (int i = 0; i < p_class->functions.size(); ++i) {
  169. const GDScriptParser::FunctionNode *func = p_class->functions[i];
  170. lsp::DocumentSymbol symbol;
  171. parse_function_symbol(func, symbol);
  172. r_symbol.children.push_back(symbol);
  173. }
  174. for (int i = 0; i < p_class->static_functions.size(); ++i) {
  175. const GDScriptParser::FunctionNode *func = p_class->static_functions[i];
  176. lsp::DocumentSymbol symbol;
  177. parse_function_symbol(func, symbol);
  178. r_symbol.children.push_back(symbol);
  179. }
  180. for (int i = 0; i < p_class->subclasses.size(); ++i) {
  181. const GDScriptParser::ClassNode *subclass = p_class->subclasses[i];
  182. lsp::DocumentSymbol symbol;
  183. parse_class_symbol(subclass, symbol);
  184. r_symbol.children.push_back(symbol);
  185. }
  186. }
  187. void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol) {
  188. const String uri = get_uri();
  189. r_symbol.name = p_func->name;
  190. r_symbol.kind = lsp::SymbolKind::Function;
  191. r_symbol.detail = "func " + p_func->name + "(";
  192. r_symbol.deprecated = false;
  193. const int line = LINE_NUMBER_TO_INDEX(p_func->line);
  194. r_symbol.range.start.line = line;
  195. r_symbol.range.start.character = p_func->column;
  196. r_symbol.range.end.line = MAX(p_func->body->end_line - 2, p_func->body->line);
  197. r_symbol.range.end.character = lines[r_symbol.range.end.line].length();
  198. r_symbol.selectionRange.start.line = r_symbol.range.start.line;
  199. r_symbol.documentation = GDScriptWorkspace::marked_documentation(parse_documentation(line));
  200. r_symbol.uri = uri;
  201. r_symbol.script_path = path;
  202. String arguments;
  203. for (int i = 0; i < p_func->arguments.size(); i++) {
  204. lsp::DocumentSymbol symbol;
  205. symbol.kind = lsp::SymbolKind::Variable;
  206. symbol.name = p_func->arguments[i];
  207. symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_func->body->line);
  208. symbol.range.start.character = p_func->body->column;
  209. symbol.range.end = symbol.range.start;
  210. symbol.uri = uri;
  211. symbol.script_path = path;
  212. r_symbol.children.push_back(symbol);
  213. if (i > 0) {
  214. arguments += ", ";
  215. }
  216. arguments += String(p_func->arguments[i]);
  217. if (p_func->argument_types[i].kind != GDScriptParser::DataType::UNRESOLVED) {
  218. arguments += ": " + p_func->argument_types[i].to_string();
  219. }
  220. int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size());
  221. if (default_value_idx >= 0) {
  222. const GDScriptParser::ConstantNode *const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(p_func->default_values[default_value_idx]);
  223. if (const_node == NULL) {
  224. const GDScriptParser::OperatorNode *operator_node = dynamic_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[default_value_idx]);
  225. if (operator_node) {
  226. const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(operator_node->next);
  227. }
  228. }
  229. if (const_node) {
  230. String value = JSON::print(const_node->value);
  231. arguments += " = " + value;
  232. }
  233. }
  234. }
  235. r_symbol.detail += arguments + ")";
  236. if (p_func->return_type.kind != GDScriptParser::DataType::UNRESOLVED) {
  237. r_symbol.detail += " -> " + p_func->return_type.to_string();
  238. }
  239. for (const Map<StringName, LocalVarNode *>::Element *E = p_func->body->variables.front(); E; E = E->next()) {
  240. lsp::DocumentSymbol symbol;
  241. const GDScriptParser::LocalVarNode *var = E->value();
  242. symbol.name = E->key();
  243. symbol.kind = lsp::SymbolKind::Variable;
  244. symbol.range.start.line = LINE_NUMBER_TO_INDEX(E->get()->line);
  245. symbol.range.start.character = E->get()->column;
  246. symbol.range.end.line = symbol.range.start.line;
  247. symbol.range.end.character = lines[symbol.range.end.line].length();
  248. symbol.uri = uri;
  249. symbol.script_path = path;
  250. symbol.detail = "var " + symbol.name;
  251. if (var->datatype.kind != GDScriptParser::DataType::UNRESOLVED) {
  252. symbol.detail += ": " + var->datatype.to_string();
  253. }
  254. symbol.documentation = GDScriptWorkspace::marked_documentation(parse_documentation(line));
  255. r_symbol.children.push_back(symbol);
  256. }
  257. }
  258. String ExtendGDScriptParser::parse_documentation(int p_line) {
  259. ERR_FAIL_INDEX_V(p_line, lines.size(), String());
  260. List<String> doc_lines;
  261. // inline comment
  262. String inline_comment = lines[p_line];
  263. int comment_start = inline_comment.find("#");
  264. if (comment_start != -1) {
  265. inline_comment = inline_comment.substr(comment_start, inline_comment.length());
  266. if (inline_comment.length() > 1) {
  267. doc_lines.push_back(inline_comment.substr(1, inline_comment.length()));
  268. }
  269. }
  270. // upper line comments
  271. for (int i = p_line - 1; i >= 0; --i) {
  272. String line_comment = lines[i].strip_edges(true, false);
  273. if (line_comment.begins_with("#")) {
  274. if (line_comment.length() > 1) {
  275. doc_lines.push_front(line_comment.substr(1, line_comment.length()));
  276. } else {
  277. doc_lines.push_front("");
  278. }
  279. } else {
  280. break;
  281. }
  282. }
  283. String doc;
  284. for (List<String>::Element *E = doc_lines.front(); E; E = E->next()) {
  285. String content = E->get();
  286. doc += content + "\n";
  287. }
  288. return doc;
  289. }
  290. String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_cursor) const {
  291. String longthing;
  292. int len = lines.size();
  293. for (int i = 0; i < len; i++) {
  294. if (i == p_cursor.line) {
  295. longthing += lines[i].substr(0, p_cursor.character);
  296. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  297. longthing += lines[i].substr(p_cursor.character, lines[i].size());
  298. } else {
  299. longthing += lines[i];
  300. }
  301. if (i != len - 1)
  302. longthing += "\n";
  303. }
  304. return longthing;
  305. }
  306. String ExtendGDScriptParser::get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol, bool p_func_requred) const {
  307. String longthing;
  308. int len = lines.size();
  309. for (int i = 0; i < len; i++) {
  310. if (i == p_cursor.line) {
  311. String line = lines[i];
  312. String first_part = line.substr(0, p_cursor.character);
  313. String last_part = line.substr(p_cursor.character, lines[i].size());
  314. if (!p_symbol.empty()) {
  315. String left_cursor_text;
  316. for (int c = p_cursor.character - 1; c >= 0; c--) {
  317. left_cursor_text = line.substr(c, p_cursor.character - c);
  318. if (p_symbol.begins_with(left_cursor_text)) {
  319. first_part = line.substr(0, c);
  320. first_part += p_symbol;
  321. break;
  322. }
  323. }
  324. }
  325. longthing += first_part;
  326. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  327. if (p_func_requred) {
  328. longthing += "("; // tell the parser this is a function call
  329. }
  330. longthing += last_part;
  331. } else {
  332. longthing += lines[i];
  333. }
  334. if (i != len - 1)
  335. longthing += "\n";
  336. }
  337. return longthing;
  338. }
  339. String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const {
  340. ERR_FAIL_INDEX_V(p_position.line, lines.size(), "");
  341. String line = lines[p_position.line];
  342. ERR_FAIL_INDEX_V(p_position.character, line.size(), "");
  343. int start_pos = p_position.character;
  344. for (int c = p_position.character; c >= 0; c--) {
  345. start_pos = c;
  346. CharType ch = line[c];
  347. bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
  348. if (!valid_char) {
  349. break;
  350. }
  351. }
  352. int end_pos = p_position.character;
  353. for (int c = p_position.character; c < line.length(); c++) {
  354. CharType ch = line[c];
  355. bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
  356. if (!valid_char) {
  357. break;
  358. }
  359. end_pos = c;
  360. }
  361. if (start_pos < end_pos) {
  362. p_offset.x = start_pos - p_position.character;
  363. p_offset.y = end_pos - p_position.character;
  364. return line.substr(start_pos + 1, end_pos - start_pos);
  365. }
  366. return "";
  367. }
  368. String ExtendGDScriptParser::get_uri() const {
  369. return GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_uri(path);
  370. }
  371. const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) const {
  372. const lsp::DocumentSymbol *ret = NULL;
  373. if (p_line < p_parent.range.start.line) {
  374. return ret;
  375. } else if (p_parent.range.start.line == p_line) {
  376. return &p_parent;
  377. } else {
  378. for (int i = 0; i < p_parent.children.size(); i++) {
  379. ret = search_symbol_defined_at_line(p_line, p_parent.children[i]);
  380. if (ret) {
  381. break;
  382. }
  383. }
  384. }
  385. return ret;
  386. }
  387. const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const {
  388. if (p_line <= 0) {
  389. return &class_symbol;
  390. }
  391. return search_symbol_defined_at_line(p_line, class_symbol);
  392. }
  393. const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String &p_name) const {
  394. const GDScriptParser::Node *head = get_parse_tree();
  395. if (const GDScriptParser::ClassNode *gdclass = dynamic_cast<const GDScriptParser::ClassNode *>(head)) {
  396. if (const Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = gdclass->constant_expressions.find(p_name)) {
  397. return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(E->get().expression->line));
  398. }
  399. for (int i = 0; i < gdclass->subclasses.size(); i++) {
  400. const ClassNode *m = gdclass->subclasses[i];
  401. if (m && m->name == p_name) {
  402. return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m->line));
  403. }
  404. }
  405. for (int i = 0; i < gdclass->variables.size(); i++) {
  406. const GDScriptParser::ClassNode::Member &m = gdclass->variables[i];
  407. if (m.identifier == p_name) {
  408. return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line));
  409. }
  410. }
  411. for (int i = 0; i < gdclass->functions.size(); i++) {
  412. const GDScriptParser::FunctionNode *m = gdclass->functions[i];
  413. if (m->name == p_name) {
  414. return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m->line));
  415. }
  416. }
  417. for (int i = 0; i < gdclass->static_functions.size(); i++) {
  418. const GDScriptParser::FunctionNode *m = gdclass->static_functions[i];
  419. if (m->name == p_name) {
  420. return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m->line));
  421. }
  422. }
  423. for (int i = 0; i < gdclass->_signals.size(); i++) {
  424. const GDScriptParser::ClassNode::Signal &m = gdclass->_signals[i];
  425. if (m.name == p_name) {
  426. return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line));
  427. }
  428. }
  429. }
  430. return NULL;
  431. }
  432. void ExtendGDScriptParser::dump_symbols(HashMap<String, lsp::DocumentedSymbolInformation> &r_symbols) {
  433. Vector<lsp::DocumentedSymbolInformation> list;
  434. class_symbol.symbol_tree_as_list(path, list, path, true);
  435. for (int i = 0; i < list.size(); i++) {
  436. const lsp::DocumentedSymbolInformation &symbol = list[i];
  437. r_symbols.set(symbol.name, symbol);
  438. }
  439. }
  440. Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) {
  441. path = p_path;
  442. code = p_code;
  443. lines = p_code.split("\n");
  444. Error err = GDScriptParser::parse(p_code, p_path.get_base_dir(), false, p_path, false, NULL, false);
  445. update_diagnostics();
  446. update_symbols();
  447. return err;
  448. }