Skip to content

Commit 43da111

Browse files
authored
lsp-completion: better ranking for case mismatch, it will be pushed to near next rank (#2234)
1 parent 46d1a7e commit 43da111

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

lsp-completion.el

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,9 @@ Others: TRIGGER-CHARS CANDIDATES"
561561
(format "\\(%s\\)" (regexp-quote (char-to-string c))))
562562
str))))
563563

564-
(defvar lsp-completion--fuz-case-sensitiveness 20
565-
"Case sensitiveness, can be in range of [0, inf).")
566-
567564
(defun lsp-completion--fuz-score (query str)
568-
"Calculate fuzzy score for STR with query QUERY."
565+
"Calculate fuzzy score for STR with query QUERY.
566+
The return is nil or in range of (0, inf)."
569567
(-when-let* ((md (cddr (or (get-text-property 0 'match-data str)
570568
(let ((re (lsp-completion--regex-fuz query)))
571569
(when (string-match re str)
@@ -610,21 +608,23 @@ Others: TRIGGER-CHARS CANDIDATES"
610608
"Update score variables given match range (A B)."
611609
(setq score-numerator (+ score-numerator (- b a)))
612610
(unless (= a len)
611+
;; case mis-match will be pushed to near next rank
612+
(unless (equal (aref query q-ind) (aref str a))
613+
(cl-incf a 0.9))
613614
(setq score-denominator
614615
(+ score-denominator
615616
(if (= a last-b) 0
616-
;; Give a higher score for match near start
617-
(+ 1 (expt (- a last-b 1) (if (eq last-b -1) 0.75 0.25))))
618-
(if (equal (aref query q-ind) (aref str a)) 0
619-
lsp-completion--fuz-case-sensitiveness))))
617+
(+ 1 (* (if (< 0 (- a last-b 1)) 1 -1)
618+
(expt (abs (- a last-b 1))
619+
;; Give a higher score for match near start
620+
(if (eq last-b -1) 0.75 0.25))))))))
620621
(setq last-b b))))
621-
(funcall update-score start start)
622622
(while md
623623
(funcall update-score start (cl-first md))
624+
;; Due to the way completion regex is constructed, `(eq end (+ start 1))`
625+
(cl-incf q-ind)
624626
(pop md)
625-
(setq start (pop md))
626-
(cl-incf q-ind))
627-
(funcall update-score len len)
627+
(setq start (pop md)))
628628
(unless (zerop len)
629629
(/ score-numerator (1+ score-denominator) 1.0))))
630630

test/lsp-completion-test.el

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,35 @@
4949
'("hash-map"
5050
"as-definition"
5151
"as-def"
52-
"aS-selection"
52+
"As-selection"
5353
"To-as-expected"
5454
"amused"
5555
"subclass-1"
5656
"superand-sort")
5757
'("as-definition" ; Prefix match
5858
"as-def" ; Also prefix match (stable)
59+
"As-selection" ; case mismatch, rank lower to near next rank
5960
"hash-map" ; middle match
6061
"amused" ; partial match with prefix match
6162
"To-as-expected" ; more in middle match
6263
"subclass-1" ; more in middle match
6364
"superand-sort" ; partial match without prefix match
64-
"aS-selection" ; case mismatch
6565
))
66+
(do-test "f"
67+
'("f" "foo" "Foo" "aFoo" "afoo")
68+
'("f" "foo" "Foo" "afoo" "aFoo"))
69+
(do-test "foo"
70+
'("foo" "afoo" "aafoo" "aaafoo" "Foo" "aFoo" "aaFoo" "aaaFoo")
71+
'("foo" "Foo" "afoo" "aFoo" "aafoo" "aaFoo" "aaafoo" "aaaFoo"))
6672
(do-test "F"
67-
'("F" "foo" "Foo")
68-
'("F" "Foo" "foo"))
73+
'("F" "foo" "Foo" "aFoo" "afoo")
74+
'("F" "Foo" "foo" "aFoo" "afoo"))
6975
(do-test "Fo"
7076
'("Fo" "daFo" "safo")
7177
'("Fo" "daFo" "safo"))
7278
(do-test "F"
7379
'("F" "daFo" "safo")
74-
'("F" "daFo" "safo"))
75-
(do-test "foo"
76-
'("foo" "afoo" "aafoo" "aaafoo")
77-
'("foo" "afoo" "aafoo" "aaafoo"))))
80+
'("F" "daFo" "safo"))))
7881

7982
(provide 'lsp-completion-test)
8083
;;; lsp-completion-test.el ends here

0 commit comments

Comments
 (0)