gdscript_text_document.cpp 16 KB

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