Adjust prefix completions to sort alphabetically instead of by length.

Other completions are still sorted by length.
https://github.com/fish-shell/fish-shell/issues/923
This commit is contained in:
ridiculousfish 2013-08-31 15:01:02 -07:00
parent 23ba7b5bff
commit c38a40d193
5 changed files with 23 additions and 31 deletions

View file

@ -312,20 +312,6 @@ completion_t &completion_t::operator=(const completion_t &him)
return *this; return *this;
} }
bool completion_t::operator < (const completion_t& rhs) const
{
return this->completion < rhs.completion;
}
bool completion_t::operator == (const completion_t& rhs) const
{
return this->completion == rhs.completion;
}
bool completion_t::operator != (const completion_t& rhs) const
{
return !(*this == rhs);
}
wcstring_list_t completions_to_wcstring_list(const std::vector<completion_t> &list) wcstring_list_t completions_to_wcstring_list(const std::vector<completion_t> &list)
{ {
@ -338,11 +324,17 @@ wcstring_list_t completions_to_wcstring_list(const std::vector<completion_t> &li
return strings; return strings;
} }
void sort_completions(std::vector<completion_t> &completions) bool completion_t::is_alphabetically_less_than(const completion_t &a, const completion_t &b)
{ {
std::sort(completions.begin(), completions.end()); return a.completion < b.completion;
} }
bool completion_t::is_alphabetically_equal_to(const completion_t &a, const completion_t &b)
{
return a.completion == b.completion;
}
/** Class representing an attempt to compute completions */ /** Class representing an attempt to compute completions */
class completer_t class completer_t
{ {

View file

@ -128,10 +128,9 @@ public:
completion_t(const completion_t &); completion_t(const completion_t &);
completion_t &operator=(const completion_t &); completion_t &operator=(const completion_t &);
/* The following are needed for sorting and uniquing completions */ /* Compare two completions. No operating overlaoding to make this always explicit (there's potentially multiple ways to complare completions). */
bool operator < (const completion_t& rhs) const; static bool is_alphabetically_less_than(const completion_t &a, const completion_t &b);
bool operator == (const completion_t& rhs) const; static bool is_alphabetically_equal_to(const completion_t &a, const completion_t &b);
bool operator != (const completion_t& rhs) const;
}; };
enum enum
@ -146,9 +145,6 @@ typedef uint32_t completion_request_flags_t;
/** Given a list of completions, returns a list of their completion fields */ /** Given a list of completions, returns a list of their completion fields */
wcstring_list_t completions_to_wcstring_list(const std::vector<completion_t> &completions); wcstring_list_t completions_to_wcstring_list(const std::vector<completion_t> &completions);
/** Sorts a list of completions */
void sort_completions(std::vector<completion_t> &completions);
/** /**
Add a completion. Add a completion.

View file

@ -1766,7 +1766,7 @@ int expand_string(const wcstring &input, std::vector<completion_t> &output, expa
case 1: case 1:
{ {
res = EXPAND_WILDCARD_MATCH; res = EXPAND_WILDCARD_MATCH;
sort_completions(expanded); std::sort(expanded.begin(), expanded.end(), completion_t::is_alphabetically_less_than);
out->insert(out->end(), expanded.begin(), expanded.end()); out->insert(out->end(), expanded.begin(), expanded.end());
break; break;
} }

View file

@ -367,6 +367,7 @@ bool path_get_config(wcstring &path)
return ! result.empty(); return ! result.empty();
} }
__attribute__((unused))
static void replace_all(wcstring &str, const wchar_t *needle, const wchar_t *replacement) static void replace_all(wcstring &str, const wchar_t *needle, const wchar_t *replacement)
{ {
size_t needle_len = wcslen(needle); size_t needle_len = wcslen(needle);

View file

@ -802,8 +802,8 @@ bool reader_data_t::expand_abbreviation_as_necessary(size_t cursor_backtrack)
/** Sorts and remove any duplicate completions in the list. */ /** Sorts and remove any duplicate completions in the list. */
static void sort_and_make_unique(std::vector<completion_t> &l) static void sort_and_make_unique(std::vector<completion_t> &l)
{ {
sort(l.begin(), l.end()); sort(l.begin(), l.end(), completion_t::is_alphabetically_less_than);
l.erase(std::unique(l.begin(), l.end()), l.end()); l.erase(std::unique(l.begin(), l.end(), completion_t::is_alphabetically_equal_to), l.end());
} }
@ -1642,12 +1642,15 @@ static bool reader_can_replace(const wcstring &in, int flags)
/* Compare two completions, ordering completions with better match types first */ /* Compare two completions, ordering completions with better match types first */
bool compare_completions_by_match_type(const completion_t &a, const completion_t &b) bool compare_completions_by_match_type(const completion_t &a, const completion_t &b)
{ {
/* Compare match types */ /* Compare match types, unless both completions are prefix (#923) in which case we always want to compare them alphabetically */
if (a.match.type != fuzzy_match_prefix || b.match.type != fuzzy_match_prefix)
{
int match_compare = a.match.compare(b.match); int match_compare = a.match.compare(b.match);
if (match_compare != 0) if (match_compare != 0)
{ {
return match_compare < 0; return match_compare < 0;
} }
}
/* Compare using file comparison */ /* Compare using file comparison */
return wcsfilecmp(a.completion.c_str(), b.completion.c_str()) < 0; return wcsfilecmp(a.completion.c_str(), b.completion.c_str()) < 0;