Skip to content

Commit 9615f58

Browse files
committed
Improvements to integer literal utilities used in SourceKit-LSP
SourceKit-LSP's refactoring action has minor extensions for the integer literal utilities. Sink them down here for more general use: * IntegerLiteralExprSyntax.Radix becomes CaseIterable * IntegerLiteralExprSyntax.Radix gains a "literalPrefix" property to get the string needed to prefix a literal with this radix.
1 parent 6c459f2 commit 9615f58

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

Sources/SwiftRefactor/IntegerLiteralUtilities.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SwiftSyntax
1717
#endif
1818

1919
extension IntegerLiteralExprSyntax {
20-
public enum Radix {
20+
public enum Radix: CaseIterable {
2121
case binary
2222
case octal
2323
case decimal
@@ -31,6 +31,17 @@ extension IntegerLiteralExprSyntax {
3131
case .hex: return 16
3232
}
3333
}
34+
35+
/// The prefix that is used to express an integer literal with this
36+
/// radix in Swift source code, e.g., "0x" for hexadecimal.
37+
public var literalPrefix: String {
38+
switch self {
39+
case .binary: "0b"
40+
case .octal: "0o"
41+
case .hex: "0x"
42+
case .decimal: ""
43+
}
44+
}
3445
}
3546

3647
public var radix: Radix {
@@ -67,15 +78,8 @@ extension IntegerLiteralExprSyntax {
6778
/// ```
6879
public func split() -> (prefix: String, value: Substring) {
6980
let text = self.literal.text
70-
switch self.radix {
71-
case .binary:
72-
return ("0b", text.dropFirst(2))
73-
case .octal:
74-
return ("0o", text.dropFirst(2))
75-
case .decimal:
76-
return ("", Substring(text))
77-
case .hex:
78-
return ("0x", text.dropFirst(2))
79-
}
81+
let radix = self.radix
82+
let literalPrefix = radix.literalPrefix
83+
return (literalPrefix, text.dropFirst(literalPrefix.count))
8084
}
8185
}

0 commit comments

Comments
 (0)