@@ -286,80 +286,59 @@ struct IndentationWidthRule: OptInRule, CorrectableRule {
286286 var previousLineIndentations : [ Indentation ] = [ ]
287287 var correctedLines = file. lines. map ( \. content)
288288
289- for (lineIndex, line) in file. lines. enumerated ( ) {
290- corrections += correctLine (
291- at: lineIndex,
292- line: line,
293- file: file,
294- in: & correctedLines,
295- trackingIndentations: & previousLineIndentations
296- )
297- }
289+ let conditionContinuationInfo = multilineConditionInfo ( in: file)
298290
299- if corrections > 0 {
300- let correctedContent = correctedLines. joined ( separator: " \n " )
301- file. write ( correctedContent)
302- }
291+ for line in file. lines {
292+ let indentationCharacterCount = line. content. countOfLeadingCharacters ( in: CharacterSet ( charactersIn: " \t " ) )
293+ if shouldSkipLine ( line: line, indentationCharacterCount: indentationCharacterCount, in: file) { continue }
303294
304- return corrections
305- }
295+ // Skip multiline condition continuation lines (they have specific alignment, don't auto-correct)
296+ if conditionContinuationInfo [ line . index ] != nil { continue }
306297
307- private func correctLine(
308- at lineIndex: Int ,
309- line: Line ,
310- file: SwiftLintFile ,
311- in correctedLines: inout [ String ] ,
312- trackingIndentations previousLineIndentations: inout [ Indentation ]
313- ) -> Int {
314- if ignoreCompilerDirective ( line: line, in: file) { return 0 }
315- let indentationCharacterCount = line. content. countOfLeadingCharacters ( in: CharacterSet ( charactersIn: " \t " ) )
316- if line. content. count == indentationCharacterCount { return 0 }
317- if ignoreComment ( line: line, in: file) || ignoreMultilineStrings ( line: line, in: file) { return 0 }
318-
319- let prefix = String ( line. content. prefix ( indentationCharacterCount) )
320- let tabCount = prefix. filter { $0 == " \t " } . count
321- let spaceCount = prefix. filter { $0 == " " } . count
322-
323- if tabCount != 0 , spaceCount != 0 { return 0 }
324-
325- let indentation : Indentation = tabCount != 0 ? . tabs( tabCount) : . spaces( spaceCount)
326-
327- guard previousLineIndentations. isNotEmpty else {
328- previousLineIndentations = [ indentation]
329- if indentation != . spaces( 0 ) {
330- correctedLines [ lineIndex] = String ( line. content. dropFirst ( indentationCharacterCount) )
331- return 1
332- }
333- return 0
334- }
298+ let prefix = IndentationPrefix ( line: line, length: indentationCharacterCount)
299+ let ( indentation, mixedViolation) = parseIndentation ( line: line, prefix: prefix, file: file)
335300
336- let linesValidationResult = previousLineIndentations. map {
337- validate ( indentation: indentation, comparingTo: $0)
338- }
301+ // Skip lines with mixed tabs/spaces
302+ if mixedViolation != nil { continue }
339303
340- if linesValidationResult . contains ( true ) {
341- if linesValidationResult . first == true {
304+ // Catch indented first line
305+ guard previousLineIndentations . isNotEmpty else {
342306 previousLineIndentations = [ indentation]
343- } else {
344- previousLineIndentations. append ( indentation)
307+ if indentation != . spaces( 0 ) {
308+ correctedLines [ line. index] = String ( line. content. dropFirst ( indentationCharacterCount) )
309+ corrections += 1
310+ }
311+ continue
345312 }
346- return 0
347- }
348313
349- guard let lastValidIndentation = previousLineIndentations. first else { return 0 }
314+ // Check if indentation is valid
315+ if validate ( indentation: indentation, comparingTo: previousLineIndentations [ 0 ] ) {
316+ previousLineIndentations = [ indentation]
317+ continue
318+ }
319+
320+ // Indentation is wrong, fix it
321+ let lastValidIndentation = previousLineIndentations [ 0 ]
322+ let correctIndentLevel = lastValidIndentation. spacesEquivalent ( indentationWidth: configuration. indentationWidth)
323+ let shouldUseTabs = prefix. tabCount > 0
324+ let correctIndent = generateIndentation ( spaceCount: correctIndentLevel, usesTabs: shouldUseTabs)
325+ let lineContent = String ( line. content. dropFirst ( indentationCharacterCount) )
326+ correctedLines [ line. index] = correctIndent + lineContent
350327
351- let correctIndentLevel = lastValidIndentation. spacesEquivalent ( indentationWidth: configuration. indentationWidth)
352- let shouldUseTabs = tabCount > 0
353- let correctIndent = generateIndentation ( spaceCount: correctIndentLevel, usesTabs: shouldUseTabs)
354- let lineContent = String ( line. content. dropFirst ( indentationCharacterCount) )
355- correctedLines [ lineIndex] = correctIndent + lineContent
328+ let correctedIndentation : Indentation = shouldUseTabs
329+ ? . tabs( correctIndent. filter { $0 == " \t " } . count)
330+ : . spaces( correctIndent. filter { $0 == " " } . count)
331+ previousLineIndentations = [ correctedIndentation]
356332
357- let correctedIndentation : Indentation = shouldUseTabs
358- ? . tabs( correctIndent. filter { $0 == " \t " } . count)
359- : . spaces( correctIndent. filter { $0 == " " } . count)
360- previousLineIndentations = [ correctedIndentation]
333+ corrections += 1
334+ }
361335
362- return 1
336+ if corrections > 0 {
337+ let correctedContent = correctedLines. joined ( separator: " \n " )
338+ file. write ( correctedContent)
339+ }
340+
341+ return corrections
363342 }
364343
365344 /// Generates an indentation string based on the number of spaces and whether tabs should be used.
0 commit comments