code_completion.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* code_completion.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 (List<PropertyInfo>::Element *E = project_props.front(); E; E = E->next()) {
  93. const PropertyInfo &prop = E->get();
  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. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  105. for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
  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(EditorSettings::get_singleton()->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. DirAccessRef 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 != "") {
  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().plus_file(filename));
  140. } else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) {
  141. suggestions.push_back(quoted(dir_access->get_current_dir().plus_file(filename)));
  142. }
  143. filename = dir_access->get_next();
  144. }
  145. }
  146. } break;
  147. case CompletionKind::SHADER_PARAMS: {
  148. print_verbose("Shared params completion for C# not implemented.");
  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 (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  159. const String &signal = E->get().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. Theme::get_default()->get_color_list(base->get_class(), &sn);
  169. for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  170. suggestions.push_back(quoted(E->get()));
  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. Theme::get_default()->get_constant_list(base->get_class(), &sn);
  180. for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  181. suggestions.push_back(quoted(E->get()));
  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. Theme::get_default()->get_font_list(base->get_class(), &sn);
  191. for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  192. suggestions.push_back(quoted(E->get()));
  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. Theme::get_default()->get_font_size_list(base->get_class(), &sn);
  202. for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  203. suggestions.push_back(quoted(E->get()));
  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. Theme::get_default()->get_stylebox_list(base->get_class(), &sn);
  213. for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  214. suggestions.push_back(quoted(E->get()));
  215. }
  216. }
  217. } break;
  218. default:
  219. ERR_FAIL_V_MSG(suggestions, "Invalid completion kind.");
  220. }
  221. return suggestions;
  222. }
  223. } // namespace gdmono