gdscript_text_document.cpp 17 KB

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