mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 12:53:13 +00:00
Factor the completion prefix behavior into its own function
In a few places, we need to add a prefix to completions that replace the token. This change factors that logic into its own function prepend_token_prefix.
This commit is contained in:
parent
0ac9f159d6
commit
97f1a8fe91
4 changed files with 22 additions and 13 deletions
|
@ -326,6 +326,13 @@ bool completion_t::is_alphabetically_equal_to(const completion_t &a, const compl
|
|||
return a.completion == b.completion;
|
||||
}
|
||||
|
||||
void completion_t::prepend_token_prefix(const wcstring &prefix)
|
||||
{
|
||||
if (this->flags & COMPLETE_REPLACES_TOKEN)
|
||||
{
|
||||
this->completion.insert(0, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
/** Class representing an attempt to compute completions */
|
||||
class completer_t
|
||||
|
@ -1647,15 +1654,11 @@ void completer_t::complete_param_expand(const wcstring &str, bool do_file, bool
|
|||
debug(3, L"Error while expanding string '%ls'", sep_string.c_str());
|
||||
}
|
||||
|
||||
/* Hack hack hack. Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by inserting our separator and prefix. */
|
||||
/* Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by inserting our separator and prefix. */
|
||||
const wcstring prefix_with_sep = wcstring(str, 0, sep_index + 1);
|
||||
for (size_t i=0; i < local_completions.size(); i++)
|
||||
{
|
||||
completion_t *c = &local_completions.at(i);
|
||||
if (c->flags & COMPLETE_REPLACES_TOKEN)
|
||||
{
|
||||
c->completion.insert(0, prefix_with_sep);
|
||||
}
|
||||
local_completions.at(i).prepend_token_prefix(prefix_with_sep);
|
||||
}
|
||||
this->completions.insert(this->completions.end(),
|
||||
local_completions.begin(),
|
||||
|
|
|
@ -125,6 +125,9 @@ public:
|
|||
/* "Naturally less than" means in a natural ordering, where digits are treated as numbers. For example, foo10 is naturally greater than foo2 (but alphabetically less than it) */
|
||||
static bool is_naturally_less_than(const completion_t &a, const completion_t &b);
|
||||
static bool is_alphabetically_equal_to(const completion_t &a, const completion_t &b);
|
||||
|
||||
/* If this completion replaces the entire token, prepend a prefix. Otherwise do nothing. */
|
||||
void prepend_token_prefix(const wcstring &prefix);
|
||||
};
|
||||
|
||||
enum
|
||||
|
|
|
@ -1475,6 +1475,11 @@ static void test_expand()
|
|||
L"/tmp/fish_expand_test/bar", L"/tmp/fish_expand_test/bax", L"/tmp/fish_expand_test/bax/xxx", L"/tmp/fish_expand_test/baz", L"/tmp/fish_expand_test/baz/xxx", wnull,
|
||||
L"Glob did the wrong thing");
|
||||
|
||||
expand_test(L"/tmp/fish_expand_test/BA", FOR_COMPLETIONS,
|
||||
L"/tmp/fish_expand_test/bar", L"/tmp/fish_expand_test/bax/", L"/tmp/fish_expand_test/baz/", wnull,
|
||||
L"Case insensitive test did the wrong thing");
|
||||
|
||||
|
||||
|
||||
if (! expand_test(L"/tmp/fish_expand_test/.*", 0, L"/tmp/fish_expand_test/.foo", 0))
|
||||
{
|
||||
|
|
|
@ -826,6 +826,7 @@ void wildcard_expander_t::expand_last_segment(const wcstring &base_dir, DIR *bas
|
|||
}
|
||||
else
|
||||
{
|
||||
// Normal wildcard expansion, not for completions
|
||||
if (wildcard_match(name_str, wc, true /* skip files with leading dots */))
|
||||
{
|
||||
const wcstring abs_path = base_dir + name_str;
|
||||
|
@ -1277,13 +1278,10 @@ static int wildcard_expand(const wchar_t *wc,
|
|||
for (size_t i=c; i<out->size(); i++)
|
||||
{
|
||||
completion_t &c = out->at(i);
|
||||
|
||||
if (c.flags & COMPLETE_REPLACES_TOKEN)
|
||||
{
|
||||
// completion = base_dir + wc_base + completion
|
||||
c.completion.insert(0, wc_base);
|
||||
c.completion.insert(0, base_dir);
|
||||
}
|
||||
|
||||
// completion = base_dir + wc_base + completion
|
||||
c.prepend_token_prefix(wc_base);
|
||||
c.prepend_token_prefix(base_dir);
|
||||
}
|
||||
}
|
||||
if (expander.interrupted())
|
||||
|
|
Loading…
Reference in a new issue