Add --scheme=[default|path|history] option to choose scoring scheme
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #2909 Close #2930
authored by Junegunn Choi · committed by Junegunn Choi · parent 4bef330 · Browse files · 9 files·+106 added−36 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+106 added−36 removed
- other:+106 added−36 removed
Provenance
- Commit
6fb41a2- Tree
5c4d900- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
4bef330
9 files changed+106 added−36 removed
modified:man/man1/fzf-tmux.1+1 added-1 removed
19 unmodified lines20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22 THE SOFTWARE.23 ..24-.TH fzf-tmux 1 "Aug 2022" "fzf 0.32.1" "fzf-tmux - open fzf in tmux split pane"24+.TH fzf-tmux 1 "Aug 2022" "fzf 0.33.0" "fzf-tmux - open fzf in tmux split pane"25 26 .SH NAME27 fzf-tmux - open fzf in tmux split pane28 40 unmodified linesmodified:man/man1/fzf.1+13 added-1 removed
19 unmodified lines20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22 THE SOFTWARE.23 ..24-.TH fzf 1 "Aug 2022" "fzf 0.32.2" "fzf - a command-line fuzzy finder"24+.TH fzf 1 "Aug 2022" "fzf 0.33.0" "fzf - a command-line fuzzy finder"25 26 .SH NAME27 fzf - a command-line fuzzy finder28 21 unmodified lines50 .TP51 .B "--literal"52 Do not normalize latin script letters for matching.53 .TP54+.BI "--scheme=" SCHEME55+Choose scoring scheme tailored for different types of input.56+57+.br58+.BR default " Generic scoring scheme designed to work well with any type of input"59+.br60+.BR path " Scoring scheme for paths (additional bonus point only after path separator)61+.br62+.BR history " Scoring scheme for command history (no additional bonus points).63+ Sets \fB--tiebreak=index\fR as well.64+.br65+.TP66 .BI "--algo=" TYPE67 Fuzzy matching algorithm (default: v2)68 69 .br977 unmodified linesmodified:shell/key-bindings.bash+1 added-1 removed
48 unmodified lines4950515253535455565745 unmodified lines48 unmodified lines}
__fzf_history__() { local output opts script opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m --read0" opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m --read0" script='BEGIN { getc; $/ = "\n\t"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++' output=$( builtin fc -lnr -2147483648 | last_hist=$(HISTTIMEFORMAT='' builtin history 1) perl -n -l0 -e "$script" |45 unmodified linesmodified:shell/key-bindings.fish+1 added-1 removed
51 unmodified lines52 53 function fzf-history-widget -d "Show command history"54 test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%55 begin56- set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT $FZF_DEFAULT_OPTS --tiebreak=index --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS +m"56+ set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT $FZF_DEFAULT_OPTS --scheme=history --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS +m"57 58 set -l FISH_MAJOR (echo $version | cut -f1 -d.)59 set -l FISH_MINOR (echo $version | cut -f2 -d.)60 112 unmodified linesmodified:shell/key-bindings.zsh+1 added-1 removed
96 unmodified lines97 fzf-history-widget() {98 local selected num99 setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null100 selected=( $(fc -rl 1 | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' |101- FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )101+ FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )102 local ret=$?103 if [ -n "$selected" ]; then104 num=$selected[1]105 if [ -n "$num" ]; then15 unmodified linesmodified:src/algo/algo_test.go+19 added-19 removed
43 unmodified lines44 for _, forward := range []bool{true, false} {45 assertMatch(t, fn, false, forward, "fooBarbaz1", "oBZ", 2, 9,46 scoreMatch*3+bonusCamel123+scoreGapStart+scoreGapExtension*3)47 assertMatch(t, fn, false, forward, "foo bar baz", "fbb", 0, 9,48- scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+49- bonusBoundaryWhite*2+2*scoreGapStart+4*scoreGapExtension)48+ scoreMatch*3+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+49+ int(bonusBoundaryWhite)*2+2*scoreGapStart+4*scoreGapExtension)50 assertMatch(t, fn, false, forward, "/AutomatorDocument.icns", "rdoc", 9, 13,51 scoreMatch*4+bonusCamel123+bonusConsecutive*2)52 assertMatch(t, fn, false, forward, "/man1/zshcompctl.1", "zshc", 6, 10,53- scoreMatch*4+bonusBoundaryDelimiter*bonusFirstCharMultiplier+bonusBoundaryDelimiter*3)53+ scoreMatch*4+int(bonusBoundaryDelimiter)*bonusFirstCharMultiplier+int(bonusBoundaryDelimiter)*3)54 assertMatch(t, fn, false, forward, "/.oh-my-zsh/cache", "zshc", 8, 13,55- scoreMatch*4+bonusBoundary*bonusFirstCharMultiplier+bonusBoundary*2+scoreGapStart+bonusBoundaryDelimiter)55+ scoreMatch*4+bonusBoundary*bonusFirstCharMultiplier+bonusBoundary*2+scoreGapStart+int(bonusBoundaryDelimiter))56 assertMatch(t, fn, false, forward, "ab0123 456", "12356", 3, 10,57 scoreMatch*5+bonusConsecutive*3+scoreGapStart+scoreGapExtension)58 assertMatch(t, fn, false, forward, "abc123 456", "12356", 3, 10,59 scoreMatch*5+bonusCamel123*bonusFirstCharMultiplier+bonusCamel123*2+bonusConsecutive+scoreGapStart+scoreGapExtension)60 assertMatch(t, fn, false, forward, "foo/bar/baz", "fbb", 0, 9,61- scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+62- bonusBoundaryDelimiter*2+2*scoreGapStart+4*scoreGapExtension)61+ scoreMatch*3+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+62+ int(bonusBoundaryDelimiter)*2+2*scoreGapStart+4*scoreGapExtension)63 assertMatch(t, fn, false, forward, "fooBarBaz", "fbb", 0, 7,64- scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+64+ scoreMatch*3+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+65 bonusCamel123*2+2*scoreGapStart+2*scoreGapExtension)66 assertMatch(t, fn, false, forward, "foo barbaz", "fbb", 0, 8,67- scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryWhite+67+ scoreMatch*3+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+int(bonusBoundaryWhite)+68 scoreGapStart*2+scoreGapExtension*3)69 assertMatch(t, fn, false, forward, "fooBar Baz", "foob", 0, 4,70- scoreMatch*4+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryWhite*3)70+ scoreMatch*4+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+int(bonusBoundaryWhite)*3)71 assertMatch(t, fn, false, forward, "xFoo-Bar Baz", "foo-b", 1, 6,72 scoreMatch*5+bonusCamel123*bonusFirstCharMultiplier+bonusCamel123*2+73 bonusNonWord+bonusBoundary)74 75 assertMatch(t, fn, true, forward, "fooBarbaz", "oBz", 2, 9,76 scoreMatch*3+bonusCamel123+scoreGapStart+scoreGapExtension*3)77 assertMatch(t, fn, true, forward, "Foo/Bar/Baz", "FBB", 0, 9,78- scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryDelimiter*2+78+ scoreMatch*3+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+int(bonusBoundaryDelimiter)*2+79 scoreGapStart*2+scoreGapExtension*4)80 assertMatch(t, fn, true, forward, "FooBarBaz", "FBB", 0, 7,81- scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusCamel123*2+81+ scoreMatch*3+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+bonusCamel123*2+82 scoreGapStart*2+scoreGapExtension*2)83 assertMatch(t, fn, true, forward, "FooBar Baz", "FooB", 0, 4,84- scoreMatch*4+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryWhite*2+85- util.Max(bonusCamel123, bonusBoundaryWhite))84+ scoreMatch*4+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+int(bonusBoundaryWhite)*2+85+ util.Max(bonusCamel123, int(bonusBoundaryWhite)))86 87 // Consecutive bonus updated88 assertMatch(t, fn, true, forward, "foo-bar", "o-ba", 2, 6,89 scoreMatch*4+bonusBoundary*3)7 unmodified lines97 }98 99 func TestFuzzyMatchBackward(t *testing.T) {100 assertMatch(t, FuzzyMatchV1, false, true, "foobar fb", "fb", 0, 4,101- scoreMatch*2+bonusBoundaryWhite*bonusFirstCharMultiplier+101+ scoreMatch*2+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+102 scoreGapStart+scoreGapExtension)103 assertMatch(t, FuzzyMatchV1, false, false, "foobar fb", "fb", 7, 9,104- scoreMatch*2+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryWhite)104+ scoreMatch*2+int(bonusBoundaryWhite)*bonusFirstCharMultiplier+int(bonusBoundaryWhite))105 }106 107 func TestExactMatchNaive(t *testing.T) {108 for _, dir := range []bool{true, false} {4 unmodified lines113 scoreMatch*3+bonusCamel123+bonusConsecutive)114 assertMatch(t, ExactMatchNaive, false, dir, "/AutomatorDocument.icns", "rdoc", 9, 13,115 scoreMatch*4+bonusCamel123+bonusConsecutive*2)116 assertMatch(t, ExactMatchNaive, false, dir, "/man1/zshcompctl.1", "zshc", 6, 10,117- scoreMatch*4+bonusBoundaryDelimiter*(bonusFirstCharMultiplier+3))117+ scoreMatch*4+int(bonusBoundaryDelimiter)*(bonusFirstCharMultiplier+3))118 assertMatch(t, ExactMatchNaive, false, dir, "/.oh-my-zsh/cache", "zsh/c", 8, 13,119- scoreMatch*5+bonusBoundary*(bonusFirstCharMultiplier+3)+bonusBoundaryDelimiter)119+ scoreMatch*5+bonusBoundary*(bonusFirstCharMultiplier+3)+int(bonusBoundaryDelimiter))120 }121 }122 123 func TestExactMatchNaiveBackward(t *testing.T) {3 unmodified lines127 scoreMatch*2+bonusConsecutive)128 }129 130 func TestPrefixMatch(t *testing.T) {131- score := scoreMatch*3 + bonusBoundaryWhite*bonusFirstCharMultiplier + bonusBoundaryWhite*2131+ score := scoreMatch*3 + int(bonusBoundaryWhite)*bonusFirstCharMultiplier + int(bonusBoundaryWhite)*2132 133 for _, dir := range []bool{true, false} {134 assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1, 0)135 assertMatch(t, PrefixMatch, false, dir, "fooBarBaz", "baz", -1, -1, 0)22 unmodified lines158 scoreMatch*3+bonusConsecutive*2)159 160 // Only when the pattern doesn't end with a space161 assertMatch(t, SuffixMatch, false, dir, "fooBarbaz ", "baz ", 6, 10,162- scoreMatch*4+bonusConsecutive*2+bonusBoundaryWhite)162+ scoreMatch*4+bonusConsecutive*2+int(bonusBoundaryWhite))163 }164 }165 166 func TestEmptyPattern(t *testing.T) {32 unmodified linesmodified:src/algo/algo.go+39 added-10 removed
78 unmodified lines79 80 import (81 "bytes"82 "fmt"83+ "os"84 "strings"85 "unicode"86 "unicode/utf8"87 1 unmodified line89 )90 91 var DEBUG bool92 92-const delimiterChars = "/,:;|"93+var delimiterChars = "/,:;|"94+95 const whiteChars = " \t\n\v\f\r\x85\xA0"96 97 func indexAt(index int, max int, forward bool) int {98 if forward {22 unmodified lines121 // 8 characters, which is approximately the average length of the words found122 // in web2 dictionary and my file system.123 bonusBoundary = scoreMatch / 2124 123- // Extra bonus for word boundary after whitespace character or beginning of the string124- bonusBoundaryWhite = bonusBoundary + 2125-126- // Extra bonus for word boundary after slash, colon, semi-colon, and comma127- bonusBoundaryDelimiter = bonusBoundary + 1128-125 // Although bonus point for non-word characters is non-contextual, we need it126 // for computing bonus points for consecutive chunks starting with a non-word127 // character.128 bonusNonWord = scoreMatch / 215 unmodified lines144 // still respected.145 bonusFirstCharMultiplier = 2146 )147 148+var (149+ // Extra bonus for word boundary after whitespace character or beginning of the string150+ bonusBoundaryWhite int16 = bonusBoundary + 2151+152+ // Extra bonus for word boundary after slash, colon, semi-colon, and comma153+ bonusBoundaryDelimiter int16 = bonusBoundary + 1154+155+ initialCharClass charClass = charWhite156+)157+158 type charClass int159 160 const (161 charWhite charClass = iota4 unmodified lines166 charLetter167 charNumber168 )169 170+func Init(scheme string) bool {171+ switch scheme {172+ case "default":173+ bonusBoundaryWhite = bonusBoundary + 2174+ bonusBoundaryDelimiter = bonusBoundary + 1175+ case "path":176+ bonusBoundaryWhite = bonusBoundary177+ bonusBoundaryDelimiter = bonusBoundary + 1178+ if os.PathSeparator == '/' {179+ delimiterChars = "/"180+ } else {181+ delimiterChars = string([]rune{os.PathSeparator, '/'})182+ }183+ initialCharClass = charDelimiter184+ case "history":185+ bonusBoundaryWhite = bonusBoundary186+ bonusBoundaryDelimiter = bonusBoundary187+ default:188+ return false189+ }190+ return true191+}192+193 func posArray(withPos bool, len int) *[]int {194 if withPos {195 pos := make([]int, 0, len)196 return &pos238 unmodified lines435 436 // Phase 2. Calculate bonus for each point437 maxScore, maxScorePos := int16(0), 0438 pidx, lastIdx := 0, 0410- pchar0, pchar, prevH0, prevClass, inGap := pattern[0], pattern[0], int16(0), charWhite, false439+ pchar0, pchar, prevH0, prevClass, inGap := pattern[0], pattern[0], int16(0), initialCharClass, false440 Tsub := T[idx:]441 H0sub, C0sub, Bsub := H0[idx:][:len(Tsub)], C0[idx:][:len(Tsub)], B[idx:][:len(Tsub)]442 for off, char := range Tsub {443 var class charClass494 unmodified lines938 }939 match = runesStr == string(pattern)940 }941 if match {913- return Result{trimmedLen, trimmedLen + lenPattern, (scoreMatch+bonusBoundaryWhite)*lenPattern +914- (bonusFirstCharMultiplier-1)*bonusBoundaryWhite}, nil942+ return Result{trimmedLen, trimmedLen + lenPattern, (scoreMatch+int(bonusBoundaryWhite))*lenPattern +943+ (bonusFirstCharMultiplier-1)*int(bonusBoundaryWhite)}, nil944 }945 return Result{-1, -1, 0}, nil946 }modified:src/options.go+20 added-1 removed
19 unmodified lines20 Search21 -x, --extended Extended-search mode22 (enabled by default; +x or --no-extended to disable)23 -e, --exact Enable Exact-match24- --algo=TYPE Fuzzy matching algorithm: [v1|v2] (default: v2)24 -i Case-insensitive match (default: smart-case match)25 +i Case-sensitive match26+ --scheme=SCHEME Scoring scheme [default|path|history]27 --literal Do not normalize latin script letters before matching28 -n, --nth=N[,..] Comma-separated list of field index expressions29 for limiting search scope. Each can be a non-zero30 integer or a range expression ([BEGIN]..[END]).162 unmodified lines193 // Options stores the values of command-line options194 type Options struct {195 Fuzzy bool196 FuzzyAlgo algo.Algo197+ Scheme string198 Extended bool199 Phony bool200 Case Case201 Normalize bool57 unmodified lines259 func defaultOptions() *Options {260 return &Options{261 Fuzzy: true,262 FuzzyAlgo: algo.FuzzyMatchV2,263+ Scheme: "default",264 Extended: true,265 Phony: false,266 Case: CaseSmart,267 Normalize: true,174 unmodified lines442 }443 return algo.FuzzyMatchV2444 }445 446+func processScheme(opts *Options) {447+ if !algo.Init(opts.Scheme) {448+ errorExit("invalid scoring scheme (expected: default|path|history)")449+ }450+ if opts.Scheme == "history" {451+ opts.Criteria = []criterion{byScore}452+ }453+}454+455 func parseBorder(str string, optional bool) tui.BorderShape {456 switch str {457 case "rounded":458 return tui.BorderRounded896 unmodified lines1355 case "--no-literal":1356 opts.Normalize = true1357 case "--algo":1358 opts.FuzzyAlgo = parseAlgo(nextString(allArgs, &i, "algorithm required (v1|v2)"))1359+ case "--scheme":1360+ opts.Scheme = strings.ToLower(nextString(allArgs, &i, "scoring scheme required (default|path|history)"))1361 case "--expect":1362 for k, v := range parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required") {1363 opts.Expect[k] = v1364 }198 unmodified lines1563 // Ignored1564 default:1565 if match, value := optString(arg, "--algo="); match {1566 opts.FuzzyAlgo = parseAlgo(value)1567+ } else if match, value := optString(arg, "--scheme="); match {1568+ opts.Scheme = strings.ToLower(value)1569 } else if match, value := optString(arg, "-q", "--query="); match {1570 opts.Query = value1571 } else if match, value := optString(arg, "-f", "--filter="); match {1572 opts.Filter = &value193 unmodified lines1766 theme.Input = boldify(theme.Input)1767 theme.Cursor = boldify(theme.Cursor)1768 theme.Spinner = boldify(theme.Spinner)1769 }1770+1771+ if opts.Scheme != "default" {1772+ processScheme(opts)1773+ }1774 }1775 1776 func expectsArbitraryString(opt string) bool {1777 switch opt {29 unmodified linesmodified:CHANGELOG.md+11 added-1 removed
1 CHANGELOG2 =========3 4-0.32.24+0.33.05 ------6+- Added `--scheme=[default|path|history]` option to choose scoring scheme7+ - (Experimental)8+ - We updated the scoring algorithm in 0.32.0, however we have learned that9+ this new scheme (`default`) is not always giving the optimal result10+ - `path`: Additional bonus point is only given the the characters after11+ path separator. You might want to choose this scheme if you have many12+ files with spaces in their paths.13+ - `history`: No additional bonus points are given so that we give more14+ weight to the chronological ordering. This is equivalent to the scoring15+ scheme before 0.32.0. This also sets `--tiebreak=index`.16 - ANSI color sequences with colon delimiters are now supported.17 ```sh18 printf "\e[38;5;208mOption 1\e[m\nOption 2" | fzf --ansi19 printf "\e[38:5:208mOption 1\e[m\nOption 2" | fzf --ansi1295 unmodified lines