Swiftformat: Infinite formatting with a multi-line interpolated string

Created on 22 Apr 2020  路  4Comments  路  Source: nicklockwood/SwiftFormat

I have this snippet:

import HypertextLiteral

func layout(root: String, body: HTML) -> HTML {
  #"""
    <html>
      <head>
          <meta charset="utf-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          \#([16, 32, 96].map { side -> HTML in
            let size = "\(side)x\(side)"
            return #"""
            <link
              rel="icon"
              type="image/png"
              sizes="\#(size)"
              href="\#(root)/static/imgs/favicon-\#(size).png" />
            """#
            }
          )
      </head>
    </html>
  """#
}

With this config:

--indent 2
--indentcase false
--trimwhitespace always
--ranges nospace
--empty tuple
--operatorfunc nospace
--ifdef noindent
--stripunusedargs closure-only
--disable andOperator
--swiftversion 5.2

After formatting, the indentation within the interpolated bit is increased, but after another formatting run it is increased even further, here's how it looks after the first run:

  #"""
    <html>
      <head>
          <meta charset="utf-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          \#([16, 32, 96].map { side -> HTML in
              let size = "\(side)x\(side)"
              return #"""
              <link
                rel="icon"
                type="image/png"
                sizes="\#(size)"
                href="\#(root)/static/imgs/favicon-\#(size).png" />
              """#

  }
          )
      </head>
    </html>
  """#

and a second one:

  #"""
    <html>
      <head>
          <meta charset="utf-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          \#([16, 32, 96].map { side -> HTML in
                let size = "\(side)x\(side)"
                return #"""
                <link
                  rel="icon"
                  type="image/png"
                  sizes="\#(size)"
                  href="\#(root)/static/imgs/favicon-\#(size).png" />
                """#


  }
          )
      </head>
    </html>
  """#
bug fixed in develop

All 4 comments

Thanks, I'll investigate.

I've been able to narrow it down to the multiline interpolation bits (not counting for the inner multi-line string), when the interpolation is on a single line it's fine:

  #"""
    <html>
      <head>
          <meta charset="utf-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          \#([16, 32, 96].map { side -> HTML in let size = "\(side)x\(side)"; return #"""
            <link
              rel="icon"
              type="image/png"
              sizes="\#(size)"
              href="\#(root)/static/imgs/favicon-\#(size).png" />
            """# })
      </head>
    </html>
  """#

Fixed in 0.44.8

Fantastic, thank you so much @nicklockwood!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebastianfrelle picture sebastianfrelle  路  4Comments

meherkasam picture meherkasam  路  3Comments

meherkasam picture meherkasam  路  3Comments

soranoba picture soranoba  路  3Comments

dcramps picture dcramps  路  3Comments