gdscript_text_document.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. 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. switch (option.kind) {
  150. case ScriptCodeCompletionOption::KIND_ENUM:
  151. item.kind = lsp::CompletionItemKind::Enum;
  152. break;
  153. case ScriptCodeCompletionOption::KIND_CLASS:
  154. item.kind = lsp::CompletionItemKind::Class;
  155. break;
  156. case ScriptCodeCompletionOption::KIND_MEMBER:
  157. item.kind = lsp::CompletionItemKind::Property;
  158. break;
  159. case ScriptCodeCompletionOption::KIND_FUNCTION:
  160. item.kind = lsp::CompletionItemKind::Method;
  161. break;
  162. case ScriptCodeCompletionOption::KIND_SIGNAL:
  163. item.kind = lsp::CompletionItemKind::Event;
  164. break;
  165. case ScriptCodeCompletionOption::KIND_CONSTANT:
  166. item.kind = lsp::CompletionItemKind::Constant;
  167. break;
  168. case ScriptCodeCompletionOption::KIND_VARIABLE:
  169. item.kind = lsp::CompletionItemKind::Variable;
  170. break;
  171. case ScriptCodeCompletionOption::KIND_FILE_PATH:
  172. item.kind = lsp::CompletionItemKind::File;
  173. break;
  174. case ScriptCodeCompletionOption::KIND_NODE_PATH:
  175. item.kind = lsp::CompletionItemKind::Snippet;
  176. break;
  177. case ScriptCodeCompletionOption::KIND_PLAIN_TEXT:
  178. item.kind = lsp::CompletionItemKind::Text;
  179. break;
  180. }
  181. arr[i] = item.to_json();
  182. i++;
  183. }
  184. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  185. arr = native_member_completions.duplicate();
  186. for (Map<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.front(); E; E = E->next()) {
  187. ExtendGDScriptParser *script = E->get();
  188. const Array &items = script->get_member_completions();
  189. const int start_size = arr.size();
  190. arr.resize(start_size + items.size());
  191. for (int i = start_size; i < arr.size(); i++) {
  192. arr[i] = items[i - start_size];
  193. }
  194. }
  195. }
  196. return arr;
  197. }
  198. Dictionary GDScriptTextDocument::rename(const Dictionary &p_params) {
  199. lsp::TextDocumentPositionParams params;
  200. params.load(p_params);
  201. String new_name = p_params["newName"];
  202. return GDScriptLanguageProtocol::get_singleton()->get_workspace()->rename(params, new_name);
  203. }
  204. Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
  205. lsp::CompletionItem item;
  206. item.load(p_params);
  207. lsp::CompletionParams params;
  208. Variant data = p_params["data"];
  209. const lsp::DocumentSymbol *symbol = nullptr;
  210. if (data.get_type() == Variant::DICTIONARY) {
  211. params.load(p_params["data"]);
  212. symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function);
  213. } else if (data.get_type() == Variant::STRING) {
  214. String query = data;
  215. Vector<String> param_symbols = query.split(SYMBOL_SEPERATOR, false);
  216. if (param_symbols.size() >= 2) {
  217. String class_ = param_symbols[0];
  218. StringName class_name = class_;
  219. String member_name = param_symbols[param_symbols.size() - 1];
  220. String inner_class_name;
  221. if (param_symbols.size() >= 3) {
  222. inner_class_name = param_symbols[1];
  223. }
  224. if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) {
  225. if (const lsp::DocumentSymbol *const *member = members->getptr(member_name)) {
  226. symbol = *member;
  227. }
  228. }
  229. if (!symbol) {
  230. if (const Map<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(class_name)) {
  231. symbol = E->get()->get_member_symbol(member_name, inner_class_name);
  232. }
  233. }
  234. }
  235. }
  236. if (symbol) {
  237. item.documentation = symbol->render();
  238. }
  239. if ((item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function) && !item.label.ends_with("):")) {
  240. item.insertText = item.label + "(";
  241. if (symbol && symbol->children.empty()) {
  242. item.insertText += ")";
  243. }
  244. } else if (item.kind == lsp::CompletionItemKind::Event) {
  245. if (params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) {
  246. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  247. item.insertText = quote_style + item.label + quote_style;
  248. }
  249. }
  250. return item.to_json(true);
  251. }
  252. Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
  253. Array arr;
  254. return arr;
  255. }
  256. Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
  257. Array arr;
  258. return arr;
  259. }
  260. Array GDScriptTextDocument::documentLink(const Dictionary &p_params) {
  261. Array ret;
  262. lsp::DocumentLinkParams params;
  263. params.load(p_params);
  264. List<lsp::DocumentLink> links;
  265. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links);
  266. for (const List<lsp::DocumentLink>::Element *E = links.front(); E; E = E->next()) {
  267. ret.push_back(E->get().to_json());
  268. }
  269. return ret;
  270. }
  271. Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
  272. Array arr;
  273. return arr;
  274. }
  275. Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
  276. lsp::TextDocumentPositionParams params;
  277. params.load(p_params);
  278. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
  279. if (symbol) {
  280. lsp::Hover hover;
  281. hover.contents = symbol->render();
  282. hover.range.start = params.position;
  283. hover.range.end = params.position;
  284. return hover.to_json();
  285. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  286. Dictionary ret;
  287. Array contents;
  288. List<const lsp::DocumentSymbol *> list;
  289. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list);
  290. for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) {
  291. if (const lsp::DocumentSymbol *s = E->get()) {
  292. contents.push_back(s->render().value);
  293. }
  294. }
  295. ret["contents"] = contents;
  296. return ret;
  297. }
  298. return Variant();
  299. }
  300. Array GDScriptTextDocument::definition(const Dictionary &p_params) {
  301. lsp::TextDocumentPositionParams params;
  302. params.load(p_params);
  303. List<const lsp::DocumentSymbol *> symbols;
  304. Array arr = this->find_symbols(params, symbols);
  305. return arr;
  306. }
  307. Variant GDScriptTextDocument::declaration(const Dictionary &p_params) {
  308. lsp::TextDocumentPositionParams params;
  309. params.load(p_params);
  310. List<const lsp::DocumentSymbol *> symbols;
  311. Array arr = this->find_symbols(params, symbols);
  312. if (arr.empty() && !symbols.empty() && !symbols.front()->get()->native_class.empty()) { // Find a native symbol
  313. const lsp::DocumentSymbol *symbol = symbols.front()->get();
  314. if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {
  315. String id;
  316. switch (symbol->kind) {
  317. case lsp::SymbolKind::Class:
  318. id = "class_name:" + symbol->name;
  319. break;
  320. case lsp::SymbolKind::Constant:
  321. id = "class_constant:" + symbol->native_class + ":" + symbol->name;
  322. break;
  323. case lsp::SymbolKind::Property:
  324. case lsp::SymbolKind::Variable:
  325. id = "class_property:" + symbol->native_class + ":" + symbol->name;
  326. break;
  327. case lsp::SymbolKind::Enum:
  328. id = "class_enum:" + symbol->native_class + ":" + symbol->name;
  329. break;
  330. case lsp::SymbolKind::Method:
  331. case lsp::SymbolKind::Function:
  332. id = "class_method:" + symbol->native_class + ":" + symbol->name;
  333. break;
  334. default:
  335. id = "class_global:" + symbol->native_class + ":" + symbol->name;
  336. break;
  337. }
  338. call_deferred("show_native_symbol_in_editor", id);
  339. } else {
  340. notify_client_show_symbol(symbol);
  341. }
  342. }
  343. return arr;
  344. }
  345. Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) {
  346. Variant ret;
  347. lsp::TextDocumentPositionParams params;
  348. params.load(p_params);
  349. lsp::SignatureHelp s;
  350. if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) {
  351. ret = s.to_json();
  352. }
  353. return ret;
  354. }
  355. GDScriptTextDocument::GDScriptTextDocument() {
  356. file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  357. }
  358. GDScriptTextDocument::~GDScriptTextDocument() {
  359. memdelete(file_checker);
  360. }
  361. void GDScriptTextDocument::sync_script_content(const String &p_path, const String &p_content) {
  362. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_path);
  363. GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content);
  364. EditorFileSystem::get_singleton()->update_file(path);
  365. Error error;
  366. Ref<GDScript> script = ResourceLoader::load(path, "", false, &error);
  367. if (error == OK) {
  368. if (script->load_source_code(path) == OK) {
  369. script->reload(true);
  370. }
  371. }
  372. }
  373. void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {
  374. ScriptEditor::get_singleton()->call_deferred("_help_class_goto", p_symbol_id);
  375. OS::get_singleton()->move_window_to_foreground();
  376. }
  377. Array GDScriptTextDocument::find_symbols(const lsp::TextDocumentPositionParams &p_location, List<const lsp::DocumentSymbol *> &r_list) {
  378. Array arr;
  379. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);
  380. if (symbol) {
  381. lsp::Location location;
  382. location.uri = symbol->uri;
  383. location.range = symbol->range;
  384. const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);
  385. if (file_checker->file_exists(path)) {
  386. arr.push_back(location.to_json());
  387. }
  388. r_list.push_back(symbol);
  389. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  390. List<const lsp::DocumentSymbol *> list;
  391. GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list);
  392. for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) {
  393. if (const lsp::DocumentSymbol *s = E->get()) {
  394. if (!s->uri.empty()) {
  395. lsp::Location location;
  396. location.uri = s->uri;
  397. location.range = s->range;
  398. arr.push_back(location.to_json());
  399. r_list.push_back(s);
  400. }
  401. }
  402. }
  403. }
  404. return arr;
  405. }