Revise ranking algorithm

This commit is contained in:
Junegunn Choi
2016-09-07 09:58:18 +09:00
parent 8ef2420677
commit 2fc7c18747
20 changed files with 961 additions and 460 deletions

View File

@@ -8,6 +8,7 @@ import (
"strings"
"unicode/utf8"
"github.com/junegunn/fzf/src/algo"
"github.com/junegunn/fzf/src/curses"
"github.com/junegunn/go-shellwords"
@@ -19,6 +20,7 @@ const usage = `usage: fzf [options]
-x, --extended Extended-search mode
(enabled by default; +x or --no-extended to disable)
-e, --exact Enable Exact-match
--algo=TYPE Fuzzy matching algorithm: [v1|v2] (default: v2)
-i Case-insensitive match (default: smart-case match)
+i Case-sensitive match
-n, --nth=N[,..] Comma-separated list of field index expressions
@@ -94,8 +96,7 @@ const (
type criterion int
const (
byMatchLen criterion = iota
byBonus
byScore criterion = iota
byLength
byBegin
byEnd
@@ -129,6 +130,7 @@ type previewOpts struct {
// Options stores the values of command-line options
type Options struct {
Fuzzy bool
FuzzyAlgo algo.Algo
Extended bool
Case Case
Nth []Range
@@ -172,6 +174,7 @@ type Options struct {
func defaultOptions() *Options {
return &Options{
Fuzzy: true,
FuzzyAlgo: algo.FuzzyMatchV2,
Extended: true,
Case: CaseSmart,
Nth: make([]Range, 0),
@@ -179,7 +182,7 @@ func defaultOptions() *Options {
Delimiter: Delimiter{},
Sort: 1000,
Tac: false,
Criteria: []criterion{byMatchLen, byBonus, byLength},
Criteria: []criterion{byScore, byLength},
Multi: false,
Ansi: false,
Mouse: true,
@@ -322,6 +325,18 @@ func isAlphabet(char uint8) bool {
return char >= 'a' && char <= 'z'
}
func parseAlgo(str string) algo.Algo {
switch str {
case "v1":
return algo.FuzzyMatchV1
case "v2":
return algo.FuzzyMatchV2
default:
errorExit("invalid algorithm (expected: v1 or v2)")
}
return algo.FuzzyMatchV2
}
func parseKeyChords(str string, message string) map[int]string {
if len(str) == 0 {
errorExit(message)
@@ -407,7 +422,7 @@ func parseKeyChords(str string, message string) map[int]string {
}
func parseTiebreak(str string) []criterion {
criteria := []criterion{byMatchLen, byBonus}
criteria := []criterion{byScore}
hasIndex := false
hasLength := false
hasBegin := false
@@ -834,6 +849,8 @@ func parseOptions(opts *Options, allArgs []string) {
case "-f", "--filter":
filter := nextString(allArgs, &i, "query string required")
opts.Filter = &filter
case "--algo":
opts.FuzzyAlgo = parseAlgo(nextString(allArgs, &i, "algorithm required (v1|v2)"))
case "--expect":
opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required")
case "--tiebreak":
@@ -962,7 +979,9 @@ func parseOptions(opts *Options, allArgs []string) {
case "--version":
opts.Version = true
default:
if match, value := optString(arg, "-q", "--query="); match {
if match, value := optString(arg, "--algo="); match {
opts.FuzzyAlgo = parseAlgo(value)
} else if match, value := optString(arg, "-q", "--query="); match {
opts.Query = value
} else if match, value := optString(arg, "-f", "--filter="); match {
opts.Filter = &value