gdscript_text_document.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /**************************************************************************/
  2. /* gdscript_text_document.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_text_document.h"
  31. #include "../gdscript.h"
  32. #include "gdscript_extend_parser.h"
  33. #include "gdscript_language_protocol.h"
  34. #include "core/os/os.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/plugins/script_text_editor.h"
  37. #include "servers/display_server.h"
  38. void GDScriptTextDocument::_bind_methods() {
  39. ClassDB::bind_method(D_METHOD("didOpen"), &GDScriptTextDocument::didOpen);
  40. ClassDB::bind_method(D_METHOD("didClose"), &GDScriptTextDocument::didClose);
  41. ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange);
  42. ClassDB::bind_method(D_METHOD("willSaveWaitUntil"), &GDScriptTextDocument::willSaveWaitUntil);
  43. ClassDB::bind_method(D_METHOD("didSave"), &GDScriptTextDocument::didSave);
  44. ClassDB::bind_method(D_METHOD("nativeSymbol"), &GDScriptTextDocument::nativeSymbol);
  45. ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol);
  46. ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion);
  47. ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve);
  48. ClassDB::bind_method(D_METHOD("rename"), &GDScriptTextDocument::rename);
  49. ClassDB::bind_method(D_METHOD("prepareRename"), &GDScriptTextDocument::prepareRename);
  50. ClassDB::bind_method(D_METHOD("references"), &GDScriptTextDocument::references);
  51. ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange);
  52. ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens);
  53. ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink);
  54. ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation);
  55. ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover);
  56. ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition);
  57. ClassDB::bind_method(D_METHOD("declaration"), &GDScriptTextDocument::declaration);
  58. ClassDB::bind_method(D_METHOD("signatureHelp"), &GDScriptTextDocument::signatureHelp);
  59. ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor);
  60. }
  61. void GDScriptTextDocument::didOpen(const Variant &p_param) {
  62. lsp::TextDocumentItem doc = load_document_item(p_param);
  63. sync_script_content(doc.uri, doc.text);
  64. }
  65. void GDScriptTextDocument::didClose(const Variant &p_param) {
  66. // Left empty on purpose. Godot does nothing special on closing a document,
  67. // but it satisfies LSP clients that require didClose be implemented.
  68. }
  69. void GDScriptTextDocument::didChange(const Variant &p_param) {
  70. lsp::TextDocumentItem doc = load_document_item(p_param);
  71. Dictionary dict = p_param;
  72. Array contentChanges = dict["contentChanges"];
  73. for (int i = 0; i < contentChanges.size(); ++i) {
  74. lsp::TextDocumentContentChangeEvent evt;
  75. evt.load(contentChanges[i]);
  76. doc.text = evt.text;
  77. }
  78. sync_script_content(doc.uri, doc.text);
  79. }
  80. void GDScriptTextDocument::willSaveWaitUntil(const Variant &p_param) {
  81. lsp::TextDocumentItem doc = load_document_item(p_param);
  82. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);
  83. Ref<Script> scr = ResourceLoader::load(path);
  84. if (scr.is_valid()) {
  85. ScriptEditor::get_singleton()->clear_docs_from_script(scr);
  86. }
  87. }
  88. void GDScriptTextDocument::didSave(const Variant &p_param) {
  89. lsp::TextDocumentItem doc = load_document_item(p_param);
  90. Dictionary dict = p_param;
  91. String text = dict["text"];
  92. sync_script_content(doc.uri, text);
  93. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);
  94. Ref<GDScript> scr = ResourceLoader::load(path);
  95. if (scr.is_valid() && (scr->load_source_code(path) == OK)) {
  96. if (scr->is_tool()) {
  97. scr->get_language()->reload_tool_script(scr, true);
  98. } else {
  99. scr->reload(true);
  100. }
  101. scr->update_exports();
  102. if (!Thread::is_main_thread()) {
  103. callable_mp(this, &GDScriptTextDocument::reload_script).call_deferred(scr);
  104. } else {
  105. reload_script(scr);
  106. }
  107. }
  108. }
  109. void GDScriptTextDocument::reload_script(Ref<GDScript> p_to_reload_script) {
  110. ScriptEditor::get_singleton()->reload_scripts(true);
  111. ScriptEditor::get_singleton()->update_docs_from_script(p_to_reload_script);
  112. ScriptEditor::get_singleton()->trigger_live_script_reload(p_to_reload_script->get_path());
  113. }
  114. lsp::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) {
  115. lsp::TextDocumentItem doc;
  116. Dictionary params = p_param;
  117. doc.load(params["textDocument"]);
  118. return doc;
  119. }
  120. void GDScriptTextDocument::notify_client_show_symbol(const lsp::DocumentSymbol *symbol) {
  121. ERR_FAIL_NULL(symbol);
  122. GDScriptLanguageProtocol::get_singleton()->notify_client("gdscript/show_native_symbol", symbol->to_json(true));
  123. }
  124. void GDScriptTextDocument::initialize() {
  125. if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  126. for (const KeyValue<StringName, ClassMembers> &E : GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members) {
  127. const ClassMembers &members = E.value;
  128. for (const KeyValue<String, const lsp::DocumentSymbol *> &F : members) {
  129. const lsp::DocumentSymbol *symbol = members.get(F.key);
  130. lsp::CompletionItem item = symbol->make_completion_item();
  131. item.data = JOIN_SYMBOLS(String(E.key), F.key);
  132. native_member_completions.push_back(item.to_json());
  133. }
  134. }
  135. }
  136. }
  137. Variant GDScriptTextDocument::nativeSymbol(const Dictionary &p_params) {
  138. Variant ret;
  139. lsp::NativeSymbolInspectParams params;
  140. params.load(p_params);
  141. if (const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_native_symbol(params)) {
  142. ret = symbol->to_json(true);
  143. notify_client_show_symbol(symbol);
  144. }
  145. return ret;
  146. }
  147. Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
  148. Dictionary params = p_params["textDocument"];
  149. String uri = params["uri"];
  150. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri);
  151. Array arr;
  152. if (HashMap<String, ExtendGDScriptParser *>::ConstIterator parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(path)) {
  153. lsp::DocumentSymbol symbol = parser->value->get_symbols();
  154. arr.push_back(symbol.to_json(true));
  155. }
  156. return arr;
  157. }
  158. Array GDScriptTextDocument::completion(const Dictionary &p_params) {
  159. Array arr;
  160. lsp::CompletionParams params;
  161. params.load(p_params);
  162. Dictionary request_data = params.to_json();
  163. List<ScriptLanguage::CodeCompletionOption> options;
  164. GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options);
  165. if (!options.is_empty()) {
  166. int i = 0;
  167. arr.resize(options.size());
  168. for (const ScriptLanguage::CodeCompletionOption &option : options) {
  169. lsp::CompletionItem item;
  170. item.label = option.display;
  171. item.data = request_data;
  172. item.insertText = option.insert_text;
  173. switch (option.kind) {
  174. case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
  175. item.kind = lsp::CompletionItemKind::Enum;
  176. break;
  177. case ScriptLanguage::CODE_COMPLETION_KIND_CLASS:
  178. item.kind = lsp::CompletionItemKind::Class;
  179. break;
  180. case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
  181. item.kind = lsp::CompletionItemKind::Property;
  182. break;
  183. case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
  184. item.kind = lsp::CompletionItemKind::Method;
  185. break;
  186. case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
  187. item.kind = lsp::CompletionItemKind::Event;
  188. break;
  189. case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
  190. item.kind = lsp::CompletionItemKind::Constant;
  191. break;
  192. case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
  193. item.kind = lsp::CompletionItemKind::Variable;
  194. break;
  195. case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
  196. item.kind = lsp::CompletionItemKind::File;
  197. break;
  198. case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
  199. item.kind = lsp::CompletionItemKind::Snippet;
  200. break;
  201. case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
  202. item.kind = lsp::CompletionItemKind::Text;
  203. break;
  204. default: {
  205. }
  206. }
  207. arr[i] = item.to_json();
  208. i++;
  209. }
  210. }
  211. return arr;
  212. }
  213. Dictionary GDScriptTextDocument::rename(const Dictionary &p_params) {
  214. lsp::TextDocumentPositionParams params;
  215. params.load(p_params);
  216. String new_name = p_params["newName"];
  217. return GDScriptLanguageProtocol::get_singleton()->get_workspace()->rename(params, new_name);
  218. }
  219. Variant GDScriptTextDocument::prepareRename(const Dictionary &p_params) {
  220. lsp::TextDocumentPositionParams params;
  221. params.load(p_params);
  222. lsp::DocumentSymbol symbol;
  223. lsp::Range range;
  224. if (GDScriptLanguageProtocol::get_singleton()->get_workspace()->can_rename(params, symbol, range)) {
  225. return Variant(range.to_json());
  226. }
  227. // `null` -> rename not valid at current location.
  228. return Variant();
  229. }
  230. Array GDScriptTextDocument::references(const Dictionary &p_params) {
  231. Array res;
  232. lsp::ReferenceParams params;
  233. params.load(p_params);
  234. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
  235. if (symbol) {
  236. Vector<lsp::Location> usages = GDScriptLanguageProtocol::get_singleton()->get_workspace()->find_all_usages(*symbol);
  237. res.resize(usages.size());
  238. int declaration_adjustment = 0;
  239. for (int i = 0; i < usages.size(); i++) {
  240. lsp::Location usage = usages[i];
  241. if (!params.context.includeDeclaration && usage.range == symbol->range) {
  242. declaration_adjustment++;
  243. continue;
  244. }
  245. res[i - declaration_adjustment] = usages[i].to_json();
  246. }
  247. if (declaration_adjustment > 0) {
  248. res.resize(res.size() - declaration_adjustment);
  249. }
  250. }
  251. return res;
  252. }
  253. Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
  254. lsp::CompletionItem item;
  255. item.load(p_params);
  256. lsp::CompletionParams params;
  257. Variant data = p_params["data"];
  258. const lsp::DocumentSymbol *symbol = nullptr;
  259. if (data.get_type() == Variant::DICTIONARY) {
  260. params.load(p_params["data"]);
  261. symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function);
  262. } else if (data.is_string()) {
  263. String query = data;
  264. Vector<String> param_symbols = query.split(SYMBOL_SEPERATOR, false);
  265. if (param_symbols.size() >= 2) {
  266. StringName class_name = param_symbols[0];
  267. const String &member_name = param_symbols[param_symbols.size() - 1];
  268. String inner_class_name;
  269. if (param_symbols.size() >= 3) {
  270. inner_class_name = param_symbols[1];
  271. }
  272. if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) {
  273. if (const lsp::DocumentSymbol *const *member = members->getptr(member_name)) {
  274. symbol = *member;
  275. }
  276. }
  277. if (!symbol) {
  278. if (HashMap<String, ExtendGDScriptParser *>::ConstIterator E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(class_name)) {
  279. symbol = E->value->get_member_symbol(member_name, inner_class_name);
  280. }
  281. }
  282. }
  283. }
  284. if (symbol) {
  285. item.documentation = symbol->render();
  286. }
  287. if (item.kind == lsp::CompletionItemKind::Event) {
  288. if (params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) {
  289. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  290. item.insertText = item.label.quote(quote_style);
  291. }
  292. }
  293. if (item.kind == lsp::CompletionItemKind::Method) {
  294. bool is_trigger_character = params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter;
  295. bool is_quote_character = params.context.triggerCharacter == "\"" || params.context.triggerCharacter == "'";
  296. if (is_trigger_character && is_quote_character && item.insertText.is_quoted()) {
  297. item.insertText = item.insertText.unquote();
  298. }
  299. }
  300. return item.to_json(true);
  301. }
  302. Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
  303. Array arr;
  304. return arr;
  305. }
  306. Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
  307. Array arr;
  308. return arr;
  309. }
  310. Array GDScriptTextDocument::documentLink(const Dictionary &p_params) {
  311. Array ret;
  312. lsp::DocumentLinkParams params;
  313. params.load(p_params);
  314. List<lsp::DocumentLink> links;
  315. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links);
  316. for (const lsp::DocumentLink &E : links) {
  317. ret.push_back(E.to_json());
  318. }
  319. return ret;
  320. }
  321. Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
  322. Array arr;
  323. return arr;
  324. }
  325. Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
  326. lsp::TextDocumentPositionParams params;
  327. params.load(p_params);
  328. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
  329. if (symbol) {
  330. lsp::Hover hover;
  331. hover.contents = symbol->render();
  332. hover.range.start = params.position;
  333. hover.range.end = params.position;
  334. return hover.to_json();
  335. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  336. Dictionary ret;
  337. Array contents;
  338. List<const lsp::DocumentSymbol *> list;
  339. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list);
  340. for (const lsp::DocumentSymbol *&E : list) {
  341. if (const lsp::DocumentSymbol *s = E) {
  342. contents.push_back(s->render().value);
  343. }
  344. }
  345. ret["contents"] = contents;
  346. return ret;
  347. }
  348. return Variant();
  349. }
  350. Array GDScriptTextDocument::definition(const Dictionary &p_params) {
  351. lsp::TextDocumentPositionParams params;
  352. params.load(p_params);
  353. List<const lsp::DocumentSymbol *> symbols;
  354. Array arr = find_symbols(params, symbols);
  355. return arr;
  356. }
  357. Variant GDScriptTextDocument::declaration(const Dictionary &p_params) {
  358. lsp::TextDocumentPositionParams params;
  359. params.load(p_params);
  360. List<const lsp::DocumentSymbol *> symbols;
  361. Array arr = find_symbols(params, symbols);
  362. if (arr.is_empty() && !symbols.is_empty() && !symbols.front()->get()->native_class.is_empty()) { // Find a native symbol
  363. const lsp::DocumentSymbol *symbol = symbols.front()->get();
  364. if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {
  365. String id;
  366. switch (symbol->kind) {
  367. case lsp::SymbolKind::Class:
  368. id = "class_name:" + symbol->name;
  369. break;
  370. case lsp::SymbolKind::Constant:
  371. id = "class_constant:" + symbol->native_class + ":" + symbol->name;
  372. break;
  373. case lsp::SymbolKind::Property:
  374. case lsp::SymbolKind::Variable:
  375. id = "class_property:" + symbol->native_class + ":" + symbol->name;
  376. break;
  377. case lsp::SymbolKind::Enum:
  378. id = "class_enum:" + symbol->native_class + ":" + symbol->name;
  379. break;
  380. case lsp::SymbolKind::Method:
  381. case lsp::SymbolKind::Function:
  382. id = "class_method:" + symbol->native_class + ":" + symbol->name;
  383. break;
  384. default:
  385. id = "class_global:" + symbol->native_class + ":" + symbol->name;
  386. break;
  387. }
  388. callable_mp(this, &GDScriptTextDocument::show_native_symbol_in_editor).call_deferred(id);
  389. } else {
  390. notify_client_show_symbol(symbol);
  391. }
  392. }
  393. return arr;
  394. }
  395. Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) {
  396. Variant ret;
  397. lsp::TextDocumentPositionParams params;
  398. params.load(p_params);
  399. lsp::SignatureHelp s;
  400. if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) {
  401. ret = s.to_json();
  402. }
  403. return ret;
  404. }
  405. GDScriptTextDocument::GDScriptTextDocument() {
  406. file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  407. }
  408. void GDScriptTextDocument::sync_script_content(const String &p_path, const String &p_content) {
  409. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_path);
  410. GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content);
  411. }
  412. void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {
  413. callable_mp(ScriptEditor::get_singleton(), &ScriptEditor::goto_help).call_deferred(p_symbol_id);
  414. DisplayServer::get_singleton()->window_move_to_foreground();
  415. }
  416. Array GDScriptTextDocument::find_symbols(const lsp::TextDocumentPositionParams &p_location, List<const lsp::DocumentSymbol *> &r_list) {
  417. Array arr;
  418. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);
  419. if (symbol) {
  420. lsp::Location location;
  421. location.uri = symbol->uri;
  422. location.range = symbol->selectionRange;
  423. const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);
  424. if (file_checker->file_exists(path)) {
  425. arr.push_back(location.to_json());
  426. }
  427. r_list.push_back(symbol);
  428. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  429. List<const lsp::DocumentSymbol *> list;
  430. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list);
  431. for (const lsp::DocumentSymbol *&E : list) {
  432. if (const lsp::DocumentSymbol *s = E) {
  433. if (!s->uri.is_empty()) {
  434. lsp::Location location;
  435. location.uri = s->uri;
  436. location.range = s->selectionRange;
  437. arr.push_back(location.to_json());
  438. r_list.push_back(s);
  439. }
  440. }
  441. }
  442. }
  443. return arr;
  444. }