Skip to content

Commit 74be1ee

Browse files
committed
fix line & column parameters
1 parent c87b7b9 commit 74be1ee

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

Sources/CodeEditCLI/Open.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,22 @@ extension CodeEditCLI {
1616
)
1717

1818
@Argument(
19-
help: "The path of a file/folder to open.",
19+
help: """
20+
The path of a file/folder to open.
21+
When opening files, line and column numbers can be appended: `index.html:42:10`
22+
""",
2023
completion: .file()
2124
)
2225
private var path: String?
2326

24-
@Option(name: .shortAndLong, help: "The line number to open a file at. Optional.")
25-
private var line: Int?
26-
27-
@Option(name: .shortAndLong, help: "The column to open a file at. Optional.")
28-
private var column: Int?
29-
3027
func run() throws {
3128
let task = Process()
3229

3330
// use the `open` cli as the executable
3431
task.launchPath = "/usr/bin/open"
3532

3633
if let path {
37-
34+
let (path, line, column) = try extractLineColumn(path)
3835
let openURL = try absolutePath(path, for: task)
3936

4037
// open CodeEdit using the url scheme
@@ -57,5 +54,24 @@ extension CodeEditCLI {
5754
}
5855
return url
5956
}
57+
58+
private func extractLineColumn(_ path: String) throws -> (path: String, line: Int?, column: Int?) {
59+
let components = path.split(separator: ":")
60+
let path = String(components[0])
61+
62+
switch components.count {
63+
case 1:
64+
return (path, nil, nil)
65+
case 2:
66+
guard let row = Int(components[1]) else { throw CLIError.invalidFileURL }
67+
return (path, row, nil)
68+
case (3...):
69+
guard let row = Int(components[1]),
70+
let column = Int(components[2]) else { throw CLIError.invalidFileURL }
71+
return (path, row, column)
72+
default:
73+
throw CLIError.invalidFileURL
74+
}
75+
}
6076
}
6177
}

Sources/CodeEditCLI/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct CodeEditCLI: ParsableCommand {
3030

3131
enum CLIError: Error {
3232
case invalidWorkingDirectory
33+
case invalidFileURL
3334
}
3435
}
3536

0 commit comments

Comments
 (0)