gdscript_text_document.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_language_protocol.h"
  33. void GDScriptTextDocument::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("didOpen"), &GDScriptTextDocument::didOpen);
  35. ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange);
  36. ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol);
  37. ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion);
  38. ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange);
  39. ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens);
  40. ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink);
  41. ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation);
  42. ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover);
  43. }
  44. void GDScriptTextDocument::didOpen(const Variant &p_param) {
  45. lsp::TextDocumentItem doc = load_document_item(p_param);
  46. sync_script_content(doc.uri, doc.text);
  47. }
  48. void GDScriptTextDocument::didChange(const Variant &p_param) {
  49. lsp::TextDocumentItem doc = load_document_item(p_param);
  50. Dictionary dict = p_param;
  51. Array contentChanges = dict["contentChanges"];
  52. for (int i = 0; i < contentChanges.size(); ++i) {
  53. lsp::TextDocumentContentChangeEvent evt;
  54. evt.load(contentChanges[i]);
  55. doc.text = evt.text;
  56. }
  57. sync_script_content(doc.uri, doc.text);
  58. }
  59. lsp::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) {
  60. lsp::TextDocumentItem doc;
  61. Dictionary params = p_param;
  62. doc.load(params["textDocument"]);
  63. print_line(doc.text);
  64. return doc;
  65. }
  66. Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
  67. Dictionary params = p_params["textDocument"];
  68. String uri = params["uri"];
  69. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(uri);
  70. Array arr;
  71. if (const Map<String, ExtendGDScriptParser *>::Element *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace().scripts.find(path)) {
  72. Vector<lsp::SymbolInformation> list;
  73. parser->get()->get_symbols().symbol_tree_as_list(uri, list);
  74. for (int i = 0; i < list.size(); i++) {
  75. arr.push_back(list[i].to_json());
  76. }
  77. }
  78. return arr;
  79. }
  80. Array GDScriptTextDocument::completion(const Dictionary &p_params) {
  81. Array arr;
  82. lsp::CompletionParams params;
  83. params.load(p_params);
  84. Dictionary request_data = params.to_json();
  85. List<ScriptCodeCompletionOption> options;
  86. GDScriptLanguageProtocol::get_singleton()->get_workspace().completion(params, &options);
  87. for (const List<ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) {
  88. const ScriptCodeCompletionOption &option = E->get();
  89. lsp::CompletionItem item;
  90. item.label = option.display;
  91. item.insertText = option.insert_text;
  92. item.data = request_data;
  93. if (params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "'" || params.context.triggerCharacter == "\"") && (option.insert_text.begins_with("'") || option.insert_text.begins_with("\""))) {
  94. item.insertText = option.insert_text.substr(1, option.insert_text.length() - 2);
  95. }
  96. switch (option.kind) {
  97. case ScriptCodeCompletionOption::KIND_ENUM:
  98. item.kind = lsp::CompletionItemKind::Enum;
  99. break;
  100. case ScriptCodeCompletionOption::KIND_CLASS:
  101. item.kind = lsp::CompletionItemKind::Class;
  102. break;
  103. case ScriptCodeCompletionOption::KIND_MEMBER:
  104. item.kind = lsp::CompletionItemKind::Property;
  105. break;
  106. case ScriptCodeCompletionOption::KIND_FUNCTION:
  107. item.kind = lsp::CompletionItemKind::Method;
  108. break;
  109. case ScriptCodeCompletionOption::KIND_SIGNAL:
  110. item.kind = lsp::CompletionItemKind::Event;
  111. break;
  112. case ScriptCodeCompletionOption::KIND_CONSTANT:
  113. item.kind = lsp::CompletionItemKind::Constant;
  114. break;
  115. case ScriptCodeCompletionOption::KIND_VARIABLE:
  116. item.kind = lsp::CompletionItemKind::Variable;
  117. break;
  118. case ScriptCodeCompletionOption::KIND_FILE_PATH:
  119. item.kind = lsp::CompletionItemKind::File;
  120. break;
  121. case ScriptCodeCompletionOption::KIND_NODE_PATH:
  122. item.kind = lsp::CompletionItemKind::Snippet;
  123. break;
  124. case ScriptCodeCompletionOption::KIND_PLAIN_TEXT:
  125. item.kind = lsp::CompletionItemKind::Text;
  126. break;
  127. }
  128. arr.push_back(item.to_json());
  129. }
  130. return arr;
  131. }
  132. Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
  133. Dictionary params = p_params["textDocument"];
  134. String path = params["uri"];
  135. Array arr;
  136. return arr;
  137. }
  138. Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
  139. Array arr;
  140. return arr;
  141. }
  142. Variant GDScriptTextDocument::documentLink(const Dictionary &p_params) {
  143. Variant ret;
  144. return ret;
  145. }
  146. Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
  147. Array arr;
  148. return arr;
  149. }
  150. Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
  151. Variant ret;
  152. return ret;
  153. }
  154. void GDScriptTextDocument::sync_script_content(const String &p_uri, const String &p_content) {
  155. String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(p_uri);
  156. GDScriptLanguageProtocol::get_singleton()->get_workspace().parse_script(path, p_content);
  157. }