Skip to content
Myr

Fix change-nth

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Proper clean-up of caches * Force rerender list after the action

authored by Junegunn Choi · committed by Junegunn Choi · parent d83eb28 · Browse files · 6 files+36 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+36 added−9 removed

  • tests:+3 added−1 removed
  • other:+33 added−8 removed

6 files+36 added−9 removed

modified:src/core.go+2 added-1 removed
377 unmodified lines
378 if val.nth != nil {379 // Change nth and clear caches380 nth = *val.nth381 patternCache = make(map[string]*Pattern)382 cache.Clear()382 inputRevision.bumpMajor()383 inputRevision.bumpMinor()384 }385 if command != nil {386 useSnapshot = val.sync387 }
87 unmodified lines
modified:src/item.go+8 added-1 removed
4 unmodified lines
56 "github.com/junegunn/fzf/src/util"7)89type transformed struct {10 // Because nth can be changed dynamically by change-nth action, we need to11 // keep the nth value at the time of transformation.12 nth []Range13 tokens []Token14}1516// Item represents each input line. 56 bytes.17type Item struct {18 text util.Chars // 32 = 24 + 1 + 1 + 2 + 412 transformed *[]Token // 819 transformed *transformed // 820 origText *[]byte // 821 colors *[]ansiOffset // 822}23
30 unmodified lines
modified:src/pattern_test.go+3 added-3 removed
133 unmodified lines
134 chunk := Chunk{count: 1}135 chunk.items[0] = Item{136 text: util.ToChars([]byte("junegunn")),137 origText: &origBytes,138 transformed: &trans}138 transformed: &transformed{pattern.nth, trans}}139 pattern.extended = extended140 matches := pattern.matchChunk(&chunk, nil, slab) // No cache141 if !(matches[0].item.text.ToString() == "junegunn" &&142 string(*matches[0].item.origText) == "junegunn.choi" &&143 reflect.DeepEqual(*matches[0].item.transformed, trans)) {143 reflect.DeepEqual((*matches[0].item.transformed).tokens, trans)) {144 t.Error("Invalid match result", matches)145 }146147 match, offsets, pos := pattern.MatchItem(&chunk.items[0], true, slab)148 if !(match.item.text.ToString() == "junegunn" &&149 string(*match.item.origText) == "junegunn.choi" &&150 offsets[0][0] == 0 && offsets[0][1] == 5 &&151 reflect.DeepEqual(*match.item.transformed, trans)) {151 reflect.DeepEqual((*match.item.transformed).tokens, trans)) {152 t.Error("Invalid match result", match, offsets, extended)153 }154 if !((*pos)[0] == 4 && (*pos)[1] == 0) {155 t.Error("Invalid pos array", *pos)
46 unmodified lines
modified:src/pattern.go+14 added-2 removed
391 unmodified lines
392}393394func (p *Pattern) transformInput(item *Item) []Token {395 if item.transformed != nil {396 return *item.transformed396 transformed := *item.transformed397 if len(transformed.nth) == len(p.nth) {398 same := true399 for idx, rangeItem := range transformed.nth {400 if rangeItem != p.nth[idx] {401 same = false402 break403 }404 }405 if same {406 return transformed.tokens407 }408 }409 }410411 tokens := Tokenize(item.text.ToString(), p.delimiter)412 ret := Transform(tokens, p.nth)401 item.transformed = &ret413 item.transformed = &transformed{p.nth, ret}414 return ret415}416417func (p *Pattern) iter(pfun algo.Algo, tokens []Token, caseSensitive bool, normalize bool, forward bool, pattern []rune, withPos bool, slab *util.Slab) (Offset, int, *[]int) {
14 unmodified lines
modified:src/terminal.go+6 added-1 removed
1624 unmodified lines
16251626 return screenWidth, screenHeight, marginInt, paddingInt1627}16281629func (t *Terminal) forceRerenderList() {1630 t.prevLines = make([]itemLine, len(t.prevLines))1631}16321633func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {1634 t.forcePreview = forcePreview1635 screenWidth, screenHeight, marginInt, paddingInt := t.adjustMarginAndPadding()1636 width := screenWidth - marginInt[1] - marginInt[3]
3005 unmodified lines
4642 // Cycle4643 if len(tokens) > 1 {4644 a.a = strings.Join(append(tokens[1:], tokens[0]), "|")4645 }4646 t.forceRerenderList()4647 case actChangeQuery:4648 t.input = []rune(a.a)4649 t.cx = len(t.input)4650 case actChangeHeader, actTransformHeader:
454 unmodified lines
5105 t.multiLine = !t.multiLine5106 req(reqList)5107 case actToggleHscroll:5108 // Force re-rendering of the list5104 t.prevLines = make([]itemLine, len(t.prevLines))5109 t.forceRerenderList()5110 t.hscroll = !t.hscroll5111 req(reqList)5112 case actTrackCurrent:5113 if t.track == trackDisabled {
676 unmodified lines
modified:test/test_go.rb+3 added-1 removed
3719 unmodified lines
3720 end37213722 def test_change_nth3723 input = [3724 *[''] * 1000,3725 'foo bar bar bar bar',3726 'foo foo bar bar bar',3727 'foo foo foo bar bar',3727 'foo foo foo foo bar'3728 'foo foo foo foo bar',3729 *[''] * 10003730 ]3731 writelines(input)3732 tmux.send_keys %(#{FZF} -qfoo -n1 --bind 'space:change-nth:2|3|4|5|' < #{tempname}), :Enter3733
553 unmodified lines