gdscript_text_document.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "gdscript_extend_parser.h"
  35. #include "gdscript_language_protocol.h"
  36. void GDScriptTextDocument::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("didOpen"), &GDScriptTextDocument::didOpen);
  38. ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange);
  39. ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol);
  40. ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion);
  41. ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve);
  42. ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange);
  43. ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens);
  44. ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink);
  45. ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation);
  46. ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover);
  47. ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition);
  48. }
  49. void GDScriptTextDocument::didOpen(const Variant &p_param) {
  50. lsp::TextDocumentItem doc = load_document_item(p_param);
  51. sync_script_content(doc.uri, doc.text);
  52. }
  53. void GDScriptTextDocument::didChange(const Variant &p_param) {
  54. lsp::TextDocumentItem doc = load_document_item(p_param);
  55. Dictionary dict = p_param;
  56. Array contentChanges = dict["contentChanges"];
  57. for (int i = 0; i < contentChanges.size(); ++i) {
  58. lsp::TextDocumentContentChangeEvent evt;
  59. evt.load(contentChanges[i]);
  60. doc.text = evt.text;
  61. }
  62. sync_script_content(doc.uri, doc.text);
  63. }
  64. lsp::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) {
  65. lsp::TextDocumentItem doc;
  66. Dictionary params = p_param;
  67. doc.load(params["textDocument"]);
  68. return doc;
  69. }
  70. void GDScriptTextDocument::initialize() {
  71. if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  72. const HashMap<StringName, ClassMembers> &native_members = GDScriptLanguageProtocol::get_singleton()->get_workspace().native_members;
  73. const StringName *class_ptr = native_members.next(NULL);
  74. while (class_ptr) {
  75. const ClassMembers &members = native_members.get(*class_ptr);
  76. const String *name = members.next(NULL);
  77. while (name) {
  78. const lsp::DocumentSymbol *symbol = members.get(*name);
  79. lsp::CompletionItem item = symbol->make_completion_item();
  80. item.data = JOIN_SYMBOLS(String(*class_ptr), *name);
  81. native_member_completions.push_back(item.to_json());
  82. name = members.next(name);
  83. }
  84. class_ptr = native_members.next(class_ptr);
  85. }
  86. }
  87. }
  88. Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
  89. Dictionary params = p_params["textDocument"];
  90. String uri = params["uri"];
  91. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(uri);
  92. Array arr;
  93. if (const Map<String, ExtendGDScriptParser *>::Element *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace().scripts.find(path)) {
  94. Vector<lsp::DocumentedSymbolInformation> list;
  95. parser->get()->get_symbols().symbol_tree_as_list(uri, list);
  96. for (int i = 0; i < list.size(); i++) {
  97. arr.push_back(list[i].to_json());
  98. }
  99. }
  100. return arr;
  101. }
  102. Array GDScriptTextDocument::completion(const Dictionary &p_params) {
  103. Array arr;
  104. lsp::CompletionParams params;
  105. params.load(p_params);
  106. Dictionary request_data = params.to_json();
  107. List<ScriptCodeCompletionOption> options;
  108. GDScriptLanguageProtocol::get_singleton()->get_workspace().completion(params, &options);
  109. if (!options.empty()) {
  110. int i = 0;
  111. arr.resize(options.size());
  112. for (const List<ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) {
  113. const ScriptCodeCompletionOption &option = E->get();
  114. lsp::CompletionItem item;
  115. item.label = option.display;
  116. item.data = request_data;
  117. switch (option.kind) {
  118. case ScriptCodeCompletionOption::KIND_ENUM:
  119. item.kind = lsp::CompletionItemKind::Enum;
  120. break;
  121. case ScriptCodeCompletionOption::KIND_CLASS:
  122. item.kind = lsp::CompletionItemKind::Class;
  123. break;
  124. case ScriptCodeCompletionOption::KIND_MEMBER:
  125. item.kind = lsp::CompletionItemKind::Property;
  126. break;
  127. case ScriptCodeCompletionOption::KIND_FUNCTION:
  128. item.kind = lsp::CompletionItemKind::Method;
  129. break;
  130. case ScriptCodeCompletionOption::KIND_SIGNAL:
  131. item.kind = lsp::CompletionItemKind::Event;
  132. break;
  133. case ScriptCodeCompletionOption::KIND_CONSTANT:
  134. item.kind = lsp::CompletionItemKind::Constant;
  135. break;
  136. case ScriptCodeCompletionOption::KIND_VARIABLE:
  137. item.kind = lsp::CompletionItemKind::Variable;
  138. break;
  139. case ScriptCodeCompletionOption::KIND_FILE_PATH:
  140. item.kind = lsp::CompletionItemKind::File;
  141. break;
  142. case ScriptCodeCompletionOption::KIND_NODE_PATH:
  143. item.kind = lsp::CompletionItemKind::Snippet;
  144. break;
  145. case ScriptCodeCompletionOption::KIND_PLAIN_TEXT:
  146. item.kind = lsp::CompletionItemKind::Text;
  147. break;
  148. }
  149. arr[i] = item.to_json();
  150. i++;
  151. }
  152. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  153. arr = native_member_completions.duplicate();
  154. for (Map<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace().scripts.front(); E; E = E->next()) {
  155. ExtendGDScriptParser *script = E->get();
  156. const Array &items = script->get_member_completions();
  157. const int start_size = arr.size();
  158. arr.resize(start_size + items.size());
  159. for (int i = start_size; i < arr.size(); i++) {
  160. arr[i] = items[i - start_size];
  161. }
  162. }
  163. }
  164. return arr;
  165. }
  166. Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
  167. lsp::CompletionItem item;
  168. item.load(p_params);
  169. lsp::CompletionParams params;
  170. Variant data = p_params["data"];
  171. const lsp::DocumentSymbol *symbol = NULL;
  172. if (data.get_type() == Variant::DICTIONARY) {
  173. params.load(p_params["data"]);
  174. symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace().resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function);
  175. } else if (data.get_type() == Variant::STRING) {
  176. String query = data;
  177. Vector<String> param_symbols = query.split(SYMBOL_SEPERATOR, false);
  178. if (param_symbols.size() >= 2) {
  179. String class_ = param_symbols[0];
  180. StringName class_name = class_;
  181. String member_name = param_symbols[param_symbols.size() - 1];
  182. String inner_class_name;
  183. if (param_symbols.size() >= 3) {
  184. inner_class_name = param_symbols[1];
  185. }
  186. if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace().native_members.getptr(class_name)) {
  187. if (const lsp::DocumentSymbol *const *member = members->getptr(member_name)) {
  188. symbol = *member;
  189. }
  190. }
  191. if (!symbol) {
  192. if (const Map<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace().scripts.find(class_name)) {
  193. symbol = E->get()->get_member_symbol(member_name, inner_class_name);
  194. }
  195. }
  196. }
  197. }
  198. if (symbol) {
  199. item.documentation = symbol->render();
  200. }
  201. if (item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function) {
  202. item.insertText = item.label + "(";
  203. if (symbol && symbol->detail.find(",") == -1) {
  204. item.insertText += ")";
  205. }
  206. } else if (item.kind == lsp::CompletionItemKind::Event) {
  207. if (params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) {
  208. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  209. item.insertText = quote_style + item.label + quote_style;
  210. }
  211. }
  212. return item.to_json(true);
  213. }
  214. Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
  215. Dictionary params = p_params["textDocument"];
  216. String path = params["uri"];
  217. Array arr;
  218. return arr;
  219. }
  220. Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
  221. Array arr;
  222. return arr;
  223. }
  224. Variant GDScriptTextDocument::documentLink(const Dictionary &p_params) {
  225. Variant ret;
  226. return ret;
  227. }
  228. Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
  229. Array arr;
  230. return arr;
  231. }
  232. Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
  233. lsp::TextDocumentPositionParams params;
  234. params.load(p_params);
  235. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace().resolve_symbol(params);
  236. if (symbol) {
  237. lsp::Hover hover;
  238. hover.contents = symbol->render();
  239. return hover.to_json();
  240. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  241. Dictionary ret;
  242. Array contents;
  243. List<const lsp::DocumentSymbol *> list;
  244. GDScriptLanguageProtocol::get_singleton()->get_workspace().resolve_related_symbols(params, list);
  245. for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) {
  246. if (const lsp::DocumentSymbol *s = E->get()) {
  247. contents.push_back(s->render().value);
  248. }
  249. }
  250. ret["contents"] = contents;
  251. return ret;
  252. }
  253. return Variant();
  254. }
  255. Array GDScriptTextDocument::definition(const Dictionary &p_params) {
  256. Array arr;
  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::Location location;
  262. location.uri = symbol->uri;
  263. location.range = symbol->range;
  264. const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(symbol->uri);
  265. if (file_checker->file_exists(path)) {
  266. arr.push_back(location.to_json());
  267. }
  268. } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
  269. List<const lsp::DocumentSymbol *> list;
  270. GDScriptLanguageProtocol::get_singleton()->get_workspace().resolve_related_symbols(params, list);
  271. for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) {
  272. if (const lsp::DocumentSymbol *s = E->get()) {
  273. lsp::Location location;
  274. location.uri = s->uri;
  275. location.range = s->range;
  276. arr.push_back(location.to_json());
  277. }
  278. }
  279. }
  280. return arr;
  281. }
  282. GDScriptTextDocument::GDScriptTextDocument() {
  283. file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  284. }
  285. GDScriptTextDocument::~GDScriptTextDocument() {
  286. memdelete(file_checker);
  287. }
  288. void GDScriptTextDocument::sync_script_content(const String &p_uri, const String &p_content) {
  289. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(p_uri);
  290. GDScriptLanguageProtocol::get_singleton()->get_workspace().parse_script(path, p_content);
  291. }