gdscript_workspace.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*************************************************************************/
  2. /* gdscript_workspace.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/config/project_settings.h"
  34. #include "core/object/script_language.h"
  35. #include "editor/doc_tools.h"
  36. #include "editor/editor_file_system.h"
  37. #include "editor/editor_help.h"
  38. #include "editor/editor_node.h"
  39. #include "gdscript_language_protocol.h"
  40. #include "scene/resources/packed_scene.h"
  41. void GDScriptWorkspace::_bind_methods() {
  42. ClassDB::bind_method(D_METHOD("symbol"), &GDScriptWorkspace::symbol);
  43. ClassDB::bind_method(D_METHOD("parse_script", "path", "content"), &GDScriptWorkspace::parse_script);
  44. ClassDB::bind_method(D_METHOD("parse_local_script", "path"), &GDScriptWorkspace::parse_local_script);
  45. ClassDB::bind_method(D_METHOD("get_file_path", "uri"), &GDScriptWorkspace::get_file_path);
  46. ClassDB::bind_method(D_METHOD("get_file_uri", "path"), &GDScriptWorkspace::get_file_uri);
  47. ClassDB::bind_method(D_METHOD("publish_diagnostics", "path"), &GDScriptWorkspace::publish_diagnostics);
  48. ClassDB::bind_method(D_METHOD("generate_script_api", "path"), &GDScriptWorkspace::generate_script_api);
  49. }
  50. void GDScriptWorkspace::remove_cache_parser(const String &p_path) {
  51. Map<String, ExtendGDScriptParser *>::Element *parser = parse_results.find(p_path);
  52. Map<String, ExtendGDScriptParser *>::Element *script = scripts.find(p_path);
  53. if (parser && script) {
  54. if (script->get() && script->get() == parser->get()) {
  55. memdelete(script->get());
  56. } else {
  57. memdelete(script->get());
  58. memdelete(parser->get());
  59. }
  60. parse_results.erase(p_path);
  61. scripts.erase(p_path);
  62. } else if (parser) {
  63. memdelete(parser->get());
  64. parse_results.erase(p_path);
  65. } else if (script) {
  66. memdelete(script->get());
  67. scripts.erase(p_path);
  68. }
  69. }
  70. const lsp::DocumentSymbol *GDScriptWorkspace::get_native_symbol(const String &p_class, const String &p_member) const {
  71. StringName class_name = p_class;
  72. StringName empty;
  73. while (class_name != empty) {
  74. if (const Map<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.find(class_name)) {
  75. const lsp::DocumentSymbol &class_symbol = E->value();
  76. if (p_member.is_empty()) {
  77. return &class_symbol;
  78. } else {
  79. for (int i = 0; i < class_symbol.children.size(); i++) {
  80. const lsp::DocumentSymbol &symbol = class_symbol.children[i];
  81. if (symbol.name == p_member) {
  82. return &symbol;
  83. }
  84. }
  85. }
  86. }
  87. class_name = ClassDB::get_parent_class(class_name);
  88. }
  89. return nullptr;
  90. }
  91. const lsp::DocumentSymbol *GDScriptWorkspace::get_script_symbol(const String &p_path) const {
  92. const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path);
  93. if (S) {
  94. return &(S->get()->get_symbols());
  95. }
  96. return nullptr;
  97. }
  98. void GDScriptWorkspace::reload_all_workspace_scripts() {
  99. List<String> paths;
  100. list_script_files("res://", paths);
  101. for (List<String>::Element *E = paths.front(); E; E = E->next()) {
  102. const String &path = E->get();
  103. Error err;
  104. String content = FileAccess::get_file_as_string(path, &err);
  105. ERR_CONTINUE(err != OK);
  106. err = parse_script(path, content);
  107. if (err != OK) {
  108. Map<String, ExtendGDScriptParser *>::Element *S = parse_results.find(path);
  109. String err_msg = "Failed parse script " + path;
  110. if (S) {
  111. err_msg += "\n" + S->get()->get_errors()[0].message;
  112. }
  113. ERR_CONTINUE_MSG(err != OK, err_msg);
  114. }
  115. }
  116. }
  117. void GDScriptWorkspace::list_script_files(const String &p_root_dir, List<String> &r_files) {
  118. Error err;
  119. DirAccessRef dir = DirAccess::open(p_root_dir, &err);
  120. if (OK == err) {
  121. dir->list_dir_begin();
  122. String file_name = dir->get_next();
  123. while (file_name.length()) {
  124. if (dir->current_is_dir() && file_name != "." && file_name != ".." && file_name != "./") {
  125. list_script_files(p_root_dir.plus_file(file_name), r_files);
  126. } else if (file_name.ends_with(".gd")) {
  127. String script_file = p_root_dir.plus_file(file_name);
  128. r_files.push_back(script_file);
  129. }
  130. file_name = dir->get_next();
  131. }
  132. }
  133. }
  134. ExtendGDScriptParser *GDScriptWorkspace::get_parse_successed_script(const String &p_path) {
  135. const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path);
  136. if (!S) {
  137. parse_local_script(p_path);
  138. S = scripts.find(p_path);
  139. }
  140. if (S) {
  141. return S->get();
  142. }
  143. return nullptr;
  144. }
  145. ExtendGDScriptParser *GDScriptWorkspace::get_parse_result(const String &p_path) {
  146. const Map<String, ExtendGDScriptParser *>::Element *S = parse_results.find(p_path);
  147. if (!S) {
  148. parse_local_script(p_path);
  149. S = parse_results.find(p_path);
  150. }
  151. if (S) {
  152. return S->get();
  153. }
  154. return nullptr;
  155. }
  156. Array GDScriptWorkspace::symbol(const Dictionary &p_params) {
  157. String query = p_params["query"];
  158. Array arr;
  159. if (!query.is_empty()) {
  160. for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
  161. Vector<lsp::DocumentedSymbolInformation> script_symbols;
  162. E->get()->get_symbols().symbol_tree_as_list(E->key(), script_symbols);
  163. for (int i = 0; i < script_symbols.size(); ++i) {
  164. if (query.is_subsequence_ofi(script_symbols[i].name)) {
  165. arr.push_back(script_symbols[i].to_json());
  166. }
  167. }
  168. }
  169. }
  170. return arr;
  171. }
  172. Error GDScriptWorkspace::initialize() {
  173. if (initialized) {
  174. return OK;
  175. }
  176. DocTools *doc = EditorHelp::get_doc_data();
  177. for (Map<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) {
  178. const DocData::ClassDoc &class_data = E->value();
  179. lsp::DocumentSymbol class_symbol;
  180. String class_name = E->key();
  181. class_symbol.name = class_name;
  182. class_symbol.native_class = class_name;
  183. class_symbol.kind = lsp::SymbolKind::Class;
  184. class_symbol.detail = String("<Native> class ") + class_name;
  185. if (!class_data.inherits.is_empty()) {
  186. class_symbol.detail += " extends " + class_data.inherits;
  187. }
  188. class_symbol.documentation = class_data.brief_description + "\n" + class_data.description;
  189. for (int i = 0; i < class_data.constants.size(); i++) {
  190. const DocData::ConstantDoc &const_data = class_data.constants[i];
  191. lsp::DocumentSymbol symbol;
  192. symbol.name = const_data.name;
  193. symbol.native_class = class_name;
  194. symbol.kind = lsp::SymbolKind::Constant;
  195. symbol.detail = "const " + class_name + "." + const_data.name;
  196. if (const_data.enumeration.length()) {
  197. symbol.detail += ": " + const_data.enumeration;
  198. }
  199. symbol.detail += " = " + const_data.value;
  200. symbol.documentation = const_data.description;
  201. class_symbol.children.push_back(symbol);
  202. }
  203. Vector<DocData::PropertyDoc> properties;
  204. properties.append_array(class_data.properties);
  205. const int theme_prop_start_idx = properties.size();
  206. properties.append_array(class_data.theme_properties);
  207. for (int i = 0; i < class_data.properties.size(); i++) {
  208. const DocData::PropertyDoc &data = class_data.properties[i];
  209. lsp::DocumentSymbol symbol;
  210. symbol.name = data.name;
  211. symbol.native_class = class_name;
  212. symbol.kind = lsp::SymbolKind::Property;
  213. symbol.detail = String(i >= theme_prop_start_idx ? "<Theme> var" : "var") + " " + class_name + "." + data.name;
  214. if (data.enumeration.length()) {
  215. symbol.detail += ": " + data.enumeration;
  216. } else {
  217. symbol.detail += ": " + data.type;
  218. }
  219. symbol.documentation = data.description;
  220. class_symbol.children.push_back(symbol);
  221. }
  222. Vector<DocData::MethodDoc> methods_signals;
  223. methods_signals.append_array(class_data.methods);
  224. const int signal_start_idx = methods_signals.size();
  225. methods_signals.append_array(class_data.signals);
  226. for (int i = 0; i < methods_signals.size(); i++) {
  227. const DocData::MethodDoc &data = methods_signals[i];
  228. lsp::DocumentSymbol symbol;
  229. symbol.name = data.name;
  230. symbol.native_class = class_name;
  231. symbol.kind = i >= signal_start_idx ? lsp::SymbolKind::Event : lsp::SymbolKind::Method;
  232. String params = "";
  233. bool arg_default_value_started = false;
  234. for (int j = 0; j < data.arguments.size(); j++) {
  235. const DocData::ArgumentDoc &arg = data.arguments[j];
  236. lsp::DocumentSymbol symbol_arg;
  237. symbol_arg.name = arg.name;
  238. symbol_arg.kind = lsp::SymbolKind::Variable;
  239. symbol_arg.detail = arg.type;
  240. if (!arg_default_value_started && !arg.default_value.is_empty()) {
  241. arg_default_value_started = true;
  242. }
  243. String arg_str = arg.name + ": " + arg.type;
  244. if (arg_default_value_started) {
  245. arg_str += " = " + arg.default_value;
  246. }
  247. if (j < data.arguments.size() - 1) {
  248. arg_str += ", ";
  249. }
  250. params += arg_str;
  251. symbol.children.push_back(symbol_arg);
  252. }
  253. if (data.qualifiers.find("vararg") != -1) {
  254. params += params.is_empty() ? "..." : ", ...";
  255. }
  256. String return_type = data.return_type;
  257. if (return_type.is_empty()) {
  258. return_type = "void";
  259. }
  260. symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + return_type;
  261. symbol.documentation = data.description;
  262. class_symbol.children.push_back(symbol);
  263. }
  264. native_symbols.insert(class_name, class_symbol);
  265. }
  266. reload_all_workspace_scripts();
  267. if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  268. for (Map<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.front(); E; E = E->next()) {
  269. ClassMembers members;
  270. const lsp::DocumentSymbol &class_symbol = E->get();
  271. for (int i = 0; i < class_symbol.children.size(); i++) {
  272. const lsp::DocumentSymbol &symbol = class_symbol.children[i];
  273. members.set(symbol.name, &symbol);
  274. }
  275. native_members.set(E->key(), members);
  276. }
  277. // cache member completions
  278. for (Map<String, ExtendGDScriptParser *>::Element *S = scripts.front(); S; S = S->next()) {
  279. S->get()->get_member_completions();
  280. }
  281. }
  282. return OK;
  283. }
  284. Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_content) {
  285. ExtendGDScriptParser *parser = memnew(ExtendGDScriptParser);
  286. Error err = parser->parse(p_content, p_path);
  287. Map<String, ExtendGDScriptParser *>::Element *last_parser = parse_results.find(p_path);
  288. Map<String, ExtendGDScriptParser *>::Element *last_script = scripts.find(p_path);
  289. if (err == OK) {
  290. remove_cache_parser(p_path);
  291. parse_results[p_path] = parser;
  292. scripts[p_path] = parser;
  293. } else {
  294. if (last_parser && last_script && last_parser->get() != last_script->get()) {
  295. memdelete(last_parser->get());
  296. }
  297. parse_results[p_path] = parser;
  298. }
  299. publish_diagnostics(p_path);
  300. return err;
  301. }
  302. Error GDScriptWorkspace::parse_local_script(const String &p_path) {
  303. Error err;
  304. String content = FileAccess::get_file_as_string(p_path, &err);
  305. if (err == OK) {
  306. err = parse_script(p_path, content);
  307. }
  308. return err;
  309. }
  310. String GDScriptWorkspace::get_file_path(const String &p_uri) const {
  311. String path = p_uri;
  312. path = path.replace(root_uri + "/", "res://");
  313. path = path.uri_decode();
  314. return path;
  315. }
  316. String GDScriptWorkspace::get_file_uri(const String &p_path) const {
  317. String uri = p_path;
  318. uri = uri.replace("res://", root_uri + "/");
  319. return uri;
  320. }
  321. void GDScriptWorkspace::publish_diagnostics(const String &p_path) {
  322. Dictionary params;
  323. Array errors;
  324. const Map<String, ExtendGDScriptParser *>::Element *ele = parse_results.find(p_path);
  325. if (ele) {
  326. const Vector<lsp::Diagnostic> &list = ele->get()->get_diagnostics();
  327. errors.resize(list.size());
  328. for (int i = 0; i < list.size(); ++i) {
  329. errors[i] = list[i].to_json();
  330. }
  331. }
  332. params["diagnostics"] = errors;
  333. params["uri"] = get_file_uri(p_path);
  334. GDScriptLanguageProtocol::get_singleton()->notify_client("textDocument/publishDiagnostics", params);
  335. }
  336. void GDScriptWorkspace::_get_owners(EditorFileSystemDirectory *efsd, String p_path, List<String> &owners) {
  337. if (!efsd) {
  338. return;
  339. }
  340. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  341. _get_owners(efsd->get_subdir(i), p_path, owners);
  342. }
  343. for (int i = 0; i < efsd->get_file_count(); i++) {
  344. Vector<String> deps = efsd->get_file_deps(i);
  345. bool found = false;
  346. for (int j = 0; j < deps.size(); j++) {
  347. if (deps[j] == p_path) {
  348. found = true;
  349. break;
  350. }
  351. }
  352. if (!found) {
  353. continue;
  354. }
  355. owners.push_back(efsd->get_file_path(i));
  356. }
  357. }
  358. Node *GDScriptWorkspace::_get_owner_scene_node(String p_path) {
  359. Node *owner_scene_node = nullptr;
  360. List<String> owners;
  361. _get_owners(EditorFileSystem::get_singleton()->get_filesystem(), p_path, owners);
  362. for (int i = 0; i < owners.size(); i++) {
  363. NodePath owner_path = owners[i];
  364. RES owner_res = ResourceLoader::load(owner_path);
  365. if (Object::cast_to<PackedScene>(owner_res.ptr())) {
  366. Ref<PackedScene> owner_packed_scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*owner_res));
  367. owner_scene_node = owner_packed_scene->instance();
  368. break;
  369. }
  370. }
  371. return owner_scene_node;
  372. }
  373. void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *r_options) {
  374. String path = get_file_path(p_params.textDocument.uri);
  375. String call_hint;
  376. bool forced = false;
  377. if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
  378. Node *owner_scene_node = _get_owner_scene_node(path);
  379. String code = parser->get_text_for_completion(p_params.position);
  380. GDScriptLanguage::get_singleton()->complete_code(code, path, owner_scene_node, r_options, forced, call_hint);
  381. if (owner_scene_node) {
  382. memdelete(owner_scene_node);
  383. }
  384. }
  385. }
  386. const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name, bool p_func_requred) {
  387. const lsp::DocumentSymbol *symbol = nullptr;
  388. String path = get_file_path(p_doc_pos.textDocument.uri);
  389. if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
  390. String symbol_identifier = p_symbol_name;
  391. Vector<String> identifier_parts = symbol_identifier.split("(");
  392. if (identifier_parts.size()) {
  393. symbol_identifier = identifier_parts[0];
  394. }
  395. lsp::Position pos = p_doc_pos.position;
  396. if (symbol_identifier.is_empty()) {
  397. Vector2i offset;
  398. symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset);
  399. pos.character += offset.y;
  400. }
  401. if (!symbol_identifier.is_empty()) {
  402. if (ScriptServer::is_global_class(symbol_identifier)) {
  403. String class_path = ScriptServer::get_global_class_path(symbol_identifier);
  404. symbol = get_script_symbol(class_path);
  405. } else {
  406. ScriptLanguage::LookupResult ret;
  407. if (OK == GDScriptLanguage::get_singleton()->lookup_code(parser->get_text_for_lookup_symbol(pos, symbol_identifier, p_func_requred), symbol_identifier, path, nullptr, ret)) {
  408. if (ret.type == ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION) {
  409. String target_script_path = path;
  410. if (!ret.script.is_null()) {
  411. target_script_path = ret.script->get_path();
  412. }
  413. if (const ExtendGDScriptParser *target_parser = get_parse_result(target_script_path)) {
  414. symbol = target_parser->get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(ret.location));
  415. }
  416. } else {
  417. String member = ret.class_member;
  418. if (member.is_empty() && symbol_identifier != ret.class_name) {
  419. member = symbol_identifier;
  420. }
  421. symbol = get_native_symbol(ret.class_name, member);
  422. }
  423. } else {
  424. symbol = parser->get_member_symbol(symbol_identifier);
  425. }
  426. }
  427. }
  428. }
  429. return symbol;
  430. }
  431. void GDScriptWorkspace::resolve_related_symbols(const lsp::TextDocumentPositionParams &p_doc_pos, List<const lsp::DocumentSymbol *> &r_list) {
  432. String path = get_file_path(p_doc_pos.textDocument.uri);
  433. if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
  434. String symbol_identifier;
  435. Vector2i offset;
  436. symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset);
  437. const StringName *class_ptr = native_members.next(nullptr);
  438. while (class_ptr) {
  439. const ClassMembers &members = native_members.get(*class_ptr);
  440. if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
  441. r_list.push_back(*symbol);
  442. }
  443. class_ptr = native_members.next(class_ptr);
  444. }
  445. for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
  446. const ExtendGDScriptParser *script = E->get();
  447. const ClassMembers &members = script->get_members();
  448. if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
  449. r_list.push_back(*symbol);
  450. }
  451. const HashMap<String, ClassMembers> &inner_classes = script->get_inner_classes();
  452. const String *_class = inner_classes.next(nullptr);
  453. while (_class) {
  454. const ClassMembers *inner_class = inner_classes.getptr(*_class);
  455. if (const lsp::DocumentSymbol *const *symbol = inner_class->getptr(symbol_identifier)) {
  456. r_list.push_back(*symbol);
  457. }
  458. _class = inner_classes.next(_class);
  459. }
  460. }
  461. }
  462. }
  463. const lsp::DocumentSymbol *GDScriptWorkspace::resolve_native_symbol(const lsp::NativeSymbolInspectParams &p_params) {
  464. if (Map<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.find(p_params.native_class)) {
  465. const lsp::DocumentSymbol &symbol = E->get();
  466. if (p_params.symbol_name.is_empty() || p_params.symbol_name == symbol.name) {
  467. return &symbol;
  468. }
  469. for (int i = 0; i < symbol.children.size(); ++i) {
  470. if (symbol.children[i].name == p_params.symbol_name) {
  471. return &(symbol.children[i]);
  472. }
  473. }
  474. }
  475. return nullptr;
  476. }
  477. void GDScriptWorkspace::resolve_document_links(const String &p_uri, List<lsp::DocumentLink> &r_list) {
  478. if (const ExtendGDScriptParser *parser = get_parse_successed_script(get_file_path(p_uri))) {
  479. const List<lsp::DocumentLink> &links = parser->get_document_links();
  480. for (const List<lsp::DocumentLink>::Element *E = links.front(); E; E = E->next()) {
  481. r_list.push_back(E->get());
  482. }
  483. }
  484. }
  485. Dictionary GDScriptWorkspace::generate_script_api(const String &p_path) {
  486. Dictionary api;
  487. if (const ExtendGDScriptParser *parser = get_parse_successed_script(p_path)) {
  488. api = parser->generate_api();
  489. }
  490. return api;
  491. }
  492. Error GDScriptWorkspace::resolve_signature(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::SignatureHelp &r_signature) {
  493. if (const ExtendGDScriptParser *parser = get_parse_result(get_file_path(p_doc_pos.textDocument.uri))) {
  494. lsp::TextDocumentPositionParams text_pos;
  495. text_pos.textDocument = p_doc_pos.textDocument;
  496. if (parser->get_left_function_call(p_doc_pos.position, text_pos.position, r_signature.activeParameter) == OK) {
  497. List<const lsp::DocumentSymbol *> symbols;
  498. if (const lsp::DocumentSymbol *symbol = resolve_symbol(text_pos)) {
  499. symbols.push_back(symbol);
  500. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  501. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(text_pos, symbols);
  502. }
  503. for (List<const lsp::DocumentSymbol *>::Element *E = symbols.front(); E; E = E->next()) {
  504. const lsp::DocumentSymbol *symbol = E->get();
  505. if (symbol->kind == lsp::SymbolKind::Method || symbol->kind == lsp::SymbolKind::Function) {
  506. lsp::SignatureInformation signature_info;
  507. signature_info.label = symbol->detail;
  508. signature_info.documentation = symbol->render();
  509. for (int i = 0; i < symbol->children.size(); i++) {
  510. const lsp::DocumentSymbol &arg = symbol->children[i];
  511. lsp::ParameterInformation arg_info;
  512. arg_info.label = arg.name;
  513. signature_info.parameters.push_back(arg_info);
  514. }
  515. r_signature.signatures.push_back(signature_info);
  516. break;
  517. }
  518. }
  519. if (r_signature.signatures.size()) {
  520. return OK;
  521. }
  522. }
  523. }
  524. return ERR_METHOD_NOT_FOUND;
  525. }
  526. GDScriptWorkspace::GDScriptWorkspace() {
  527. ProjectSettings::get_singleton()->get_resource_path();
  528. }
  529. GDScriptWorkspace::~GDScriptWorkspace() {
  530. Set<String> cached_parsers;
  531. for (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.front(); E; E = E->next()) {
  532. cached_parsers.insert(E->key());
  533. }
  534. for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
  535. cached_parsers.insert(E->key());
  536. }
  537. for (Set<String>::Element *E = cached_parsers.front(); E; E = E->next()) {
  538. remove_cache_parser(E->get());
  539. }
  540. }