code_completion.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**************************************************************************/
  2. /* code_completion.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "code_completion.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/object/script_language.h"
  33. #include "editor/editor_file_system.h"
  34. #include "editor/editor_settings.h"
  35. #include "scene/gui/control.h"
  36. #include "scene/main/node.h"
  37. #include "scene/theme/theme_db.h"
  38. namespace gdmono {
  39. // Almost everything here is taken from functions used by GDScript for code completion, adapted for C#.
  40. _FORCE_INLINE_ String quoted(const String &p_str) {
  41. return "\"" + p_str + "\"";
  42. }
  43. void _add_nodes_suggestions(const Node *p_base, const Node *p_node, PackedStringArray &r_suggestions) {
  44. if (p_node != p_base && !p_node->get_owner()) {
  45. return;
  46. }
  47. String path_relative_to_orig = p_base->get_path_to(p_node);
  48. r_suggestions.push_back(quoted(path_relative_to_orig));
  49. for (int i = 0; i < p_node->get_child_count(); i++) {
  50. _add_nodes_suggestions(p_base, p_node->get_child(i), r_suggestions);
  51. }
  52. }
  53. Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  54. if (p_current->get_owner() != p_base && p_base != p_current) {
  55. return nullptr;
  56. }
  57. Ref<Script> c = p_current->get_script();
  58. if (c == p_script) {
  59. return p_current;
  60. }
  61. for (int i = 0; i < p_current->get_child_count(); i++) {
  62. Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
  63. if (found) {
  64. return found;
  65. }
  66. }
  67. return nullptr;
  68. }
  69. void _get_directory_contents(EditorFileSystemDirectory *p_dir, PackedStringArray &r_suggestions) {
  70. for (int i = 0; i < p_dir->get_file_count(); i++) {
  71. r_suggestions.push_back(quoted(p_dir->get_file_path(i)));
  72. }
  73. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  74. _get_directory_contents(p_dir->get_subdir(i), r_suggestions);
  75. }
  76. }
  77. Node *_try_find_owner_node_in_tree(const Ref<Script> p_script) {
  78. SceneTree *tree = SceneTree::get_singleton();
  79. if (!tree) {
  80. return nullptr;
  81. }
  82. Node *base = tree->get_edited_scene_root();
  83. if (base) {
  84. base = _find_node_for_script(base, base, p_script);
  85. }
  86. return base;
  87. }
  88. PackedStringArray get_code_completion(CompletionKind p_kind, const String &p_script_file) {
  89. PackedStringArray suggestions;
  90. switch (p_kind) {
  91. case CompletionKind::INPUT_ACTIONS: {
  92. List<PropertyInfo> project_props;
  93. ProjectSettings::get_singleton()->get_property_list(&project_props);
  94. for (const PropertyInfo &prop : project_props) {
  95. if (!prop.name.begins_with("input/")) {
  96. continue;
  97. }
  98. String name = prop.name.substr(prop.name.find("/") + 1, prop.name.length());
  99. suggestions.push_back(quoted(name));
  100. }
  101. } break;
  102. case CompletionKind::NODE_PATHS: {
  103. {
  104. // Autoloads.
  105. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  106. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  107. const ProjectSettings::AutoloadInfo &info = E.value;
  108. suggestions.push_back(quoted("/root/" + String(info.name)));
  109. }
  110. }
  111. {
  112. // Current edited scene tree
  113. Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
  114. Node *base = _try_find_owner_node_in_tree(script);
  115. if (base) {
  116. _add_nodes_suggestions(base, base, suggestions);
  117. }
  118. }
  119. } break;
  120. case CompletionKind::RESOURCE_PATHS: {
  121. if (bool(EDITOR_GET("text_editor/completion/complete_file_paths"))) {
  122. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), suggestions);
  123. }
  124. } break;
  125. case CompletionKind::SCENE_PATHS: {
  126. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  127. List<String> directories;
  128. directories.push_back(dir_access->get_current_dir());
  129. while (!directories.is_empty()) {
  130. dir_access->change_dir(directories.back()->get());
  131. directories.pop_back();
  132. dir_access->list_dir_begin();
  133. String filename = dir_access->get_next();
  134. while (!filename.is_empty()) {
  135. if (filename == "." || filename == "..") {
  136. filename = dir_access->get_next();
  137. continue;
  138. }
  139. if (dir_access->dir_exists(filename)) {
  140. directories.push_back(dir_access->get_current_dir().path_join(filename));
  141. } else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) {
  142. suggestions.push_back(quoted(dir_access->get_current_dir().path_join(filename)));
  143. }
  144. filename = dir_access->get_next();
  145. }
  146. }
  147. } break;
  148. case CompletionKind::SHADER_PARAMS: {
  149. print_verbose("Shader uniforms completion for C# is not implemented yet.");
  150. } break;
  151. case CompletionKind::SIGNALS: {
  152. Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
  153. List<MethodInfo> signals;
  154. script->get_script_signal_list(&signals);
  155. StringName native = script->get_instance_base_type();
  156. if (native != StringName()) {
  157. ClassDB::get_signal_list(native, &signals, /* p_no_inheritance: */ false);
  158. }
  159. for (const MethodInfo &E : signals) {
  160. const String &signal = E.name;
  161. suggestions.push_back(quoted(signal));
  162. }
  163. } break;
  164. case CompletionKind::THEME_COLORS: {
  165. Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
  166. Node *base = _try_find_owner_node_in_tree(script);
  167. if (base && Object::cast_to<Control>(base)) {
  168. List<StringName> sn;
  169. ThemeDB::get_singleton()->get_default_theme()->get_color_list(base->get_class(), &sn);
  170. for (const StringName &E : sn) {
  171. suggestions.push_back(quoted(E));
  172. }
  173. }
  174. } break;
  175. case CompletionKind::THEME_CONSTANTS: {
  176. Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
  177. Node *base = _try_find_owner_node_in_tree(script);
  178. if (base && Object::cast_to<Control>(base)) {
  179. List<StringName> sn;
  180. ThemeDB::get_singleton()->get_default_theme()->get_constant_list(base->get_class(), &sn);
  181. for (const StringName &E : sn) {
  182. suggestions.push_back(quoted(E));
  183. }
  184. }
  185. } break;
  186. case CompletionKind::THEME_FONTS: {
  187. Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
  188. Node *base = _try_find_owner_node_in_tree(script);
  189. if (base && Object::cast_to<Control>(base)) {
  190. List<StringName> sn;
  191. ThemeDB::get_singleton()->get_default_theme()->get_font_list(base->get_class(), &sn);
  192. for (const StringName &E : sn) {
  193. suggestions.push_back(quoted(E));
  194. }
  195. }
  196. } break;
  197. case CompletionKind::THEME_FONT_SIZES: {
  198. Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
  199. Node *base = _try_find_owner_node_in_tree(script);
  200. if (base && Object::cast_to<Control>(base)) {
  201. List<StringName> sn;
  202. ThemeDB::get_singleton()->get_default_theme()->get_font_size_list(base->get_class(), &sn);
  203. for (const StringName &E : sn) {
  204. suggestions.push_back(quoted(E));
  205. }
  206. }
  207. } break;
  208. case CompletionKind::THEME_STYLES: {
  209. Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
  210. Node *base = _try_find_owner_node_in_tree(script);
  211. if (base && Object::cast_to<Control>(base)) {
  212. List<StringName> sn;
  213. ThemeDB::get_singleton()->get_default_theme()->get_stylebox_list(base->get_class(), &sn);
  214. for (const StringName &E : sn) {
  215. suggestions.push_back(quoted(E));
  216. }
  217. }
  218. } break;
  219. default:
  220. ERR_FAIL_V_MSG(suggestions, "Invalid completion kind.");
  221. }
  222. return suggestions;
  223. }
  224. } // namespace gdmono