gdscript_text_document.cpp 17 KB

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