code_completion.cpp 9.2 KB

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