@@ -64,6 +64,56 @@ export function replaceIndentAfterLastNewline(ws: string, newIndent: string): st
6464 return ws . substring ( 0 , lastNewline + 1 ) + newIndent ;
6565}
6666
67+ /**
68+ * Checks if a Space contains any newlines (in whitespace or comment suffixes).
69+ */
70+ export function spaceContainsNewline ( space : J . Space | undefined ) : boolean {
71+ if ( ! space ) return false ;
72+ if ( space . whitespace . includes ( "\n" ) ) return true ;
73+ return space . comments . some ( c => c . suffix . includes ( "\n" ) ) ;
74+ }
75+
76+ /**
77+ * Normalizes indentation in an entire Space, including both whitespace and comment suffixes.
78+ * Each newline followed by whitespace gets its indentation normalized to the target indent.
79+ *
80+ * @param space The Space to normalize
81+ * @param targetIndent The indentation to use after newlines
82+ * @returns The normalized Space, or the original if unchanged
83+ */
84+ export function normalizeSpaceIndent ( space : J . Space , targetIndent : string ) : J . Space {
85+ let changed = false ;
86+
87+ // Normalize whitespace
88+ let newWhitespace = space . whitespace ;
89+ if ( space . whitespace . includes ( "\n" ) ) {
90+ newWhitespace = replaceIndentAfterLastNewline ( space . whitespace , targetIndent ) ;
91+ changed = changed || newWhitespace !== space . whitespace ;
92+ }
93+
94+ // Normalize comment suffixes
95+ const newComments = space . comments . map ( comment => {
96+ if ( comment . suffix . includes ( "\n" ) ) {
97+ const newSuffix = replaceIndentAfterLastNewline ( comment . suffix , targetIndent ) ;
98+ if ( newSuffix !== comment . suffix ) {
99+ changed = true ;
100+ return { ...comment , suffix : newSuffix } ;
101+ }
102+ }
103+ return comment ;
104+ } ) ;
105+
106+ if ( ! changed ) {
107+ return space ;
108+ }
109+
110+ return {
111+ ...space ,
112+ whitespace : newWhitespace ,
113+ comments : newComments
114+ } ;
115+ }
116+
67117/**
68118 * Handles element removal from lists while preserving LST formatting.
69119 * Automatically applies prefixes from removed elements to the next kept element,
0 commit comments