@papercraftgames/estragon 0.1.0-beta.28 → 0.1.0-beta.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/addons/estragon/compiler/emitter.gd +72 -1
- package/addons/estragon/compiler/frontend.gd +2 -0
- package/addons/estragon/compiler/model.gd +14 -0
- package/addons/estragon/compiler/render_calls.gd +354 -0
- package/addons/estragon/compiler/render_calls.gd.uid +1 -0
- package/addons/estragon/editor/format_edit.gd +54 -0
- package/addons/estragon/editor/format_edit.gd.uid +1 -0
- package/addons/estragon/editor/formatter.gd +166 -0
- package/addons/estragon/editor/formatter.gd.uid +1 -0
- package/addons/estragon/editor/language_services.gd +43 -0
- package/addons/estragon/editor/plugin.cfg +1 -1
- package/addons/estragon/editor/plugin.gd +7 -0
- package/addons/estragon/runtime/composition/binding.gd +16 -1
- package/addons/estragon/runtime/composition/computation.gd +56 -13
- package/addons/estragon/runtime/composition/preset.gd +2 -1
- package/addons/estragon/runtime/composition/render_cache.gd +36 -0
- package/addons/estragon/runtime/composition/render_cache.gd.uid +1 -0
- package/addons/estragon/runtime/composition/state.gd +158 -1
- package/package.json +1 -1
|
@@ -56,6 +56,9 @@ func _resource(resource: Resource) -> String:
|
|
|
56
56
|
|
|
57
57
|
func emit() -> MappedWriter:
|
|
58
58
|
_parts(_module.parts)
|
|
59
|
+
var metadata := "_GOGO_RENDER_FUNCTIONS_"+_file.name.sha256_text().substr(0,16)
|
|
60
|
+
while _module.identifiers.has(metadata): metadata += "_"
|
|
61
|
+
_writer.append("\nconst "+metadata+" = "+var_to_str({"reactive":_module.render_functions,"ordinary":_module.ordinary_functions}))
|
|
59
62
|
_writer.append("\n")
|
|
60
63
|
var exports := {}
|
|
61
64
|
for binding in _module.presets:
|
|
@@ -137,6 +140,7 @@ func _parts(parts: Array[Model.SyntaxNode]) -> void:
|
|
|
137
140
|
if part is Model.Code: _code(part.span)
|
|
138
141
|
elif part is Model.Bindable: _bindable(part)
|
|
139
142
|
elif part is Model.Layout: _layout(part)
|
|
143
|
+
elif part is Model.RenderCall: _render_call(part)
|
|
140
144
|
elif part is Model.Literal and part.kind=="preset-reference": _writer.append(str(part.value),part.span)
|
|
141
145
|
else: _call(part)
|
|
142
146
|
|
|
@@ -166,6 +170,16 @@ func _call(node: Model.Component) -> void:
|
|
|
166
170
|
factory.root = node
|
|
167
171
|
_factories.append(factory)
|
|
168
172
|
_collect(node, factory)
|
|
173
|
+
var keyed: Model.Attribute
|
|
174
|
+
for attribute in node.attributes:
|
|
175
|
+
if attribute.kind=="key": keyed = attribute
|
|
176
|
+
var key_symbol := "_gogo_mount_key"
|
|
177
|
+
while _module.identifiers.has(key_symbol): key_symbol += "_"
|
|
178
|
+
if keyed!=null:
|
|
179
|
+
_writer.append("preload("+_resource(State)+").in_slot("+_string(_file.name+":"+str(node.span.start.offset))+", ",node.span)
|
|
180
|
+
if keyed.value is Model.GdExpression: _parts(keyed.value.parts)
|
|
181
|
+
else: _writer.append(literal(keyed.value),keyed.value.span)
|
|
182
|
+
_writer.append(", func("+key_symbol+"): return ",node.span)
|
|
169
183
|
var multiline := factory.arguments.size() > 3
|
|
170
184
|
var start := _file.line_start(node.span.start.line)
|
|
171
185
|
var end := start
|
|
@@ -178,11 +192,13 @@ func _call(node: Model.Component) -> void:
|
|
|
178
192
|
if multiline: _writer.append("\n" + base + _module.indentation, argument.expression.span)
|
|
179
193
|
_writer.append("(", argument.expression.span)
|
|
180
194
|
if argument.spread: _writer.append("preload("+_resource(Element)+").snapshot(",argument.expression.span)
|
|
181
|
-
if argument.
|
|
195
|
+
if keyed!=null and argument.expression==keyed.value: _writer.append(key_symbol,argument.expression.span)
|
|
196
|
+
elif argument.binding_path.is_empty(): _parts(argument.expression.parts)
|
|
182
197
|
else: _binding_argument(argument)
|
|
183
198
|
if argument.spread: _writer.append(")",argument.expression.span)
|
|
184
199
|
_writer.append(")", argument.expression.span)
|
|
185
200
|
_writer.append(("\n" + base if multiline else "") + ")", node.span)
|
|
201
|
+
if keyed!=null: _writer.append(")",node.span)
|
|
186
202
|
|
|
187
203
|
func _factory(factory: Factory) -> void:
|
|
188
204
|
_writer.append("\n\n" + _module.indentation + "static func " + factory.name + "(", factory.root.span)
|
|
@@ -326,3 +342,58 @@ func _binding_argument(argument: Argument) -> void:
|
|
|
326
342
|
endpoint += "."+str(name)
|
|
327
343
|
checks.append(endpoint+" != null")
|
|
328
344
|
_writer.append("preload("+_resource(Binding)+").new(self, func("+owner+"): return "+endpoint+" if "+" and ".join(checks)+" else null, "+_string(field)+", func("+source+", "+edit+": bool): "+source+"."+field+" = "+edit+", "+_string(".".join(argument.binding_path))+")",argument.expression.span)
|
|
345
|
+
|
|
346
|
+
func _render_call(node: Model.RenderCall) -> void:
|
|
347
|
+
var cast: String = node.result_type
|
|
348
|
+
if node.statement or cast in ["void","Variant"]: cast = ""
|
|
349
|
+
if not cast.is_empty(): _writer.append("(",node.span)
|
|
350
|
+
var member := not node.receiver.is_empty()
|
|
351
|
+
var base := member and node.receiver.size()==1 and node.receiver[0] is Model.Code and _file.text.substr(node.receiver[0].span.start.index,node.receiver[0].span.end.index-node.receiver[0].span.start.index).strip_edges()=="super"
|
|
352
|
+
_writer.append("preload("+_resource(State)+")."+("base_at" if base else "method_at" if member else "call_at")+"("+_string(_file.name+":"+str(node.span.start.offset))+", ",node.span)
|
|
353
|
+
if base:
|
|
354
|
+
_writer.append("self, "+_string(_output_directory.path_join(_file.generated_path().get_file()))+", "+_string(node.method),node.span)
|
|
355
|
+
elif member:
|
|
356
|
+
_parts(node.receiver)
|
|
357
|
+
_writer.append(", "+_string(node.method),node.span)
|
|
358
|
+
else: _parts(node.callee)
|
|
359
|
+
_writer.append(", [",node.span)
|
|
360
|
+
for index in range(node.arguments.size()):
|
|
361
|
+
if index>0: _writer.append(", ",node.span)
|
|
362
|
+
_parts(node.arguments[index].parts)
|
|
363
|
+
if member:
|
|
364
|
+
var receiver := "_gogo_receiver"
|
|
365
|
+
var args := "_gogo_arguments"
|
|
366
|
+
while _module.identifiers.has(receiver): receiver += "_"
|
|
367
|
+
while _module.identifiers.has(args): args += "_"
|
|
368
|
+
_writer.append("], func("+receiver+", "+args+"): "+("" if node.result_type=="void" or node.statement else "return ")+("super" if base else receiver)+"."+node.method+"(",node.span)
|
|
369
|
+
for index in range(node.arguments.size()):
|
|
370
|
+
if index>0: _writer.append(", ",node.span)
|
|
371
|
+
_writer.append(args+"["+str(index)+"]",node.span)
|
|
372
|
+
_writer.append(")",node.span)
|
|
373
|
+
else: _writer.append("]",node.span)
|
|
374
|
+
# Never run/retain the proof closure: native static checking still sees the
|
|
375
|
+
# authored call and its original argument types before source publication.
|
|
376
|
+
_writer.append(", func(): ",node.span)
|
|
377
|
+
if node.callable_value:
|
|
378
|
+
_parts(node.callee)
|
|
379
|
+
_writer.append(".call(",node.span)
|
|
380
|
+
for index in range(node.arguments.size()):
|
|
381
|
+
if index>0: _writer.append(", ",node.span)
|
|
382
|
+
_parts(node.arguments[index].parts)
|
|
383
|
+
_writer.append(")",node.span)
|
|
384
|
+
else: _parts(node.original)
|
|
385
|
+
if node.reactive_lambda: _writer.append(", true",node.span)
|
|
386
|
+
_writer.append(")",node.span)
|
|
387
|
+
if not cast.is_empty(): _writer.append(" as "+_memo_type(cast)+")",node.span)
|
|
388
|
+
|
|
389
|
+
func _memo_type(type: String) -> String:
|
|
390
|
+
var source := Source.new(type)
|
|
391
|
+
var lexer = preload("lexer.gd").new(source)
|
|
392
|
+
var cursor := 0
|
|
393
|
+
var result := ""
|
|
394
|
+
while cursor<type.length():
|
|
395
|
+
var token = lexer.next_token(cursor)
|
|
396
|
+
if token==null: return type
|
|
397
|
+
cursor = token.span.end.index
|
|
398
|
+
result += _module.import_aliases.get(token.text,token.text) if token.kind=="identifier" else token.text
|
|
399
|
+
return result
|
|
@@ -8,6 +8,7 @@ const Bindable = preload("bindable.gd")
|
|
|
8
8
|
const Validator = preload("validator.gd")
|
|
9
9
|
const Composition = preload("composition.gd")
|
|
10
10
|
const Layouts = preload("layouts.gd")
|
|
11
|
+
const RenderCalls = preload("render_calls.gd")
|
|
11
12
|
const ImportAliases = preload("import_aliases.gd")
|
|
12
13
|
|
|
13
14
|
static func analyze(text: String, source_name: String = "<source>", output_path: String = "", project_sources = null) -> Model.Result:
|
|
@@ -20,6 +21,7 @@ static func analyze(text: String, source_name: String = "<source>", output_path:
|
|
|
20
21
|
if module != null and file.diagnostics.is_empty(): Composition.new(file).apply(module)
|
|
21
22
|
if module != null and file.diagnostics.is_empty(): Validator.new(file).validate(module)
|
|
22
23
|
if module != null and file.diagnostics.is_empty(): ImportAliases.new(file).apply(module)
|
|
24
|
+
if module != null and file.diagnostics.is_empty(): RenderCalls.new(file).apply(module)
|
|
23
25
|
result.tokens = parser.lexer.tokens
|
|
24
26
|
result.diagnostics = file.diagnostics
|
|
25
27
|
result.module = module if result.diagnostics.is_empty() else null
|
|
@@ -97,7 +97,21 @@ class Preset extends RefCounted:
|
|
|
97
97
|
var mutable := false
|
|
98
98
|
var exported := false
|
|
99
99
|
|
|
100
|
+
class RenderCall extends SyntaxNode:
|
|
101
|
+
var result_type: String = ""
|
|
102
|
+
var statement := false
|
|
103
|
+
var reactive_lambda := false
|
|
104
|
+
var callable_value := false
|
|
105
|
+
var callee: Array[SyntaxNode] = []
|
|
106
|
+
var receiver: Array[SyntaxNode] = []
|
|
107
|
+
var method: String = ""
|
|
108
|
+
var arguments: Array = []
|
|
109
|
+
var original: Array[SyntaxNode] = []
|
|
110
|
+
func _init() -> void: kind = "render-call"
|
|
111
|
+
|
|
100
112
|
class Module extends RefCounted:
|
|
113
|
+
var render_functions: Array = []
|
|
114
|
+
var ordinary_functions: Array = []
|
|
101
115
|
var presets: Array = []
|
|
102
116
|
var import_aliases: Dictionary = {}
|
|
103
117
|
var identifier_imports: Array = []
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
@tool
|
|
2
|
+
extends RefCounted
|
|
3
|
+
## Token/span call lowering. GDScript stays native; authored GGX marks functions.
|
|
4
|
+
const Model = preload("model.gd")
|
|
5
|
+
const Parser = preload("parser.gd")
|
|
6
|
+
const Layouts = preload("layouts.gd")
|
|
7
|
+
const Resolver = preload("native/resolver.gd")
|
|
8
|
+
const Source = preload("source.gd")
|
|
9
|
+
const Lexer = preload("lexer.gd")
|
|
10
|
+
var file
|
|
11
|
+
var module
|
|
12
|
+
var functions: Array = []
|
|
13
|
+
var returns: Dictionary = {}
|
|
14
|
+
var resolver
|
|
15
|
+
var declarations: Array = []
|
|
16
|
+
var methods: Dictionary = {}
|
|
17
|
+
var native_methods: Dictionary = {}
|
|
18
|
+
var builtin_types: Dictionary = {}
|
|
19
|
+
var lambda_bindings: Array = []
|
|
20
|
+
var lexical
|
|
21
|
+
const VARIANT_TYPES := ["Nil","bool","int","float","String","StringName","Vector2","Vector2i","Rect2","Rect2i","Vector3","Vector3i","Transform2D","Vector4","Vector4i","Plane","Quaternion","AABB","Basis","Transform3D","Projection","Color","NodePath","RID","Object","Callable","Signal","Dictionary","Array","PackedByteArray","PackedInt32Array","PackedInt64Array","PackedFloat32Array","PackedFloat64Array","PackedStringArray","PackedVector2Array","PackedVector3Array","PackedColorArray"]
|
|
22
|
+
const INTRINSICS := ["preload","load","str","int","float","bool","String","StringName","Vector2","Vector2i","Vector3","Vector3i","Vector4","Vector4i","Color","Rect2","Rect2i","Transform2D","Dictionary","Array","Callable","Signal","NodePath","RID","assert","print","prints","print_debug","push_error","push_warning","is_instance_valid","is_same","typeof","weakref","range","min","max","minf","maxf","mini","maxi","abs","absf","absi","clamp","clampf","clampi","snapped","lerp","floor","ceil","round","sin","cos","sqrt"]
|
|
23
|
+
# Native traversal callbacks are syntax-transparent, not opaque application
|
|
24
|
+
# helper bodies. Their GGX/lambda children run in the current render context.
|
|
25
|
+
const TRAVERSAL := ["map","filter","reduce","any","all","sort_custom"]
|
|
26
|
+
func _init(source) -> void: file = source
|
|
27
|
+
func apply(value) -> void:
|
|
28
|
+
module = value
|
|
29
|
+
for name in VARIANT_TYPES: builtin_types[name] = true
|
|
30
|
+
resolver = Resolver.new(file)
|
|
31
|
+
resolver.scan(module)
|
|
32
|
+
var tokens := _tokens(module.parts)
|
|
33
|
+
returns = _returns(tokens)
|
|
34
|
+
returns.merge(_inherited_returns(file,tokens,{}),false)
|
|
35
|
+
lexical = Layouts.new(file)
|
|
36
|
+
lexical.module = module
|
|
37
|
+
lexical._index()
|
|
38
|
+
_lambdas(tokens)
|
|
39
|
+
_declarations(tokens)
|
|
40
|
+
for index in range(tokens.size()-1):
|
|
41
|
+
if tokens[index].text=="func": methods[tokens[index+1].text] = true
|
|
42
|
+
for native in ["Node2D","Node3D"]:
|
|
43
|
+
for method in ClassDB.class_get_method_list(native): native_methods[str(method.name)] = true
|
|
44
|
+
for part in module.parts:
|
|
45
|
+
if part is Model.Bindable and resolver.aliases.has(part.type_name): declarations.append({"name":part.name,"type":part.type_name,"span":part.declaration_span})
|
|
46
|
+
for index in range(tokens.size()-2):
|
|
47
|
+
if tokens[index].text!="func" or tokens[index+1].kind!="identifier": continue
|
|
48
|
+
var prefix: String = file.text.substr(file.line_start(tokens[index].span.start.line),tokens[index].span.start.index-file.line_start(tokens[index].span.start.line))
|
|
49
|
+
if prefix.strip_edges() not in ["","static"]: continue
|
|
50
|
+
var opening := index+2
|
|
51
|
+
if tokens[opening].text!="(": continue
|
|
52
|
+
var closing := _close(tokens,opening)
|
|
53
|
+
if closing<0: continue
|
|
54
|
+
var colon := closing+1
|
|
55
|
+
while colon<tokens.size() and tokens[colon].text!=":": colon += 1
|
|
56
|
+
if colon>=tokens.size(): continue
|
|
57
|
+
var end: int = file.text.length()
|
|
58
|
+
for following in range(colon+1,tokens.size()):
|
|
59
|
+
if tokens[following].span.start.column==1:
|
|
60
|
+
end = tokens[following].span.start.index
|
|
61
|
+
break
|
|
62
|
+
var begin: int = tokens[colon].span.end.index
|
|
63
|
+
var reactive := false
|
|
64
|
+
for token in tokens:
|
|
65
|
+
if token.kind=="ggx" and token.span.start.index>=begin and token.span.end.index<=end: reactive = true
|
|
66
|
+
if not reactive and tokens[index+1].text!="build_view": module.ordinary_functions.append(tokens[index+1].text)
|
|
67
|
+
if reactive or tokens[index+1].text=="build_view":
|
|
68
|
+
functions.append({"start":begin,"end":end})
|
|
69
|
+
module.render_functions.append(tokens[index+1].text)
|
|
70
|
+
for identifier in module.identifiers:
|
|
71
|
+
if identifier.begins_with("_GOGO_RENDER_FUNCTIONS_"):
|
|
72
|
+
file.fail("Reserved compiler render metadata prefix _GOGO_RENDER_FUNCTIONS_",0)
|
|
73
|
+
return
|
|
74
|
+
if module.identifiers.has("_GOGO_RENDER_FUNCTIONS") or module.identifiers.has("_GOGO_ORDINARY_FUNCTIONS"):
|
|
75
|
+
file.fail("Reserved compiler render metadata name _GOGO_RENDER_FUNCTIONS",0)
|
|
76
|
+
return
|
|
77
|
+
module.parts.assign(_rewrite(module.parts))
|
|
78
|
+
func _tokens(parts: Array, source = null) -> Array:
|
|
79
|
+
var result: Array = []
|
|
80
|
+
var lexer := Lexer.new(file if source==null else source)
|
|
81
|
+
for part in parts:
|
|
82
|
+
if part is Model.Component:
|
|
83
|
+
result.append({"kind":"ggx","text":"<ggx>","span":part.span})
|
|
84
|
+
elif part is Model.Code:
|
|
85
|
+
var cursor: int = part.span.start.index
|
|
86
|
+
while cursor<part.span.end.index:
|
|
87
|
+
var token = lexer.next_token(cursor)
|
|
88
|
+
if token==null: break
|
|
89
|
+
cursor = token.span.end.index
|
|
90
|
+
if token.kind not in ["whitespace","newline","comment","continuation"]: result.append(token)
|
|
91
|
+
return result
|
|
92
|
+
func _close(tokens: Array, start: int) -> int:
|
|
93
|
+
var stack: Array = []
|
|
94
|
+
for index in range(start,tokens.size()):
|
|
95
|
+
var text: String = tokens[index].text
|
|
96
|
+
if text in ["(","[","{"]: stack.append(text)
|
|
97
|
+
elif text in [")","]","}"]:
|
|
98
|
+
if stack.is_empty(): return -1
|
|
99
|
+
stack.pop_back()
|
|
100
|
+
if stack.is_empty(): return index
|
|
101
|
+
return -1
|
|
102
|
+
func _reactive(offset: int) -> bool:
|
|
103
|
+
for function in functions:
|
|
104
|
+
if offset>=function.start and offset<function.end: return true
|
|
105
|
+
return false
|
|
106
|
+
func _rewrite(parts: Array, force: bool = false, value_context: bool = false) -> Array:
|
|
107
|
+
# Descend into islands first; enclosing calls treat each entire island as an
|
|
108
|
+
# operand and never lex attribute text as GDScript.
|
|
109
|
+
for part in parts:
|
|
110
|
+
if part is Model.Layout:
|
|
111
|
+
for binding in part.bindings:
|
|
112
|
+
for group in binding.groups:
|
|
113
|
+
for attr in group.attributes:
|
|
114
|
+
if attr.value is Model.GdExpression: attr.value.parts.assign(_rewrite(attr.value.parts,true,true))
|
|
115
|
+
elif part is Model.Component:
|
|
116
|
+
for attr in part.attributes:
|
|
117
|
+
if attr.value is Model.GdExpression: attr.value.parts.assign(_rewrite(attr.value.parts,force,true))
|
|
118
|
+
for child in part.children:
|
|
119
|
+
if child is Model.GdExpression: child.parts.assign(_rewrite(child.parts,force,true))
|
|
120
|
+
elif child is Model.Component: _rewrite([child],force)
|
|
121
|
+
for child in part.text_body:
|
|
122
|
+
if child is Model.GdExpression: child.parts.assign(_rewrite(child.parts,false,true))
|
|
123
|
+
elif part is Model.GdExpression: part.parts.assign(_rewrite(part.parts,force,true))
|
|
124
|
+
var tokens := _tokens(parts)
|
|
125
|
+
var calls: Array = []
|
|
126
|
+
for index in range(1,tokens.size()):
|
|
127
|
+
if tokens[index].text!="(" or tokens[index-1].kind!="identifier" or not force and not _reactive(tokens[index].span.start.index): continue
|
|
128
|
+
var name: String = tokens[index-1].text
|
|
129
|
+
if name in Lexer.KEYWORDS or name in INTRINSICS or ClassDB.class_exists(name) or builtin_types.has(name): continue
|
|
130
|
+
if index>1 and tokens[index-2].text=="func": continue
|
|
131
|
+
if name=="new": continue
|
|
132
|
+
var scripted := false
|
|
133
|
+
if index>2 and tokens[index-2].text==".":
|
|
134
|
+
var receiver: String = tokens[index-3].text
|
|
135
|
+
scripted = resolver.aliases.has(receiver) or not _receiver_type(receiver,tokens[index].span.start.index).is_empty()
|
|
136
|
+
if name in TRAVERSAL and not scripted and not methods.has(name): continue
|
|
137
|
+
if native_methods.has(name) and not scripted and not methods.has(name) and not (name in ["call","callv"] and index>1 and tokens[index-2].text=="."): continue
|
|
138
|
+
var end := _close(tokens,index)
|
|
139
|
+
if end<0: continue
|
|
140
|
+
var begin := index-1
|
|
141
|
+
# Ordinary qualified method chains, including parenthesized/subscripted
|
|
142
|
+
# receivers, stay actual native method references in emitted code.
|
|
143
|
+
while begin>1 and tokens[begin-1].text==".":
|
|
144
|
+
begin -= 2
|
|
145
|
+
if tokens[begin].text in [")","]","}"]:
|
|
146
|
+
var depth := 1
|
|
147
|
+
var close: String = tokens[begin].text
|
|
148
|
+
while begin>0 and depth>0:
|
|
149
|
+
begin -= 1
|
|
150
|
+
if tokens[begin].text==close: depth += 1
|
|
151
|
+
elif tokens[begin].text=={")":"(","]":"[","}":"{"}[close]: depth -= 1
|
|
152
|
+
if begin>0 and tokens[begin-1].kind=="identifier": begin -= 1
|
|
153
|
+
var args: Array = []
|
|
154
|
+
var cursor := index+1
|
|
155
|
+
var nested := 0
|
|
156
|
+
for offset in range(index+1,end):
|
|
157
|
+
var text: String = tokens[offset].text
|
|
158
|
+
if text in ["(","[","{"]: nested += 1
|
|
159
|
+
elif text in [")","]","}"]: nested -= 1
|
|
160
|
+
elif text=="," and nested==0:
|
|
161
|
+
args.append(file.span(tokens[cursor].span.start.index,tokens[offset].span.start.index))
|
|
162
|
+
cursor = offset+1
|
|
163
|
+
if cursor<end: args.append(file.span(tokens[cursor].span.start.index,tokens[end].span.start.index))
|
|
164
|
+
calls.append({"span":file.span(tokens[begin].span.start.index,tokens[end].span.end.index),"callee":file.span(tokens[begin].span.start.index,tokens[index-1].span.end.index),"arguments":args,"result_type":_type(tokens,begin,index-1),"statement":_statement(tokens,begin,end,value_context),"receiver":file.span(tokens[begin].span.start.index,tokens[index-2].span.start.index) if index>1 and tokens[index-2].text=="." else null,"method":name,"callable_value":begin==index-1 and not methods.has(name) and lexical.symbols.any(func(symbol): return symbol.name==name),"reactive_lambda":_lambda_at(tokens[begin].text,tokens[index].span.start.index) if begin==index-3 and name in ["call","callv"] else _lambda_at(name,tokens[index].span.start.index) if begin==index-1 or begin==index-3 and tokens[begin].text=="self" else false})
|
|
165
|
+
calls.sort_custom(func(a,b): return a.span.start.index<b.span.start.index or a.span.start.index==b.span.start.index and a.span.end.index>b.span.end.index)
|
|
166
|
+
if parts.is_empty(): return parts
|
|
167
|
+
return _range(parts,parts[0].span.start.index,parts[-1].span.end.index,calls)
|
|
168
|
+
func _range(parts: Array, start: int, end: int, calls: Array) -> Array:
|
|
169
|
+
var result: Array = []
|
|
170
|
+
var cursor := start
|
|
171
|
+
for item in calls:
|
|
172
|
+
if item.span.start.index<cursor or item.span.end.index>end: continue
|
|
173
|
+
result.append_array(_slice(parts,cursor,item.span.start.index))
|
|
174
|
+
var call := Model.RenderCall.new()
|
|
175
|
+
call.span = item.span
|
|
176
|
+
call.result_type = item.result_type
|
|
177
|
+
call.statement = item.statement
|
|
178
|
+
call.reactive_lambda = item.reactive_lambda
|
|
179
|
+
call.callable_value = item.callable_value
|
|
180
|
+
if item.receiver!=null:
|
|
181
|
+
call.receiver.assign(_range(parts,item.receiver.start.index,item.receiver.end.index,calls))
|
|
182
|
+
call.method = item.method
|
|
183
|
+
call.callee.assign(_slice(parts,item.callee.start.index,item.callee.end.index))
|
|
184
|
+
call.original.assign(_slice(parts,item.span.start.index,item.span.end.index))
|
|
185
|
+
for argument in item.arguments:
|
|
186
|
+
var expression := Model.GdExpression.new()
|
|
187
|
+
expression.span = argument
|
|
188
|
+
expression.parts.assign(_range(parts,argument.start.index,argument.end.index,calls))
|
|
189
|
+
call.arguments.append(expression)
|
|
190
|
+
result.append(call)
|
|
191
|
+
cursor = item.span.end.index
|
|
192
|
+
result.append_array(_slice(parts,cursor,end))
|
|
193
|
+
return result
|
|
194
|
+
func _slice(parts: Array, start: int, end: int) -> Array:
|
|
195
|
+
var result: Array = []
|
|
196
|
+
for part in parts:
|
|
197
|
+
if part.span.end.index<=start or part.span.start.index>=end: continue
|
|
198
|
+
if part is Model.Code:
|
|
199
|
+
var code := Model.Code.new()
|
|
200
|
+
code.span = file.span(maxi(start,part.span.start.index),mini(end,part.span.end.index))
|
|
201
|
+
result.append(code)
|
|
202
|
+
else: result.append(part)
|
|
203
|
+
return result
|
|
204
|
+
|
|
205
|
+
func _returns(tokens: Array) -> Dictionary:
|
|
206
|
+
var result := {}
|
|
207
|
+
for index in range(tokens.size()-2):
|
|
208
|
+
if tokens[index].text!="func" or tokens[index+1].kind!="identifier" or tokens[index+2].text!="(": continue
|
|
209
|
+
var end := _close(tokens,index+2)
|
|
210
|
+
if end<0 or end+2>=tokens.size() or tokens[end+1].text!="->": continue
|
|
211
|
+
var type := ""
|
|
212
|
+
var cursor := end+2
|
|
213
|
+
while cursor<tokens.size() and tokens[cursor].text!=":":
|
|
214
|
+
type += tokens[cursor].text
|
|
215
|
+
cursor += 1
|
|
216
|
+
result[tokens[index+1].text] = type
|
|
217
|
+
return result
|
|
218
|
+
func _type(tokens: Array, begin: int, end: int) -> String:
|
|
219
|
+
if begin==end or end-begin==2 and tokens[begin].text in ["self","super"]: return returns.get(tokens[end].text,"")
|
|
220
|
+
var receiver := begin+2 if tokens[begin].text=="self" else begin
|
|
221
|
+
if end-receiver!=2: return ""
|
|
222
|
+
var alias: String = tokens[receiver].text
|
|
223
|
+
if not resolver.aliases.has(alias): alias = _receiver_type(alias,tokens[end].span.start.index)
|
|
224
|
+
if not resolver.aliases.has(alias): return ""
|
|
225
|
+
var path: String = resolver.aliases[alias]
|
|
226
|
+
var authored: String = path.get_base_dir().get_base_dir().path_join(path.get_file().get_basename()+".gogo")
|
|
227
|
+
if path.get_base_dir().get_file()=="generated" and FileAccess.file_exists(authored): path = authored
|
|
228
|
+
if not FileAccess.file_exists(path): return ""
|
|
229
|
+
var project = resolver.scope()
|
|
230
|
+
var key: String = project.authored(path)
|
|
231
|
+
var text: String = project.read(key) if project.sources.has(key) else FileAccess.get_file_as_string(path)
|
|
232
|
+
var source := Source.new(text,path)
|
|
233
|
+
var imported_module = Parser.new(source).parse()
|
|
234
|
+
if imported_module==null: return ""
|
|
235
|
+
var type: String = _returns(_tokens(imported_module.parts,source)).get(tokens[end].text,"")
|
|
236
|
+
if type.is_empty(): return ""
|
|
237
|
+
# Imported types live on the source Script alias, never an instance variable.
|
|
238
|
+
var lexer := Lexer.new(Source.new(type))
|
|
239
|
+
var cursor := 0
|
|
240
|
+
var result := ""
|
|
241
|
+
var qualified := false
|
|
242
|
+
var variants: Array = ["void","Variant"]
|
|
243
|
+
variants.append_array(VARIANT_TYPES)
|
|
244
|
+
var globals: Array = []
|
|
245
|
+
for item in ProjectSettings.get_global_class_list(): globals.append(str(item["class"]))
|
|
246
|
+
while cursor<type.length():
|
|
247
|
+
var token = lexer.next_token(cursor)
|
|
248
|
+
if token==null: return ""
|
|
249
|
+
cursor = token.span.end.index
|
|
250
|
+
var name: String = token.text
|
|
251
|
+
if token.kind=="identifier" and not qualified and name not in variants and name not in globals and not ClassDB.class_exists(name): result += alias+"."
|
|
252
|
+
result += name
|
|
253
|
+
qualified = name=="."
|
|
254
|
+
return result
|
|
255
|
+
func _declarations(tokens: Array) -> void:
|
|
256
|
+
for index in range(tokens.size()-3):
|
|
257
|
+
var token = tokens[index]
|
|
258
|
+
if token.text in ["var","const"] and tokens[index+1].kind=="identifier":
|
|
259
|
+
var name: String = tokens[index+1].text
|
|
260
|
+
var type := ""
|
|
261
|
+
if tokens[index+2].text==":": type = tokens[index+3].text
|
|
262
|
+
elif tokens[index+2].text in ["=",":="]: type = tokens[index+3].text
|
|
263
|
+
if resolver.aliases.has(type): declarations.append({"name":name,"type":type,"span":token.span})
|
|
264
|
+
elif token.text==":" and index>0 and tokens[index-1].kind=="identifier" and resolver.aliases.has(tokens[index+1].text):
|
|
265
|
+
declarations.append({"name":tokens[index-1].text,"type":tokens[index+1].text,"span":tokens[index-1].span})
|
|
266
|
+
func _receiver_type(name: String, offset: int) -> String:
|
|
267
|
+
var result := ""
|
|
268
|
+
for declaration in declarations:
|
|
269
|
+
if declaration.name!=name or declaration.span.start.index>=offset: continue
|
|
270
|
+
# A declaration leaves scope at the next same/lower-indented statement.
|
|
271
|
+
var indentation: int = declaration.span.start.column-1
|
|
272
|
+
var visible := true
|
|
273
|
+
if indentation>0:
|
|
274
|
+
for line in range(declaration.span.start.line+1,file.position(offset).line+1):
|
|
275
|
+
var start: int = file.line_start(line)
|
|
276
|
+
var end: int = file.line_start(line+1)
|
|
277
|
+
var text: String = file.text.substr(start,end-start)
|
|
278
|
+
if text.strip_edges().is_empty() or text.strip_edges().begins_with("#"): continue
|
|
279
|
+
var width: int = text.length()-text.strip_edges(true,false).length()
|
|
280
|
+
if width<indentation: visible = false; break
|
|
281
|
+
if visible: result = declaration.type
|
|
282
|
+
return result
|
|
283
|
+
|
|
284
|
+
func _lambdas(tokens: Array) -> void:
|
|
285
|
+
for index in range(2,tokens.size()):
|
|
286
|
+
if tokens[index].text!="func" or tokens[index-1].text not in ["=",":="] or tokens[index-2].kind!="identifier": continue
|
|
287
|
+
var binding_index := index-4 if index>3 and tokens[index-3].text==":" else index-2
|
|
288
|
+
var end: int = file.text.length()
|
|
289
|
+
var line: int = tokens[index].span.start.line
|
|
290
|
+
var indentation := 0
|
|
291
|
+
while file.text[file.line_start(line)+indentation] in [" ","\t"]: indentation += 1
|
|
292
|
+
for next in range(index+1,tokens.size()):
|
|
293
|
+
if tokens[next].span.start.line>line and tokens[next].span.start.column-1<=indentation:
|
|
294
|
+
end = tokens[next].span.start.index
|
|
295
|
+
break
|
|
296
|
+
var reactive := false
|
|
297
|
+
for next in range(index+1,tokens.size()):
|
|
298
|
+
if tokens[next].span.start.index>=end: break
|
|
299
|
+
if tokens[next].kind=="ggx": reactive = true
|
|
300
|
+
lambda_bindings.append({"name":tokens[binding_index].text,"start":tokens[index].span.start.index,"end":end,"reactive":reactive,"scope":lexical._scope(tokens[index].span.start.index)})
|
|
301
|
+
func _lambda_at(name: String, offset: int) -> bool:
|
|
302
|
+
var scope: int = lexical._scope(offset)
|
|
303
|
+
while scope>=0:
|
|
304
|
+
var found = null
|
|
305
|
+
for binding in lambda_bindings:
|
|
306
|
+
if binding.name==name and binding.scope==scope and binding.start<offset: found = binding
|
|
307
|
+
for symbol in lexical.symbols:
|
|
308
|
+
if symbol.name==name and symbol.scope==scope and symbol.span.start.index<offset and (found==null or symbol.span.start.index>found.start): return false
|
|
309
|
+
if found!=null: return found.reactive
|
|
310
|
+
scope = lexical.scopes[scope].parent
|
|
311
|
+
return false
|
|
312
|
+
|
|
313
|
+
# Return signatures are source-only and include ordinary inherited helpers.
|
|
314
|
+
func _inherited_returns(source, tokens: Array, seen: Dictionary) -> Dictionary:
|
|
315
|
+
for index in range(tokens.size()-1):
|
|
316
|
+
if tokens[index].text!="extends" or tokens[index].span.start.column!=1: continue
|
|
317
|
+
var target = tokens[index+1]
|
|
318
|
+
var path: String = ""
|
|
319
|
+
if target.kind=="string":
|
|
320
|
+
path = target.text.substr(1,target.text.length()-2)
|
|
321
|
+
if not path.is_absolute_path(): path = source.generated_path().get_base_dir().path_join(path).simplify_path()
|
|
322
|
+
else: path = resolver.aliases.get(target.text,resolver.globals.get(target.text,""))
|
|
323
|
+
if path.is_empty() or seen.has(path): return {}
|
|
324
|
+
seen[path] = true
|
|
325
|
+
var authored: String = path.get_base_dir().get_base_dir().path_join(path.get_file().get_basename()+".gogo")
|
|
326
|
+
if path.get_base_dir().get_file()=="generated" and FileAccess.file_exists(authored): path = authored
|
|
327
|
+
if not FileAccess.file_exists(path): return {}
|
|
328
|
+
var project = resolver.scope()
|
|
329
|
+
var key: String = project.authored(path)
|
|
330
|
+
var text: String = project.read(key) if project.sources.has(key) else FileAccess.get_file_as_string(path)
|
|
331
|
+
var imported = Source.new(text,path,path if path.get_extension()=="gd" else "")
|
|
332
|
+
var parsed = Parser.new(imported).parse()
|
|
333
|
+
if parsed==null: return {}
|
|
334
|
+
var inherited := _tokens(parsed.parts,imported)
|
|
335
|
+
var result := _returns(inherited)
|
|
336
|
+
result.merge(_inherited_returns(imported,inherited,seen),false)
|
|
337
|
+
return result
|
|
338
|
+
return {}
|
|
339
|
+
|
|
340
|
+
# Preserve a native expression statement's ignored result. In particular a
|
|
341
|
+
# Variant Array.append() must not be returned by an adapter (it returns void).
|
|
342
|
+
func _statement(tokens: Array, begin: int, end: int, value_context: bool) -> bool:
|
|
343
|
+
var stack: Array = []
|
|
344
|
+
for index in range(begin):
|
|
345
|
+
var text: String = tokens[index].text
|
|
346
|
+
if text in ["(","[","{"]: stack.append(text)
|
|
347
|
+
elif text in [")","]","}"] and not stack.is_empty(): stack.pop_back()
|
|
348
|
+
var previous: String = tokens[begin-1].text if begin>0 else ""
|
|
349
|
+
var prefix: String = file.text.substr(file.line_start(tokens[begin].span.start.line),tokens[begin].span.start.index-file.line_start(tokens[begin].span.start.line)).strip_edges()
|
|
350
|
+
var boundary: bool = previous==";" or previous==":" and not stack.has("{") or prefix.is_empty() and stack.is_empty() and not value_context
|
|
351
|
+
if not boundary: return false
|
|
352
|
+
if end+1>=tokens.size(): return true
|
|
353
|
+
var next = tokens[end+1]
|
|
354
|
+
return next.text==";" or next.span.start.line>tokens[end].span.end.line
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid://lvcgd23gowfq
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
@tool
|
|
2
|
+
extends RefCounted
|
|
3
|
+
## Apply only leading-whitespace patches through the native undo history.
|
|
4
|
+
static func _column(row: int, column: int, edits: Array) -> int:
|
|
5
|
+
for edit in edits:
|
|
6
|
+
if edit.line==row:
|
|
7
|
+
return column+edit.new.length()-edit.old.length() if column>=edit.old.length() else mini(column,edit.new.length())
|
|
8
|
+
return column
|
|
9
|
+
static func apply(code: CodeEdit, report: Dictionary) -> bool:
|
|
10
|
+
if not report.ok or report.edits.is_empty(): return report.ok
|
|
11
|
+
var carets: Array = []
|
|
12
|
+
var positions: Array = []
|
|
13
|
+
for index in range(code.get_caret_count()):
|
|
14
|
+
var row := code.get_caret_line(index)
|
|
15
|
+
var column := _column(row,code.get_caret_column(index),report.edits)
|
|
16
|
+
var point := Vector2i(row,column)
|
|
17
|
+
if positions.has(point): return false # Never silently merge native carets.
|
|
18
|
+
positions.append(point)
|
|
19
|
+
var item := {"row":row,"column":column,"selected":code.has_selection(index)}
|
|
20
|
+
if item.selected:
|
|
21
|
+
var from_row := code.get_selection_from_line(index)
|
|
22
|
+
var from_column := code.get_selection_from_column(index)
|
|
23
|
+
var to_row := code.get_selection_to_line(index)
|
|
24
|
+
var to_column := code.get_selection_to_column(index)
|
|
25
|
+
item.from_row = from_row
|
|
26
|
+
item.from_column = _column(from_row,from_column,report.edits)
|
|
27
|
+
item.to_row = to_row
|
|
28
|
+
item.to_column = _column(to_row,to_column,report.edits)
|
|
29
|
+
item.reverse = row==from_row and code.get_caret_column(index)==from_column
|
|
30
|
+
carets.append(item)
|
|
31
|
+
var vertical := code.scroll_vertical
|
|
32
|
+
var horizontal := code.scroll_horizontal
|
|
33
|
+
code.cancel_code_completion()
|
|
34
|
+
code.begin_complex_operation()
|
|
35
|
+
code.deselect()
|
|
36
|
+
code.remove_secondary_carets()
|
|
37
|
+
for index in range(report.edits.size()-1,-1,-1):
|
|
38
|
+
var edit: Dictionary = report.edits[index]
|
|
39
|
+
if not edit.old.is_empty(): code.remove_text(edit.line,0,edit.line,edit.old.length())
|
|
40
|
+
code.set_caret_line(edit.line,false)
|
|
41
|
+
code.set_caret_column(0,false)
|
|
42
|
+
code.insert_text_at_caret(edit.new,0)
|
|
43
|
+
for index in range(carets.size()):
|
|
44
|
+
var item: Dictionary = carets[index]
|
|
45
|
+
var caret := 0 if index==0 else code.add_caret(item.row,item.column)
|
|
46
|
+
code.set_caret_line(item.row,false,true,0,caret)
|
|
47
|
+
code.set_caret_column(item.column,false,caret)
|
|
48
|
+
if item.selected:
|
|
49
|
+
if item.reverse: code.select(item.to_row,item.to_column,item.from_row,item.from_column,caret)
|
|
50
|
+
else: code.select(item.from_row,item.from_column,item.to_row,item.to_column,caret)
|
|
51
|
+
code.end_complex_operation()
|
|
52
|
+
code.scroll_vertical = vertical
|
|
53
|
+
code.scroll_horizontal = horizontal
|
|
54
|
+
return true
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid://bre15gqbs36bq
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
@tool
|
|
2
|
+
extends RefCounted
|
|
3
|
+
## Leading indentation only. Native parser islands own markup/expression bounds;
|
|
4
|
+
## original GDScript suite relationships and every physical row are retained.
|
|
5
|
+
const Source = preload("../compiler/source.gd")
|
|
6
|
+
const Parser = preload("../compiler/parser.gd")
|
|
7
|
+
const Lexer = preload("../compiler/lexer.gd")
|
|
8
|
+
const Model = preload("../compiler/model.gd")
|
|
9
|
+
var file
|
|
10
|
+
var lines: PackedStringArray
|
|
11
|
+
var targets: Dictionary = {}
|
|
12
|
+
var protected: Dictionary = {}
|
|
13
|
+
var comments: Array = []
|
|
14
|
+
var size := 4
|
|
15
|
+
var unit := "\t"
|
|
16
|
+
|
|
17
|
+
func format_source(text: String, path: String = "<Gogo>", spaces: bool = false, indent_size: int = 4) -> Dictionary:
|
|
18
|
+
file = Source.new(text,path)
|
|
19
|
+
lines = text.split("\n")
|
|
20
|
+
targets.clear()
|
|
21
|
+
protected.clear()
|
|
22
|
+
comments.clear()
|
|
23
|
+
size = maxi(1,indent_size)
|
|
24
|
+
unit = " ".repeat(size) if spaces else "\t"
|
|
25
|
+
var parser := Parser.new(file)
|
|
26
|
+
var module = parser.parse()
|
|
27
|
+
if module!=null:
|
|
28
|
+
for token in parser.lexer.tokens:
|
|
29
|
+
if token.kind=="markup-comment": comments.append(token.span)
|
|
30
|
+
if token.kind in ["string","markup-string","markup-comment"] or token.kind=="markup-text" and not token.text.strip_edges().is_empty():
|
|
31
|
+
for line in range(token.span.start.line+1,token.span.end.line+1):
|
|
32
|
+
if file.line_start(line)<token.span.end.index: protected[line-1] = true
|
|
33
|
+
_gd(module.parts,0)
|
|
34
|
+
if not file.diagnostics.is_empty():
|
|
35
|
+
var records: Array = []
|
|
36
|
+
for diagnostic in file.diagnostics:
|
|
37
|
+
records.append({"source":path,"line":diagnostic.span.start.line,"column":diagnostic.span.start.column,"message":diagnostic.message})
|
|
38
|
+
return {"ok":false,"text":text,"edits":[],"diagnostics":records}
|
|
39
|
+
var edits: Array = []
|
|
40
|
+
for row in range(lines.size()):
|
|
41
|
+
if not targets.has(row) or protected.has(row) or lines[row].strip_edges().is_empty(): continue
|
|
42
|
+
var old := _prefix(row)
|
|
43
|
+
var next := unit.repeat(targets[row])
|
|
44
|
+
if old!=next:
|
|
45
|
+
edits.append({"line":row,"old":old,"new":next})
|
|
46
|
+
lines[row] = next+lines[row].substr(old.length())
|
|
47
|
+
return {"ok":true,"text":"\n".join(lines),"edits":edits,"diagnostics":[]}
|
|
48
|
+
|
|
49
|
+
func _prefix(row: int) -> String:
|
|
50
|
+
var end := 0
|
|
51
|
+
while end<lines[row].length() and lines[row][end] in [" ","\t"]: end += 1
|
|
52
|
+
return lines[row].left(end)
|
|
53
|
+
func _width(row: int) -> int:
|
|
54
|
+
var width := 0
|
|
55
|
+
for character in _prefix(row): width = (width/size+1)*size if character=="\t" else width+1
|
|
56
|
+
return width
|
|
57
|
+
func _at(offset: int, level: int) -> void:
|
|
58
|
+
var row: int = file.position(offset).line-1
|
|
59
|
+
var prefix: String = file.text.substr(file.line_start(row+1),offset-file.line_start(row+1))
|
|
60
|
+
if prefix.strip_edges().is_empty(): targets[row] = maxi(0,level)
|
|
61
|
+
func _row(offset: int) -> int: return file.position(offset).line-1
|
|
62
|
+
|
|
63
|
+
func _tokens(parts: Array) -> Array:
|
|
64
|
+
var result: Array = []
|
|
65
|
+
var lexer := Lexer.new(file)
|
|
66
|
+
for part in parts:
|
|
67
|
+
if part is Model.Component or part is Model.Layout:
|
|
68
|
+
result.append({"kind":"island","text":"<GGX>","span":part.span,"node":part})
|
|
69
|
+
elif part is Model.Code:
|
|
70
|
+
var cursor: int = part.span.start.index
|
|
71
|
+
while cursor<part.span.end.index and file.diagnostics.is_empty():
|
|
72
|
+
var token = lexer.next_token(cursor)
|
|
73
|
+
if token==null: break
|
|
74
|
+
result.append({"kind":token.kind,"text":token.text,"span":token.span})
|
|
75
|
+
cursor = token.span.end.index
|
|
76
|
+
return result
|
|
77
|
+
|
|
78
|
+
func _gd(parts: Array, floor_level: int) -> void:
|
|
79
|
+
var tokens := _tokens(parts)
|
|
80
|
+
var blocks: Array = []
|
|
81
|
+
var brackets: Array = []
|
|
82
|
+
var statement := false
|
|
83
|
+
var pending := false
|
|
84
|
+
var continued := false
|
|
85
|
+
var last := ""
|
|
86
|
+
var level := floor_level
|
|
87
|
+
for token in tokens:
|
|
88
|
+
if not file.diagnostics.is_empty(): return
|
|
89
|
+
var row: int = token.span.start.line-1
|
|
90
|
+
if token.kind=="whitespace": continue
|
|
91
|
+
if token.kind=="comment":
|
|
92
|
+
var comment_level := level+brackets.size()
|
|
93
|
+
if brackets.is_empty() and not blocks.is_empty():
|
|
94
|
+
var width := _width(row)
|
|
95
|
+
for block in blocks:
|
|
96
|
+
if block.input<=width: comment_level = block.output
|
|
97
|
+
if pending and width>blocks[-1].input: comment_level = blocks[-1].output+1
|
|
98
|
+
_at(token.span.start.index,comment_level)
|
|
99
|
+
continue
|
|
100
|
+
if token.kind=="continuation": continued = true; continue
|
|
101
|
+
if token.kind=="newline":
|
|
102
|
+
if brackets.is_empty() and not continued and statement:
|
|
103
|
+
pending = last==":"
|
|
104
|
+
statement = false
|
|
105
|
+
continued = false
|
|
106
|
+
continue
|
|
107
|
+
if not statement:
|
|
108
|
+
var width := _width(row)
|
|
109
|
+
if blocks.is_empty():
|
|
110
|
+
# Inline islands/attributes inherit the physical header's target.
|
|
111
|
+
var base: int = targets.get(row,floor_level)
|
|
112
|
+
blocks.append({"input":width,"output":base})
|
|
113
|
+
elif pending:
|
|
114
|
+
if width<=blocks[-1].input:
|
|
115
|
+
file.fail("Cannot infer GDScript suite indentation; indent its body before formatting",token.span.start.index)
|
|
116
|
+
return
|
|
117
|
+
blocks.append({"input":width,"output":blocks[-1].output+1})
|
|
118
|
+
else:
|
|
119
|
+
while blocks.size()>1 and width<blocks[-1].input: blocks.pop_back()
|
|
120
|
+
if width!=blocks[-1].input:
|
|
121
|
+
file.fail("Ambiguous GDScript dedent/indent; source unchanged",token.span.start.index)
|
|
122
|
+
return
|
|
123
|
+
level = blocks[-1].output
|
|
124
|
+
pending = false
|
|
125
|
+
statement = true
|
|
126
|
+
var closing: bool = token.text in [")","]","}"]
|
|
127
|
+
var token_level: int = level+brackets.size()-(1 if closing and not brackets.is_empty() else 0)
|
|
128
|
+
_at(token.span.start.index,token_level)
|
|
129
|
+
if token.kind=="island":
|
|
130
|
+
var base: int = targets.get(row,token_level)
|
|
131
|
+
_node(token.node.root if token.node is Model.Layout else token.node,base)
|
|
132
|
+
elif token.text in ["(","[","{"]: brackets.append(token.text)
|
|
133
|
+
elif closing:
|
|
134
|
+
if brackets.is_empty() or brackets[-1]!={")":"(","]":"[","}":"{"}[token.text]:
|
|
135
|
+
file.fail("Unmatched GDScript delimiter; source unchanged",token.span.start.index)
|
|
136
|
+
return
|
|
137
|
+
brackets.pop_back()
|
|
138
|
+
last = token.text
|
|
139
|
+
if not brackets.is_empty():
|
|
140
|
+
file.fail("Incomplete GDScript delimiter; source unchanged",file.text.length())
|
|
141
|
+
if pending or statement and last==":" and brackets.is_empty():
|
|
142
|
+
file.fail("Incomplete GDScript suite; source unchanged",file.text.length())
|
|
143
|
+
|
|
144
|
+
func _expression(expression, level: int) -> void:
|
|
145
|
+
_at(expression.span.start.index,level)
|
|
146
|
+
_gd(expression.parts,level+1)
|
|
147
|
+
_at(expression.span.end.index-1,level)
|
|
148
|
+
func _node(node, level: int) -> void:
|
|
149
|
+
_at(node.span.start.index,level)
|
|
150
|
+
var end: int = node.name_span.end.index
|
|
151
|
+
for attribute in node.attributes:
|
|
152
|
+
_at(attribute.name_span.start.index,level+1)
|
|
153
|
+
if attribute.value is Model.GdExpression: _expression(attribute.value,level+1)
|
|
154
|
+
end = attribute.span.end.index
|
|
155
|
+
while end<file.text.length() and file.text[end] in [" ","\t","\r","\n"]: end += 1
|
|
156
|
+
_at(end,level)
|
|
157
|
+
for comment in comments:
|
|
158
|
+
if comment.start.index>end and comment.end.index<node.span.end.index: _at(comment.start.index,level+1)
|
|
159
|
+
for child in node.children:
|
|
160
|
+
if child is Model.Component: _node(child,level+1)
|
|
161
|
+
elif child is Model.GdExpression: _expression(child,level+1)
|
|
162
|
+
# The parser already authenticated this closing tag; never search literals.
|
|
163
|
+
if file.text.substr(end,2)!="/>":
|
|
164
|
+
var closing: int = node.span.end.index-1
|
|
165
|
+
while closing>node.span.start.index and file.text[closing]!="<": closing -= 1
|
|
166
|
+
_at(closing,level)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid://bp6c5wsr48txe
|
|
@@ -3,6 +3,10 @@ extends Node
|
|
|
3
3
|
## Owns only authenticated Gogo CodeEdit connections and a dedicated gutter.
|
|
4
4
|
signal diagnostics_changed(records, phase)
|
|
5
5
|
signal source_edited(path)
|
|
6
|
+
signal format_finished(report)
|
|
7
|
+
const Formatter = preload("formatter.gd")
|
|
8
|
+
const FormatEdit = preload("format_edit.gd")
|
|
9
|
+
var last_format: Dictionary = {}
|
|
6
10
|
const Context = preload("lexical_context.gd")
|
|
7
11
|
const Acceptance = preload("completion_edit.gd")
|
|
8
12
|
const Catalog = preload("components.gd")
|
|
@@ -286,3 +290,42 @@ func navigate(record: Dictionary) -> void:
|
|
|
286
290
|
code.grab_focus()
|
|
287
291
|
return
|
|
288
292
|
await get_tree().process_frame
|
|
293
|
+
|
|
294
|
+
func _format_entry() -> Dictionary:
|
|
295
|
+
if not _active or not interface.get_script_editor().is_visible_in_tree(): return {}
|
|
296
|
+
var current = interface.get_script_editor().get_current_editor()
|
|
297
|
+
if current==null: return {}
|
|
298
|
+
for id in entries:
|
|
299
|
+
var entry: Dictionary = entries[id]
|
|
300
|
+
var code = entry.code.get_ref()
|
|
301
|
+
if current==entry.editor.get_ref() and code!=null and current.get_base_editor()==code and code.is_visible_in_tree() and code.editable: return {"id":id,"entry":entry,"code":code}
|
|
302
|
+
return {}
|
|
303
|
+
func _input(event: InputEvent) -> void:
|
|
304
|
+
if not event is InputEventKey or not event.pressed or event.echo or event.keycode!=KEY_I: return
|
|
305
|
+
if event.alt_pressed or event.shift_pressed or not (event.ctrl_pressed or event.meta_pressed): return
|
|
306
|
+
var current := _format_entry()
|
|
307
|
+
if current.is_empty() or not current.code.has_focus(): return
|
|
308
|
+
get_viewport().set_input_as_handled()
|
|
309
|
+
format_current()
|
|
310
|
+
func format_current() -> void:
|
|
311
|
+
var current := _format_entry()
|
|
312
|
+
if current.is_empty():
|
|
313
|
+
last_format = {"ok":false,"changed":false,"diagnostics":[],"message":"Open an authored Gogo tab to format its indentation."}
|
|
314
|
+
format_finished.emit(last_format)
|
|
315
|
+
return
|
|
316
|
+
var code: CodeEdit = current.code
|
|
317
|
+
var report := Formatter.new().format_source(code.text,current.entry.path,code.indent_use_spaces,code.indent_size)
|
|
318
|
+
var changed: bool = report.ok and not report.edits.is_empty()
|
|
319
|
+
if changed:
|
|
320
|
+
current.entry.mutating = true
|
|
321
|
+
var applied := FormatEdit.apply(code,report)
|
|
322
|
+
current.entry.mutating = false
|
|
323
|
+
if not applied:
|
|
324
|
+
report.ok = false
|
|
325
|
+
report.diagnostics = [{"source":current.entry.path,"line":1,"column":1,"message":"Formatting would merge carets; use a single caret or move carets out of indentation. Source unchanged."}]
|
|
326
|
+
changed = false
|
|
327
|
+
else: _changed(current.id,false)
|
|
328
|
+
report.changed = changed
|
|
329
|
+
report.message = "Formatted Gogo indentation." if changed else "Gogo indentation is already formatted." if report.ok else "Cannot format Gogo safely; source unchanged."
|
|
330
|
+
last_format = report
|
|
331
|
+
format_finished.emit(report)
|
|
@@ -56,6 +56,7 @@ func _enter_tree() -> void:
|
|
|
56
56
|
_services = Services.new(_interface,_navigation)
|
|
57
57
|
add_child(_services)
|
|
58
58
|
_services.diagnostics_changed.connect(_typing_report)
|
|
59
|
+
_services.format_finished.connect(_format_report)
|
|
59
60
|
_highlighter_association.associated.connect(_services.attach)
|
|
60
61
|
_highlighter_association.start(_interface)
|
|
61
62
|
_new_script = NewScript.new(_interface,_navigation)
|
|
@@ -76,6 +77,7 @@ func _enter_tree() -> void:
|
|
|
76
77
|
_creation_menu.create = _new_script.show_dialog
|
|
77
78
|
call("add_context_menu_plugin",3,_creation_menu) # CONTEXT_SLOT_FILESYSTEM_CREATE
|
|
78
79
|
add_tool_menu_item("Add Missing Gogo Imports",_services.add_missing_imports)
|
|
80
|
+
add_tool_menu_item("Format Gogo / Auto Indent",_services.format_current)
|
|
79
81
|
_timer = Timer.new()
|
|
80
82
|
_timer.one_shot = true
|
|
81
83
|
_timer.wait_time = 0.4
|
|
@@ -258,6 +260,7 @@ func _exit_tree() -> void:
|
|
|
258
260
|
_creation_menu.create = Callable()
|
|
259
261
|
_creation_menu = null
|
|
260
262
|
remove_tool_menu_item("Add Missing Gogo Imports")
|
|
263
|
+
remove_tool_menu_item("Format Gogo / Auto Indent")
|
|
261
264
|
if _generated_editors != null:
|
|
262
265
|
_highlighter_association.associated.disconnect(_generated_editors.attach)
|
|
263
266
|
_generated_editors.stop()
|
|
@@ -296,3 +299,7 @@ func _exit_tree() -> void:
|
|
|
296
299
|
_diagnostics.free()
|
|
297
300
|
_diagnostics = null
|
|
298
301
|
_interface = null
|
|
302
|
+
|
|
303
|
+
func _format_report(report: Dictionary) -> void:
|
|
304
|
+
_typing_report(report.diagnostics,report.message)
|
|
305
|
+
if not report.ok and _diagnostics!=null: make_bottom_panel_item_visible(_diagnostics)
|
|
@@ -15,6 +15,7 @@ var computation: WeakRef
|
|
|
15
15
|
var connected := false
|
|
16
16
|
var writing := false
|
|
17
17
|
func _init(owner: Object, reader: Callable, property: StringName, writer: Callable, identity: String) -> void:
|
|
18
|
+
if owner==null: return
|
|
18
19
|
route = identity
|
|
19
20
|
root = weakref(owner)
|
|
20
21
|
read = reader
|
|
@@ -26,7 +27,7 @@ func _init(owner: Object, reader: Callable, property: StringName, writer: Callab
|
|
|
26
27
|
value = endpoint.get(field)
|
|
27
28
|
valid = typeof(value)==TYPE_BOOL and endpoint.has_signal(str(field)+"_changed")
|
|
28
29
|
func current():
|
|
29
|
-
var owner = root.get_ref()
|
|
30
|
+
var owner = root.get_ref() if root!=null else null
|
|
30
31
|
var endpoint = read.call(owner) if is_instance_valid(owner) else null
|
|
31
32
|
return endpoint if is_instance_valid(endpoint) else null
|
|
32
33
|
func same(other) -> bool:
|
|
@@ -60,3 +61,17 @@ func edit(next: bool) -> void:
|
|
|
60
61
|
# Reentrant model callbacks can restore the old value; commit must still
|
|
61
62
|
# restore a control whose local user value diverged from its snapshot.
|
|
62
63
|
scope.schedule()
|
|
64
|
+
|
|
65
|
+
# A desired linkage is reusable data; each native mount owns its mutable edit
|
|
66
|
+
# connection. Copy without evaluating getters outside the render read scope.
|
|
67
|
+
func snapshot():
|
|
68
|
+
var result = get_script().new(null,Callable(),&"",Callable(),"")
|
|
69
|
+
result.root = root
|
|
70
|
+
result.source = source
|
|
71
|
+
result.field = field
|
|
72
|
+
result.route = route
|
|
73
|
+
result.read = read
|
|
74
|
+
result.write = write
|
|
75
|
+
result.value = value
|
|
76
|
+
result.valid = valid
|
|
77
|
+
return result
|
|
@@ -25,6 +25,9 @@ class Mounted extends RefCounted:
|
|
|
25
25
|
var handlers: Dictionary = {}
|
|
26
26
|
var bindings: Dictionary = {}
|
|
27
27
|
var children: Array = []
|
|
28
|
+
var render_cache = preload("render_cache.gd").new()
|
|
29
|
+
var cache_observers: Dictionary = {}
|
|
30
|
+
var notification_callbacks: Dictionary = {}
|
|
28
31
|
var host: WeakRef
|
|
29
32
|
var projection
|
|
30
33
|
var _borrow_seen: Dictionary = {}
|
|
@@ -34,6 +37,7 @@ var state: WeakRef
|
|
|
34
37
|
var parent_computation: WeakRef
|
|
35
38
|
var mounted: Mounted
|
|
36
39
|
var subscriptions: Dictionary = {}
|
|
40
|
+
var committed_reads: Dictionary = {}
|
|
37
41
|
var queued := false
|
|
38
42
|
var active := true
|
|
39
43
|
var disposed := false
|
|
@@ -59,6 +63,7 @@ func _init(owner: Node, manager: Resource) -> void:
|
|
|
59
63
|
ancestor = ancestor.get_parent()
|
|
60
64
|
func schedule(_previous: Variant = null, _value: Variant = null) -> void:
|
|
61
65
|
if not active or disposed: return
|
|
66
|
+
for entry in render_cache.children.get("$root",[]): entry.invalidate()
|
|
62
67
|
if Profile.is_enabled():
|
|
63
68
|
Profile.add("schedule_requests")
|
|
64
69
|
if queued: Profile.add("coalesced")
|
|
@@ -77,6 +82,9 @@ func depth() -> int:
|
|
|
77
82
|
return result
|
|
78
83
|
func suspend() -> void:
|
|
79
84
|
active = false
|
|
85
|
+
render_cache = preload("render_cache.gd").new()
|
|
86
|
+
cache_observers.clear()
|
|
87
|
+
committed_reads.clear()
|
|
80
88
|
projection.stop()
|
|
81
89
|
Hosts.replace_projection(self,{}, {})
|
|
82
90
|
layout_host.stop()
|
|
@@ -87,26 +95,26 @@ func resume() -> void:
|
|
|
87
95
|
layout_host.start()
|
|
88
96
|
schedule()
|
|
89
97
|
func _disconnect() -> void:
|
|
90
|
-
for
|
|
98
|
+
for key in subscriptions:
|
|
99
|
+
var pair: Array = subscriptions[key]
|
|
91
100
|
var source = pair[0].get_ref()
|
|
92
|
-
if source != null and source.is_connected(pair[1],
|
|
93
|
-
source.disconnect(pair[1],
|
|
101
|
+
if source != null and source.is_connected(pair[1],notification_callbacks.get(key,Callable())):
|
|
102
|
+
source.disconnect(pair[1],notification_callbacks[key])
|
|
94
103
|
if Profile.is_enabled(): Profile.add("subscription_disconnects")
|
|
95
104
|
subscriptions.clear()
|
|
105
|
+
notification_callbacks.clear()
|
|
96
106
|
func _subscribe(next: Dictionary) -> void:
|
|
97
107
|
for key in subscriptions.keys():
|
|
98
108
|
if next.has(key): continue
|
|
99
109
|
var pair: Array = subscriptions[key]
|
|
100
110
|
var source = pair[0].get_ref()
|
|
101
|
-
if source != null and source.is_connected(pair[1],
|
|
102
|
-
source.disconnect(pair[1],
|
|
111
|
+
if source != null and source.is_connected(pair[1],notification_callbacks.get(key,Callable())):
|
|
112
|
+
source.disconnect(pair[1],notification_callbacks[key])
|
|
103
113
|
if Profile.is_enabled(): Profile.add("subscription_disconnects")
|
|
114
|
+
for key in notification_callbacks.keys():
|
|
115
|
+
if not next.has(key): notification_callbacks.erase(key)
|
|
104
116
|
for key in next:
|
|
105
|
-
if subscriptions.has(key):
|
|
106
|
-
var pair: Array = next[key]
|
|
107
|
-
var source = pair[0].get_ref()
|
|
108
|
-
if Profile.is_enabled(): Profile.add("subscription_connects")
|
|
109
|
-
if source != null and source.connect(pair[1],schedule) != OK: push_error("F01 observation connection failed")
|
|
117
|
+
if not subscriptions.has(key): _connect_observer(key,next[key])
|
|
110
118
|
subscriptions = next
|
|
111
119
|
func _flush() -> void:
|
|
112
120
|
queued = false
|
|
@@ -119,9 +127,13 @@ func _flush() -> void:
|
|
|
119
127
|
if Profile.is_enabled(): Profile.add("views")
|
|
120
128
|
_rendering = true
|
|
121
129
|
_again = false
|
|
130
|
+
manager.enter_render(self)
|
|
122
131
|
manager.begin()
|
|
123
|
-
var desired =
|
|
132
|
+
var desired = manager.call_at("$root",Callable(owner,"build_view"),[])
|
|
124
133
|
var reads: Dictionary = manager.finish()
|
|
134
|
+
reads.merge(manager.leave_render(),true)
|
|
135
|
+
# New reads are connected eagerly by watch(). Retain previous native-tree
|
|
136
|
+
# subscriptions on failed preflight; prune them only after successful validation.
|
|
125
137
|
if Profile.is_enabled(): Profile.elapsed("view",started)
|
|
126
138
|
_borrow_seen.clear()
|
|
127
139
|
_observes_children = false
|
|
@@ -129,13 +141,17 @@ func _flush() -> void:
|
|
|
129
141
|
desired = _normalize(desired)
|
|
130
142
|
_rendering = false
|
|
131
143
|
if not desired is Element or not _valid(desired,mounted) or not _preflight(desired,mounted,owner) or not _rescue_clear(desired,mounted,owner):
|
|
144
|
+
var repair_reads := committed_reads.duplicate()
|
|
145
|
+
repair_reads.merge(reads,true)
|
|
146
|
+
_subscribe(repair_reads)
|
|
132
147
|
push_error(validation_error if not validation_error.is_empty() else "Invalid composition output or foreign boundary conflict; previous tree retained")
|
|
133
148
|
return
|
|
149
|
+
committed_reads = reads.duplicate()
|
|
150
|
+
_subscribe(reads)
|
|
134
151
|
# A nested array may be an empty get_children() result. Observe native
|
|
135
152
|
# changes without inspecting the expression source or polling the tree.
|
|
136
153
|
if _observes_children: projection.start()
|
|
137
154
|
else: projection.stop()
|
|
138
|
-
_subscribe(reads)
|
|
139
155
|
# Catch mutation after a read during first composition/commit as a later
|
|
140
156
|
# pass. Source-side effects are unsupported, but never recurse into SceneTree.
|
|
141
157
|
_rendering = true
|
|
@@ -170,7 +186,9 @@ func _normalize(value, ancestors: Array = [], level: int = 0):
|
|
|
170
186
|
result.placement = value.placement.duplicate(true)
|
|
171
187
|
result.origins = value.origins.duplicate()
|
|
172
188
|
result.spread_properties = value.spread_properties.duplicate()
|
|
173
|
-
|
|
189
|
+
for key in value.bindings:
|
|
190
|
+
var linkage = value.bindings[key]
|
|
191
|
+
result.bindings[key] = linkage.snapshot() if linkage is Binding else linkage
|
|
174
192
|
if not _flatten(value.children,[],result.children,next,level+1): return null
|
|
175
193
|
return result
|
|
176
194
|
func _key_valid(key) -> bool:
|
|
@@ -780,3 +798,28 @@ func _unindex(record) -> void:
|
|
|
780
798
|
state.get_ref().managed.erase(record.id)
|
|
781
799
|
Hosts.clear_placement(record.id)
|
|
782
800
|
for child in record.children: _unindex(child)
|
|
801
|
+
|
|
802
|
+
func _notified(key: String) -> void:
|
|
803
|
+
for entry in cache_observers.get(key,[]): entry.invalidate()
|
|
804
|
+
schedule()
|
|
805
|
+
|
|
806
|
+
func watch(source: Object, event: StringName, entry) -> void:
|
|
807
|
+
var key := str(source.get_instance_id())+":"+str(event)
|
|
808
|
+
if not cache_observers.has(key): cache_observers[key] = []
|
|
809
|
+
if not cache_observers[key].has(entry): cache_observers[key].append(entry)
|
|
810
|
+
if subscriptions.has(key): return
|
|
811
|
+
var pair := [weakref(source),event]
|
|
812
|
+
subscriptions[key] = pair
|
|
813
|
+
_connect_observer(key,pair)
|
|
814
|
+
|
|
815
|
+
func _connect_observer(key: String, pair: Array) -> void:
|
|
816
|
+
var source = pair[0].get_ref()
|
|
817
|
+
if source==null: return
|
|
818
|
+
var arity := 0
|
|
819
|
+
for event in source.get_signal_list():
|
|
820
|
+
if event.name==pair[1]: arity = event.args.size()
|
|
821
|
+
var callback := Callable(self,"_notified").bind(key)
|
|
822
|
+
if arity>0: callback = callback.unbind(arity)
|
|
823
|
+
notification_callbacks[key] = callback
|
|
824
|
+
if Profile.is_enabled(): Profile.add("subscription_connects")
|
|
825
|
+
if source.connect(pair[1],callback)!=OK: push_error("Composition observation connection failed")
|
|
@@ -3,6 +3,7 @@ extends RefCounted
|
|
|
3
3
|
## Reusable description defaults, not Nodes. Evaluation occurs inside the caller's
|
|
4
4
|
## existing composition read scope. Native types/identity/commit remain authoritative.
|
|
5
5
|
const ESTRAGON_PRESET := true
|
|
6
|
+
const State = preload("state.tres")
|
|
6
7
|
const Definition = preload("../node/internal/grid_definition.gd")
|
|
7
8
|
const Element = preload("element.gd")
|
|
8
9
|
var base: Variant
|
|
@@ -42,7 +43,7 @@ func describe(instance: Element) -> Element:
|
|
|
42
43
|
return instance
|
|
43
44
|
var merged := {}
|
|
44
45
|
var locations := {}
|
|
45
|
-
for group in defaults.call():
|
|
46
|
+
for group in State.in_slot(instance.source+"/defaults",null,func(_key): return defaults.call()):
|
|
46
47
|
var expanded := Element.new(desired.type)
|
|
47
48
|
expanded.expand(group)
|
|
48
49
|
if not expanded.attribute_error.is_empty():
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
extends RefCounted
|
|
2
|
+
## One invocation record under the existing owner Computation; never a Node.
|
|
3
|
+
var parent: WeakRef
|
|
4
|
+
var children: Dictionary = {}
|
|
5
|
+
var reads: Dictionary = {}
|
|
6
|
+
var arguments: Array = []
|
|
7
|
+
var target: Variant
|
|
8
|
+
var result: Variant
|
|
9
|
+
var dirty := true
|
|
10
|
+
var keyed := false
|
|
11
|
+
var initialized := false
|
|
12
|
+
func invalidate() -> void:
|
|
13
|
+
if dirty: return
|
|
14
|
+
dirty = true
|
|
15
|
+
var ancestor = parent.get_ref() if parent!=null else null
|
|
16
|
+
if ancestor!=null: ancestor.invalidate()
|
|
17
|
+
static func argument(value):
|
|
18
|
+
if typeof(value)==TYPE_OBJECT: return weakref(value) if is_instance_valid(value) else null
|
|
19
|
+
return value
|
|
20
|
+
static func equivalent(previous, value) -> bool:
|
|
21
|
+
if previous is WeakRef: return is_instance_valid(value) and previous.get_ref()==value
|
|
22
|
+
if typeof(previous)!=typeof(value): return false
|
|
23
|
+
if value is Array or value is Dictionary: return is_same(previous,value)
|
|
24
|
+
return previous==value
|
|
25
|
+
func matches(values: Array) -> bool:
|
|
26
|
+
if arguments.size()!=values.size(): return false
|
|
27
|
+
for index in range(values.size()):
|
|
28
|
+
if not equivalent(arguments[index],values[index]): return false
|
|
29
|
+
return true
|
|
30
|
+
func collect(output: Dictionary, index: Dictionary) -> void:
|
|
31
|
+
for key in reads:
|
|
32
|
+
output[key] = reads[key]
|
|
33
|
+
if not index.has(key): index[key] = []
|
|
34
|
+
index[key].append(self)
|
|
35
|
+
for bucket in children.values():
|
|
36
|
+
for child in bucket: child.collect(output,index)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid://cxagf7qhklyot
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
@tool
|
|
2
2
|
extends Resource
|
|
3
|
+
const Cache = preload("render_cache.gd")
|
|
4
|
+
const Element = preload("element.gd")
|
|
3
5
|
const Profile = preload("profile.tres")
|
|
4
6
|
## Framework-owned shared scope stack; never stored on application objects.
|
|
5
7
|
var scopes: Array = []
|
|
8
|
+
var invocations: Array = []
|
|
9
|
+
var owners: Array = []
|
|
10
|
+
var opaque := 0
|
|
6
11
|
var records: Dictionary = {}
|
|
7
12
|
var managed: Dictionary = {}
|
|
8
13
|
var dirty: Array = []
|
|
@@ -42,10 +47,11 @@ func drain() -> void:
|
|
|
42
47
|
func begin() -> void:
|
|
43
48
|
scopes.append({})
|
|
44
49
|
func observe(source: Object, event: StringName) -> void:
|
|
45
|
-
if scopes.is_empty(): return
|
|
50
|
+
if scopes.is_empty() or opaque>0: return
|
|
46
51
|
if Profile.is_enabled(): Profile.add("getter_reads")
|
|
47
52
|
var key := str(source.get_instance_id())+":"+str(event)
|
|
48
53
|
scopes.back()[key] = [weakref(source),event]
|
|
54
|
+
if not owners.is_empty() and not invocations.is_empty(): owners.back().watch(source,event,invocations.back().entry)
|
|
49
55
|
func finish() -> Dictionary:
|
|
50
56
|
return scopes.pop_back()
|
|
51
57
|
func lifecycle(host: Node, what: int) -> void:
|
|
@@ -74,3 +80,154 @@ func unmount(host: Node, rescue: Node = null) -> void:
|
|
|
74
80
|
if records.has(id):
|
|
75
81
|
records[id].dispose(true,rescue)
|
|
76
82
|
records.erase(id)
|
|
83
|
+
|
|
84
|
+
# A function is the reactive unit. Ordinary bodies run behind an observation
|
|
85
|
+
# boundary, while their arguments were already evaluated by native GDScript.
|
|
86
|
+
func call_at(site: String, callable: Callable, arguments: Array, _native_proof: Callable = Callable(), reactive_lambda: bool = false, cache_target: Variant = null, native_adapter: Callable = Callable(), adapter_receiver: Variant = null):
|
|
87
|
+
if owners.is_empty() or opaque>0: return native_adapter.call(adapter_receiver,arguments) if native_adapter.is_valid() else callable.callv(arguments)
|
|
88
|
+
# Variant built-ins (Array/Dictionary/String) have no Object receiver.
|
|
89
|
+
# They remain native operations; application Script methods have stable IDs.
|
|
90
|
+
if callable.get_object_id()==0: return callable.callv(arguments)
|
|
91
|
+
var owner = owners.back()
|
|
92
|
+
var parent: Dictionary = invocations.back()
|
|
93
|
+
var ordinal: int = parent.ordinals.get(site,0)
|
|
94
|
+
parent.ordinals[site] = ordinal+1
|
|
95
|
+
var bucket: Array = parent.entry.children.get(site,[])
|
|
96
|
+
var entry = null
|
|
97
|
+
# Keyed results follow their argument identity on reorders. Explicit keyed
|
|
98
|
+
# GGX scopes below handle repeated zero-argument calls inside keyed lambdas.
|
|
99
|
+
for candidate in bucket:
|
|
100
|
+
if candidate.keyed and not parent.used.has(candidate) and candidate.matches(arguments):
|
|
101
|
+
entry = candidate
|
|
102
|
+
break
|
|
103
|
+
if entry==null and ordinal<bucket.size() and not parent.used.has(bucket[ordinal]): entry = bucket[ordinal]
|
|
104
|
+
if entry==null:
|
|
105
|
+
entry = Cache.new()
|
|
106
|
+
entry.parent = weakref(parent.entry)
|
|
107
|
+
bucket.append(entry)
|
|
108
|
+
parent.entry.children[site] = bucket
|
|
109
|
+
parent.used[entry] = true
|
|
110
|
+
var object: Object = instance_from_id(callable.get_object_id())
|
|
111
|
+
var method: StringName = &"" if callable.is_custom() else callable.get_method()
|
|
112
|
+
var target = callable if cache_target==null else cache_target
|
|
113
|
+
var hit: bool = entry.initialized and not entry.dirty and typeof(entry.target)==typeof(target) and entry.target==target and entry.matches(arguments)
|
|
114
|
+
if hit:
|
|
115
|
+
if Profile.is_enabled(): Profile.add("render_cache_hits")
|
|
116
|
+
return entry.result
|
|
117
|
+
if Profile.is_enabled(): Profile.add("render_cache_misses")
|
|
118
|
+
entry.target = target
|
|
119
|
+
entry.arguments.clear()
|
|
120
|
+
for value in arguments: entry.arguments.append(Cache.argument(value))
|
|
121
|
+
var reactive := reactive_lambda or _reactive(object,method)
|
|
122
|
+
entry.dirty = false
|
|
123
|
+
entry.initialized = true
|
|
124
|
+
var argument_reads := {}
|
|
125
|
+
# Observable arguments invalidate independently of the body observation
|
|
126
|
+
# boundary. CORE02 can supply real upward change signals without revisions.
|
|
127
|
+
for value in arguments:
|
|
128
|
+
if typeof(value)!=TYPE_OBJECT or not is_instance_valid(value): continue
|
|
129
|
+
for signal_info in value.get_signal_list():
|
|
130
|
+
var event: StringName = signal_info.name
|
|
131
|
+
if event in [&"changed",&"tree_exiting"] or str(event).ends_with("_changed"):
|
|
132
|
+
argument_reads[str(value.get_instance_id())+":"+str(event)] = [weakref(value),event]
|
|
133
|
+
owner.watch(value,event,entry)
|
|
134
|
+
var frame := {"entry":entry,"ordinals":{},"used":{}}
|
|
135
|
+
invocations.append(frame)
|
|
136
|
+
begin()
|
|
137
|
+
if not reactive: opaque += 1
|
|
138
|
+
var result = native_adapter.call(adapter_receiver,arguments) if native_adapter.is_valid() else callable.callv(arguments)
|
|
139
|
+
if not reactive: opaque -= 1
|
|
140
|
+
entry.reads = finish()
|
|
141
|
+
entry.reads.merge(argument_reads,true)
|
|
142
|
+
invocations.pop_back()
|
|
143
|
+
_prune(frame)
|
|
144
|
+
entry.result = result
|
|
145
|
+
entry.keyed = result is Element and result.keyed
|
|
146
|
+
return result
|
|
147
|
+
func _reactive(object, method: StringName) -> bool:
|
|
148
|
+
if method==&"build_view": return true
|
|
149
|
+
if not is_instance_valid(object): return false
|
|
150
|
+
var script = object if object is Script else object.get_script()
|
|
151
|
+
while script is Script:
|
|
152
|
+
var metadata := _metadata(script)
|
|
153
|
+
if metadata.is_empty(): return false # Ordinary imported .gd body.
|
|
154
|
+
if method in metadata.ordinary: return false
|
|
155
|
+
if method in metadata.reactive: return true
|
|
156
|
+
script = script.get_base_script()
|
|
157
|
+
return false
|
|
158
|
+
func _prune(frame: Dictionary) -> void:
|
|
159
|
+
for site in frame.entry.children.keys():
|
|
160
|
+
var retained: Array = []
|
|
161
|
+
for entry in frame.entry.children[site]:
|
|
162
|
+
if frame.used.has(entry): retained.append(entry)
|
|
163
|
+
if retained.is_empty(): frame.entry.children.erase(site)
|
|
164
|
+
else: frame.entry.children[site] = retained
|
|
165
|
+
func enter_render(owner) -> void:
|
|
166
|
+
owners.append(owner)
|
|
167
|
+
invocations.append({"entry":owner.render_cache,"ordinals":{},"used":{}})
|
|
168
|
+
func leave_render() -> Dictionary:
|
|
169
|
+
var frame: Dictionary = invocations.pop_back()
|
|
170
|
+
_prune(frame)
|
|
171
|
+
var owner = owners.pop_back()
|
|
172
|
+
var reads := {}
|
|
173
|
+
owner.cache_observers.clear()
|
|
174
|
+
owner.render_cache.collect(reads,owner.cache_observers)
|
|
175
|
+
return reads
|
|
176
|
+
func in_slot(site: String, key, body: Callable):
|
|
177
|
+
if owners.is_empty() or opaque>0: return body.call(key)
|
|
178
|
+
var parent: Dictionary = invocations.back()
|
|
179
|
+
var identity := site+":"+str(typeof(key))+":"+str(key)
|
|
180
|
+
if key==null:
|
|
181
|
+
var ordinal: int = parent.ordinals.get(identity,0)
|
|
182
|
+
parent.ordinals[identity] = ordinal+1
|
|
183
|
+
identity += ":"+str(ordinal)
|
|
184
|
+
var bucket: Array = parent.entry.children.get(identity,[])
|
|
185
|
+
var group = bucket[0] if not bucket.is_empty() else Cache.new()
|
|
186
|
+
group.parent = weakref(parent.entry)
|
|
187
|
+
group.dirty = false
|
|
188
|
+
parent.entry.children[identity] = [group]
|
|
189
|
+
parent.used[group] = true
|
|
190
|
+
var frame := {"entry":group,"ordinals":{},"used":{}}
|
|
191
|
+
invocations.append(frame)
|
|
192
|
+
var result = body.call(key)
|
|
193
|
+
invocations.pop_back()
|
|
194
|
+
_prune(frame)
|
|
195
|
+
return result
|
|
196
|
+
|
|
197
|
+
func method_at(site: String, receiver, method: StringName, arguments: Array, native_call: Callable, _native_proof: Callable = Callable(), reactive_lambda: bool = false):
|
|
198
|
+
if receiver is Callable and method in [&"call",&"callv"]:
|
|
199
|
+
return call_at(site,receiver,arguments[0] if method==&"callv" else arguments,Callable(),reactive_lambda)
|
|
200
|
+
if typeof(receiver)==TYPE_OBJECT:
|
|
201
|
+
if not is_instance_valid(receiver): return null
|
|
202
|
+
if method in [&"call",&"callv"] and not arguments.is_empty() and (arguments[0] is String or arguments[0] is StringName):
|
|
203
|
+
return call_at(site,Callable(receiver,arguments[0]),arguments[1] if method==&"callv" else arguments.slice(1))
|
|
204
|
+
if receiver is Script or receiver.has_method(method): return call_at(site,Callable(receiver,method),arguments)
|
|
205
|
+
var value = receiver.get(method)
|
|
206
|
+
if value is Callable: return call_at(site,value,arguments,Callable(),reactive_lambda)
|
|
207
|
+
return null
|
|
208
|
+
# Native Variant methods have no script body/dependency boundary. This also
|
|
209
|
+
# preserves normal Array callback traversal without a second evaluator.
|
|
210
|
+
return native_call.call(receiver,arguments)
|
|
211
|
+
|
|
212
|
+
# super is native syntax, never an Object value. Its adapter executes the real
|
|
213
|
+
# base call while a stable lexical identity owns the cache between invocations.
|
|
214
|
+
func base_at(site: String, receiver: Object, declaring_path: String, method: StringName, arguments: Array, native_call: Callable, _native_proof: Callable = Callable()):
|
|
215
|
+
var script = receiver.get_script()
|
|
216
|
+
while script is Script and script.resource_path!=declaring_path: script = script.get_base_script()
|
|
217
|
+
var reactive := false
|
|
218
|
+
if script is Script:
|
|
219
|
+
script = script.get_base_script()
|
|
220
|
+
while script is Script:
|
|
221
|
+
var metadata := _metadata(script)
|
|
222
|
+
if metadata.is_empty() or method in metadata.ordinary: break
|
|
223
|
+
if method==&"build_view" or method in metadata.reactive: reactive = true; break
|
|
224
|
+
script = script.get_base_script()
|
|
225
|
+
return call_at(site,native_call,arguments,Callable(),reactive,[receiver.get_instance_id(),declaring_path,method],native_call,receiver)
|
|
226
|
+
|
|
227
|
+
func _metadata(script: Script) -> Dictionary:
|
|
228
|
+
var constants: Dictionary = script.get_script_constant_map()
|
|
229
|
+
var base = script.get_base_script()
|
|
230
|
+
var inherited: Dictionary = base.get_script_constant_map() if base!=null else {}
|
|
231
|
+
for key in constants:
|
|
232
|
+
if str(key).begins_with("_GOGO_RENDER_FUNCTIONS_") and not inherited.has(key): return constants[key]
|
|
233
|
+
return {}
|
package/package.json
CHANGED