gdscript_workspace.cpp 25 KB

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