gdscript_text_document.cpp 17 KB

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