@@ -175,7 +175,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
175
175
break ;
176
176
case "template" :
177
177
currentSchema . template = child ;
178
- await compileUINodeChildren ( child , parentIdent ) ;
178
+ await compileUINode ( child ) ;
179
179
break ;
180
180
default :
181
181
throw SyntaxError ( `Unknown node: ${ child . name } ` ) ;
@@ -188,50 +188,54 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
188
188
schemas [ currentSchema . name ] = currentSchema ;
189
189
}
190
190
191
- async function compileUINodeChildren ( node , ident ) {
191
+ async function compileWidgetNodeChildren ( node , ident ) {
192
192
if ( ! Array . isArray ( node . children ) ) {
193
193
return ;
194
194
}
195
- ( await Promise . all ( node . children . map ( compileUINode ) ) ) . forEach (
196
- ( childIdent ) => {
197
- if ( childIdent ) {
198
- currentSchema . templateLines . push (
199
- `ui_widget_append(${ ident } , ${ childIdent } );`
200
- ) ;
201
- }
202
- }
195
+ const identList = await Promise . all (
196
+ node . children . map ( async ( node ) => {
197
+ const childIdent = allocWidgetNodeIdent ( node ) ;
198
+ await compileWidgetNode ( node , childIdent ) ;
199
+ return childIdent ;
200
+ } )
203
201
) ;
202
+ identList . forEach ( ( childIdent ) => {
203
+ currentSchema . templateLines . push (
204
+ `ui_widget_append(${ ident } , ${ childIdent } );`
205
+ ) ;
206
+ } ) ;
204
207
}
205
208
206
- async function compileUINode ( node ) {
209
+ function allocWidgetNodeIdent ( node ) {
207
210
let ident ;
208
- let widgetType ;
209
211
const attrs = node . attributes || { } ;
212
+ const widgetType = [ "w" , "widget" ] . includes ( node . name )
213
+ ? attrs . type
214
+ : node . name ;
210
215
211
- if ( [ "w" , "widget" ] . includes ( node . name ) ) {
212
- widgetType = attrs . type ;
216
+ if ( attrs . ref && typeof attrs . ref === "string" ) {
217
+ ident = toIdent ( attrs . ref ) ;
218
+ currentSchema . refs . push ( ident ) ;
219
+ ident = `refs->${ ident } ` ;
213
220
} else {
214
- widgetType = node . name ;
215
- }
216
- if (
217
- currentSchema . template . children . length == 1 &&
218
- currentSchema . template . children [ 0 ] === node
219
- ) {
220
- ident = parentIdent ;
221
- } else {
222
- if ( attrs . ref && typeof attrs . ref === "string" ) {
223
- ident = toIdent ( attrs . ref ) ;
224
- currentSchema . refs . push ( ident ) ;
225
- ident = `refs->${ ident } ` ;
226
- } else {
227
- ident = `w[${ count ++ } ]` ;
228
- }
229
- currentSchema . templateLines . push (
230
- `${ ident } = ui_create_widget(${
231
- widgetType ? `"${ widgetType } "` : "NULL"
232
- } );`
233
- ) ;
221
+ ident = `w[${ count ++ } ]` ;
234
222
}
223
+ currentSchema . templateLines . push (
224
+ `${ ident } = ui_create_widget(${ widgetType ? `"${ widgetType } "` : "NULL" } );`
225
+ ) ;
226
+ return ident ;
227
+ }
228
+
229
+ async function compileUINode ( node ) {
230
+ return compileWidgetNode (
231
+ node . children . length == 1 ? node . children [ 0 ] : node ,
232
+ parentIdent
233
+ ) ;
234
+ }
235
+
236
+ async function compileWidgetNode ( node , ident ) {
237
+ const attrs = node . attributes || { } ;
238
+
235
239
Object . keys ( attrs ) . forEach ( ( attrName ) => {
236
240
switch ( attrName ) {
237
241
case "ref" :
@@ -267,8 +271,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
267
271
`ui_widget_set_text(${ ident } , (const char*)${ allocTextVar ( node . text ) } );`
268
272
) ;
269
273
}
270
- compileUINodeChildren ( node , ident ) ;
271
- return ident ;
274
+ compileWidgetNodeChildren ( node , ident ) ;
272
275
}
273
276
274
277
function compileNode ( node ) {
@@ -279,7 +282,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
279
282
}
280
283
state = stateEnum . PARSE_UI ;
281
284
currentSchema . body = node ;
282
- return compileUINodeChildren ( node , parentIdent ) ;
285
+ return compileUINode ( node ) ;
283
286
case "lcui-app" :
284
287
break ;
285
288
case "resource" :
0 commit comments