Skip to content

Commit 7e7d17c

Browse files
committed
Change the --offsets argument to take a single pair of offsets, and support passing multiple of them.
1 parent 88beb85 commit 7e7d17c

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

Sources/swift-format/Frontend/Frontend.swift

+2-10
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,11 @@ class Frontend {
125125
return
126126
}
127127

128-
var selection: Selection = .infinite
129-
if let offsets = lintFormatOptions.offsets {
130-
selection = Selection(offsetRanges: offsets)
131-
}
132128
let fileToProcess = FileToProcess(
133129
fileHandle: FileHandle.standardInput,
134130
url: URL(fileURLWithPath: lintFormatOptions.assumeFilename ?? "<stdin>"),
135131
configuration: configuration,
136-
selection: selection)
132+
selection: Selection(offsetRanges: lintFormatOptions.offsets))
137133
processFile(fileToProcess)
138134
}
139135

@@ -176,15 +172,11 @@ class Frontend {
176172
return nil
177173
}
178174

179-
var selection: Selection = .infinite
180-
if let offsets = lintFormatOptions.offsets {
181-
selection = Selection(offsetRanges: offsets)
182-
}
183175
return FileToProcess(
184176
fileHandle: sourceFile,
185177
url: url,
186178
configuration: configuration,
187-
selection: selection
179+
selection: Selection(offsetRanges: lintFormatOptions.offsets)
188180
)
189181
}
190182

Sources/swift-format/Subcommands/LintFormatOptions.swift

+12-16
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ struct LintFormatOptions: ParsableArguments {
3232
@Option(
3333
name: .long,
3434
help: """
35-
A list of comma-separated "start:end" pairs specifying UTF-8 offsets of the ranges to format.
35+
A "start:end" pair specifying UTF-8 offsets of the range to format. Multiple ranges can be
36+
formatted by specifying several --offsets arguments.
3637
""")
37-
var offsets: [Range<Int>]?
38-
38+
var offsets: [Range<Int>]
3939

4040
/// The filename for the source code when reading from standard input, to include in diagnostic
4141
/// messages.
@@ -105,7 +105,7 @@ struct LintFormatOptions: ParsableArguments {
105105
throw ValidationError("'--assume-filename' is only valid when reading from stdin")
106106
}
107107

108-
if offsets?.isEmpty == false && paths.count > 1 {
108+
if !offsets.isEmpty && paths.count > 1 {
109109
throw ValidationError("'--offsets' is only valid when processing a single file")
110110
}
111111

@@ -125,23 +125,19 @@ struct LintFormatOptions: ParsableArguments {
125125
}
126126
}
127127

128-
extension [Range<Int>] {
128+
extension Range<Int> {
129129
public init?(argument: String) {
130-
let pairs = argument.components(separatedBy: ",")
131-
let ranges: [Range<Int>] = pairs.compactMap {
132-
let pair = $0.components(separatedBy: ":")
133-
if pair.count == 2, let start = Int(pair[0]), let end = Int(pair[1]), start <= end {
134-
return start ..< end
135-
} else {
136-
return nil
137-
}
130+
let pair = argument.components(separatedBy: ":")
131+
if pair.count == 2, let start = Int(pair[0]), let end = Int(pair[1]), start <= end {
132+
self = start ..< end
133+
} else {
134+
return nil
138135
}
139-
self = ranges
140136
}
141137
}
142138

143139
#if compiler(>=6)
144-
extension [Range<Int>] : @retroactive ExpressibleByArgument {}
140+
extension Range<Int> : @retroactive ExpressibleByArgument {}
145141
#else
146-
extension [Range<Int>] : ExpressibleByArgument {}
142+
extension Range<Int> : ExpressibleByArgument {}
147143
#endif

0 commit comments

Comments
 (0)