Skip to content
Open
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
20 changes: 6 additions & 14 deletions Sources/SwiftRefactor/FormatRawStringLiteral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ import SwiftSyntax
/// ```swift
/// ##"The # of values is \(count)"##
/// ##"Hello \#(world)"##
/// "Hello World"
/// #"Hello World"#
/// ```
public struct FormatRawStringLiteral: SyntaxRefactoringProvider {
public static func refactor(syntax lit: StringLiteralExprSyntax, in context: Void) -> StringLiteralExprSyntax {
var maximumHashes = 0
for segment in lit.segments {
switch segment {
case .expressionSegment(let expr):
if let rawStringDelimiter = expr.pounds {
// Pick up any delimiters in interpolation segments \#...#(...)
maximumHashes = max(maximumHashes, rawStringDelimiter.text.longestRun(of: "#"))
}
case .expressionSegment:
// Valid string interpolations require matching the delimiter count
return lit
case .stringSegment(let string):
// Find the longest run of # characters in the content of the literal.
maximumHashes = max(maximumHashes, string.content.text.longestRun(of: "#"))
Expand All @@ -54,14 +52,8 @@ public struct FormatRawStringLiteral: SyntaxRefactoringProvider {
}
}

guard maximumHashes > 0 else {
return
lit
.with(\.openingPounds, lit.openingPounds?.with(\.tokenKind, .rawStringPoundDelimiter("")))
.with(\.closingPounds, lit.closingPounds?.with(\.tokenKind, .rawStringPoundDelimiter("")))
}

let delimiters = String(repeating: "#", count: maximumHashes + 1)
// Ensure at least one delimiter for raw string literals
let delimiters = String(repeating: "#", count: max(1, maximumHashes + 1))
return
lit
.with(\.openingPounds, lit.openingPounds?.with(\.tokenKind, .rawStringPoundDelimiter(delimiters)))
Expand Down
9 changes: 6 additions & 3 deletions Tests/SwiftRefactorTest/FormatRawStringLiteral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ final class FormatRawStringLiteralTest: XCTestCase {
func testDelimiterPlacement() throws {
let tests = [
(#line, literal: #" "Hello World" "#, expectation: #" "Hello World" "#),
(#line, literal: ##" #"Hello World" "##, expectation: #" "Hello World" "#),
(#line, literal: ##" #"Hello World"# "##, expectation: #" "Hello World" "#),
(#line, literal: ##" #"Hello World"# "##, expectation: ##" #"Hello World"# "##),
(#line, literal: #####" "####" "#####, expectation: #####" "####" "#####),
(#line, literal: #####" #"####"# "#####, expectation: ######" #####"####"##### "######),
(#line, literal: #####" #"\####(hello)"# "#####, expectation: ######" #####"\####(hello)"##### "######),
(
#line, literal: #######" #"###### \####(hello) ##"# "#######,
expectation: ########" #######"###### \####(hello) ##"####### "########
),
(#line, literal: ########" #######"hello \(world) "####### "########, expectation: #" "hello \(world) " "#),
(#line, literal: ########" #######"hello \(world) "####### "########, expectation: ##" #"hello \(world) "# "##),
(#line, literal: ###" ##"""## "###, expectation: ##" #"""# "##),
(#line, literal: ####" ###"C:\Users\swift"### "####, expectation: ##" #"C:\Users\swift"# "##),
(#line, literal: #####" ####"He said "Hi""#### "#####, expectation: ##" #"He said "Hi""# "##),
(#line, literal: #####" ###"Value: \###(count)"### "#####, expectation: ####" ###"Value: \###(count)"### "####),
]

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