code_completion.cpp 9.1 KB

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