-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDetector.gd
306 lines (208 loc) · 8.42 KB
/
Detector.gd
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
class_name Detector
extends Object
##
## This file contains GDScript detection functions
## Developed by Johannes Witt and Hugo Locurcio
## Placed into the Public Domain
##
### Detectors ###
# Detectors are only meant to return a bool, #
# depending on the detected content. #
# Detectors may be used on complete lines #
## Returns true if string begins with as
static func is_as(string: String) -> bool:
return string.begins_with("as ")
## Returns true if string begins with is
static func is_is(string: String) -> bool:
return string.begins_with("is ")
static func is_in(string: String) -> bool:
return string.find(" in ") != -1
## Returns true if string is a built-in class
static func is_builtin(string: String) -> bool:
return string.substr(0, string.find("(")).strip_edges() in Utility.BUILTIN_CLASSES
## Returns true if string begins with $
static func is_getnode(string: String) -> bool:
return string.begins_with("$")
## Returns true on pass
static func is_pass(string: String) -> bool:
return string.begins_with("pass")
static func is_negation(string) -> bool:
return string.begins_with("not") || string.begins_with("!")
## Returns true if string begins with (
static func is_group(string: String) -> bool:
return string.begins_with("(")
static func is_nodepath(string: String) -> bool:
return string.begins_with("@")
## Returns true if complete line is a comment
## # Comment
static func is_comment(string: String) -> bool:
return string.begins_with("#")
## Returns true if line contains a comment
## code() # Comment
static func has_comment(string: String) -> bool:
return string.count("#") > 0
## Returns true if line is a declaration
## var x
static func is_declaration(string: String) -> bool:
return string.begins_with("var")
## Returns true if line is a const delcaration
## const MY_CONST
static func is_const_declaration(string: String) -> bool:
return string.begins_with("const")
## Returns true if line is a while loop
static func is_while(string: String) -> bool:
return string.begins_with("while")
## Returns true if line is a for or foreach loop
static func is_for(string: String) -> bool:
return string.begins_with("for")
## Returns true if line is for loop and not has range keyword
static func is_foreach(string: String) -> bool:
return string.find("range(") == -1
## Returns true if line is if
## if x:
static func is_if(string: String) -> bool:
return string.begins_with("if")
## Returns true if line is elif
## elif y:
static func is_elif(string: String) -> bool:
return string.begins_with("elif")
## Returns true if line is else
## else:
static func is_else(string: String) -> bool:
return string.begins_with("else:")
## Returns true if line is initialization
## var x = 10
static func is_initialization(string: String) -> bool:
return (is_declaration(string) || is_const_declaration(string)) && string.count("=") > 0
## Returns true if variable declared in line is private
## var _private
static func is_private_var(string: String) -> bool:
return is_private(Utility.get_var_name_from_d(string, is_const_declaration(string)))
## Returns true, if string starts with underscore
static func is_private(string: String) -> bool:
return string.begins_with("_")
## Returns true, if a variable declaration is typed
## var x : Node
## var y := ""
static func is_typed_declaration(string: String) -> bool:
return is_declaration(string) && Utility.find_not_in_string(string, ":") > 0
## Returns true if string starts with func
static func is_function_declaration(string: String) -> bool:
return string.begins_with("func") || string.begins_with("static")
## Returns true if function does not start with an underscore
static func is_public_function(string: String) -> bool:
return is_function_declaration(string) && !string.substr(5).begins_with("_")
## Returns true if functions starts with an underscore
static func is_private_function(string: String) -> bool:
return is_function_declaration(string) && string.substr(5).begins_with("_")
## Returns true if function is static func
static func is_static_function(string: String):
return is_function_declaration(string) && string.begins_with("static")
## Returns true if function is a virtual function in Godot
## like _ready, _process, etc.
static func is_overriding_virtual_function(string: String) -> bool:
return is_private_function(string) && string.substr(5, string.find("(") - 5) in Utility.VIRTUAL_FUNCTIONS
## Returns true if string is a virtual function name
static func is_virtual(string: String) -> bool:
return is_private(string) && string in Utility.VIRTUAL_FUNCTIONS
## Returns true if string has an opening brace "("
static func is_function(string: String) -> bool:
return string.count("(") > 0
## Returns true if string begins with extends, class_name, or tool
static func is_file_scope(string: String) -> bool:
return (string.begins_with("extends")
|| string.begins_with("class_name")
|| string.begins_with("tool"))
## Returns true, if string is a string
## "..." -> true
static func is_string(string: String) -> bool:
return string[0] == '"' && string[string.length() - 1] == '"'
## Returns true if next method is .new(
static func is_constructor(string: String) -> bool:
var new_pos = string.find(".new(")
var brace = string.find("(")
return brace != 0 && new_pos != -1 && new_pos <= string.find(".")
## Returns true if string is a method
## ABC -> false
## ABC( -> true
## ABC. -> false
## ABC(. -> true
## ABC.( -> false
static func is_method(string: String) -> bool:
var brace = string.find("(")
var dot = string.find(".")
return brace != 0 && (brace != -1 && brace < dot) || (brace != -1 && dot == -1)
## Returns true if string is potentially a variable or property
## ABC -> true
## ABC( -> false
## ABC. -> true
## ABC(. -> false
## ABC.( -> true
static func is_var(string: String) -> bool:
var brace = string.find("(")
var dot = string.find(".")
return (dot < brace && dot != -1) || brace == -1
## Returns true if string was declared in local scope
static func is_local_var(string: String, local_vars) -> bool:
for i in local_vars:
if local_vars[i].has(string):
return true
return false
## Returns true if string was declared in global scope
static func is_global_var(string: String, global_vars) -> bool:
return global_vars.has(string)
## Returns true if string contains an assignment
static func is_assignment(string: String) -> bool:
for op in Utility.ASSIGNMENT_OPERATORS:
var pos = Utility.find_not_in_string(string, op)
if pos != -1:
return true
return false
## Returns true if string contains a mathematical expression
static func is_math(string: String) -> bool:
for op in Utility.MATH_OPERATORS:
var pos = Utility.find_not_in_string(string, op)
if pos != -1:
return true
return false
## Returns true if string contains a bitwise operation
static func is_bitwise(string: String) -> bool:
for op in Utility.BITWISE_OPERATORS:
var pos = Utility.find_not_in_string(string, op)
if pos != -1:
return true
return false
## Returns true if string contains an comparison
static func is_comparison(string: String) -> bool:
for op in Utility.COMPARISON_OPERATORS:
var pos = string.find(op)
if pos != -1:
return true
return false
## Returns true if variable is declared local scope
static func var_in_local_vars(string: String, lsv) -> bool:
return !Utility.get_var_in_local_vars(string, lsv).empty()
## Returns true if string is presumably a constant
## THIS_IS_A_CONSTANT
static func is_constant(string: String) -> bool:
return string.casecmp_to(string.to_upper()) == 0
## Returns true if string is "true" or "false"
static func is_bool_exp(string: String) -> bool:
return string in ["true", "false"]
## Return true if string begins with "return"
static func is_return(string: String) -> bool:
return string.begins_with("return")
## Returns true if string begins with [ and has no commas
static func is_subscription(string: String) -> bool:
return string.begins_with("[") and string.find(",") == -1
## Returns true if string begins with [
static func is_array(string: String) -> bool:
return string.begins_with("[")
## Returns true if there is a slash in the string
## We really don't have any chance to securly distingish a property
## string from a normal subscription string
static func is_probably_property_string(string: String) -> bool:
return string.begins_with("[") && string.find("/") != -1
## Returns true if string begins with class
static func _is_class(string: String) -> bool:
return string.begins_with("class")