gdscript_workspace.cpp 27 KB

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