@@ -44,31 +44,15 @@ class ASTNode:
44
44
children = []
45
45
arguments = {}
46
46
47
- func get_code_block () -> String :
48
- var code_block : String = data .code_template # get multiline code_template from block definition
49
-
50
- # insert args
51
-
52
- # check if args match an overload in the resource
53
-
54
- for arg_name in arguments :
55
- # Use parentheses to be safe
56
- var argument = arguments [arg_name ]
57
- var code_string : String
58
- if argument is ASTValueNode :
59
- code_string = argument .get_code ()
60
- else :
61
- code_string = BlockAST .raw_input_to_code_string (argument )
62
-
63
- code_block = code_block .replace ("{%s }" % arg_name , code_string )
64
-
47
+ func _get_code_block () -> String :
48
+ var code_block : String = BlockAST .format_code_template (data .code_template , arguments )
65
49
return IDHandler .make_unique (code_block )
66
50
67
51
func get_code (depth : int ) -> String :
68
52
var code : String = ""
69
53
70
54
# append code block
71
- var code_block := get_code_block ()
55
+ var code_block := _get_code_block ()
72
56
code_block = code_block .indent ("\t " .repeat (depth ))
73
57
74
58
code += code_block + "\n "
@@ -91,21 +75,7 @@ class ASTValueNode:
91
75
arguments = {}
92
76
93
77
func get_code () -> String :
94
- var code : String = data .code_template # get code_template from block definition
95
-
96
- # check if args match an overload in the resource
97
-
98
- for arg_name in arguments :
99
- # Use parentheses to be safe
100
- var argument = arguments [arg_name ]
101
- var code_string : String
102
- if argument is ASTValueNode :
103
- code_string = argument .get_code ()
104
- else :
105
- code_string = BlockAST .raw_input_to_code_string (argument )
106
-
107
- code = code .replace ("{%s }" % arg_name , code_string )
108
-
78
+ var code : String = BlockAST .format_code_template (data .code_template , arguments )
109
79
return IDHandler .make_unique ("(%s )" % code )
110
80
111
81
@@ -127,18 +97,42 @@ func to_string_recursive(node: ASTNode, depth: int) -> String:
127
97
return string
128
98
129
99
100
+ static func format_code_template (code_template : String , arguments : Dictionary ) -> String :
101
+ for argument_name in arguments :
102
+ # Use parentheses to be safe
103
+ var argument_value : Variant = arguments [argument_name ]
104
+ var code_string : String
105
+ var raw_string : String
106
+
107
+ if argument_value is OptionData :
108
+ # Temporary hack: previously, the value was stored as an OptionData
109
+ # object with a list of items and a "selected" property. If we are
110
+ # using an older block script where that is the case, convert the
111
+ # value to the value of its selected item.
112
+ # See also, ParameterInput._update_option_input.
113
+ argument_value = argument_value .items [argument_value .selected ]
114
+
115
+ if argument_value is ASTValueNode :
116
+ code_string = argument_value .get_code ()
117
+ raw_string = code_string
118
+ else :
119
+ code_string = BlockAST .raw_input_to_code_string (argument_value )
120
+ raw_string = str (argument_value )
121
+
122
+ code_template = code_template .replace ("{{ %s }} " % argument_name , raw_string )
123
+ code_template = code_template .replace ("{%s }" % argument_name , code_string )
124
+
125
+ return code_template
126
+
127
+
130
128
static func raw_input_to_code_string (input ) -> String :
131
129
match typeof (input ):
132
130
TYPE_STRING :
133
- return "'%s '" % input .replace ( " \\ " , " \\\\ " ). replace ( "'" , " \\ '" )
131
+ return "'%s '" % input .c_escape ( )
134
132
TYPE_VECTOR2 :
135
133
return "Vector2%s " % str (input )
136
134
TYPE_COLOR :
137
135
return "Color%s " % str (input )
138
- TYPE_OBJECT :
139
- if input is OptionData :
140
- var option_data := input as OptionData
141
- return option_data .items [option_data .selected ]
142
136
_ :
143
137
return "%s " % input
144
138
0 commit comments