Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/chunklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import (

func TestChunkList(t *testing.T) {
// FIXME global
sortCriteria = []criterion{byScore, byLength}
sortCriteria = []criterion{criterion{
by: byScore,
arg: "",
}, criterion{
by: byLength,
arg: "",
}}

cl := NewChunkList(func(item *Item, s []byte) bool {
item.text = util.ToChars(s)
Expand Down
4 changes: 2 additions & 2 deletions src/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ func Run(opts *Options, revision string) {
// Matcher
forward := true
for _, cri := range opts.Criteria[1:] {
if cri == byEnd {
if cri.by == byEnd {
forward = false
break
}
if cri == byBegin {
if cri.by == byBegin {
break
}
}
Expand Down
42 changes: 35 additions & 7 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,19 @@ const (
)

// Sort criteria
type criterion int
type criterion struct {
by by
arg string
}

type by int

const (
byScore criterion = iota
byScore by = iota
byLength
byBegin
byEnd
byProximity
)

type sizeSpec struct {
Expand Down Expand Up @@ -212,7 +218,7 @@ func defaultOptions() *Options {
Delimiter: Delimiter{},
Sort: 1000,
Tac: false,
Criteria: []criterion{byScore, byLength},
Criteria: []criterion{criterion{by: byScore, arg: ""}, criterion{by: byLength, arg: ""}},
Multi: false,
Ansi: false,
Mouse: true,
Expand Down Expand Up @@ -488,7 +494,7 @@ func parseKeyChords(str string, message string) map[int]string {
}

func parseTiebreak(str string) []criterion {
criteria := []criterion{byScore}
criteria := []criterion{criterion{by: byScore, arg: ""}}
hasIndex := false
hasLength := false
hasBegin := false
Expand All @@ -503,18 +509,40 @@ func parseTiebreak(str string) []criterion {
*notExpected = true
}
for _, str := range strings.Split(strings.ToLower(str), ",") {
if strings.HasPrefix(str, "proximity=") {
path := strings.Join(strings.Split(str, "=")[1:], "=")
if path != "" {
criteria = append(criteria, criterion{
by: byProximity,
arg: path,
})
continue
} else {
errorExit("empty proximity path given")
}
}

switch str {
case "index":
check(&hasIndex, "index")
case "length":
check(&hasLength, "length")
criteria = append(criteria, byLength)
criteria = append(criteria, criterion{
by: byLength,
arg: "",
})
case "begin":
check(&hasBegin, "begin")
criteria = append(criteria, byBegin)
criteria = append(criteria, criterion{
by: byBegin,
arg: "",
})
case "end":
check(&hasEnd, "end")
criteria = append(criteria, byEnd)
criteria = append(criteria, criterion{
by: byEnd,
arg: "",
})
default:
errorExit("invalid sort criterion: " + str)
}
Expand Down
19 changes: 17 additions & 2 deletions src/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"
"sort"
"unicode"
"path/filepath"

"github.com/junegunn/fzf/src/tui"
"github.com/junegunn/fzf/src/util"
Expand Down Expand Up @@ -47,7 +48,7 @@ func buildResult(item *Item, offsets []Offset, score int) Result {

for idx, criterion := range sortCriteria {
val := uint16(math.MaxUint16)
switch criterion {
switch criterion.by {
case byScore:
// Higher is better
val = math.MaxUint16 - util.AsUint16(score)
Expand All @@ -63,12 +64,26 @@ func buildResult(item *Item, offsets []Offset, score int) Result {
break
}
}
if criterion == byBegin {
if criterion.by == byBegin {
val = util.AsUint16(minEnd - whitePrefixLen)
} else {
val = util.AsUint16(math.MaxUint16 - math.MaxUint16*(maxEnd-whitePrefixLen)/int(item.TrimLength()))
}
}
case byProximity:
// val is number of shared prefixes
path := filepath.SplitList(criterion.arg)
candidate := filepath.SplitList(item.text.ToString())
end := util.Min(len(path), len(candidate))
val = 0
for idx := 0; idx < end; idx++ {
if path[idx] == candidate[idx] {
val++
} else {
break
}
}

}
result.points[3-idx] = val
}
Expand Down
8 changes: 7 additions & 1 deletion src/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ func TestRankComparison(t *testing.T) {
// Match length, string length, index
func TestResultRank(t *testing.T) {
// FIXME global
sortCriteria = []criterion{byScore, byLength}
sortCriteria = []criterion{criterion{
by: byScore,
arg: "",
}, criterion{
by: byLength,
arg: "",
}}

strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}
item1 := buildResult(
Expand Down