gdscript_text_document.cpp 18 KB

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