From c38a40d1930d0af2f0d078896fe02fd0bee5fabd Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 31 Aug 2013 15:01:02 -0700 Subject: [PATCH] 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 --- complete.cpp | 24 ++++++++---------------- complete.h | 12 ++++-------- expand.cpp | 2 +- path.cpp | 1 + reader.cpp | 15 +++++++++------ 5 files changed, 23 insertions(+), 31 deletions(-) diff --git a/complete.cpp b/complete.cpp index 8fbcb1d34..ad2bbb143 100644 --- a/complete.cpp +++ b/complete.cpp @@ -312,20 +312,6 @@ completion_t &completion_t::operator=(const completion_t &him) 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 &list) { @@ -338,11 +324,17 @@ wcstring_list_t completions_to_wcstring_list(const std::vector &li return strings; } -void sort_completions(std::vector &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 completer_t { diff --git a/complete.h b/complete.h index da94bacdc..85208d69a 100644 --- a/complete.h +++ b/complete.h @@ -127,11 +127,10 @@ public: completion_t(const wcstring &comp, const wcstring &desc = L"", string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact), int flags_val = 0); completion_t(const completion_t &); completion_t &operator=(const completion_t &); - - /* The following are needed for sorting and uniquing completions */ - bool operator < (const completion_t& rhs) const; - bool operator == (const completion_t& rhs) const; - bool operator != (const completion_t& rhs) const; + + /* Compare two completions. No operating overlaoding to make this always explicit (there's potentially multiple ways to complare completions). */ + static bool is_alphabetically_less_than(const completion_t &a, const completion_t &b); + static bool is_alphabetically_equal_to(const completion_t &a, const completion_t &b); }; enum @@ -146,9 +145,6 @@ typedef uint32_t completion_request_flags_t; /** Given a list of completions, returns a list of their completion fields */ wcstring_list_t completions_to_wcstring_list(const std::vector &completions); -/** Sorts a list of completions */ -void sort_completions(std::vector &completions); - /** Add a completion. diff --git a/expand.cpp b/expand.cpp index dc83c385d..9dfe50266 100644 --- a/expand.cpp +++ b/expand.cpp @@ -1766,7 +1766,7 @@ int expand_string(const wcstring &input, std::vector &output, expa case 1: { 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()); break; } diff --git a/path.cpp b/path.cpp index 27aadbd3e..3a0b9c83d 100644 --- a/path.cpp +++ b/path.cpp @@ -367,6 +367,7 @@ bool path_get_config(wcstring &path) return ! result.empty(); } +__attribute__((unused)) static void replace_all(wcstring &str, const wchar_t *needle, const wchar_t *replacement) { size_t needle_len = wcslen(needle); diff --git a/reader.cpp b/reader.cpp index 11b45e9d2..cd9d9e450 100644 --- a/reader.cpp +++ b/reader.cpp @@ -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. */ static void sort_and_make_unique(std::vector &l) { - sort(l.begin(), l.end()); - l.erase(std::unique(l.begin(), l.end()), l.end()); + sort(l.begin(), l.end(), completion_t::is_alphabetically_less_than); + l.erase(std::unique(l.begin(), l.end(), completion_t::is_alphabetically_equal_to), l.end()); } @@ -1642,11 +1642,14 @@ static bool reader_can_replace(const wcstring &in, int flags) /* Compare two completions, ordering completions with better match types first */ bool compare_completions_by_match_type(const completion_t &a, const completion_t &b) { - /* Compare match types */ - int match_compare = a.match.compare(b.match); - if (match_compare != 0) + /* 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) { - return match_compare < 0; + int match_compare = a.match.compare(b.match); + if (match_compare != 0) + { + return match_compare < 0; + } } /* Compare using file comparison */