gdscript_workspace.cpp 27 KB

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