Files
swift-mirror/test/SourceKit/CodeFormat/indent-multiline-string.swift
Ben Barham 77c70e9fe6 [SourceKit/CodeFormat] Indent lines in multi-line strings
When the multi-line string is unterminated, the indentation of a line
will be the same as the first previous line that had contents, unless
that line is the line containing the start quotes. In that case the
indentation will be:
  - the same as the start quote indentation, if the quotes are the only
    contents on the line, or
  - an extra indentation level to the start quote line otherwise

Lines within a terminated multi-line string or lines with content will
only ever be indented if their current indentation is invalid (ie. their
indentation level is less than that of their end quotes).

This rule is to prevent any signficant (and possibly unintended)
whitespace being added to existing strings during a whole file/range
format - Xcode does not remove whitespace from whitespace-only lines by
default. This could be improved if the reformat was sent the actual
range rather than a line at a time. Different indentation could then be
chosen if the range was in fact a single line.

Resolves rdar://32181422
2020-08-13 12:35:57 +10:00

45 lines
1.5 KiB
Swift

func foo() {
let s1 = """
this is line1
this is line2
this is line3
this is a line with interpolation \(1 +
2)
"""
let s2 = """
"""
}
// RUN: %sourcekitd-test -req=format -line=2 %s >%t.response
// RUN: %sourcekitd-test -req=format -line=3 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=4 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=5 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=6 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=7 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=8 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=9 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=10 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=11 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=12 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=15 %s >>%t.response
// RUN: %FileCheck --strict-whitespace %s <%t.response
// CHECK: key.sourcetext: " let s1 = \"\"\""
// CHECK: key.sourcetext: " "
// CHECK: key.sourcetext: " this is line1"
// CHECK: key.sourcetext: " "
// CHECK: key.sourcetext: " this is line2"
// CHECK: key.sourcetext: " "
// CHECK: key.sourcetext: " "
// CHECK: key.sourcetext: " this is line3"
// CHECK: key.sourcetext: " this is a line with interpolation \\(1 +"
// CHECK: key.sourcetext: " 2)"
// CHECK: key.sourcetext: " \"\"\""
// CHECK: key.sourcetext: "\"\"\""