gdscript_text_document.cpp 14 KB

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