aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2024-07-07 17:50:48 +0200
committerTom Smeding <tom@tomsmeding.com>2024-07-07 17:50:48 +0200
commit37346eaed90376979255eabc82725ceea77eabb5 (patch)
treef8f402625a68decec5ed59dab047baf8ea8f5ba5
parent32455a71d45ad329a9f5d6b523e23f82e6cb73e1 (diff)
Better sorting
-rw-r--r--japanese.vim21
1 files 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('<SID>') .. "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