junegunn / fzf.vim

fzf :heart: vim
MIT License
9.55k stars 583 forks source link

Is it able to include filename when select item on Rg command candidate results? #1419

Closed lilx2018 closed 1 year ago

lilx2018 commented 1 year ago

I use Rg command to search word in files, after search done, then I will continue hit some keys to narrow down candidates from too many files. I hope it can filter by specifying the filename, but Rg command seem only support to filter by text line, not support to filter by filename.

lilx2018 commented 1 year ago

I find Rg support to filter by filename but often priority is too low which not on show.

junegunn commented 1 year ago

Yeah, a trick would be to append : to your search query. e.g. pathfragment:, pathfragment:contentfragment.

lilx2018 commented 1 year ago

I modify the source code a little to let the filename of highest priority of scores.

src\algo\algo.go, func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab util.Slab) (Result, *[]int) { ...

// --------------------   Lilx, add code start  -------------------------
str := input.ToString()
p1 := strings.LastIndex(str, "\\")
if p1 < 0 {
    p1 = strings.LastIndex(str, "/")
}

p2 := strings.Index(str, ":")
if p2 > p1+1 && strings.Index(str[p1+1:p2], string(pattern)) == 0 {
    maxScore += 1000
} else if p1 >= 0 && strings.Index(str[p1+1:], string(pattern)) == 0 {
    maxScore += 1000
}
// --------------------   Lilx, add code end -------------------------

// Start offset we return here is only relevant when begin tiebreak is used.
// However finding the accurate offset requires backtracking, and we don't
// want to pay extra cost for the option that has lost its importance.
return Result{j, maxScorePos + 1, int(maxScore)}, pos

}