Skip to content

Commit b018295

Browse files
committed
chore: wip
1 parent 764b967 commit b018295

File tree

1 file changed

+22
-110
lines changed

1 file changed

+22
-110
lines changed

src/extract.ts

Lines changed: 22 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -531,90 +531,6 @@ 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-
// Initialize bracket stack with additional context
548-
interface BracketInfo {
549-
char: string
550-
indent: string
551-
isArray: boolean // Track if this is an Array type bracket
552-
}
553-
const bracketStack: BracketInfo[] = []
554-
debugLog(undefined, 'indent-stack', 'Initializing bracket stack')
555-
556-
const formattedLines = lines.map((line, i) => {
557-
const trimmed = line.trim()
558-
if (!trimmed) {
559-
debugLog(undefined, 'indent-empty', `Empty line at index ${i}`)
560-
return ''
561-
}
562-
563-
// Track Array type specifically
564-
const isArrayStart = trimmed.startsWith('Array<')
565-
const openBrackets = (trimmed.match(/[{<[]/g) || [])
566-
const closeBrackets = (trimmed.match(/[}\]>]/g) || [])
567-
568-
debugLog(undefined, 'indent-brackets', `Line ${i}: opens=${openBrackets.length}, closes=${closeBrackets.length}, isArray=${isArrayStart}, content="${trimmed}"`)
569-
570-
let currentIndent = baseIndent
571-
if (i > 0 || closeBrackets.length > 0) {
572-
if (closeBrackets.length > 0 && bracketStack.length > 0) {
573-
const lastBracket = bracketStack[bracketStack.length - 1]
574-
// For Array closing bracket, use base indent
575-
if (lastBracket.isArray) {
576-
currentIndent = baseIndent
577-
debugLog(undefined, 'indent-close-array', `Using base indent for Array closing: "${currentIndent}"`)
578-
}
579-
else {
580-
currentIndent = lastBracket.indent
581-
debugLog(undefined, 'indent-close', `Using stack indent for closing: "${currentIndent}"`)
582-
}
583-
bracketStack.pop()
584-
}
585-
else {
586-
currentIndent = baseIndent + ' '.repeat(bracketStack.length)
587-
debugLog(undefined, 'indent-content', `Using content indent at depth ${bracketStack.length}: "${currentIndent}"`)
588-
}
589-
}
590-
591-
// Handle opening brackets with Array context
592-
if (openBrackets.length > 0) {
593-
openBrackets.forEach((bracket) => {
594-
const isArrayBracket = trimmed.startsWith('Array') && bracket === '<'
595-
bracketStack.push({
596-
char: bracket,
597-
indent: currentIndent,
598-
isArray: isArrayBracket,
599-
})
600-
debugLog(undefined, 'indent-open', `Pushed bracket "${bracket}" with indent "${currentIndent}", isArray=${isArrayBracket}`)
601-
})
602-
}
603-
604-
const formattedLine = `${currentIndent}${trimmed}`
605-
debugLog(undefined, 'indent-line', `Formatted line ${i}: "${formattedLine}"`)
606-
607-
if (!isLast && i === lines.length - 1 && !formattedLine.endsWith(' |')) {
608-
return `${formattedLine} |`
609-
}
610-
return formattedLine
611-
}).filter(Boolean)
612-
613-
const result = formattedLines.join('\n')
614-
debugLog(undefined, 'indent-result', `Final multiline result:\n${result}`)
615-
return result
616-
}
617-
618534
function inferValueType(value: string): string {
619535
value = value.trim()
620536

@@ -655,8 +571,8 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
655571
}
656572

657573
const baseIndent = ' '.repeat(indentLevel)
658-
const elementIndent = ' '.repeat(indentLevel + 1)
659-
debugLog(state, 'infer-array-indent', `Base indent="${baseIndent}", Element indent="${elementIndent}"`)
574+
const contentIndent = ' '.repeat(indentLevel + 1)
575+
debugLog(state, 'infer-array-indent', `Base indent="${baseIndent}", Content indent="${contentIndent}"`)
660576

661577
const elements = splitArrayElements(content, state)
662578
debugLog(state, 'array-split', `Elements after split: ${JSON.stringify(elements)}`)
@@ -705,43 +621,39 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
705621
const needsMultiline = types.some(type =>
706622
type.includes('\n') || type.includes('{') || type.length > 40 || types.join(' | ').length > 60,
707623
)
708-
debugLog(state, 'multiline-check', `Needs multiline: ${needsMultiline}, Reason: ${
709-
types.find(type => type.includes('\n'))
710-
? 'contains newline'
711-
: types.find(type => type.includes('{'))
712-
? 'contains object'
713-
: types.find(type => type.length > 40)
714-
? 'type too long'
715-
: types.join(' | ').length > 60 ? 'combined types too long' : 'none'
716-
}`)
624+
debugLog(state, 'multiline-check', `Needs multiline: ${needsMultiline}`)
717625

718626
if (needsMultiline) {
719627
debugLog(state, 'multiline-start', `Starting multiline formatting with ${types.length} types`)
720628

721-
// Construct parts separately for better control
629+
// Format types with proper indentation
722630
const formattedTypes = types.map((type, index) => {
723631
const isLast = index === types.length - 1
724-
debugLog(state, 'type-formatting', `Formatting type ${index}, isLast: ${isLast}`)
725-
const formatted = indentMultilineType(type, elementIndent, isLast)
726-
debugLog(state, 'type-formatted', `Type ${index} formatted result:\n${formatted}`)
727-
return formatted
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 ? '' : ' |')
728646
})
729647

730-
// Build multiline array with controlled indentation
731-
const typeContent = formattedTypes.join('\n')
732-
const result = [
648+
// Simple structure: opener, content, closer all aligned
649+
return [
733650
`${baseIndent}Array<`,
734-
typeContent,
651+
formattedTypes.join('\n'),
735652
`${baseIndent}>`,
736653
].join('\n')
737-
738-
debugLog(state, 'multiline-result', `Final multiline result:\n${result}`)
739-
return result
740654
}
741655

742-
const singleLineResult = `Array<${types.join(' | ')}>`
743-
debugLog(state, 'single-line-array', `Single-line array type: ${singleLineResult}`)
744-
return singleLineResult
656+
return `Array<${types.join(' | ')}>`
745657
}
746658

747659
/**

0 commit comments

Comments
 (0)