gdscript_text_document.cpp 15 KB

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