@@ -16,25 +16,22 @@ extension CodeEditCLI {
16
16
)
17
17
18
18
@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
+ """ ,
20
23
completion: . file( )
21
24
)
22
25
private var path : String ?
23
26
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
-
30
27
func run( ) throws {
31
28
let task = Process ( )
32
29
33
30
// use the `open` cli as the executable
34
31
task. launchPath = " /usr/bin/open "
35
32
36
33
if let path {
37
-
34
+ let ( path , line , column ) = try extractLineColumn ( path )
38
35
let openURL = try absolutePath ( path, for: task)
39
36
40
37
// open CodeEdit using the url scheme
@@ -57,5 +54,35 @@ extension CodeEditCLI {
57
54
}
58
55
return url
59
56
}
57
+
58
+ private func extractLineColumn( _ path: String ) throws -> ( path: String , line: Int ? , column: Int ? ) {
59
+
60
+ // split the string at `:` to get line and column numbers
61
+ let components = path. split ( separator: " : " )
62
+
63
+ // set path to only the first component
64
+ guard let first = components. first else {
65
+ throw CLIError . invalidFileURL
66
+ }
67
+ let path = String ( first)
68
+
69
+ // switch on the number of components
70
+ switch components. count {
71
+ case 1 : // no line or column number provided
72
+ return ( path, nil , nil )
73
+
74
+ case 2 : // only line number provided
75
+ guard let row = Int ( components [ 1 ] ) else { throw CLIError . invalidFileURL }
76
+ return ( path, row, nil )
77
+
78
+ case 3 : // line and column number provided
79
+ guard let row = Int ( components [ 1 ] ) ,
80
+ let column = Int ( components [ 2 ] ) else { throw CLIError . invalidFileURL }
81
+ return ( path, row, column)
82
+
83
+ default : // any other case throw an error since this is invalid
84
+ throw CLIError . invalidFileURL
85
+ }
86
+ }
60
87
}
61
88
}
0 commit comments