fixed the issue that leading and trailing trivia are lost after string interpolation conversion

This commit is contained in:
Kai Lau
2024-07-18 00:39:21 -07:00
parent e4cb1bdbaa
commit db3867bdbb
3 changed files with 10 additions and 4 deletions

View File

@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
import LSPLogging
#if canImport(os)
import os
#endif

View File

@@ -56,11 +56,13 @@ struct ConvertStringConcatenationToStringInterpolation: SyntaxRefactoringProvide
[
ExprSyntax(
StringLiteralExprSyntax(
leadingTrivia: syntax.leadingTrivia,
openingPounds: commonPounds,
openingQuote: .stringQuoteToken(),
segments: segments,
closingQuote: .stringQuoteToken(),
closingPounds: commonPounds
closingPounds: commonPounds,
trailingTrivia: componentsOnly.last?.kind == .stringLiteralExpr ? syntax.trailingTrivia : nil
)
)
]

View File

@@ -1009,8 +1009,10 @@ final class CodeActionTests: XCTestCase {
func testConvertStringConcatenationToStringInterpolation() async throws {
try await assertCodeActions(
#"""
1⃣#"["# + 2⃣key + ": \(3d) " + 4⃣value + ##"]"##5️⃣
0️⃣
1⃣/*leading*/ #"["# + 2⃣key + ": \(3d) " + 4⃣value + ##"]"## /*trailing*/5
"""#,
markers: ["1", "2", "3", "4", "5"],
ranges: [("1", "2"), ("3", "4"), ("1", "5")],
exhaustive: false
) { uri, positions in
@@ -1022,9 +1024,10 @@ final class CodeActionTests: XCTestCase {
changes: [
uri: [
TextEdit(
range: positions["1️⃣"]..<positions["5"],
range: positions["0️⃣"]..<positions["5"],
newText: ###"""
##"[\##(key): \##(d) \##(value)]"##
/*leading*/ ##"[\##(key): \##(d) \##(value)]"## /*trailing*/
"""###
)
]