gdscript_workspace.cpp 22 KB

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