gdscript_workspace.cpp 25 KB

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