Skip to content

feat: added config options to change what code editor will be opened based on the files inside #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified SourceTree.alfredworkflow
Binary file not shown.
50 changes: 49 additions & 1 deletion workflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,59 @@ extension SourceTree.SourceTreePlist {
return namePathGroups.map { (name, path) in
let alt = Workflow.AlfredItemModItem(valid: true, arg: "open \"\(path)\"", subtitle: "Reveal in Finder")
// default using `code` aka VS Code to open project
let editCli = ProcessInfo.processInfo.environment["EDITOR_CLI"] ?? "code"
let editCli = parseEditorCliConfig(with: path)
let cmd = Workflow.AlfredItemModItem(valid: true, arg: "\(editCli) \"\(path)\"", subtitle: "Open in code editor")
return Workflow.AlfredItem(title: name, subtitle: path, arg: path, mods: Workflow.AlfredMods(cmd: cmd, alt: alt))
}
}

private var defaultEditorCliConfig: String{
get {
"""
code=*
"""
}
}
private func parseEditorCliConfig(with path: String) -> String {
let editorCliConfig = ProcessInfo.processInfo.environment["EDITOR_CLI_CONFIG"] ?? defaultEditorCliConfig

let lines = editorCliConfig.components(separatedBy: .newlines)
var list = [(String, [String])]()

for row in lines {
let row = row.components(separatedBy: "=")
let cli = row[0]
let extensions = row[1]

list.append((cli, Array(extensions.components(separatedBy: ","))))
}

for (cli, extensions) in list {
guard !extensions.isEmpty else {
continue
}

if extensions[0] == "*" {
return cli
}

let fileManager = FileManager.default
let enumerator = fileManager.enumerator(atPath: path)

while let element = enumerator?.nextObject() as? String {
for ext in extensions {
if element.hasSuffix(ext) {
return cli
}
}
// only check the top level files
enumerator?.skipDescendants()
}
}

// if we didn't find anything then we just return "code"
return "code"
}
}

/**
Expand Down