nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 640 forks source link

In a triple-quoted string containing contiguous blank lines, an interpolated value causes loss of blank lines #1078

Closed rudifa closed 2 years ago

rudifa commented 2 years ago

That is, multiple blank lines following a \(someExpression) get squashed into a single blank line.

Example: when you swiftformat a file containing this construct ...

    var interpolated = "XXX"
    var shortSample2: String {
        return """
        AAA
        \(interpolated)

        BBB

        """
    }

... the 4 blank lines following \(interpolated) get squashed into a single blank line:

    var shortSample2: String {
        return """
        AAA
        \(interpolated)

        BBB

        """
    }

This is so with the current release 0.48.18, as well as with the 0.49.0 from the develop branch.

The attached .zip file contains SampleMultilineStrings.swift and run-test.sh to aid in the investigation.

The SampleMultilineStrings.swift contains several sample strings similar to shortSample2 above. When run as a swift script it prints the line counts for the sample strings:

investigate-squashed-blank-lines % cp SampleMultilineStrings.swift formatted-SampleMultilineStrings.swift                                                          

investigate-squashed-blank-lines % swiftformat formatted-SampleMultilineStrings.swift
Running SwiftFormat...
warning: No Swift version was specified, so some formatting features were disabled. Specify the version of Swift you are using with the --swiftversion option, or by adding a .swift-version file to your project.
SwiftFormat completed in 0.1s.
1/1 files formatted.

investigate-squashed-blank-lines % ./formatted-SampleMultilineStrings.swift

running formatted-SampleMultilineStrings.swift...
realLifeSample1 OK
realLifeSample2 FAILED: expected 32 lines, actual 26
shortSample1 OK
shortSample2 FAILED: expected 10 lines, actual 7
shortSample3 FAILED: expected 10 lines, actual 9
shortSample4 OK
shortSample5 FAILED: expected 10 lines, actual 9
shortSample6 OK

The script run-test.sh mechanizes above sequence, for repetitive tests.

investigate-squashed-blank-lines.zip

rudifa commented 2 years ago

I submitted a pull request containing a fix for this problem, Develop rudifa #1079

nicklockwood commented 2 years ago

@rudifa landed in 0.49.0