From 9615f58bda45e94cd4d4f1051c867a75083d13ad Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Thu, 25 Apr 2024 10:39:17 -0700 Subject: [PATCH 1/3] 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. --- .../IntegerLiteralUtilities.swift | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Sources/SwiftRefactor/IntegerLiteralUtilities.swift b/Sources/SwiftRefactor/IntegerLiteralUtilities.swift index 187d800f807..7f0e948a9f3 100644 --- a/Sources/SwiftRefactor/IntegerLiteralUtilities.swift +++ b/Sources/SwiftRefactor/IntegerLiteralUtilities.swift @@ -17,7 +17,7 @@ import SwiftSyntax #endif extension IntegerLiteralExprSyntax { - public enum Radix { + public enum Radix: CaseIterable { case binary case octal case decimal @@ -31,6 +31,17 @@ extension IntegerLiteralExprSyntax { case .hex: return 16 } } + + /// The prefix that is used to express an integer literal with this + /// radix in Swift source code, e.g., "0x" for hexadecimal. + public var literalPrefix: String { + switch self { + case .binary: "0b" + case .octal: "0o" + case .hex: "0x" + case .decimal: "" + } + } } public var radix: Radix { @@ -67,15 +78,8 @@ extension IntegerLiteralExprSyntax { /// ``` public func split() -> (prefix: String, value: Substring) { let text = self.literal.text - switch self.radix { - case .binary: - return ("0b", text.dropFirst(2)) - case .octal: - return ("0o", text.dropFirst(2)) - case .decimal: - return ("", Substring(text)) - case .hex: - return ("0x", text.dropFirst(2)) - } + let radix = self.radix + let literalPrefix = radix.literalPrefix + return (literalPrefix, text.dropFirst(literalPrefix.count)) } } From 7c13741422808e33a99139d07d09249d14af31e9 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Thu, 25 Apr 2024 11:38:16 -0700 Subject: [PATCH 2/3] Add 'return' --- Sources/SwiftRefactor/IntegerLiteralUtilities.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftRefactor/IntegerLiteralUtilities.swift b/Sources/SwiftRefactor/IntegerLiteralUtilities.swift index 7f0e948a9f3..35d5ef15be7 100644 --- a/Sources/SwiftRefactor/IntegerLiteralUtilities.swift +++ b/Sources/SwiftRefactor/IntegerLiteralUtilities.swift @@ -36,10 +36,10 @@ extension IntegerLiteralExprSyntax { /// radix in Swift source code, e.g., "0x" for hexadecimal. public var literalPrefix: String { switch self { - case .binary: "0b" - case .octal: "0o" - case .hex: "0x" - case .decimal: "" + case .binary: return "0b" + case .octal: return "0o" + case .hex: return "0x" + case .decimal: return "" } } } From 13f747deeb34c78fb1dc7fe2cd8948705ced58dc Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Thu, 25 Apr 2024 19:12:09 -0700 Subject: [PATCH 3/3] Drop CaseIterable --- Sources/SwiftRefactor/IntegerLiteralUtilities.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftRefactor/IntegerLiteralUtilities.swift b/Sources/SwiftRefactor/IntegerLiteralUtilities.swift index 35d5ef15be7..3b668fcf85b 100644 --- a/Sources/SwiftRefactor/IntegerLiteralUtilities.swift +++ b/Sources/SwiftRefactor/IntegerLiteralUtilities.swift @@ -17,7 +17,7 @@ import SwiftSyntax #endif extension IntegerLiteralExprSyntax { - public enum Radix: CaseIterable { + public enum Radix { case binary case octal case decimal