gdscript_text_document.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "gdscript_extend_parser.h"
  33. #include "gdscript_language_protocol.h"
  34. void GDScriptTextDocument::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("didOpen"), &GDScriptTextDocument::didOpen);
  36. ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange);
  37. ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol);
  38. ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion);
  39. ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve);
  40. ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange);
  41. ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens);
  42. ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink);
  43. ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation);
  44. ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover);
  45. ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition);
  46. }
  47. void GDScriptTextDocument::didOpen(const Variant &p_param) {
  48. lsp::TextDocumentItem doc = load_document_item(p_param);
  49. sync_script_content(doc.uri, doc.text);
  50. }
  51. void GDScriptTextDocument::didChange(const Variant &p_param) {
  52. lsp::TextDocumentItem doc = load_document_item(p_param);
  53. Dictionary dict = p_param;
  54. Array contentChanges = dict["contentChanges"];
  55. for (int i = 0; i < contentChanges.size(); ++i) {
  56. lsp::TextDocumentContentChangeEvent evt;
  57. evt.load(contentChanges[i]);
  58. doc.text = evt.text;
  59. }
  60. sync_script_content(doc.uri, doc.text);
  61. }
  62. lsp::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) {
  63. lsp::TextDocumentItem doc;
  64. Dictionary params = p_param;
  65. doc.load(params["textDocument"]);
  66. print_line(doc.text);
  67. return doc;
  68. }
  69. Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
  70. Dictionary params = p_params["textDocument"];
  71. String uri = params["uri"];
  72. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(uri);
  73. Array arr;
  74. if (const Map<String, ExtendGDScriptParser *>::Element *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace().scripts.find(path)) {
  75. Vector<lsp::DocumentedSymbolInformation> list;
  76. parser->get()->get_symbols().symbol_tree_as_list(uri, list);
  77. for (int i = 0; i < list.size(); i++) {
  78. arr.push_back(list[i].to_json());
  79. }
  80. }
  81. return arr;
  82. }
  83. Array GDScriptTextDocument::completion(const Dictionary &p_params) {
  84. Array arr;
  85. lsp::CompletionParams params;
  86. params.load(p_params);
  87. Dictionary request_data = params.to_json();
  88. List<ScriptCodeCompletionOption> options;
  89. GDScriptLanguageProtocol::get_singleton()->get_workspace().completion(params, &options);
  90. for (const List<ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) {
  91. const ScriptCodeCompletionOption &option = E->get();
  92. lsp::CompletionItem item;
  93. item.label = option.display;
  94. item.insertText = option.insert_text;
  95. item.data = request_data;
  96. if (params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "'" || params.context.triggerCharacter == "\"") && (option.insert_text.begins_with("'") || option.insert_text.begins_with("\""))) {
  97. item.insertText = option.insert_text.substr(1, option.insert_text.length() - 2);
  98. }
  99. switch (option.kind) {
  100. case ScriptCodeCompletionOption::KIND_ENUM:
  101. item.kind = lsp::CompletionItemKind::Enum;
  102. break;
  103. case ScriptCodeCompletionOption::KIND_CLASS:
  104. item.kind = lsp::CompletionItemKind::Class;
  105. break;
  106. case ScriptCodeCompletionOption::KIND_MEMBER:
  107. item.kind = lsp::CompletionItemKind::Property;
  108. break;
  109. case ScriptCodeCompletionOption::KIND_FUNCTION:
  110. item.kind = lsp::CompletionItemKind::Method;
  111. break;
  112. case ScriptCodeCompletionOption::KIND_SIGNAL:
  113. item.kind = lsp::CompletionItemKind::Event;
  114. break;
  115. case ScriptCodeCompletionOption::KIND_CONSTANT:
  116. item.kind = lsp::CompletionItemKind::Constant;
  117. break;
  118. case ScriptCodeCompletionOption::KIND_VARIABLE:
  119. item.kind = lsp::CompletionItemKind::Variable;
  120. break;
  121. case ScriptCodeCompletionOption::KIND_FILE_PATH:
  122. item.kind = lsp::CompletionItemKind::File;
  123. break;
  124. case ScriptCodeCompletionOption::KIND_NODE_PATH:
  125. item.kind = lsp::CompletionItemKind::Snippet;
  126. break;
  127. case ScriptCodeCompletionOption::KIND_PLAIN_TEXT:
  128. item.kind = lsp::CompletionItemKind::Text;
  129. break;
  130. }
  131. arr.push_back(item.to_json());
  132. }
  133. return arr;
  134. }
  135. Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
  136. lsp::CompletionItem item;
  137. item.load(p_params);
  138. lsp::CompletionParams params;
  139. params.load(p_params["data"]);
  140. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace().resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function);
  141. if (symbol) {
  142. item.documentation = symbol->render();
  143. }
  144. return item.to_json();
  145. }
  146. Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
  147. Dictionary params = p_params["textDocument"];
  148. String path = params["uri"];
  149. Array arr;
  150. return arr;
  151. }
  152. Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
  153. Array arr;
  154. return arr;
  155. }
  156. Variant GDScriptTextDocument::documentLink(const Dictionary &p_params) {
  157. Variant ret;
  158. return ret;
  159. }
  160. Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
  161. Array arr;
  162. return arr;
  163. }
  164. Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
  165. Variant ret;
  166. lsp::TextDocumentPositionParams params;
  167. params.load(p_params);
  168. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace().resolve_symbol(params);
  169. if (symbol) {
  170. lsp::Hover hover;
  171. hover.contents = symbol->render();
  172. ret = hover.to_json();
  173. }
  174. return ret;
  175. }
  176. Array GDScriptTextDocument::definition(const Dictionary &p_params) {
  177. Array arr;
  178. lsp::TextDocumentPositionParams params;
  179. params.load(p_params);
  180. const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace().resolve_symbol(params);
  181. if (symbol) {
  182. lsp::Location location;
  183. location.uri = symbol->uri;
  184. location.range = symbol->range;
  185. const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(symbol->uri);
  186. if (file_checker->file_exists(path)) {
  187. arr.push_back(location.to_json());
  188. }
  189. }
  190. return arr;
  191. }
  192. GDScriptTextDocument::GDScriptTextDocument() {
  193. file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  194. }
  195. GDScriptTextDocument::~GDScriptTextDocument() {
  196. memdelete(file_checker);
  197. }
  198. void GDScriptTextDocument::sync_script_content(const String &p_uri, const String &p_content) {
  199. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(p_uri);
  200. GDScriptLanguageProtocol::get_singleton()->get_workspace().parse_script(path, p_content);
  201. }