Skip to content

Commit db3867b

Browse files
committed
fixed the issue that leading and trailing trivia are lost after string interpolation conversion
1 parent e4cb1bd commit db3867b

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

Sources/SKSupport/LineTable.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import LSPLogging
14+
1415
#if canImport(os)
1516
import os
1617
#endif

Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ struct ConvertStringConcatenationToStringInterpolation: SyntaxRefactoringProvide
5656
[
5757
ExprSyntax(
5858
StringLiteralExprSyntax(
59+
leadingTrivia: syntax.leadingTrivia,
5960
openingPounds: commonPounds,
6061
openingQuote: .stringQuoteToken(),
6162
segments: segments,
6263
closingQuote: .stringQuoteToken(),
63-
closingPounds: commonPounds
64+
closingPounds: commonPounds,
65+
trailingTrivia: componentsOnly.last?.kind == .stringLiteralExpr ? syntax.trailingTrivia : nil
6466
)
6567
)
6668
]

Tests/SourceKitLSPTests/CodeActionTests.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,10 @@ final class CodeActionTests: XCTestCase {
10091009
func testConvertStringConcatenationToStringInterpolation() async throws {
10101010
try await assertCodeActions(
10111011
#"""
1012-
1️⃣#"["# + 2️⃣key + ": \(3️⃣d) " + 4️⃣value + ##"]"##5️⃣
1012+
0️⃣
1013+
1️⃣/*leading*/ #"["# + 2️⃣key + ": \(3️⃣d) " + 4️⃣value + ##"]"## /*trailing*/5️⃣
10131014
"""#,
1015+
markers: ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣"],
10141016
ranges: [("1️⃣", "2️⃣"), ("3️⃣", "4️⃣"), ("1️⃣", "5️⃣")],
10151017
exhaustive: false
10161018
) { uri, positions in
@@ -1022,9 +1024,10 @@ final class CodeActionTests: XCTestCase {
10221024
changes: [
10231025
uri: [
10241026
TextEdit(
1025-
range: positions["1️⃣"]..<positions["5️⃣"],
1027+
range: positions["0️⃣"]..<positions["5️⃣"],
10261028
newText: ###"""
1027-
##"[\##(key): \##(d) \##(value)]"##
1029+
1030+
/*leading*/ ##"[\##(key): \##(d) \##(value)]"## /*trailing*/
10281031
"""###
10291032
)
10301033
]

0 commit comments

Comments
 (0)