gdscript_workspace.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*************************************************************************/
  2. /* gdscript_workspace.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_workspace.h"
  31. #include "../gdscript.h"
  32. #include "../gdscript_parser.h"
  33. #include "core/project_settings.h"
  34. #include "core/script_language.h"
  35. #include "editor/editor_help.h"
  36. #include "gdscript_language_protocol.h"
  37. void GDScriptWorkspace::_bind_methods() {
  38. ClassDB::bind_method(D_METHOD("symbol"), &GDScriptWorkspace::symbol);
  39. }
  40. void GDScriptWorkspace::remove_cache_parser(const String &p_path) {
  41. Map<String, ExtendGDScriptParser *>::Element *parser = parse_results.find(p_path);
  42. Map<String, ExtendGDScriptParser *>::Element *script = scripts.find(p_path);
  43. if (parser && script) {
  44. if (script->get() && script->get() == script->get()) {
  45. memdelete(script->get());
  46. } else {
  47. memdelete(script->get());
  48. memdelete(parser->get());
  49. }
  50. parse_results.erase(p_path);
  51. scripts.erase(p_path);
  52. } else if (parser) {
  53. memdelete(parser->get());
  54. parse_results.erase(p_path);
  55. } else if (script) {
  56. memdelete(script->get());
  57. scripts.erase(p_path);
  58. }
  59. }
  60. const lsp::DocumentSymbol *GDScriptWorkspace::get_native_symbol(const String &p_class, const String &p_member) const {
  61. StringName class_name = p_class;
  62. StringName end_pos;
  63. while (class_name != end_pos) {
  64. if (const Map<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.find(class_name)) {
  65. const lsp::DocumentSymbol &class_symbol = E->value();
  66. if (p_member.empty()) {
  67. return &class_symbol;
  68. } else {
  69. for (int i = 0; i < class_symbol.children.size(); i++) {
  70. const lsp::DocumentSymbol &symbol = class_symbol.children[i];
  71. if (symbol.name == p_member) {
  72. return &symbol;
  73. }
  74. }
  75. }
  76. }
  77. class_name = ClassDB::get_parent_class(class_name);
  78. }
  79. return NULL;
  80. }
  81. const lsp::DocumentSymbol *GDScriptWorkspace::get_script_symbol(const String &p_path) const {
  82. const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path);
  83. if (S) {
  84. return &(S->get()->get_symbols());
  85. }
  86. return NULL;
  87. }
  88. void GDScriptWorkspace::reload_all_workspace_scripts() {
  89. List<String> pathes;
  90. list_script_files("res://", pathes);
  91. for (List<String>::Element *E = pathes.front(); E; E = E->next()) {
  92. const String &path = E->get();
  93. Error err;
  94. String content = FileAccess::get_file_as_string(path, &err);
  95. ERR_CONTINUE(err != OK);
  96. err = parse_script(path, content);
  97. if (err != OK) {
  98. Map<String, ExtendGDScriptParser *>::Element *S = parse_results.find(path);
  99. String err_msg = "Failed parse script " + path;
  100. if (S) {
  101. err_msg += "\n" + S->get()->get_error();
  102. }
  103. ERR_EXPLAIN(err_msg);
  104. ERR_CONTINUE(err != OK);
  105. }
  106. }
  107. }
  108. void GDScriptWorkspace::list_script_files(const String &p_root_dir, List<String> &r_files) {
  109. Error err;
  110. DirAccessRef dir = DirAccess::open(p_root_dir, &err);
  111. if (OK == err) {
  112. dir->list_dir_begin();
  113. String file_name = dir->get_next();
  114. while (file_name.length()) {
  115. if (dir->current_is_dir() && file_name != "." && file_name != ".." && file_name != "./") {
  116. list_script_files(p_root_dir.plus_file(file_name), r_files);
  117. } else if (file_name.ends_with(".gd")) {
  118. String script_file = p_root_dir.plus_file(file_name);
  119. r_files.push_back(script_file);
  120. }
  121. file_name = dir->get_next();
  122. }
  123. }
  124. }
  125. ExtendGDScriptParser *GDScriptWorkspace::get_parse_successed_script(const String &p_path) {
  126. const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path);
  127. if (!S) {
  128. parse_local_script(p_path);
  129. S = scripts.find(p_path);
  130. }
  131. if (S) {
  132. return S->get();
  133. }
  134. return NULL;
  135. }
  136. ExtendGDScriptParser *GDScriptWorkspace::get_parse_result(const String &p_path) {
  137. const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path);
  138. if (!S) {
  139. S = parse_results.find(p_path);
  140. if (!S) {
  141. parse_local_script(p_path);
  142. S = scripts.find(p_path);
  143. }
  144. }
  145. if (S) {
  146. return S->get();
  147. }
  148. return NULL;
  149. }
  150. String GDScriptWorkspace::marked_documentation(const String &p_bbcode) {
  151. String markdown = p_bbcode.strip_edges();
  152. Vector<String> lines = markdown.split("\n");
  153. bool in_code_block = false;
  154. int code_block_indent = -1;
  155. markdown = "";
  156. for (int i = 0; i < lines.size(); i++) {
  157. String line = lines[i];
  158. line = line.replace("[code]", "`");
  159. line = line.replace("[/code]", "`");
  160. line = line.replace("[i]", "*");
  161. line = line.replace("[/i]", "*");
  162. line = line.replace("[b]", "**");
  163. line = line.replace("[/b]", "**");
  164. line = line.replace("[u]", "__");
  165. line = line.replace("[/u]", "__");
  166. int block_start = line.find("[codeblock]");
  167. if (block_start != -1) {
  168. code_block_indent = block_start;
  169. in_code_block = true;
  170. line = "'''gdscript";
  171. line = "\n";
  172. } else if (in_code_block) {
  173. line = "\t" + line.substr(code_block_indent, line.length());
  174. } else {
  175. line = line.strip_edges();
  176. }
  177. if (in_code_block && line.find("[/codeblock]") != -1) {
  178. line = "'''\n";
  179. line = "\n";
  180. in_code_block = false;
  181. }
  182. if (!in_code_block && i < lines.size() - 1) {
  183. line += "\n";
  184. }
  185. markdown += line + "\n";
  186. }
  187. return markdown;
  188. }
  189. Array GDScriptWorkspace::symbol(const Dictionary &p_params) {
  190. String query = p_params["query"];
  191. Array arr;
  192. if (!query.empty()) {
  193. for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
  194. Vector<lsp::DocumentedSymbolInformation> script_symbols;
  195. E->get()->get_symbols().symbol_tree_as_list(E->key(), script_symbols);
  196. for (int i = 0; i < script_symbols.size(); ++i) {
  197. if (query.is_subsequence_ofi(script_symbols[i].name)) {
  198. arr.push_back(script_symbols[i].to_json());
  199. }
  200. }
  201. }
  202. }
  203. return arr;
  204. }
  205. Error GDScriptWorkspace::initialize() {
  206. if (initialized) return OK;
  207. DocData *doc = EditorHelp::get_doc_data();
  208. for (Map<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) {
  209. const DocData::ClassDoc &class_data = E->value();
  210. lsp::DocumentSymbol class_symbol;
  211. String class_name = E->key();
  212. class_symbol.name = class_name;
  213. class_symbol.kind = lsp::SymbolKind::Class;
  214. class_symbol.detail = String("<Native> class ") + class_name;
  215. if (!class_data.inherits.empty()) {
  216. class_symbol.detail += " extends " + class_data.inherits;
  217. }
  218. class_symbol.documentation = marked_documentation(class_data.brief_description) + "\n" + marked_documentation(class_data.description);
  219. for (int i = 0; i < class_data.constants.size(); i++) {
  220. const DocData::ConstantDoc &const_data = class_data.constants[i];
  221. lsp::DocumentSymbol symbol;
  222. symbol.name = const_data.name;
  223. symbol.kind = lsp::SymbolKind::Constant;
  224. symbol.detail = "const " + class_name + "." + const_data.name;
  225. if (const_data.enumeration.length()) {
  226. symbol.detail += ": " + const_data.enumeration;
  227. }
  228. symbol.detail += " = " + const_data.value;
  229. symbol.documentation = marked_documentation(const_data.description);
  230. class_symbol.children.push_back(symbol);
  231. }
  232. Vector<DocData::PropertyDoc> properties;
  233. properties.append_array(class_data.properties);
  234. const int theme_prop_start_idx = properties.size();
  235. properties.append_array(class_data.theme_properties);
  236. for (int i = 0; i < class_data.properties.size(); i++) {
  237. const DocData::PropertyDoc &data = class_data.properties[i];
  238. lsp::DocumentSymbol symbol;
  239. symbol.name = data.name;
  240. symbol.kind = lsp::SymbolKind::Property;
  241. symbol.detail = String(i >= theme_prop_start_idx ? "<Theme> var" : "var") + " " + class_name + "." + data.name;
  242. if (data.enumeration.length()) {
  243. symbol.detail += ": " + data.enumeration;
  244. } else {
  245. symbol.detail += ": " + data.type;
  246. }
  247. symbol.documentation = marked_documentation(data.description);
  248. class_symbol.children.push_back(symbol);
  249. }
  250. Vector<DocData::MethodDoc> methods_signals;
  251. methods_signals.append_array(class_data.methods);
  252. const int signal_start_idx = methods_signals.size();
  253. methods_signals.append_array(class_data.signals);
  254. for (int i = 0; i < methods_signals.size(); i++) {
  255. const DocData::MethodDoc &data = methods_signals[i];
  256. lsp::DocumentSymbol symbol;
  257. symbol.name = data.name;
  258. symbol.kind = i >= signal_start_idx ? lsp::SymbolKind::Event : lsp::SymbolKind::Method;
  259. String params = "";
  260. bool arg_default_value_started = false;
  261. for (int j = 0; j < data.arguments.size(); j++) {
  262. const DocData::ArgumentDoc &arg = data.arguments[j];
  263. if (!arg_default_value_started && !arg.default_value.empty()) {
  264. arg_default_value_started = true;
  265. }
  266. String arg_str = arg.name + ": " + arg.type;
  267. if (arg_default_value_started) {
  268. arg_str += " = " + arg.default_value;
  269. }
  270. if (j < data.arguments.size() - 1) {
  271. arg_str += ", ";
  272. }
  273. params += arg_str;
  274. }
  275. if (data.qualifiers.find("vararg") != -1) {
  276. params += params.empty() ? "..." : ", ...";
  277. }
  278. symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + data.return_type;
  279. symbol.documentation = marked_documentation(data.description);
  280. class_symbol.children.push_back(symbol);
  281. }
  282. native_symbols.insert(class_name, class_symbol);
  283. }
  284. reload_all_workspace_scripts();
  285. return OK;
  286. }
  287. Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_content) {
  288. ExtendGDScriptParser *parser = memnew(ExtendGDScriptParser);
  289. Error err = parser->parse(p_content, p_path);
  290. Map<String, ExtendGDScriptParser *>::Element *last_parser = parse_results.find(p_path);
  291. Map<String, ExtendGDScriptParser *>::Element *last_script = scripts.find(p_path);
  292. if (err == OK) {
  293. remove_cache_parser(p_path);
  294. parse_results[p_path] = parser;
  295. scripts[p_path] = parser;
  296. } else {
  297. if (last_parser && last_script && last_parser->get() != last_script->get()) {
  298. memdelete(last_parser->get());
  299. }
  300. parse_results[p_path] = parser;
  301. }
  302. publish_diagnostics(p_path);
  303. return err;
  304. }
  305. Error GDScriptWorkspace::parse_local_script(const String &p_path) {
  306. Error err;
  307. String content = FileAccess::get_file_as_string(p_path, &err);
  308. if (err == OK) {
  309. err = parse_script(p_path, content);
  310. }
  311. return err;
  312. }
  313. String GDScriptWorkspace::get_file_path(const String &p_uri) const {
  314. String path = p_uri.replace("file://", "").http_unescape();
  315. path = path.replace(root + "/", "res://");
  316. return ProjectSettings::get_singleton()->localize_path(path);
  317. }
  318. String GDScriptWorkspace::get_file_uri(const String &p_path) const {
  319. String path = ProjectSettings::get_singleton()->globalize_path(p_path);
  320. return "file://" + path;
  321. }
  322. void GDScriptWorkspace::publish_diagnostics(const String &p_path) {
  323. Dictionary params;
  324. Array errors;
  325. const Map<String, ExtendGDScriptParser *>::Element *ele = parse_results.find(p_path);
  326. if (ele) {
  327. const Vector<lsp::Diagnostic> &list = ele->get()->get_diagnostics();
  328. errors.resize(list.size());
  329. for (int i = 0; i < list.size(); ++i) {
  330. errors[i] = list[i].to_json();
  331. }
  332. }
  333. params["diagnostics"] = errors;
  334. params["uri"] = get_file_uri(p_path);
  335. GDScriptLanguageProtocol::get_singleton()->notify_client("textDocument/publishDiagnostics", params);
  336. }
  337. void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *r_options) {
  338. String path = get_file_path(p_params.textDocument.uri);
  339. String call_hint;
  340. bool forced = false;
  341. if (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.find(path)) {
  342. String code = E->get()->get_text_for_completion(p_params.position);
  343. GDScriptLanguage::get_singleton()->complete_code(code, path, NULL, r_options, forced, call_hint);
  344. }
  345. }
  346. const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name, bool p_func_requred) {
  347. const lsp::DocumentSymbol *symbol = NULL;
  348. String path = get_file_path(p_doc_pos.textDocument.uri);
  349. if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
  350. String symbol_identifier = p_symbol_name;
  351. lsp::Position pos = p_doc_pos.position;
  352. if (symbol_identifier.empty()) {
  353. Vector2i offset;
  354. symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset);
  355. pos.character += offset.y;
  356. }
  357. if (!symbol_identifier.empty()) {
  358. if (ScriptServer::is_global_class(symbol_identifier)) {
  359. String class_path = ScriptServer::get_global_class_path(symbol_identifier);
  360. symbol = get_script_symbol(class_path);
  361. } else {
  362. ScriptLanguage::LookupResult ret;
  363. if (OK == GDScriptLanguage::get_singleton()->lookup_code(parser->get_text_for_lookup_symbol(pos, symbol_identifier, p_func_requred), symbol_identifier, path, NULL, ret)) {
  364. if (ret.type == ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION) {
  365. String target_script_path = path;
  366. if (!ret.script.is_null()) {
  367. target_script_path = ret.script->get_path();
  368. }
  369. if (const ExtendGDScriptParser *target_parser = get_parse_result(target_script_path)) {
  370. symbol = target_parser->get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(ret.location));
  371. }
  372. } else {
  373. String member = ret.class_member;
  374. if (member.empty() && symbol_identifier != ret.class_name) {
  375. member = symbol_identifier;
  376. }
  377. symbol = get_native_symbol(ret.class_name, member);
  378. }
  379. } else {
  380. symbol = parser->get_member_symbol(symbol_identifier);
  381. }
  382. }
  383. }
  384. }
  385. return symbol;
  386. }
  387. GDScriptWorkspace::GDScriptWorkspace() {
  388. ProjectSettings::get_singleton()->get_resource_path();
  389. }
  390. GDScriptWorkspace::~GDScriptWorkspace() {
  391. Set<String> cached_parsers;
  392. for (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.front(); E; E = E->next()) {
  393. cached_parsers.insert(E->key());
  394. }
  395. for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
  396. cached_parsers.insert(E->key());
  397. }
  398. for (Set<String>::Element *E = cached_parsers.front(); E; E = E->next()) {
  399. remove_cache_parser(E->get());
  400. }
  401. }