gdscript_workspace.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 NULL;
  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 NULL;
  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 NULL;
  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 NULL;
  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) return OK;
  173. DocData *doc = EditorHelp::get_doc_data();
  174. for (Map<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) {
  175. const DocData::ClassDoc &class_data = E->value();
  176. lsp::DocumentSymbol class_symbol;
  177. String class_name = E->key();
  178. class_symbol.name = class_name;
  179. class_symbol.native_class = class_name;
  180. class_symbol.kind = lsp::SymbolKind::Class;
  181. class_symbol.detail = String("<Native> class ") + class_name;
  182. if (!class_data.inherits.empty()) {
  183. class_symbol.detail += " extends " + class_data.inherits;
  184. }
  185. class_symbol.documentation = class_data.brief_description + "\n" + class_data.description;
  186. for (int i = 0; i < class_data.constants.size(); i++) {
  187. const DocData::ConstantDoc &const_data = class_data.constants[i];
  188. lsp::DocumentSymbol symbol;
  189. symbol.name = const_data.name;
  190. symbol.native_class = class_name;
  191. symbol.kind = lsp::SymbolKind::Constant;
  192. symbol.detail = "const " + class_name + "." + const_data.name;
  193. if (const_data.enumeration.length()) {
  194. symbol.detail += ": " + const_data.enumeration;
  195. }
  196. symbol.detail += " = " + const_data.value;
  197. symbol.documentation = const_data.description;
  198. class_symbol.children.push_back(symbol);
  199. }
  200. Vector<DocData::PropertyDoc> properties;
  201. properties.append_array(class_data.properties);
  202. const int theme_prop_start_idx = properties.size();
  203. properties.append_array(class_data.theme_properties);
  204. for (int i = 0; i < class_data.properties.size(); i++) {
  205. const DocData::PropertyDoc &data = class_data.properties[i];
  206. lsp::DocumentSymbol symbol;
  207. symbol.name = data.name;
  208. symbol.native_class = class_name;
  209. symbol.kind = lsp::SymbolKind::Property;
  210. symbol.detail = String(i >= theme_prop_start_idx ? "<Theme> var" : "var") + " " + class_name + "." + data.name;
  211. if (data.enumeration.length()) {
  212. symbol.detail += ": " + data.enumeration;
  213. } else {
  214. symbol.detail += ": " + data.type;
  215. }
  216. symbol.documentation = data.description;
  217. class_symbol.children.push_back(symbol);
  218. }
  219. Vector<DocData::MethodDoc> methods_signals;
  220. methods_signals.append_array(class_data.methods);
  221. const int signal_start_idx = methods_signals.size();
  222. methods_signals.append_array(class_data.signals);
  223. for (int i = 0; i < methods_signals.size(); i++) {
  224. const DocData::MethodDoc &data = methods_signals[i];
  225. lsp::DocumentSymbol symbol;
  226. symbol.name = data.name;
  227. symbol.native_class = class_name;
  228. symbol.kind = i >= signal_start_idx ? lsp::SymbolKind::Event : lsp::SymbolKind::Method;
  229. String params = "";
  230. bool arg_default_value_started = false;
  231. for (int j = 0; j < data.arguments.size(); j++) {
  232. const DocData::ArgumentDoc &arg = data.arguments[j];
  233. lsp::DocumentSymbol symbol_arg;
  234. symbol_arg.name = arg.name;
  235. symbol_arg.kind = lsp::SymbolKind::Variable;
  236. symbol_arg.detail = arg.type;
  237. if (!arg_default_value_started && !arg.default_value.empty()) {
  238. arg_default_value_started = true;
  239. }
  240. String arg_str = arg.name + ": " + arg.type;
  241. if (arg_default_value_started) {
  242. arg_str += " = " + arg.default_value;
  243. }
  244. if (j < data.arguments.size() - 1) {
  245. arg_str += ", ";
  246. }
  247. params += arg_str;
  248. symbol.children.push_back(symbol_arg);
  249. }
  250. if (data.qualifiers.find("vararg") != -1) {
  251. params += params.empty() ? "..." : ", ...";
  252. }
  253. String return_type = data.return_type;
  254. if (return_type.empty()) {
  255. return_type = "void";
  256. }
  257. symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + return_type;
  258. symbol.documentation = data.description;
  259. class_symbol.children.push_back(symbol);
  260. }
  261. native_symbols.insert(class_name, class_symbol);
  262. }
  263. reload_all_workspace_scripts();
  264. if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  265. for (Map<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.front(); E; E = E->next()) {
  266. ClassMembers members;
  267. const lsp::DocumentSymbol &class_symbol = E->get();
  268. for (int i = 0; i < class_symbol.children.size(); i++) {
  269. const lsp::DocumentSymbol &symbol = class_symbol.children[i];
  270. members.set(symbol.name, &symbol);
  271. }
  272. native_members.set(E->key(), members);
  273. }
  274. // cache member completions
  275. for (Map<String, ExtendGDScriptParser *>::Element *S = scripts.front(); S; S = S->next()) {
  276. S->get()->get_member_completions();
  277. }
  278. }
  279. return OK;
  280. }
  281. Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_content) {
  282. ExtendGDScriptParser *parser = memnew(ExtendGDScriptParser);
  283. Error err = parser->parse(p_content, p_path);
  284. Map<String, ExtendGDScriptParser *>::Element *last_parser = parse_results.find(p_path);
  285. Map<String, ExtendGDScriptParser *>::Element *last_script = scripts.find(p_path);
  286. if (err == OK) {
  287. remove_cache_parser(p_path);
  288. parse_results[p_path] = parser;
  289. scripts[p_path] = parser;
  290. } else {
  291. if (last_parser && last_script && last_parser->get() != last_script->get()) {
  292. memdelete(last_parser->get());
  293. }
  294. parse_results[p_path] = parser;
  295. }
  296. publish_diagnostics(p_path);
  297. return err;
  298. }
  299. Error GDScriptWorkspace::parse_local_script(const String &p_path) {
  300. Error err;
  301. String content = FileAccess::get_file_as_string(p_path, &err);
  302. if (err == OK) {
  303. err = parse_script(p_path, content);
  304. }
  305. return err;
  306. }
  307. String GDScriptWorkspace::get_file_path(const String &p_uri) const {
  308. String path = p_uri;
  309. path = path.replace(root_uri + "/", "res://");
  310. path = path.http_unescape();
  311. return path;
  312. }
  313. String GDScriptWorkspace::get_file_uri(const String &p_path) const {
  314. String uri = p_path;
  315. uri = uri.replace("res://", root_uri + "/");
  316. return uri;
  317. }
  318. void GDScriptWorkspace::publish_diagnostics(const String &p_path) {
  319. Dictionary params;
  320. Array errors;
  321. const Map<String, ExtendGDScriptParser *>::Element *ele = parse_results.find(p_path);
  322. if (ele) {
  323. const Vector<lsp::Diagnostic> &list = ele->get()->get_diagnostics();
  324. errors.resize(list.size());
  325. for (int i = 0; i < list.size(); ++i) {
  326. errors[i] = list[i].to_json();
  327. }
  328. }
  329. params["diagnostics"] = errors;
  330. params["uri"] = get_file_uri(p_path);
  331. GDScriptLanguageProtocol::get_singleton()->notify_client("textDocument/publishDiagnostics", params);
  332. }
  333. void GDScriptWorkspace::_get_owners(EditorFileSystemDirectory *efsd, String p_path, List<String> &owners) {
  334. if (!efsd)
  335. return;
  336. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  337. _get_owners(efsd->get_subdir(i), p_path, owners);
  338. }
  339. for (int i = 0; i < efsd->get_file_count(); i++) {
  340. Vector<String> deps = efsd->get_file_deps(i);
  341. bool found = false;
  342. for (int j = 0; j < deps.size(); j++) {
  343. if (deps[j] == p_path) {
  344. found = true;
  345. break;
  346. }
  347. }
  348. if (!found)
  349. continue;
  350. owners.push_back(efsd->get_file_path(i));
  351. }
  352. }
  353. Node *GDScriptWorkspace::_get_owner_node(String p_path) {
  354. Node *owner_node = NULL;
  355. List<String> owners;
  356. _get_owners(EditorFileSystem::get_singleton()->get_filesystem(), p_path, owners);
  357. if (owners.size() > 0) {
  358. NodePath owner_path = owners[0];
  359. Ref<PackedScene> owner_res = ResourceLoader::load(owner_path);
  360. owner_node = owner_res->instance(PackedScene::GEN_EDIT_STATE_DISABLED);
  361. }
  362. return owner_node;
  363. }
  364. void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *r_options) {
  365. String path = get_file_path(p_params.textDocument.uri);
  366. String call_hint;
  367. bool forced = false;
  368. if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
  369. Node *owner_node = _get_owner_node(path);
  370. String code = parser->get_text_for_completion(p_params.position);
  371. GDScriptLanguage::get_singleton()->complete_code(code, path, owner_node, r_options, forced, call_hint);
  372. if (owner_node) {
  373. memdelete(owner_node);
  374. }
  375. }
  376. }
  377. const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name, bool p_func_requred) {
  378. const lsp::DocumentSymbol *symbol = NULL;
  379. String path = get_file_path(p_doc_pos.textDocument.uri);
  380. if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
  381. String symbol_identifier = p_symbol_name;
  382. Vector<String> identifier_parts = symbol_identifier.split("(");
  383. if (identifier_parts.size()) {
  384. symbol_identifier = identifier_parts[0];
  385. }
  386. lsp::Position pos = p_doc_pos.position;
  387. if (symbol_identifier.empty()) {
  388. Vector2i offset;
  389. symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset);
  390. pos.character += offset.y;
  391. }
  392. if (!symbol_identifier.empty()) {
  393. if (ScriptServer::is_global_class(symbol_identifier)) {
  394. String class_path = ScriptServer::get_global_class_path(symbol_identifier);
  395. symbol = get_script_symbol(class_path);
  396. } else {
  397. ScriptLanguage::LookupResult ret;
  398. if (OK == GDScriptLanguage::get_singleton()->lookup_code(parser->get_text_for_lookup_symbol(pos, symbol_identifier, p_func_requred), symbol_identifier, path, NULL, ret)) {
  399. if (ret.type == ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION) {
  400. String target_script_path = path;
  401. if (!ret.script.is_null()) {
  402. target_script_path = ret.script->get_path();
  403. }
  404. if (const ExtendGDScriptParser *target_parser = get_parse_result(target_script_path)) {
  405. symbol = target_parser->get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(ret.location));
  406. }
  407. } else {
  408. String member = ret.class_member;
  409. if (member.empty() && symbol_identifier != ret.class_name) {
  410. member = symbol_identifier;
  411. }
  412. symbol = get_native_symbol(ret.class_name, member);
  413. }
  414. } else {
  415. symbol = parser->get_member_symbol(symbol_identifier);
  416. }
  417. }
  418. }
  419. }
  420. return symbol;
  421. }
  422. void GDScriptWorkspace::resolve_related_symbols(const lsp::TextDocumentPositionParams &p_doc_pos, List<const lsp::DocumentSymbol *> &r_list) {
  423. String path = get_file_path(p_doc_pos.textDocument.uri);
  424. if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
  425. String symbol_identifier;
  426. Vector2i offset;
  427. symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset);
  428. const StringName *class_ptr = native_members.next(NULL);
  429. while (class_ptr) {
  430. const ClassMembers &members = native_members.get(*class_ptr);
  431. if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
  432. r_list.push_back(*symbol);
  433. }
  434. class_ptr = native_members.next(class_ptr);
  435. }
  436. for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
  437. const ExtendGDScriptParser *script = E->get();
  438. const ClassMembers &members = script->get_members();
  439. if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
  440. r_list.push_back(*symbol);
  441. }
  442. const HashMap<String, ClassMembers> &inner_classes = script->get_inner_classes();
  443. const String *_class = inner_classes.next(NULL);
  444. while (_class) {
  445. const ClassMembers *inner_class = inner_classes.getptr(*_class);
  446. if (const lsp::DocumentSymbol *const *symbol = inner_class->getptr(symbol_identifier)) {
  447. r_list.push_back(*symbol);
  448. }
  449. _class = inner_classes.next(_class);
  450. }
  451. }
  452. }
  453. }
  454. const lsp::DocumentSymbol *GDScriptWorkspace::resolve_native_symbol(const lsp::NativeSymbolInspectParams &p_params) {
  455. if (Map<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.find(p_params.native_class)) {
  456. const lsp::DocumentSymbol &symbol = E->get();
  457. if (p_params.symbol_name.empty() || p_params.symbol_name == symbol.name) {
  458. return &symbol;
  459. }
  460. for (int i = 0; i < symbol.children.size(); ++i) {
  461. if (symbol.children[i].name == p_params.symbol_name) {
  462. return &(symbol.children[i]);
  463. }
  464. }
  465. }
  466. return NULL;
  467. }
  468. void GDScriptWorkspace::resolve_document_links(const String &p_uri, List<lsp::DocumentLink> &r_list) {
  469. if (const ExtendGDScriptParser *parser = get_parse_successed_script(get_file_path(p_uri))) {
  470. const List<lsp::DocumentLink> &links = parser->get_document_links();
  471. for (const List<lsp::DocumentLink>::Element *E = links.front(); E; E = E->next()) {
  472. r_list.push_back(E->get());
  473. }
  474. }
  475. }
  476. Dictionary GDScriptWorkspace::generate_script_api(const String &p_path) {
  477. Dictionary api;
  478. if (const ExtendGDScriptParser *parser = get_parse_successed_script(p_path)) {
  479. api = parser->generate_api();
  480. }
  481. return api;
  482. }
  483. Error GDScriptWorkspace::resolve_signature(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::SignatureHelp &r_signature) {
  484. if (const ExtendGDScriptParser *parser = get_parse_result(get_file_path(p_doc_pos.textDocument.uri))) {
  485. lsp::TextDocumentPositionParams text_pos;
  486. text_pos.textDocument = p_doc_pos.textDocument;
  487. if (parser->get_left_function_call(p_doc_pos.position, text_pos.position, r_signature.activeParameter) == OK) {
  488. List<const lsp::DocumentSymbol *> symbols;
  489. if (const lsp::DocumentSymbol *symbol = resolve_symbol(text_pos)) {
  490. symbols.push_back(symbol);
  491. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  492. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(text_pos, symbols);
  493. }
  494. for (List<const lsp::DocumentSymbol *>::Element *E = symbols.front(); E; E = E->next()) {
  495. const lsp::DocumentSymbol *symbol = E->get();
  496. if (symbol->kind == lsp::SymbolKind::Method || symbol->kind == lsp::SymbolKind::Function) {
  497. lsp::SignatureInformation signature_info;
  498. signature_info.label = symbol->detail;
  499. signature_info.documentation = symbol->render();
  500. for (int i = 0; i < symbol->children.size(); i++) {
  501. const lsp::DocumentSymbol &arg = symbol->children[i];
  502. lsp::ParameterInformation arg_info;
  503. arg_info.label = arg.name;
  504. signature_info.parameters.push_back(arg_info);
  505. }
  506. r_signature.signatures.push_back(signature_info);
  507. break;
  508. }
  509. }
  510. if (r_signature.signatures.size()) {
  511. return OK;
  512. }
  513. }
  514. }
  515. return ERR_METHOD_NOT_FOUND;
  516. }
  517. GDScriptWorkspace::GDScriptWorkspace() {
  518. ProjectSettings::get_singleton()->get_resource_path();
  519. }
  520. GDScriptWorkspace::~GDScriptWorkspace() {
  521. Set<String> cached_parsers;
  522. for (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.front(); E; E = E->next()) {
  523. cached_parsers.insert(E->key());
  524. }
  525. for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
  526. cached_parsers.insert(E->key());
  527. }
  528. for (Set<String>::Element *E = cached_parsers.front(); E; E = E->next()) {
  529. remove_cache_parser(E->get());
  530. }
  531. }