@@ -531,6 +531,89 @@ function createImportTrackingState(): ImportTrackingState {
531531 }
532532}
533533
534+ function indentMultilineType ( type : string , baseIndent : string , isLast : boolean ) : string {
535+ debugLog ( undefined , 'indent-multiline' , `Processing multiline type with baseIndent="${ baseIndent } ", isLast=${ isLast } ` )
536+ debugLog ( undefined , 'indent-input' , `Input type:\n${ type } ` )
537+
538+ const lines = type . split ( '\n' )
539+ debugLog ( undefined , 'indent-lines' , `Split into ${ lines . length } lines` )
540+
541+ if ( lines . length === 1 ) {
542+ const result = `${ baseIndent } ${ type } ${ isLast ? '' : ' |' } `
543+ debugLog ( undefined , 'indent-single' , `Single line result: ${ result } ` )
544+ return result
545+ }
546+
547+ interface BracketInfo {
548+ char : string
549+ indent : string
550+ isArray : boolean
551+ }
552+ const bracketStack : BracketInfo [ ] = [ ]
553+ debugLog ( undefined , 'indent-stack' , 'Initializing bracket stack' )
554+
555+ const formattedLines = lines . map ( ( line , i ) => {
556+ const trimmed = line . trim ( )
557+ if ( ! trimmed ) {
558+ debugLog ( undefined , 'indent-empty' , `Empty line at index ${ i } ` )
559+ return ''
560+ }
561+
562+ // Track Array type specifically
563+ const isArrayStart = trimmed . startsWith ( 'Array<' )
564+ const openBrackets = ( trimmed . match ( / [ { < [ ] / g) || [ ] )
565+ const closeBrackets = ( trimmed . match ( / [ } \] > ] / g) || [ ] )
566+
567+ debugLog ( undefined , 'indent-brackets' , `Line ${ i } : opens=${ openBrackets . length } , closes=${ closeBrackets . length } , isArray=${ isArrayStart } , content="${ trimmed } "` )
568+
569+ let currentIndent = baseIndent
570+ if ( i > 0 ) {
571+ // For closing brackets of Array types, use base indent
572+ if ( closeBrackets . length > 0 && bracketStack . length > 0 && bracketStack [ bracketStack . length - 1 ] . isArray ) {
573+ currentIndent = baseIndent
574+ debugLog ( undefined , 'indent-close-array' , `Using base indent for Array closing: "${ currentIndent } "` )
575+ }
576+ // For content and other closings, use indented level
577+ else {
578+ currentIndent = baseIndent + ' ' . repeat ( bracketStack . length )
579+ debugLog ( undefined , 'indent-content' , `Using content indent at depth ${ bracketStack . length } : "${ currentIndent } "` )
580+ }
581+ }
582+
583+ // Handle opening brackets with Array context
584+ if ( openBrackets . length > 0 ) {
585+ openBrackets . forEach ( ( bracket ) => {
586+ const isArrayBracket = trimmed . startsWith ( 'Array' ) && bracket === '<'
587+ bracketStack . push ( {
588+ char : bracket ,
589+ indent : currentIndent ,
590+ isArray : isArrayBracket ,
591+ } )
592+ debugLog ( undefined , 'indent-open' , `Pushed bracket "${ bracket } " with indent "${ currentIndent } ", isArray=${ isArrayBracket } ` )
593+ } )
594+ }
595+
596+ // Handle closing brackets
597+ if ( closeBrackets . length > 0 ) {
598+ for ( let j = 0 ; j < closeBrackets . length ; j ++ ) {
599+ if ( bracketStack . length > 0 ) {
600+ bracketStack . pop ( )
601+ }
602+ }
603+ }
604+
605+ // Add union operator for non-last lines that don't end with a closing bracket
606+ if ( ! isLast && i === lines . length - 1 && ! trimmed . endsWith ( ' |' ) ) {
607+ return `${ currentIndent } ${ trimmed } |`
608+ }
609+ return `${ currentIndent } ${ trimmed } `
610+ } ) . filter ( Boolean )
611+
612+ const result = formattedLines . join ( '\n' )
613+ debugLog ( undefined , 'indent-result' , `Final multiline result:\n${ result } ` )
614+ return result
615+ }
616+
534617function inferValueType ( value : string ) : string {
535618 value = value . trim ( )
536619
@@ -571,8 +654,7 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
571654 }
572655
573656 const baseIndent = ' ' . repeat ( indentLevel )
574- const contentIndent = ' ' . repeat ( indentLevel + 1 )
575- debugLog ( state , 'infer-array-indent' , `Base indent="${ baseIndent } ", Content indent="${ contentIndent } "` )
657+ debugLog ( state , 'infer-array-indent' , `Base indent="${ baseIndent } "` )
576658
577659 const elements = splitArrayElements ( content , state )
578660 debugLog ( state , 'array-split' , `Elements after split: ${ JSON . stringify ( elements ) } ` )
@@ -626,31 +708,13 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
626708 if ( needsMultiline ) {
627709 debugLog ( state , 'multiline-start' , `Starting multiline formatting with ${ types . length } types` )
628710
629- // Format types with proper indentation
630- const formattedTypes = types . map ( ( type , index ) => {
631- const isLast = index === types . length - 1
632- const lines = type . split ( '\n' )
633-
634- if ( lines . length === 1 ) {
635- return `${ contentIndent } ${ type } ${ isLast ? '' : ' |' } `
636- }
637-
638- // For multiline types, maintain their internal structure
639- return lines . map ( ( line , i ) => {
640- const trimmed = line . trim ( )
641- if ( ! trimmed )
642- return ''
643- // Indent content one more level than current
644- return i === 0 ? `${ contentIndent } ${ trimmed } ` : `${ contentIndent } ${ trimmed } `
645- } ) . filter ( Boolean ) . join ( '\n' ) + ( isLast ? '' : ' |' )
646- } )
711+ // Instead of manual formatting, use indentMultilineType
712+ const formattedContent = types . map ( ( type , i ) => {
713+ const isLast = i === types . length - 1
714+ return indentMultilineType ( type , `${ baseIndent } ` , isLast )
715+ } ) . join ( '\n' )
647716
648- // Simple structure: opener, content, closer all aligned
649- return [
650- `${ baseIndent } Array<` ,
651- formattedTypes . join ( '\n' ) ,
652- `${ baseIndent } >` ,
653- ] . join ( '\n' )
717+ return `Array<\n${ formattedContent } \n${ baseIndent } >`
654718 }
655719
656720 return `Array<${ types . join ( ' | ' ) } >`
0 commit comments