import type { TLine } from 'syntx' ; export const collapseLines = ( lines , collapsedMetas ) : ( TLine | null ) [ ] => { const resultLines = [ ] ; let i = 0 ; while ( i < lines . length ) { const line = lines [ i ] ; if ( ! collapsedMetas . includes ( line . meta ) ) { resultLines . push ( line ) ; i ++ ; continue ; } const lineMeta = line . meta ; resultLines . push ( line ) ; i ++ ; let nextSameMetaIndex = 0 ; let endOfImmediateLinesIndex = 0 ; let k = i ; while ( k < lines . length && lineMeta === lines [ k ] . meta ) { k ++ ; } endOfImmediateLinesIndex = k ; while ( k < lines . length ) { if ( lineMeta === lines [ k ] . meta ) { nextSameMetaIndex = k ; break ; } k ++ ; } if ( nextSameMetaIndex > 0 ) { while ( i < endOfImmediateLinesIndex ) { resultLines . push ( lines [ i ] ) ; i ++ ; } while ( i <= nextSameMetaIndex ) { resultLines . push ( null ) ; i ++ ; } } else { while ( i < endOfImmediateLinesIndex ) { resultLines . push ( null ) ; i ++ ; } } } return resultLines ; } ;