gdscript_text_document.cpp 16 KB

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