@@ -44,31 +44,15 @@ class ASTNode:
4444 children = []
4545 arguments = {}
4646
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 )
6549 return IDHandler .make_unique (code_block )
6650
6751 func get_code (depth : int ) -> String :
6852 var code : String = ""
6953
7054 # append code block
71- var code_block := get_code_block ()
55+ var code_block := _get_code_block ()
7256 code_block = code_block .indent ("\t " .repeat (depth ))
7357
7458 code += code_block + "\n "
@@ -91,21 +75,7 @@ class ASTValueNode:
9175 arguments = {}
9276
9377 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 )
10979 return IDHandler .make_unique ("(%s )" % code )
11080
11181
@@ -127,18 +97,42 @@ func to_string_recursive(node: ASTNode, depth: int) -> String:
12797 return string
12898
12999
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+
130128static func raw_input_to_code_string (input ) -> String :
131129 match typeof (input ):
132130 TYPE_STRING :
133- return "'%s '" % input .replace ( " \\ " , " \\\\ " ). replace ( "'" , " \\ '" )
131+ return "'%s '" % input .c_escape ( )
134132 TYPE_VECTOR2 :
135133 return "Vector2%s " % str (input )
136134 TYPE_COLOR :
137135 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 ]
142136 _ :
143137 return "%s " % input
144138
0 commit comments