Skip to content

Commit 0590f35

Browse files
authored
Merge pull request swiftlang#186 from dabelknap/verbatim-length
Correct the Verbatim Token Length Calculation
2 parents 3d9df88 + 4d2e1a7 commit 0590f35

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Sources/SwiftFormatPrettyPrint/PrettyPrint.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,17 @@ public class PrettyPrinter {
355355
lengths.append(comment.length)
356356
total += comment.length
357357

358-
case .verbatim:
359-
lengths.append(maxLineLength)
360-
total += maxLineLength
358+
case .verbatim(let verbatim):
359+
var length: Int
360+
if verbatim.lines.count > 1 {
361+
length = maxLineLength
362+
} else if verbatim.lines.count == 0 {
363+
length = 0
364+
} else {
365+
length = verbatim.lines[0].count
366+
}
367+
lengths.append(length)
368+
total += length
361369
}
362370
}
363371

Tests/SwiftFormatPrettyPrintTests/StringTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ public class StringTests: PrettyPrintTestCase {
33
let input =
44
"""
55
let a = "abc"
6+
myFun("Some string \\(a + b)")
67
let b = "A really long string that should not wrap"
78
let c = "A really long string with \\(a + b) some expressions \\(c + d)"
89
"""
910

1011
let expected =
1112
"""
1213
let a = "abc"
14+
myFun("Some string \\(a + b)")
1315
let b =
1416
"A really long string that should not wrap"
1517
let c =
1618
"A really long string with \\(a + b) some expressions \\(c + d)"
1719
1820
"""
1921

20-
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
22+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 35)
2123
}
2224
}

0 commit comments

Comments
 (0)