gdscript_workspace.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*************************************************************************/
  2. /* gdscript_workspace.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_workspace.h"
  31. #include "../gdscript.h"
  32. #include "../gdscript_parser.h"
  33. #include "core/project_settings.h"
  34. #include "gdscript_language_protocol.h"
  35. void GDScriptWorkspace::_bind_methods() {
  36. ClassDB::bind_method(D_METHOD("symbol"), &GDScriptWorkspace::symbol);
  37. }
  38. void GDScriptWorkspace::remove_cache_parser(const String &p_path) {
  39. Map<String, ExtendGDScriptParser *>::Element *parser = parse_results.find(p_path);
  40. Map<String, ExtendGDScriptParser *>::Element *script = scripts.find(p_path);
  41. if (parser && script) {
  42. if (script->get() && script->get() == script->get()) {
  43. memdelete(script->get());
  44. } else {
  45. memdelete(script->get());
  46. memdelete(parser->get());
  47. }
  48. parse_results.erase(p_path);
  49. scripts.erase(p_path);
  50. } else if (parser) {
  51. memdelete(parser->get());
  52. parse_results.erase(p_path);
  53. } else if (script) {
  54. memdelete(script->get());
  55. scripts.erase(p_path);
  56. }
  57. }
  58. Array GDScriptWorkspace::symbol(const Dictionary &p_params) {
  59. String query = p_params["query"];
  60. Array arr;
  61. if (!query.empty()) {
  62. for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
  63. Vector<lsp::SymbolInformation> script_symbols;
  64. E->get()->get_symbols().symbol_tree_as_list(E->key(), script_symbols);
  65. for (int i = 0; i < script_symbols.size(); ++i) {
  66. if (query.is_subsequence_ofi(script_symbols[i].name)) {
  67. arr.push_back(script_symbols[i].to_json());
  68. }
  69. }
  70. }
  71. }
  72. return arr;
  73. }
  74. Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_content) {
  75. ExtendGDScriptParser *parser = memnew(ExtendGDScriptParser);
  76. Error err = parser->parse(p_content, p_path);
  77. Map<String, ExtendGDScriptParser *>::Element *last_parser = parse_results.find(p_path);
  78. Map<String, ExtendGDScriptParser *>::Element *last_script = scripts.find(p_path);
  79. if (err == OK) {
  80. remove_cache_parser(p_path);
  81. parse_results[p_path] = parser;
  82. scripts[p_path] = parser;
  83. } else {
  84. if (last_parser && last_script && last_parser->get() != last_script->get()) {
  85. memdelete(last_parser->get());
  86. }
  87. parse_results[p_path] = parser;
  88. }
  89. publish_diagnostics(p_path);
  90. return err;
  91. }
  92. String GDScriptWorkspace::get_file_path(const String &p_uri) const {
  93. String path = p_uri.replace("file://", "").http_unescape();
  94. path = path.replace(root + "/", "res://");
  95. return ProjectSettings::get_singleton()->localize_path(path);
  96. }
  97. String GDScriptWorkspace::get_file_uri(const String &p_path) const {
  98. String path = ProjectSettings::get_singleton()->globalize_path(p_path);
  99. return "file://" + path;
  100. }
  101. void GDScriptWorkspace::publish_diagnostics(const String &p_path) {
  102. Dictionary params;
  103. Array errors;
  104. const Map<String, ExtendGDScriptParser *>::Element *ele = parse_results.find(p_path);
  105. if (ele) {
  106. const Vector<lsp::Diagnostic> &list = ele->get()->get_diagnostics();
  107. errors.resize(list.size());
  108. for (int i = 0; i < list.size(); ++i) {
  109. errors[i] = list[i].to_json();
  110. }
  111. }
  112. params["diagnostics"] = errors;
  113. params["uri"] = get_file_uri(p_path);
  114. GDScriptLanguageProtocol::get_singleton()->notify_client("textDocument/publishDiagnostics", params);
  115. }
  116. void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *r_options) {
  117. String path = get_file_path(p_params.textDocument.uri);
  118. String call_hint;
  119. bool forced = false;
  120. if (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.find(path)) {
  121. String code = E->get()->get_text_for_completion(p_params.position);
  122. GDScriptLanguage::get_singleton()->complete_code(code, path, NULL, r_options, forced, call_hint);
  123. }
  124. }
  125. GDScriptWorkspace::GDScriptWorkspace() {
  126. ProjectSettings::get_singleton()->get_resource_path();
  127. }
  128. GDScriptWorkspace::~GDScriptWorkspace() {
  129. Set<String> cached_parsers;
  130. for (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.front(); E; E = E->next()) {
  131. cached_parsers.insert(E->key());
  132. }
  133. for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
  134. cached_parsers.insert(E->key());
  135. }
  136. for (Set<String>::Element *E = cached_parsers.front(); E; E = E->next()) {
  137. remove_cache_parser(E->get());
  138. }
  139. }