2
0

gdscript_workspace.cpp 27 KB

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