From d6ad5b6d3c5102dc7837bae1ae8f5528fa7b68b6 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 10 Sep 2015 06:10:17 -0700 Subject: linenoise: sort completions by length. Give the shorter completions first, and lexicographically within each length equivalence class. This effectively gives the user a breadth-first search through the trie of possible suffixes. * linenoise/linenoise.c (compare_completions): New static function. (complete_line): Apply qsort to the collected vector of strings. Easy does it. --- linenoise/linenoise.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'linenoise') diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c index 0f220512..32f427aa 100644 --- a/linenoise/linenoise.c +++ b/linenoise/linenoise.c @@ -274,6 +274,22 @@ static void free_completions(lino_completions_t *lc) { static void sync_data_to_buf(lino_t *l); static void refresh_line(lino_t *l); +static int compare_completions(const void *larg, const void *rarg) +{ + const char **lelem = (const char **) larg; + const char **relem = (const char **) rarg; + const char *lstr = *lelem, *rstr = *relem; + size_t llen = strlen(lstr); + size_t rlen = strlen(rstr); + + if (llen < rlen) + return -1; + if (llen > rlen) + return 1; + + return strcmp(lstr, rstr); +} + /* This is an helper function for edit() and is called when the * user types the key in order to complete the string currently in the * input. @@ -286,6 +302,9 @@ static int complete_line(lino_t *ls) { char c = 0; ls->completion_callback(ls->data, &lc, ls->cb_ctx); + + qsort(lc.cvec, lc.len, sizeof *lc.cvec, compare_completions); + if (lc.len == 0) { generate_beep(ls); } else { -- cgit v1.2.3