reader handle_completions(): also truncate common prefix when replacing

I don't know why we don't apply the common-prefix truncation logic
when all completions are replacing.
Let's do that.
This commit is contained in:
Johannes Altmanninger
2026-01-02 13:52:45 +01:00
parent 2f6b1eaaf9
commit e79b00d9d1
2 changed files with 35 additions and 24 deletions

View File

@@ -6791,31 +6791,35 @@ fn best<T: Borrow<Completion>>(
}
// Print the completion list.
let mut prefix = WString::new();
if will_replace_token {
if use_prefix {
prefix.push_utfstr(&common_prefix);
}
} else if tok.len() + common_prefix.len() <= PREFIX_MAX_LEN {
prefix.push_utfstr(&tok);
prefix.push_utfstr(&common_prefix);
let prefix = if will_replace_token && !use_prefix {
Cow::Borrowed(L!(""))
} else {
// Collapse parent directories and append end of string
prefix.push(get_ellipsis_char());
let full = tok + common_prefix;
let truncated = &full[full.len() - PREFIX_MAX_LEN..];
let (i, last_component) = truncated.split('/').enumerate().last().unwrap();
if i == 0 {
// No path separators were found in the common prefix, so we can't collapse
// any further
prefix.push_utfstr(&truncated);
let mut prefix = WString::new();
let full = if will_replace_token {
common_prefix.to_owned()
} else {
// Discard any parent directories and include whats left
prefix.push('/');
prefix.push_utfstr(last_component);
tok + common_prefix
};
}
if full.len() <= PREFIX_MAX_LEN {
prefix = full;
} else {
// Collapse parent directories and append end of string
prefix.push(get_ellipsis_char());
let truncated = &full[full.len() - PREFIX_MAX_LEN..];
let (i, last_component) = truncated.split('/').enumerate().last().unwrap();
if i == 0 {
// No path separators were found in the common prefix, so we can't collapse
// any further
prefix.push_utfstr(&truncated);
} else {
// Discard any parent directories and include whats left
prefix.push('/');
prefix.push_utfstr(last_component);
};
}
Cow::Owned(prefix)
};
if use_prefix {
let common_prefix_len = common_prefix.len();
@@ -6831,7 +6835,7 @@ fn best<T: Borrow<Completion>>(
}
// Update the pager data.
self.pager.set_prefix(Cow::Owned(prefix), true);
self.pager.set_prefix(prefix, true);
self.pager.set_completions(&surviving_completions, true);
// Modify the command line to reflect the new pager.
self.pager_selection_changed();

View File

@@ -35,7 +35,7 @@ isolated-tmux send-keys 'echo $FISH_TEST_v' Tab
tmux-sleep
isolated-tmux capture-pane -p
# CHECK: prompt 1> echo $FISH_TEST_VAR_
# CHECK: $FISH_TEST_VAR_1 (Variable: /) $FISH_TEST_VAR_2 (Variable: /)
# CHECK: TEST_VAR_1 (Variable: /) TEST_VAR_2 (Variable: /)
mkdir -p clang/include
touch clang/INSTALL.txt
@@ -51,3 +51,10 @@ tmux-sleep
isolated-tmux capture-pane -p
# CHECK: prompt 1> : clang/include
# CHECK: clang/INSTALL.txt …/include/ …/include-2/
isolated-tmux send-keys C-u 'complete truncate_custom_compl -a "Foo/bar/baz/{111,222,333}"' \
Enter C-l 'truncate_custom_compl foo' Tab
tmux-sleep
isolated-tmux capture-pane -p
# CHECK: prompt 2> truncate_custom_compl Foo/bar/baz/
# CHECK: …/111 …/222 …/333