@@ -298,11 +298,59 @@ extension SourceTree.SourceTreePlist {
298
298
return namePathGroups. map { ( name, path) in
299
299
let alt = Workflow . AlfredItemModItem ( valid: true , arg: " open \" \( path) \" " , subtitle: " Reveal in Finder " )
300
300
// default using `code` aka VS Code to open project
301
- let editCli = ProcessInfo . processInfo . environment [ " EDITOR_CLI " ] ?? " code "
301
+ let editCli = parseEditorCliConfig ( with : path )
302
302
let cmd = Workflow . AlfredItemModItem ( valid: true , arg: " \( editCli) \" \( path) \" " , subtitle: " Open in code editor " )
303
303
return Workflow . AlfredItem ( title: name, subtitle: path, arg: path, mods: Workflow . AlfredMods ( cmd: cmd, alt: alt) )
304
304
}
305
305
}
306
+
307
+ private var defaultEditorCliConfig : String {
308
+ get {
309
+ """
310
+ code=*
311
+ """
312
+ }
313
+ }
314
+ private func parseEditorCliConfig( with path: String ) -> String {
315
+ let editorCliConfig = ProcessInfo . processInfo. environment [ " EDITOR_CLI_CONFIG " ] ?? defaultEditorCliConfig
316
+
317
+ let lines = editorCliConfig. components ( separatedBy: . newlines)
318
+ var list = [ ( String, [ String] ) ] ( )
319
+
320
+ for row in lines {
321
+ let row = row. components ( separatedBy: " = " )
322
+ let cli = row [ 0 ]
323
+ let extensions = row [ 1 ]
324
+
325
+ list. append ( ( cli, Array ( extensions. components ( separatedBy: " , " ) ) ) )
326
+ }
327
+
328
+ for (cli, extensions) in list {
329
+ guard !extensions. isEmpty else {
330
+ continue
331
+ }
332
+
333
+ if extensions [ 0 ] == " * " {
334
+ return cli
335
+ }
336
+
337
+ let fileManager = FileManager . default
338
+ let enumerator = fileManager. enumerator ( atPath: path)
339
+
340
+ while let element = enumerator? . nextObject ( ) as? String {
341
+ for ext in extensions {
342
+ if element. hasSuffix ( ext) {
343
+ return cli
344
+ }
345
+ }
346
+ // only check the top level files
347
+ enumerator? . skipDescendants ( )
348
+ }
349
+ }
350
+
351
+ // if we didn't find anything then we just return "code"
352
+ return " code "
353
+ }
306
354
}
307
355
308
356
/**
0 commit comments