From 37346eaed90376979255eabc82725ceea77eabb5 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 7 Jul 2024 17:50:48 +0200 Subject: Better sorting --- japanese.vim | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/japanese.vim b/japanese.vim index ef80960..85b84f9 100644 --- a/japanese.vim +++ b/japanese.vim @@ -193,12 +193,22 @@ function s:rd_score_hit(hit) return a:hit[1] * pow(0.85, a:hit[2] - a:hit[3]) endfunction -" Sorts in-place. -function s:rd_sort_hits(hits) - " right - left results in descending sort. +" Compare function suitable for |sort()|. +function s:rd_compare_hits(h1, h2) + " In general: right - left results in descending sort. + + " Prioritise exact matches (i.e. reading length == shared prefix length) + let d = (a:h2[2] == a:h2[3]) - (a:h1[2] == a:h1[3]) + if d != 0 | return d | endif + " We don't need exact rounding here; if the scores are <1 apart, they're " basically equal anyway. So float2nr() is enough. - call sort(a:hits, {h1, h2 -> float2nr(s:rd_score_hit(h2) - s:rd_score_hit(h1))}) + return float2nr(s:rd_score_hit(a:h2) - s:rd_score_hit(a:h1)) +endfunction + +" Sorts in-place. +function s:rd_sort_hits(hits) + call sort(a:hits, funcref(expand('') .. "rd_compare_hits")) endfunction " Returns list of |complete-items| that can replace the kanastr. @@ -206,8 +216,9 @@ function s:rd_complete_kana(kanastr) const kanacharlen = strcharlen(a:kanastr) let hits = s:rd_find_kana(a:kanastr) + " echom "hits1:" hits call s:rd_sort_hits(hits) - " echom "hits:" hits + " echom "hits2:" hits let result = [] for hit in hits -- cgit v1.2.3-70-g09d2