fzf/src/pattern.go

399 lines
9.4 KiB
Go
Raw Normal View History

2015-01-01 19:49:30 +00:00
package fzf
import (
"regexp"
"strings"
2015-01-12 03:56:17 +00:00
"github.com/junegunn/fzf/src/algo"
"github.com/junegunn/fzf/src/util"
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 {
typ termType
inv bool
text []rune
caseSensitive bool
origText []rune
2015-01-01 19:49:30 +00:00
}
2015-11-08 15:58:20 +00:00
type termSet []term
2015-01-11 18:01:24 +00:00
// Pattern represents search pattern
2015-01-01 19:49:30 +00:00
type Pattern struct {
2015-11-03 13:49:32 +00:00
fuzzy bool
2016-09-07 00:58:18 +00:00
fuzzyAlgo algo.Algo
2015-11-03 13:49:32 +00:00
extended bool
2015-01-01 19:49:30 +00:00
caseSensitive bool
normalize bool
forward bool
2015-01-01 19:49:30 +00:00
text []rune
2015-11-08 15:58:20 +00:00
termSets []termSet
cacheable bool
delimiter Delimiter
2015-01-01 19:49:30 +00:00
nth []Range
2016-09-07 00:58:18 +00:00
procFun map[termType]algo.Algo
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
2015-11-03 13:49:32 +00:00
// search 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
func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool,
cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {
2015-01-01 19:49:30 +00:00
var asString string
2015-11-03 13:49:32 +00:00
if extended {
2015-01-01 19:49:30 +00:00
asString = strings.Trim(string(runes), " ")
2015-11-03 13:49:32 +00:00
} else {
2015-01-01 19:49:30 +00:00
asString = string(runes)
}
cached, found := _patternCache[asString]
if found {
return cached
}
caseSensitive := true
2015-11-08 15:58:20 +00:00
termSets := []termSet{}
2015-01-01 19:49:30 +00:00
2015-11-03 13:49:32 +00:00
if extended {
termSets = parseTerms(fuzzy, caseMode, normalize, asString)
2015-11-08 15:58:20 +00:00
Loop:
for _, termSet := range termSets {
for idx, term := range termSet {
// If the query contains inverse search terms or OR operators,
// we cannot cache the search scope
if !cacheable || idx > 0 || term.inv || !fuzzy && term.typ != termExact {
2015-11-08 15:58:20 +00:00
cacheable = false
break Loop
}
2015-01-01 19:49:30 +00:00
}
}
2015-11-03 13:49:32 +00:00
} else {
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{
2015-11-03 13:49:32 +00:00
fuzzy: fuzzy,
2016-09-07 00:58:18 +00:00
fuzzyAlgo: fuzzyAlgo,
2015-11-03 13:49:32 +00:00
extended: extended,
2015-01-01 19:49:30 +00:00
caseSensitive: caseSensitive,
normalize: normalize,
forward: forward,
text: []rune(asString),
2015-11-08 15:58:20 +00:00
termSets: termSets,
cacheable: cacheable,
2015-01-01 19:49:30 +00:00
nth: nth,
delimiter: delimiter,
2016-09-07 00:58:18 +00:00
procFun: make(map[termType]algo.Algo)}
2015-01-01 19:49:30 +00:00
2016-09-07 00:58:18 +00:00
ptr.procFun[termFuzzy] = fuzzyAlgo
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
}
func parseTerms(fuzzy bool, caseMode Case, normalize bool, str string) []termSet {
2015-01-01 19:49:30 +00:00
tokens := _splitRegex.Split(str, -1)
2015-11-08 15:58:20 +00:00
sets := []termSet{}
set := termSet{}
switchSet := false
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
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-11-03 13:49:32 +00:00
if !fuzzy {
2015-01-11 18:01:24 +00:00
typ = termExact
2015-01-01 19:49:30 +00:00
}
2015-11-08 15:58:20 +00:00
if text == "|" {
switchSet = false
continue
}
2015-01-01 19:49:30 +00:00
if strings.HasPrefix(text, "!") {
inv = true
typ = termExact
2015-01-01 19:49:30 +00:00
text = text[1:]
}
if strings.HasPrefix(text, "'") {
2015-11-03 13:49:32 +00:00
// Flip exactness
if fuzzy && !inv {
2015-01-11 18:01:24 +00:00
typ = termExact
2015-01-01 19:49:30 +00:00
text = text[1:]
2015-11-03 13:49:32 +00:00
} else {
typ = termFuzzy
text = text[1:]
2015-01-01 19:49:30 +00:00
}
} 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-11-08 15:58:20 +00:00
if switchSet {
sets = append(sets, set)
set = termSet{}
}
textRunes := []rune(text)
if normalize {
textRunes = algo.NormalizeRunes(textRunes)
}
2015-11-08 15:58:20 +00:00
set = append(set, term{
typ: typ,
inv: inv,
text: textRunes,
caseSensitive: caseSensitive,
origText: origText})
2015-11-08 15:58:20 +00:00
switchSet = true
2015-01-01 19:49:30 +00:00
}
}
2015-11-08 15:58:20 +00:00
if len(set) > 0 {
sets = append(sets, set)
}
return sets
2015-01-01 19:49:30 +00:00
}
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-11-03 13:49:32 +00:00
if !p.extended {
2015-01-01 19:49:30 +00:00
return len(p.text) == 0
}
2015-11-08 15:58:20 +00:00
return len(p.termSets) == 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-11-03 13:49:32 +00:00
if !p.extended {
2015-01-01 19:49:30 +00:00
return p.AsString()
}
cacheableTerms := []string{}
2015-11-08 15:58:20 +00:00
for _, termSet := range p.termSets {
if len(termSet) == 1 && !termSet[0].inv && (p.fuzzy || termSet[0].typ == termExact) {
2015-11-08 15:58:20 +00:00
cacheableTerms = append(cacheableTerms, string(termSet[0].origText))
2015-01-01 19:49:30 +00:00
}
}
return strings.Join(cacheableTerms, " ")
}
2015-01-11 18:01:24 +00:00
// Match returns the list of matches Items in the given Chunk
2016-09-07 00:58:18 +00:00
func (p *Pattern) Match(chunk *Chunk, slab *util.Slab) []*Result {
2015-01-01 19:49:30 +00:00
// ChunkCache: Exact match
cacheKey := p.CacheKey()
2015-11-08 15:58:20 +00:00
if p.cacheable {
2015-01-01 19:49:30 +00:00
if cached, found := _cache.Find(chunk, cacheKey); found {
return cached
}
}
// Prefix/suffix cache
var space []*Result
2015-01-10 18:53:07 +00:00
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 {
space = cached
2015-01-10 18:53:07 +00:00
break Loop
2015-01-01 19:49:30 +00:00
}
}
}
2016-09-07 00:58:18 +00:00
matches := p.matchChunk(chunk, space, slab)
2015-01-01 19:49:30 +00:00
2015-11-08 15:58:20 +00:00
if p.cacheable {
2015-01-01 19:49:30 +00:00
_cache.Add(chunk, cacheKey, matches)
}
return matches
}
2016-09-07 00:58:18 +00:00
func (p *Pattern) matchChunk(chunk *Chunk, space []*Result, slab *util.Slab) []*Result {
matches := []*Result{}
if space == nil {
for idx := range *chunk {
if match, _, _ := p.MatchItem(&(*chunk)[idx], false, slab); match != nil {
matches = append(matches, match)
}
}
} else {
for _, result := range space {
2016-09-07 00:58:18 +00:00
if match, _, _ := p.MatchItem(result.item, false, slab); match != nil {
matches = append(matches, match)
}
}
}
return matches
}
// MatchItem returns true if the Item is a match
2016-09-07 00:58:18 +00:00
func (p *Pattern) MatchItem(item *Item, withPos bool, slab *util.Slab) (*Result, []Offset, *[]int) {
if p.extended {
if offsets, bonus, pos := p.extendedMatch(item, withPos, slab); len(offsets) == len(p.termSets) {
return buildResult(item, offsets, bonus), offsets, pos
}
2016-09-07 00:58:18 +00:00
return nil, nil, nil
}
offset, bonus, pos := p.basicMatch(item, withPos, slab)
if sidx := offset[0]; sidx >= 0 {
2016-08-19 16:46:54 +00:00
offsets := []Offset{offset}
return buildResult(item, offsets, bonus), offsets, pos
}
2016-09-07 00:58:18 +00:00
return nil, nil, nil
}
func (p *Pattern) basicMatch(item *Item, withPos bool, slab *util.Slab) (Offset, int, *[]int) {
input := p.prepareInput(item)
2015-11-03 13:49:32 +00:00
if p.fuzzy {
return p.iter(p.fuzzyAlgo, input, p.caseSensitive, p.normalize, p.forward, p.text, withPos, slab)
2015-11-03 13:49:32 +00:00
}
return p.iter(algo.ExactMatchNaive, input, p.caseSensitive, p.normalize, p.forward, p.text, withPos, slab)
2015-01-01 19:49:30 +00:00
}
func (p *Pattern) extendedMatch(item *Item, withPos bool, slab *util.Slab) ([]Offset, int, *[]int) {
input := p.prepareInput(item)
offsets := []Offset{}
2016-09-07 00:58:18 +00:00
var totalScore int
var allPos *[]int
if withPos {
allPos = &[]int{}
}
2015-11-08 15:58:20 +00:00
for _, termSet := range p.termSets {
var offset Offset
2016-09-07 00:58:18 +00:00
var currentScore int
matched := false
2015-11-08 15:58:20 +00:00
for _, term := range termSet {
pfun := p.procFun[term.typ]
off, score, pos := p.iter(pfun, input, term.caseSensitive, p.normalize, p.forward, term.text, withPos, slab)
if sidx := off[0]; sidx >= 0 {
2015-11-08 15:58:20 +00:00
if term.inv {
continue
2015-11-08 15:58:20 +00:00
}
offset, currentScore = off, score
matched = true
2016-09-07 00:58:18 +00:00
if withPos {
if pos != nil {
*allPos = append(*allPos, *pos...)
} else {
for idx := off[0]; idx < off[1]; idx++ {
*allPos = append(*allPos, int(idx))
}
}
}
2015-11-08 15:58:20 +00:00
break
} else if term.inv {
offset, currentScore = Offset{0, 0}, 0
matched = true
continue
2015-01-01 19:49:30 +00:00
}
}
if matched {
offsets = append(offsets, offset)
2016-09-07 00:58:18 +00:00
totalScore += currentScore
}
2015-01-01 19:49:30 +00:00
}
return offsets, totalScore, allPos
2015-01-01 19:49:30 +00:00
}
func (p *Pattern) prepareInput(item *Item) []Token {
2015-01-01 19:49:30 +00:00
if item.transformed != nil {
return item.transformed
}
var ret []Token
if len(p.nth) == 0 {
ret = []Token{Token{text: &item.text, prefixLength: 0}}
} else {
2015-01-01 19:49:30 +00:00
tokens := Tokenize(item.text, p.delimiter)
ret = Transform(tokens, p.nth)
}
item.transformed = ret
return ret
}
func (p *Pattern) iter(pfun algo.Algo, tokens []Token, caseSensitive bool, normalize bool, forward bool, pattern []rune, withPos bool, slab *util.Slab) (Offset, int, *[]int) {
for _, part := range tokens {
if res, pos := pfun(caseSensitive, normalize, forward, *part.text, pattern, withPos, slab); res.Start >= 0 {
sidx := int32(res.Start) + part.prefixLength
eidx := int32(res.End) + part.prefixLength
2016-09-07 00:58:18 +00:00
if pos != nil {
for idx := range *pos {
(*pos)[idx] += int(part.prefixLength)
}
}
return Offset{sidx, eidx}, res.Score, pos
2015-01-01 19:49:30 +00:00
}
}
return Offset{-1, -1}, 0, nil
2015-01-01 19:49:30 +00:00
}