gdscript_text_document.cpp 17 KB

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