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