gdscript_text_document.cpp 16 KB

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