1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*************************************************************************/
/* code_completion.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "code_completion.h"
#include "core/config/project_settings.h"
#include "editor/editor_file_system.h"
#include "editor/editor_settings.h"
#include "scene/gui/control.h"
#include "scene/main/node.h"
namespace gdmono {
// Almost everything here is taken from functions used by GDScript for code completion, adapted for C#.
_FORCE_INLINE_ String quoted(const String &p_str) {
return "\"" + p_str + "\"";
}
void _add_nodes_suggestions(const Node *p_base, const Node *p_node, PackedStringArray &r_suggestions) {
if (p_node != p_base && !p_node->get_owner()) {
return;
}
String path_relative_to_orig = p_base->get_path_to(p_node);
r_suggestions.push_back(quoted(path_relative_to_orig));
for (int i = 0; i < p_node->get_child_count(); i++) {
_add_nodes_suggestions(p_base, p_node->get_child(i), r_suggestions);
}
}
Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
if (p_current->get_owner() != p_base && p_base != p_current) {
return nullptr;
}
Ref<Script> c = p_current->get_script();
if (c == p_script) {
return p_current;
}
for (int i = 0; i < p_current->get_child_count(); i++) {
Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
if (found) {
return found;
}
}
return nullptr;
}
void _get_directory_contents(EditorFileSystemDirectory *p_dir, PackedStringArray &r_suggestions) {
for (int i = 0; i < p_dir->get_file_count(); i++) {
r_suggestions.push_back(quoted(p_dir->get_file_path(i)));
}
for (int i = 0; i < p_dir->get_subdir_count(); i++) {
_get_directory_contents(p_dir->get_subdir(i), r_suggestions);
}
}
Node *_try_find_owner_node_in_tree(const Ref<Script> p_script) {
SceneTree *tree = SceneTree::get_singleton();
if (!tree) {
return nullptr;
}
Node *base = tree->get_edited_scene_root();
if (base) {
base = _find_node_for_script(base, base, p_script);
}
return base;
}
PackedStringArray get_code_completion(CompletionKind p_kind, const String &p_script_file) {
PackedStringArray suggestions;
switch (p_kind) {
case CompletionKind::INPUT_ACTIONS: {
List<PropertyInfo> project_props;
ProjectSettings::get_singleton()->get_property_list(&project_props);
for (const PropertyInfo &prop : project_props) {
if (!prop.name.begins_with("input/")) {
continue;
}
String name = prop.name.substr(prop.name.find("/") + 1, prop.name.length());
suggestions.push_back(quoted(name));
}
} break;
case CompletionKind::NODE_PATHS: {
{
// AutoLoads
Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
const ProjectSettings::AutoloadInfo &info = E->value();
suggestions.push_back(quoted("/root/" + String(info.name)));
}
}
{
// Current edited scene tree
Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
Node *base = _try_find_owner_node_in_tree(script);
if (base) {
_add_nodes_suggestions(base, base, suggestions);
}
}
} break;
case CompletionKind::RESOURCE_PATHS: {
if (bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), suggestions);
}
} break;
case CompletionKind::SCENE_PATHS: {
DirAccessRef dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
List<String> directories;
directories.push_back(dir_access->get_current_dir());
while (!directories.is_empty()) {
dir_access->change_dir(directories.back()->get());
directories.pop_back();
dir_access->list_dir_begin();
String filename = dir_access->get_next();
while (filename != "") {
if (filename == "." || filename == "..") {
filename = dir_access->get_next();
continue;
}
if (dir_access->dir_exists(filename)) {
directories.push_back(dir_access->get_current_dir().plus_file(filename));
} else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) {
suggestions.push_back(quoted(dir_access->get_current_dir().plus_file(filename)));
}
filename = dir_access->get_next();
}
}
} break;
case CompletionKind::SHADER_PARAMS: {
print_verbose("Shared params completion for C# not implemented.");
} break;
case CompletionKind::SIGNALS: {
Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
List<MethodInfo> signals;
script->get_script_signal_list(&signals);
StringName native = script->get_instance_base_type();
if (native != StringName()) {
ClassDB::get_signal_list(native, &signals, /* p_no_inheritance: */ false);
}
for (const MethodInfo &E : signals) {
const String &signal = E.name;
suggestions.push_back(quoted(signal));
}
} break;
case CompletionKind::THEME_COLORS: {
Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
Node *base = _try_find_owner_node_in_tree(script);
if (base && Object::cast_to<Control>(base)) {
List<StringName> sn;
Theme::get_default()->get_color_list(base->get_class(), &sn);
for (const StringName &E : sn) {
suggestions.push_back(quoted(E));
}
}
} break;
case CompletionKind::THEME_CONSTANTS: {
Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
Node *base = _try_find_owner_node_in_tree(script);
if (base && Object::cast_to<Control>(base)) {
List<StringName> sn;
Theme::get_default()->get_constant_list(base->get_class(), &sn);
for (const StringName &E : sn) {
suggestions.push_back(quoted(E));
}
}
} break;
case CompletionKind::THEME_FONTS: {
Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
Node *base = _try_find_owner_node_in_tree(script);
if (base && Object::cast_to<Control>(base)) {
List<StringName> sn;
Theme::get_default()->get_font_list(base->get_class(), &sn);
for (const StringName &E : sn) {
suggestions.push_back(quoted(E));
}
}
} break;
case CompletionKind::THEME_FONT_SIZES: {
Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
Node *base = _try_find_owner_node_in_tree(script);
if (base && Object::cast_to<Control>(base)) {
List<StringName> sn;
Theme::get_default()->get_font_size_list(base->get_class(), &sn);
for (const StringName &E : sn) {
suggestions.push_back(quoted(E));
}
}
} break;
case CompletionKind::THEME_STYLES: {
Ref<Script> script = ResourceLoader::load(p_script_file.simplify_path());
Node *base = _try_find_owner_node_in_tree(script);
if (base && Object::cast_to<Control>(base)) {
List<StringName> sn;
Theme::get_default()->get_stylebox_list(base->get_class(), &sn);
for (const StringName &E : sn) {
suggestions.push_back(quoted(E));
}
}
} break;
default:
ERR_FAIL_V_MSG(suggestions, "Invalid completion kind.");
}
return suggestions;
}
} // namespace gdmono
|