Skip to content

Remove folder name prefix in autocompletion suggestions - #11250

Closed
TheRealFalcon wants to merge 3 commits into
fish-shell:masterfrom
TheRealFalcon:collapse-dirs
Closed

TheRealFalcon wants to merge 3 commits into
fish-shell:masterfrom
TheRealFalcon:collapse-dirs

Conversation

@TheRealFalcon

Copy link
Copy Markdown
Contributor

Description

Remove folder name prefix in autocompletion suggestions

Fixes issue #8618

Additional info

Example behavior:
image

This didn't break any tests locally for me. I'm willing to add new tests for this behavior, but I could use some direction as to where they would need to go and what sort of setup and/or mocking would be required.

TODOs:

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.rst

Comment thread src/reader.rs Outdated
prefix.push_utfstr(&full[full.len() - PREFIX_MAX_LEN..]);
let truncated = &full[full.len() - PREFIX_MAX_LEN..];
let parts: Vec<&wstr> = truncated.split('/').collect();
if parts.len() < 2 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a good change. I'll try it for a while.
I haven't re-read the issue yet - maybe we can briefly summarize the reason for this change.

I gues zsh will only show the basename even if there is no truncation going on,
but we don't need to go there - your approach is safer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after re-reading the issue, I think we should eventually have a generic way
of making sure that the common prefix is not way larger than any of the suffixes.

But that can definitely be done later, and I expect that we'll still want to keep the special handling of "/".

Comment thread src/reader.rs
// any further
prefix.push_utfstr(&truncated);
} else {
// Discard any parent directories and include whats left

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works for absolute paths but it doesn't seem to work for ~/some/path, can we fix that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm misunderstanding you, it should already work for relative directories, but it depends on the length of the prefix. For example, here's my fish config dir:
image

But even with an absolute path, a short prefix won't collapse:
image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah my bad

Comment thread src/reader.rs Outdated
prefix.push_utfstr(&full[full.len() - PREFIX_MAX_LEN..]);
let truncated = &full[full.len() - PREFIX_MAX_LEN..];
let parts: Vec<&wstr> = truncated.split('/').collect();
if parts.len() < 2 {

@krobelus krobelus Mar 15, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parts.len() < 2 is a bit confusing because there's always at least one part.
Better make that obvious by checking parts.len() == 1.

Actually we should also get rid of the gratuitous vector, how about

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);
};

Comment thread src/reader.rs Outdated
prefix.push_utfstr(&full[full.len() - PREFIX_MAX_LEN..]);
let truncated = &full[full.len() - PREFIX_MAX_LEN..];
let parts: Vec<&wstr> = truncated.split('/').collect();
if parts.len() < 2 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should also allow = and : as separators, since we sort of do that when computing completions.
I guess let's wait until we find a use case; the LHS is not usually very large.

@krobelus krobelus added this to the fish 4.1 milestone Mar 15, 2025
Comment thread src/reader.rs
prefix.push_utfstr(&common_prefix);
} else {
// Append just the end of the string.
// Collapse parent directories and append end of string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably add to the changelog, maybe something like

- When tab completion results are truncated, the common directory name is omitted.

Comment thread src/reader.rs
prefix.push_utfstr(&common_prefix);
} else {
// Append just the end of the string.
// Collapse parent directories and append end of string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't break any tests locally for me. I'm willing to add new tests for this behavior, but I could use some direction as to where they would need to go and what sort of setup and/or mocking would be required.

We don't have mocking for src/reader.rs yet, so either add that or write
a system test, maybe in tmux-pager.fish. To run, use

cargo b && tests/test_driver.py target/debug tests/checks/tmux-pager.fish

@TheRealFalcon

Copy link
Copy Markdown
Contributor Author

I believe I have addressed all of the comments and added a new check file with some basic tests for this behavior.

@krobelus krobelus closed this in 0dbfb4c Mar 20, 2025
@omentic

omentic commented Jul 20, 2025

Copy link
Copy Markdown
Contributor

Thanks! :-)

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants