gdscript_workspace.cpp 27 KB

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