Skip to content

Commit 228ce15

Browse files
authored
Merge pull request #4 from CollinHemeltjen/main
feat: added config options to change what code editor will be opened based on the files inside
2 parents 7094091 + 58b321d commit 228ce15

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

SourceTree.alfredworkflow

4.15 KB
Binary file not shown.

workflow.swift

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,59 @@ extension SourceTree.SourceTreePlist {
298298
return namePathGroups.map { (name, path) in
299299
let alt = Workflow.AlfredItemModItem(valid: true, arg: "open \"\(path)\"", subtitle: "Reveal in Finder")
300300
// default using `code` aka VS Code to open project
301-
let editCli = ProcessInfo.processInfo.environment["EDITOR_CLI"] ?? "code"
301+
let editCli = parseEditorCliConfig(with: path)
302302
let cmd = Workflow.AlfredItemModItem(valid: true, arg: "\(editCli) \"\(path)\"", subtitle: "Open in code editor")
303303
return Workflow.AlfredItem(title: name, subtitle: path, arg: path, mods: Workflow.AlfredMods(cmd: cmd, alt: alt))
304304
}
305305
}
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+
}
306354
}
307355

308356
/**

0 commit comments

Comments
 (0)