code_completion.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/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, PoolStringArray &r_suggestions) {
  42. if (p_node != p_base && !p_node->get_owner())
  43. return;
  44. String path_relative_to_orig = p_base->get_path_to(p_node);
  45. r_suggestions.push_back(quoted(path_relative_to_orig));
  46. for (int i = 0; i < p_node->get_child_count(); i++) {
  47. _add_nodes_suggestions(p_base, p_node->get_child(i), r_suggestions);
  48. }
  49. }
  50. Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  51. if (p_current->get_owner() != p_base && p_base != p_current)
  52. return nullptr;
  53. Ref<Script> c = p_current->get_script();
  54. if (c == p_script)
  55. return p_current;
  56. for (int i = 0; i < p_current->get_child_count(); i++) {
  57. Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
  58. if (found)
  59. return found;
  60. }
  61. return nullptr;
  62. }
  63. void _get_directory_contents(EditorFileSystemDirectory *p_dir, PoolStringArray &r_suggestions) {
  64. for (int i = 0; i < p_dir->get_file_count(); i++) {
  65. r_suggestions.push_back(quoted(p_dir->get_file_path(i)));
  66. }
  67. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  68. _get_directory_contents(p_dir->get_subdir(i), r_suggestions);
  69. }
  70. }
  71. Node *_try_find_owner_node_in_tree(const Ref<Script> p_script) {
  72. SceneTree *tree = SceneTree::get_singleton();
  73. if (!tree)
  74. return nullptr;
  75. Node *base = tree->get_edited_scene_root();
  76. if (base) {
  77. base = _find_node_for_script(base, base, p_script);
  78. }
  79. return base;
  80. }
  81. PoolStringArray get_code_completion(CompletionKind p_kind, const String &p_script_file) {
  82. PoolStringArray suggestions;
  83. switch (p_kind) {
  84. case CompletionKind::INPUT_ACTIONS: {
  85. List<PropertyInfo> project_props;
  86. ProjectSettings::get_singleton()->get_property_list(&project_props);
  87. for (List<PropertyInfo>::Element *E = project_props.front(); E; E = E->next()) {
  88. const PropertyInfo &prop = E->get();
  89. if (!prop.name.begins_with("input/"))
  90. continue;
  91. String name = prop.name.substr(prop.name.find("/") + 1, prop.name.length());
  92. suggestions.push_back(quoted(name));
  93. }
  94. } break;
  95. case CompletionKind::NODE_PATHS: {
  96. {
  97. // AutoLoads
  98. List<PropertyInfo> props;
  99. ProjectSettings::get_singleton()->get_property_list(&props);
  100. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  101. String s = E->get().name;
  102. if (!s.begins_with("autoload/")) {
  103. continue;
  104. }
  105. String name = s.get_slice("/", 1);
  106. suggestions.push_back(quoted("/root/" + 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. DirAccessRef dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  125. List<String> directories;
  126. directories.push_back(dir_access->get_current_dir());
  127. while (!directories.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 != "") {
  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 (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  158. const String &signal = E->get().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 (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  169. suggestions.push_back(quoted(E->get()));
  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 (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  180. suggestions.push_back(quoted(E->get()));
  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 (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  191. suggestions.push_back(quoted(E->get()));
  192. }
  193. }
  194. } break;
  195. case CompletionKind::THEME_STYLES: {
  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_stylebox_list(base->get_class(), &sn);
  201. for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  202. suggestions.push_back(quoted(E->get()));
  203. }
  204. }
  205. } break;
  206. default:
  207. ERR_FAIL_V_MSG(suggestions, "Invalid completion kind.");
  208. }
  209. return suggestions;
  210. }
  211. } // namespace gdmono