Skip to content

Commit

Permalink
fix(completion): handle -o {default,dirnames,plusdirs}
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno committed Dec 30, 2024
1 parent ddea1e9 commit 5812a47
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
40 changes: 27 additions & 13 deletions brush-core/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct GenerationOptions {
// Options
/// Perform rest of default completions if no completions are generated.
pub bash_default: bool,
/// Use default filename completion if no completions are generated.
/// Use default readline-style filename completion if no completions are generated.
pub default: bool,
/// Treat completions as directory names.
pub dir_names: bool,
Expand Down Expand Up @@ -335,23 +335,37 @@ impl Spec {
no_trailing_space_at_end_of_line: options.no_space,
};

if options.plus_dirs {
// Also add dir name completion.
let mut dir_candidates = get_file_completions(
shell,
context.token_to_complete,
/* must_be_dir */ true,
)
.await;
candidates.append(&mut dir_candidates);
}

// If we still haven't found any completion candidates by now, then consider whether any
// requests were made for fallbacks.
if candidates.is_empty() {
if options.bash_default {
// TODO: if we have no completions, then fall back to default "bash" completion
//
// TODO: if we have no completions, then fall back to default "bash" completions.
// It's not clear what exactly this means, though. From basic testing, it doesn't
// seem to include basic file and directory name completion.
//
tracing::debug!(target: trace_categories::COMPLETION, "UNIMPLEMENTED: complete -o bashdefault");
}
if options.default {
// TODO: if we have no completions, then fall back to default file name completion
tracing::debug!(target: trace_categories::COMPLETION, "UNIMPLEMENTED: complete -o default");
if options.default || options.dir_names {
// N.B. We approximate "default" readline completion behavior by getting file and
// dir completions.
let must_be_dir = options.dir_names;

let mut default_candidates =
get_file_completions(shell, context.token_to_complete, must_be_dir).await;
candidates.append(&mut default_candidates);
}
if options.dir_names {
// TODO: if we have no completions, then fall back to performing dir name completion
tracing::debug!(target: trace_categories::COMPLETION, "UNIMPLEMENTED: complete -o dirnames");
}
}
if options.plus_dirs {
// Also add dir name completion.
tracing::debug!(target: trace_categories::COMPLETION, "UNIMPLEMENTED: complete -o plusdirs");
}

// Sort, unless blocked by options.
Expand Down
2 changes: 1 addition & 1 deletion brush-interactive/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn postprocess_completion_candidate(
}
}
if options.no_autoquote_filenames {
tracing::debug!(target: trace_categories::COMPLETION, "don't autoquote filenames");
tracing::debug!(target: trace_categories::COMPLETION, "UNIMPLEMENTED: don't autoquote filenames");
}
if completing_end_of_line && !options.no_trailing_space_at_end_of_line {
if !options.treat_as_filenames || !candidate.ends_with(std::path::MAIN_SEPARATOR) {
Expand Down
37 changes: 37 additions & 0 deletions brush-shell/tests/cases/builtins/compgen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,40 @@ cases:
stdin: |
echo "[Take 1]"
compgen -W 'somebody something' -X '&b*' some
- name: "compgen -o dirnames"
stdin: |
echo "[Take 1]"
compgen -W 'completion' -o dirnames -- c | sort
echo "[Take 2]"
mkdir subdir
touch subfile
compgen -W 'completion' -o dirnames -- s | sort
- name: "compgen -o default"
stdin: |
echo "[Take 1]"
compgen -W 'completion' -o default -- c | sort
echo "[Take 2]"
mkdir subdir
touch subfile
compgen -W 'completion' -o default -- s | sort
- name: "compgen -o bashdefault"
stdin: |
echo "[Take 1]"
compgen -W 'completion' -o bashdefault -- c | sort
echo "[Take 2]"
mkdir subdir
touch subfile
compgen -W 'completion' -o bashdefault -- s | sort
- name: "compgen -o plusdirs"
stdin: |
echo "[Take 1]"
touch cfile
mkdir cdir
compgen -W 'completion' -o plusdirs -o default -S suffix -- c | sort
8 changes: 8 additions & 0 deletions brush-shell/tests/cases/builtins/complete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ cases:
complete -A ${action} mycmd
complete -p mycmd
done
- name: "Roundtrip: complete -o options"
stdin: |
for opt in bashdefault default dirnames filenames noquote nosort nospace plusdirs; do
echo "--- Testing option: ${opt} ------------------"
complete -o ${opt} mycmd_${opt}
complete -p mycmd_${opt}
done

0 comments on commit 5812a47

Please sign in to comment.