Skip to content
Closed
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
30 changes: 29 additions & 1 deletion Sources/SwiftRefactor/FormatRawStringLiteral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,35 @@ public struct FormatRawStringLiteral: SyntaxRefactoringProvider {
}
}

guard maximumHashes > 0 else {
var shouldRemoveHashes = maximumHashes == 0
let quote = lit.openingQuote.text

if shouldRemoveHashes {
if quote == "\"" {
// Check for the #"""..."""# case which parses as quote=""" and content wrapped in quotes.
// If we remove hashes, we get """...""", which is invalid single-line multiline syntax.
for segment in lit.segments {
if case .stringSegment(let s) = segment {
if s.content.text.hasPrefix("\"") && s.content.text.hasSuffix("\"") {
shouldRemoveHashes = false
break
}
}
}
} else if quote == "\"\"\"" {
// Check for single-line multiline strings.
let containsNewline =
lit.openingQuote.trailingTrivia.description.contains("\n")
|| lit.segments.contains(where: { $0.description.contains("\n") })
|| lit.closingQuote.leadingTrivia.description.contains("\n")

if !containsNewline {
shouldRemoveHashes = false
}
}
}

if shouldRemoveHashes {
return
lit
.with(\.openingPounds, lit.openingPounds?.with(\.tokenKind, .rawStringPoundDelimiter("")))
Expand Down
2 changes: 2 additions & 0 deletions Tests/SwiftRefactorTest/FormatRawStringLiteral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ final class FormatRawStringLiteralTest: XCTestCase {
expectation: ########" #######"###### \####(hello) ##"####### "########
),
(#line, literal: ########" #######"hello \(world) "####### "########, expectation: #" "hello \(world) " "#),
// Test for SourceKit-LSP #2465: Single-line multiline strings MUST have at least one hash.
(#line, literal: ##"#"""hello"""#"##, expectation: ##"#"""hello"""#"##),
]

for (line, literal, expectation) in tests {
Expand Down