Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/Rules/Indent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,10 @@ public extension FormatRule {
}

if formatter.options.indentStrings {
formatter.forEach(.startOfScope("\"\"\"")) { stringStartIndex, _ in
formatter.forEachToken(where: {
// Match multiline string literals (""", #""", ##""", etc.) but not multiline regex literals (#/.../#)
$0.isStartOfScope && $0.isMultilineStringDelimiter && $0.string.contains("\"")
}) { stringStartIndex, _ in
let baseIndent = formatter.currentIndentForLine(at: stringStartIndex)
let expectedIndent = baseIndent + formatter.options.indent

Expand Down
106 changes: 106 additions & 0 deletions Tests/Rules/IndentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3773,6 +3773,112 @@ final class IndentTests: XCTestCase {
testFormatting(for: input, output, rule: .indent, options: options)
}

func testIndentRawMultilineStringInMethod() {
let input = ##"""
func foo() {
let sql = #"""
SELECT *
FROM authors
WHERE authors.name LIKE '%David%'
"""#
}
"""##
let output = ##"""
func foo() {
let sql = #"""
SELECT *
FROM authors
WHERE authors.name LIKE '%David%'
"""#
}
"""##
let options = FormatOptions(indentStrings: true)
testFormatting(for: input, output, rule: .indent, options: options)
}

func testNoIndentRawMultilineStringWithOmittedReturn() {
let input = ##"""
var string: String {
#"""
SELECT *
FROM authors
WHERE authors.name LIKE '%David%'
"""#
}
"""##
let options = FormatOptions(indentStrings: true)
testFormatting(for: input, rule: .indent, options: options)
}

func testIndentRawMultilineStringAtTopLevel() {
let input = ##"""
let sql = #"""
SELECT *
FROM authors,
books
WHERE authors.name LIKE '%David%'
AND pubdate < $1
"""#
"""##
let output = ##"""
let sql = #"""
SELECT *
FROM authors,
books
WHERE authors.name LIKE '%David%'
AND pubdate < $1
"""#
"""##
let options = FormatOptions(indent: " ", indentStrings: true)
testFormatting(for: input, output, rule: .indent, options: options)
}

func testUnindentRawMultilineStringAtTopLevel() {
let input = ##"""
let sql = #"""
SELECT *
FROM authors,
books
WHERE authors.name LIKE '%David%'
AND pubdate < $1
"""#
"""##
let output = ##"""
let sql = #"""
SELECT *
FROM authors,
books
WHERE authors.name LIKE '%David%'
AND pubdate < $1
"""#
"""##
let options = FormatOptions(indent: " ", indentStrings: false)
testFormatting(for: input, output, rule: .indent, options: options)
}

func testIndentDoubleHashRawMultilineStringInMethod() {
let input = ###"""
func foo() {
let sql = ##"""
SELECT *
FROM authors
WHERE authors.name LIKE '%David%'
"""##
}
"""###
let output = ###"""
func foo() {
let sql = ##"""
SELECT *
FROM authors
WHERE authors.name LIKE '%David%'
"""##
}
"""###
let options = FormatOptions(indentStrings: true)
testFormatting(for: input, output, rule: .indent, options: options)
}

func testIndentUnderIndentedMultilineStringPreservesBlankLineIndent() {
let input = #"""
class Main {
Expand Down