gdscript_text_document.cpp 16 KB

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