Skip to content

Commit c82d930

Browse files
authored
refactor: unify load_editor_adapter and load_ai_adapter (#116)
Extract shared logic into _load_adapter() parameterized by type. load_editor_adapter and load_ai_adapter become thin wrappers passing type-specific strings. No public API change.
1 parent 9f3f395 commit c82d930

1 file changed

Lines changed: 26 additions & 35 deletions

File tree

bin/gtr

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,9 +1818,11 @@ resolve_workspace_file() {
18181818
}
18191819

18201820
# Load editor adapter
1821-
load_editor_adapter() {
1822-
local editor="$1"
1823-
local adapter_file="$GTR_DIR/adapters/editor/${editor}.sh"
1821+
# Load an adapter by type (shared implementation for editor and AI adapters)
1822+
# Usage: _load_adapter <type> <name> <label> <builtin_list> <path_hint>
1823+
_load_adapter() {
1824+
local type="$1" name="$2" label="$3" builtin_list="$4" path_hint="$5"
1825+
local adapter_file="$GTR_DIR/adapters/${type}/${name}.sh"
18241826

18251827
# Try loading explicit adapter first (allows special handling)
18261828
if [ -f "$adapter_file" ]; then
@@ -1830,47 +1832,36 @@ load_editor_adapter() {
18301832

18311833
# Generic fallback: check if command exists in PATH
18321834
# Extract first word (command name) from potentially multi-word string
1833-
local cmd_name="${editor%% *}"
1835+
local cmd_name="${name%% *}"
18341836

18351837
if ! command -v "$cmd_name" >/dev/null 2>&1; then
1836-
log_error "Editor '$editor' not found"
1837-
log_info "Built-in adapters: cursor, vscode, zed, idea, pycharm, webstorm, vim, nvim, emacs, sublime, nano, atom"
1838-
log_info "Or use any editor command available in your PATH (e.g., code-insiders, fleet)"
1838+
log_error "$label '$name' not found"
1839+
log_info "Built-in adapters: $builtin_list"
1840+
log_info "Or use any $label command available in your PATH (e.g., $path_hint)"
18391841
exit 1
18401842
fi
18411843

18421844
# Set globals for generic adapter functions
1843-
# Note: $editor may contain arguments (e.g., "code --wait")
1844-
GTR_EDITOR_CMD="$editor"
1845-
GTR_EDITOR_CMD_NAME="$cmd_name"
1846-
}
1847-
1848-
# Load AI adapter
1849-
load_ai_adapter() {
1850-
local ai_tool="$1"
1851-
local adapter_file="$GTR_DIR/adapters/ai/${ai_tool}.sh"
1852-
1853-
# Try loading explicit adapter first (allows special handling)
1854-
if [ -f "$adapter_file" ]; then
1855-
. "$adapter_file"
1856-
return 0
1845+
# Note: $name may contain arguments (e.g., "code --wait", "bunx @github/copilot@latest")
1846+
if [ "$type" = "editor" ]; then
1847+
GTR_EDITOR_CMD="$name"
1848+
GTR_EDITOR_CMD_NAME="$cmd_name"
1849+
else
1850+
GTR_AI_CMD="$name"
1851+
GTR_AI_CMD_NAME="$cmd_name"
18571852
fi
1853+
}
18581854

1859-
# Generic fallback: check if command exists in PATH
1860-
# Extract first word (command name) from potentially multi-word string
1861-
local cmd_name="${ai_tool%% *}"
1862-
1863-
if ! command -v "$cmd_name" >/dev/null 2>&1; then
1864-
log_error "AI tool '$ai_tool' not found"
1865-
log_info "Built-in adapters: aider, auggie, claude, codex, continue, copilot, cursor, gemini, opencode"
1866-
log_info "Or use any AI tool command available in your PATH (e.g., bunx, gpt)"
1867-
exit 1
1868-
fi
1855+
load_editor_adapter() {
1856+
_load_adapter "editor" "$1" "Editor" \
1857+
"cursor, vscode, zed, idea, pycharm, webstorm, vim, nvim, emacs, sublime, nano, atom" \
1858+
"code-insiders, fleet"
1859+
}
18691860

1870-
# Set globals for generic adapter functions
1871-
# Note: $ai_tool may contain arguments (e.g., "bunx @github/copilot@latest")
1872-
GTR_AI_CMD="$ai_tool"
1873-
GTR_AI_CMD_NAME="$cmd_name"
1861+
load_ai_adapter() {
1862+
_load_adapter "ai" "$1" "AI tool" \
1863+
"aider, auggie, claude, codex, continue, copilot, cursor, gemini, opencode" \
1864+
"bunx, gpt"
18741865
}
18751866

18761867
# Help command

0 commit comments

Comments
 (0)