2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2015-01-11 14:49:12 +00:00
|
|
|
"sort"
|
2015-01-01 19:49:30 +00:00
|
|
|
"strings"
|
2015-01-12 03:56:17 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/algo"
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// fuzzy
|
|
|
|
// 'exact
|
|
|
|
// ^exact-prefix
|
|
|
|
// exact-suffix$
|
|
|
|
// !not-fuzzy
|
|
|
|
// !'not-exact
|
|
|
|
// !^not-exact-prefix
|
|
|
|
// !not-exact-suffix$
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
type termType int
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
const (
|
2015-01-11 18:01:24 +00:00
|
|
|
termFuzzy termType = iota
|
|
|
|
termExact
|
|
|
|
termPrefix
|
|
|
|
termSuffix
|
2015-06-08 14:16:31 +00:00
|
|
|
termEqual
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
type term struct {
|
2015-04-21 13:18:05 +00:00
|
|
|
typ termType
|
|
|
|
inv bool
|
|
|
|
text []rune
|
|
|
|
caseSensitive bool
|
|
|
|
origText []rune
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Pattern represents search pattern
|
2015-01-01 19:49:30 +00:00
|
|
|
type Pattern struct {
|
|
|
|
mode Mode
|
|
|
|
caseSensitive bool
|
|
|
|
text []rune
|
2015-01-11 18:01:24 +00:00
|
|
|
terms []term
|
2015-01-01 19:49:30 +00:00
|
|
|
hasInvTerm bool
|
|
|
|
delimiter *regexp.Regexp
|
|
|
|
nth []Range
|
2015-08-02 05:00:18 +00:00
|
|
|
procFun map[termType]func(bool, []rune, []rune) (int, int)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_patternCache map[string]*Pattern
|
|
|
|
_splitRegex *regexp.Regexp
|
|
|
|
_cache ChunkCache
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
_splitRegex = regexp.MustCompile("\\s+")
|
2015-03-31 13:05:02 +00:00
|
|
|
clearPatternCache()
|
|
|
|
clearChunkCache()
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func clearPatternCache() {
|
2015-03-31 13:05:02 +00:00
|
|
|
// We can uniquely identify the pattern for a given string since
|
|
|
|
// mode and caseMode do not change while the program is running
|
2015-01-01 19:49:30 +00:00
|
|
|
_patternCache = make(map[string]*Pattern)
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:05:02 +00:00
|
|
|
func clearChunkCache() {
|
|
|
|
_cache = NewChunkCache()
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// BuildPattern builds Pattern object from the given arguments
|
2015-01-01 19:49:30 +00:00
|
|
|
func BuildPattern(mode Mode, caseMode Case,
|
|
|
|
nth []Range, delimiter *regexp.Regexp, runes []rune) *Pattern {
|
|
|
|
|
|
|
|
var asString string
|
|
|
|
switch mode {
|
2015-01-11 18:01:24 +00:00
|
|
|
case ModeExtended, ModeExtendedExact:
|
2015-01-01 19:49:30 +00:00
|
|
|
asString = strings.Trim(string(runes), " ")
|
|
|
|
default:
|
|
|
|
asString = string(runes)
|
|
|
|
}
|
|
|
|
|
|
|
|
cached, found := _patternCache[asString]
|
|
|
|
if found {
|
|
|
|
return cached
|
|
|
|
}
|
|
|
|
|
|
|
|
caseSensitive, hasInvTerm := true, false
|
2015-01-11 18:01:24 +00:00
|
|
|
terms := []term{}
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
switch mode {
|
2015-01-11 18:01:24 +00:00
|
|
|
case ModeExtended, ModeExtendedExact:
|
2015-04-21 13:18:05 +00:00
|
|
|
terms = parseTerms(mode, caseMode, asString)
|
2015-01-01 19:49:30 +00:00
|
|
|
for _, term := range terms {
|
|
|
|
if term.inv {
|
|
|
|
hasInvTerm = true
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 13:18:05 +00:00
|
|
|
default:
|
|
|
|
lowerString := strings.ToLower(asString)
|
|
|
|
caseSensitive = caseMode == CaseRespect ||
|
|
|
|
caseMode == CaseSmart && lowerString != asString
|
|
|
|
if !caseSensitive {
|
|
|
|
asString = lowerString
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ptr := &Pattern{
|
|
|
|
mode: mode,
|
|
|
|
caseSensitive: caseSensitive,
|
2015-04-21 13:18:05 +00:00
|
|
|
text: []rune(asString),
|
2015-01-01 19:49:30 +00:00
|
|
|
terms: terms,
|
|
|
|
hasInvTerm: hasInvTerm,
|
|
|
|
nth: nth,
|
|
|
|
delimiter: delimiter,
|
2015-08-02 05:00:18 +00:00
|
|
|
procFun: make(map[termType]func(bool, []rune, []rune) (int, int))}
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2015-01-12 03:56:17 +00:00
|
|
|
ptr.procFun[termFuzzy] = algo.FuzzyMatch
|
2015-06-08 14:16:31 +00:00
|
|
|
ptr.procFun[termEqual] = algo.EqualMatch
|
2015-01-12 03:56:17 +00:00
|
|
|
ptr.procFun[termExact] = algo.ExactMatchNaive
|
|
|
|
ptr.procFun[termPrefix] = algo.PrefixMatch
|
|
|
|
ptr.procFun[termSuffix] = algo.SuffixMatch
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
_patternCache[asString] = ptr
|
|
|
|
return ptr
|
|
|
|
}
|
|
|
|
|
2015-04-21 13:18:05 +00:00
|
|
|
func parseTerms(mode Mode, caseMode Case, str string) []term {
|
2015-01-01 19:49:30 +00:00
|
|
|
tokens := _splitRegex.Split(str, -1)
|
2015-01-11 18:01:24 +00:00
|
|
|
terms := []term{}
|
2015-01-01 19:49:30 +00:00
|
|
|
for _, token := range tokens {
|
2015-01-11 18:01:24 +00:00
|
|
|
typ, inv, text := termFuzzy, false, token
|
2015-04-21 13:18:05 +00:00
|
|
|
lowerText := strings.ToLower(text)
|
|
|
|
caseSensitive := caseMode == CaseRespect ||
|
|
|
|
caseMode == CaseSmart && text != lowerText
|
|
|
|
if !caseSensitive {
|
|
|
|
text = lowerText
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
origText := []rune(text)
|
2015-01-11 18:01:24 +00:00
|
|
|
if mode == ModeExtendedExact {
|
|
|
|
typ = termExact
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(text, "!") {
|
|
|
|
inv = true
|
|
|
|
text = text[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(text, "'") {
|
2015-01-11 18:01:24 +00:00
|
|
|
if mode == ModeExtended {
|
|
|
|
typ = termExact
|
2015-01-01 19:49:30 +00:00
|
|
|
text = text[1:]
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(text, "^") {
|
2015-06-08 14:16:31 +00:00
|
|
|
if strings.HasSuffix(text, "$") {
|
|
|
|
typ = termEqual
|
|
|
|
text = text[1 : len(text)-1]
|
|
|
|
} else {
|
|
|
|
typ = termPrefix
|
|
|
|
text = text[1:]
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
} else if strings.HasSuffix(text, "$") {
|
2015-01-11 18:01:24 +00:00
|
|
|
typ = termSuffix
|
2015-01-01 19:49:30 +00:00
|
|
|
text = text[:len(text)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) > 0 {
|
2015-01-11 18:01:24 +00:00
|
|
|
terms = append(terms, term{
|
2015-04-21 13:18:05 +00:00
|
|
|
typ: typ,
|
|
|
|
inv: inv,
|
|
|
|
text: []rune(text),
|
|
|
|
caseSensitive: caseSensitive,
|
|
|
|
origText: origText})
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return terms
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// IsEmpty returns true if the pattern is effectively empty
|
2015-01-01 19:49:30 +00:00
|
|
|
func (p *Pattern) IsEmpty() bool {
|
2015-01-11 18:01:24 +00:00
|
|
|
if p.mode == ModeFuzzy {
|
2015-01-01 19:49:30 +00:00
|
|
|
return len(p.text) == 0
|
|
|
|
}
|
2015-01-11 18:01:24 +00:00
|
|
|
return len(p.terms) == 0
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// AsString returns the search query in string type
|
2015-01-01 19:49:30 +00:00
|
|
|
func (p *Pattern) AsString() string {
|
|
|
|
return string(p.text)
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// CacheKey is used to build string to be used as the key of result cache
|
2015-01-01 19:49:30 +00:00
|
|
|
func (p *Pattern) CacheKey() string {
|
2015-01-11 18:01:24 +00:00
|
|
|
if p.mode == ModeFuzzy {
|
2015-01-01 19:49:30 +00:00
|
|
|
return p.AsString()
|
|
|
|
}
|
|
|
|
cacheableTerms := []string{}
|
|
|
|
for _, term := range p.terms {
|
|
|
|
if term.inv {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cacheableTerms = append(cacheableTerms, string(term.origText))
|
|
|
|
}
|
|
|
|
return strings.Join(cacheableTerms, " ")
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Match returns the list of matches Items in the given Chunk
|
2015-01-01 19:49:30 +00:00
|
|
|
func (p *Pattern) Match(chunk *Chunk) []*Item {
|
|
|
|
space := chunk
|
|
|
|
|
|
|
|
// ChunkCache: Exact match
|
|
|
|
cacheKey := p.CacheKey()
|
|
|
|
if !p.hasInvTerm { // Because we're excluding Inv-term from cache key
|
|
|
|
if cached, found := _cache.Find(chunk, cacheKey); found {
|
|
|
|
return cached
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 18:53:07 +00:00
|
|
|
// ChunkCache: Prefix/suffix match
|
|
|
|
Loop:
|
|
|
|
for idx := 1; idx < len(cacheKey); idx++ {
|
|
|
|
// [---------| ] | [ |---------]
|
|
|
|
// [--------| ] | [ |--------]
|
|
|
|
// [-------| ] | [ |-------]
|
|
|
|
prefix := cacheKey[:len(cacheKey)-idx]
|
|
|
|
suffix := cacheKey[idx:]
|
|
|
|
for _, substr := range [2]*string{&prefix, &suffix} {
|
|
|
|
if cached, found := _cache.Find(chunk, *substr); found {
|
2015-01-01 19:49:30 +00:00
|
|
|
cachedChunk := Chunk(cached)
|
|
|
|
space = &cachedChunk
|
2015-01-10 18:53:07 +00:00
|
|
|
break Loop
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-01 02:16:38 +00:00
|
|
|
matches := p.matchChunk(space)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
if !p.hasInvTerm {
|
|
|
|
_cache.Add(chunk, cacheKey, matches)
|
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
2015-03-01 02:16:38 +00:00
|
|
|
func (p *Pattern) matchChunk(chunk *Chunk) []*Item {
|
|
|
|
matches := []*Item{}
|
|
|
|
if p.mode == ModeFuzzy {
|
|
|
|
for _, item := range *chunk {
|
|
|
|
if sidx, eidx := p.fuzzyMatch(item); sidx >= 0 {
|
|
|
|
matches = append(matches,
|
|
|
|
dupItem(item, []Offset{Offset{int32(sidx), int32(eidx)}}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, item := range *chunk {
|
|
|
|
if offsets := p.extendedMatch(item); len(offsets) == len(p.terms) {
|
|
|
|
matches = append(matches, dupItem(item, offsets))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchItem returns true if the Item is a match
|
|
|
|
func (p *Pattern) MatchItem(item *Item) bool {
|
|
|
|
if p.mode == ModeFuzzy {
|
|
|
|
sidx, _ := p.fuzzyMatch(item)
|
|
|
|
return sidx >= 0
|
|
|
|
}
|
|
|
|
offsets := p.extendedMatch(item)
|
|
|
|
return len(offsets) == len(p.terms)
|
|
|
|
}
|
|
|
|
|
2015-01-10 16:47:46 +00:00
|
|
|
func dupItem(item *Item, offsets []Offset) *Item {
|
2015-01-11 14:49:12 +00:00
|
|
|
sort.Sort(ByOrder(offsets))
|
2015-01-10 16:47:46 +00:00
|
|
|
return &Item{
|
|
|
|
text: item.text,
|
|
|
|
origText: item.origText,
|
|
|
|
transformed: item.transformed,
|
2015-01-11 14:49:12 +00:00
|
|
|
index: item.index,
|
2015-01-10 16:47:46 +00:00
|
|
|
offsets: offsets,
|
2015-03-18 16:59:14 +00:00
|
|
|
colors: item.colors,
|
2015-01-11 14:49:12 +00:00
|
|
|
rank: Rank{0, 0, item.index}}
|
2015-01-10 16:47:46 +00:00
|
|
|
}
|
|
|
|
|
2015-03-01 02:16:38 +00:00
|
|
|
func (p *Pattern) fuzzyMatch(item *Item) (int, int) {
|
|
|
|
input := p.prepareInput(item)
|
2015-04-21 13:18:05 +00:00
|
|
|
return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.text)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-01 02:16:38 +00:00
|
|
|
func (p *Pattern) extendedMatch(item *Item) []Offset {
|
|
|
|
input := p.prepareInput(item)
|
|
|
|
offsets := []Offset{}
|
|
|
|
for _, term := range p.terms {
|
|
|
|
pfun := p.procFun[term.typ]
|
2015-04-21 13:18:05 +00:00
|
|
|
if sidx, eidx := p.iter(pfun, input, term.caseSensitive, term.text); sidx >= 0 {
|
2015-03-01 02:16:38 +00:00
|
|
|
if term.inv {
|
|
|
|
break
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-03-01 02:16:38 +00:00
|
|
|
offsets = append(offsets, Offset{int32(sidx), int32(eidx)})
|
|
|
|
} else if term.inv {
|
|
|
|
offsets = append(offsets, Offset{0, 0})
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-01 02:16:38 +00:00
|
|
|
return offsets
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-02 05:00:18 +00:00
|
|
|
func (p *Pattern) prepareInput(item *Item) []Token {
|
2015-01-01 19:49:30 +00:00
|
|
|
if item.transformed != nil {
|
|
|
|
return item.transformed
|
|
|
|
}
|
|
|
|
|
2015-08-02 05:00:18 +00:00
|
|
|
var ret []Token
|
2015-01-01 19:49:30 +00:00
|
|
|
if len(p.nth) > 0 {
|
|
|
|
tokens := Tokenize(item.text, p.delimiter)
|
|
|
|
ret = Transform(tokens, p.nth)
|
|
|
|
} else {
|
2015-08-02 05:00:18 +00:00
|
|
|
ret = []Token{Token{text: item.text, prefixLength: 0}}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
item.transformed = ret
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-08-02 05:00:18 +00:00
|
|
|
func (p *Pattern) iter(pfun func(bool, []rune, []rune) (int, int),
|
|
|
|
tokens []Token, caseSensitive bool, pattern []rune) (int, int) {
|
|
|
|
for _, part := range tokens {
|
2015-01-01 19:49:30 +00:00
|
|
|
prefixLength := part.prefixLength
|
2015-04-21 13:18:05 +00:00
|
|
|
if sidx, eidx := pfun(caseSensitive, part.text, pattern); sidx >= 0 {
|
2015-01-01 19:49:30 +00:00
|
|
|
return sidx + prefixLength, eidx + prefixLength
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, -1
|
|
|
|
}
|