2
0

gdscript_text_document.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*************************************************************************/
  2. /* gdscript_text_document.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_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("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("foldingRange"), &GDScriptTextDocument::foldingRange);
  49. ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens);
  50. ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink);
  51. ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation);
  52. ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover);
  53. ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition);
  54. ClassDB::bind_method(D_METHOD("declaration"), &GDScriptTextDocument::declaration);
  55. ClassDB::bind_method(D_METHOD("signatureHelp"), &GDScriptTextDocument::signatureHelp);
  56. ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor);
  57. }
  58. void GDScriptTextDocument::didOpen(const Variant &p_param) {
  59. lsp::TextDocumentItem doc = load_document_item(p_param);
  60. sync_script_content(doc.uri, doc.text);
  61. }
  62. void GDScriptTextDocument::didClose(const Variant &p_param) {
  63. // Left empty on purpose. Godot does nothing special on closing a document,
  64. // but it satisfies LSP clients that require didClose be implemented.
  65. }
  66. void GDScriptTextDocument::didChange(const Variant &p_param) {
  67. lsp::TextDocumentItem doc = load_document_item(p_param);
  68. Dictionary dict = p_param;
  69. Array contentChanges = dict["contentChanges"];
  70. for (int i = 0; i < contentChanges.size(); ++i) {
  71. lsp::TextDocumentContentChangeEvent evt;
  72. evt.load(contentChanges[i]);
  73. doc.text = evt.text;
  74. }
  75. sync_script_content(doc.uri, doc.text);
  76. }
  77. void GDScriptTextDocument::didSave(const Variant &p_param) {
  78. lsp::TextDocumentItem doc = load_document_item(p_param);
  79. Dictionary dict = p_param;
  80. String text = dict["text"];
  81. sync_script_content(doc.uri, text);
  82. /*String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);
  83. Ref<GDScript> script = ResourceLoader::load(path);
  84. script->load_source_code(path);
  85. script->reload(true);*/
  86. }
  87. lsp::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) {
  88. lsp::TextDocumentItem doc;
  89. Dictionary params = p_param;
  90. doc.load(params["textDocument"]);
  91. return doc;
  92. }
  93. void GDScriptTextDocument::notify_client_show_symbol(const lsp::DocumentSymbol *symbol) {
  94. ERR_FAIL_NULL(symbol);
  95. GDScriptLanguageProtocol::get_singleton()->notify_client("gdscript/show_native_symbol", symbol->to_json(true));
  96. }
  97. void GDScriptTextDocument::initialize() {
  98. if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  99. const HashMap<StringName, ClassMembers> &native_members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members;
  100. const StringName *class_ptr = native_members.next(nullptr);
  101. while (class_ptr) {
  102. const ClassMembers &members = native_members.get(*class_ptr);
  103. const String *name = members.next(nullptr);
  104. while (name) {
  105. const lsp::DocumentSymbol *symbol = members.get(*name);
  106. lsp::CompletionItem item = symbol->make_completion_item();
  107. item.data = JOIN_SYMBOLS(String(*class_ptr), *name);
  108. native_member_completions.push_back(item.to_json());
  109. name = members.next(name);
  110. }
  111. class_ptr = native_members.next(class_ptr);
  112. }
  113. }
  114. }
  115. Variant GDScriptTextDocument::nativeSymbol(const Dictionary &p_params) {
  116. Variant ret;
  117. lsp::NativeSymbolInspectParams params;
  118. params.load(p_params);
  119. if (const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_native_symbol(params)) {
  120. ret = symbol->to_json(true);
  121. notify_client_show_symbol(symbol);
  122. }
  123. return ret;
  124. }
  125. Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
  126. Dictionary params = p_params["textDocument"];
  127. String uri = params["uri"];
  128. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri);
  129. Array arr;
  130. if (const Map<String, ExtendGDScriptParser *>::Element *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(path)) {
  131. Vector<lsp::DocumentedSymbolInformation> list;
  132. parser->get()->get_symbols().symbol_tree_as_list(uri, list);
  133. for (int i = 0; i < list.size(); i++) {
  134. arr.push_back(list[i].to_json());
  135. }
  136. }
  137. return arr;
  138. }
  139. Array GDScriptTextDocument::completion(const Dictionary &p_params) {
  140. Array arr;
  141. lsp::CompletionParams params;
  142. params.load(p_params);
  143. Dictionary request_data = params.to_json();
  144. List<ScriptLanguage::CodeCompletionOption> options;
  145. GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options);
  146. if (!options.is_empty()) {
  147. int i = 0;
  148. arr.resize(options.size());
  149. for (const ScriptLanguage::CodeCompletionOption &option : options) {
  150. lsp::CompletionItem item;
  151. item.label = option.display;
  152. item.data = request_data;
  153. switch (option.kind) {
  154. case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
  155. item.kind = lsp::CompletionItemKind::Enum;
  156. break;
  157. case ScriptLanguage::CODE_COMPLETION_KIND_CLASS:
  158. item.kind = lsp::CompletionItemKind::Class;
  159. break;
  160. case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
  161. item.kind = lsp::CompletionItemKind::Property;
  162. break;
  163. case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
  164. item.kind = lsp::CompletionItemKind::Method;
  165. break;
  166. case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
  167. item.kind = lsp::CompletionItemKind::Event;
  168. break;
  169. case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
  170. item.kind = lsp::CompletionItemKind::Constant;
  171. break;
  172. case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
  173. item.kind = lsp::CompletionItemKind::Variable;
  174. break;
  175. case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
  176. item.kind = lsp::CompletionItemKind::File;
  177. break;
  178. case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
  179. item.kind = lsp::CompletionItemKind::Snippet;
  180. break;
  181. case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
  182. item.kind = lsp::CompletionItemKind::Text;
  183. break;
  184. default: {
  185. }
  186. }
  187. arr[i] = item.to_json();
  188. i++;
  189. }
  190. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  191. arr = native_member_completions.duplicate();
  192. for (KeyValue<String, ExtendGDScriptParser *> &E : GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts) {
  193. ExtendGDScriptParser *script = E.value;
  194. const Array &items = script->get_member_completions();
  195. const int start_size = arr.size();
  196. arr.resize(start_size + items.size());
  197. for (int i = start_size; i < arr.size(); i++) {
  198. arr[i] = items[i - start_size];
  199. }
  200. }
  201. }
  202. return arr;
  203. }
  204. Dictionary GDScriptTextDocument::rename(const Dictionary &p_params) {
  205. lsp::TextDocumentPositionParams params;
  206. params.load(p_params);
  207. String new_name = p_params["newName"];
  208. return GDScriptLanguageProtocol::get_singleton()->get_workspace()->rename(params, new_name);
  209. }
  210. Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
  211. lsp::CompletionItem item;
  212. item.load(p_params);
  213. lsp::CompletionParams params;
  214. Variant data = p_params["data"];
  215. const lsp::DocumentSymbol *symbol = nullptr;
  216. if (data.get_type() == Variant::DICTIONARY) {
  217. params.load(p_params["data"]);
  218. symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function);
  219. } else if (data.get_type() == Variant::STRING) {
  220. String query = data;
  221. Vector<String> param_symbols = query.split(SYMBOL_SEPERATOR, false);
  222. if (param_symbols.size() >= 2) {
  223. String class_ = param_symbols[0];
  224. StringName class_name = class_;
  225. String member_name = param_symbols[param_symbols.size() - 1];
  226. String inner_class_name;
  227. if (param_symbols.size() >= 3) {
  228. inner_class_name = param_symbols[1];
  229. }
  230. if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) {
  231. if (const lsp::DocumentSymbol *const *member = members->getptr(member_name)) {
  232. symbol = *member;
  233. }
  234. }
  235. if (!symbol) {
  236. if (const Map<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(class_name)) {
  237. symbol = E->get()->get_member_symbol(member_name, inner_class_name);
  238. }
  239. }
  240. }
  241. }
  242. if (symbol) {
  243. item.documentation = symbol->render();
  244. }
  245. if ((item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function) && !item.label.ends_with("):")) {
  246. item.insertText = item.label + "(";
  247. if (symbol && symbol->children.is_empty()) {
  248. item.insertText += ")";
  249. }
  250. } else if (item.kind == lsp::CompletionItemKind::Event) {
  251. if (params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) {
  252. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  253. item.insertText = item.label.quote(quote_style);
  254. }
  255. }
  256. return item.to_json(true);
  257. }
  258. Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
  259. Array arr;
  260. return arr;
  261. }
  262. Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
  263. Array arr;
  264. return arr;
  265. }
  266. Array GDScriptTextDocument::documentLink(const Dictionary &p_params) {
  267. Array ret;
  268. lsp::DocumentLinkParams params;
  269. params.load(p_params);
  270. List<lsp::DocumentLink> links;
  271. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links);
  272. for (const lsp::DocumentLink &E : links) {
  273. ret.push_back(E.to_json());
  274. }
  275. return ret;
  276. }
  277. Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
  278. Array arr;
  279. return arr;
  280. }
  281. Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
  282. lsp::TextDocumentPositionParams params;
  283. params.load(p_params);
  284. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
  285. if (symbol) {
  286. lsp::Hover hover;
  287. hover.contents = symbol->render();
  288. hover.range.start = params.position;
  289. hover.range.end = params.position;
  290. return hover.to_json();
  291. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  292. Dictionary ret;
  293. Array contents;
  294. List<const lsp::DocumentSymbol *> list;
  295. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list);
  296. for (const lsp::DocumentSymbol *&E : list) {
  297. if (const lsp::DocumentSymbol *s = E) {
  298. contents.push_back(s->render().value);
  299. }
  300. }
  301. ret["contents"] = contents;
  302. return ret;
  303. }
  304. return Variant();
  305. }
  306. Array GDScriptTextDocument::definition(const Dictionary &p_params) {
  307. lsp::TextDocumentPositionParams params;
  308. params.load(p_params);
  309. List<const lsp::DocumentSymbol *> symbols;
  310. Array arr = this->find_symbols(params, symbols);
  311. return arr;
  312. }
  313. Variant GDScriptTextDocument::declaration(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. if (arr.is_empty() && !symbols.is_empty() && !symbols.front()->get()->native_class.is_empty()) { // Find a native symbol
  319. const lsp::DocumentSymbol *symbol = symbols.front()->get();
  320. if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {
  321. String id;
  322. switch (symbol->kind) {
  323. case lsp::SymbolKind::Class:
  324. id = "class_name:" + symbol->name;
  325. break;
  326. case lsp::SymbolKind::Constant:
  327. id = "class_constant:" + symbol->native_class + ":" + symbol->name;
  328. break;
  329. case lsp::SymbolKind::Property:
  330. case lsp::SymbolKind::Variable:
  331. id = "class_property:" + symbol->native_class + ":" + symbol->name;
  332. break;
  333. case lsp::SymbolKind::Enum:
  334. id = "class_enum:" + symbol->native_class + ":" + symbol->name;
  335. break;
  336. case lsp::SymbolKind::Method:
  337. case lsp::SymbolKind::Function:
  338. id = "class_method:" + symbol->native_class + ":" + symbol->name;
  339. break;
  340. default:
  341. id = "class_global:" + symbol->native_class + ":" + symbol->name;
  342. break;
  343. }
  344. call_deferred(SNAME("show_native_symbol_in_editor"), id);
  345. } else {
  346. notify_client_show_symbol(symbol);
  347. }
  348. }
  349. return arr;
  350. }
  351. Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) {
  352. Variant ret;
  353. lsp::TextDocumentPositionParams params;
  354. params.load(p_params);
  355. lsp::SignatureHelp s;
  356. if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) {
  357. ret = s.to_json();
  358. }
  359. return ret;
  360. }
  361. GDScriptTextDocument::GDScriptTextDocument() {
  362. file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  363. }
  364. GDScriptTextDocument::~GDScriptTextDocument() {
  365. memdelete(file_checker);
  366. }
  367. void GDScriptTextDocument::sync_script_content(const String &p_path, const String &p_content) {
  368. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_path);
  369. GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content);
  370. EditorFileSystem::get_singleton()->update_file(path);
  371. Error error;
  372. Ref<GDScript> script = ResourceLoader::load(path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &error);
  373. if (error == OK) {
  374. if (script->load_source_code(path) == OK) {
  375. script->reload(true);
  376. }
  377. }
  378. }
  379. void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {
  380. ScriptEditor::get_singleton()->call_deferred(SNAME("_help_class_goto"), p_symbol_id);
  381. DisplayServer::get_singleton()->window_move_to_foreground();
  382. }
  383. Array GDScriptTextDocument::find_symbols(const lsp::TextDocumentPositionParams &p_location, List<const lsp::DocumentSymbol *> &r_list) {
  384. Array arr;
  385. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);
  386. if (symbol) {
  387. lsp::Location location;
  388. location.uri = symbol->uri;
  389. location.range = symbol->range;
  390. const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);
  391. if (file_checker->file_exists(path)) {
  392. arr.push_back(location.to_json());
  393. }
  394. r_list.push_back(symbol);
  395. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  396. List<const lsp::DocumentSymbol *> list;
  397. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list);
  398. for (const lsp::DocumentSymbol *&E : list) {
  399. if (const lsp::DocumentSymbol *s = E) {
  400. if (!s->uri.is_empty()) {
  401. lsp::Location location;
  402. location.uri = s->uri;
  403. location.range = s->range;
  404. arr.push_back(location.to_json());
  405. r_list.push_back(s);
  406. }
  407. }
  408. }
  409. }
  410. return arr;
  411. }