Remove Offset slice from Result struct
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 3e88849 · Browse files · 9 files·+55 added−46 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+55 added−46 removed
- other:+55 added−46 removed
Provenance
- Commit
827a83e- Tree
f8f7533- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
3e88849
9 files changed+55 added−46 removed
modified:src/core.go+5 added-3 removed
159 unmodified lines160 if streamingFilter {161 reader := Reader{162 func(runes []byte) bool {163 item := chunkList.trans(runes, 0)164+ if item != nil {164- if item != nil && pattern.MatchItem(item) != nil {165- fmt.Println(item.text.ToString())166- found = true165+ if result, _ := pattern.MatchItem(item); result != nil {166+ fmt.Println(item.text.ToString())167+ found = true168+ }169 }170 return false171 }, eventBox, opts.ReadZero}172 reader.ReadSource()113 unmodified linesmodified:src/matcher.go+1 added-1 removed
209 unmodified lines210 for _ = range slices {211 partialResult := <-resultChan212 partialResults[partialResult.index] = partialResult.matches213 }214- return NewMerger(partialResults, m.sort, m.tac), false214+ return NewMerger(pattern, partialResults, m.sort, m.tac), false215 }216 217 // Reset is called to interrupt/signal the ongoing search218 func (m *Matcher) Reset(chunks []*Chunk, patternRunes []rune, cancel bool, final bool, sort bool) {10 unmodified linesmodified:src/merger_test.go+5 added-12 removed
15 unmodified lines16 }17 18 func randResult() *Result {19 str := fmt.Sprintf("%d", rand.Uint32())20- offsets := make([]Offset, rand.Int()%3)21- for idx := range offsets {22- sidx := int32(rand.Uint32() % 20)23- eidx := sidx + int32(rand.Uint32()%20)24- offsets[idx] = Offset{sidx, eidx}25- }20 return &Result{27- item: &Item{text: util.RunesToChars([]rune(str))},28- offsets: offsets,29- rank: rank{index: rand.Int31()}}21+ item: &Item{text: util.RunesToChars([]rune(str))},22+ rank: rank{index: rand.Int31()}}23 }24 25 func TestEmptyMerger(t *testing.T) {26 assert(t, EmptyMerger.Length() == 0, "Not empty")29 unmodified lines56 lists, items := buildLists(false)57 cnt := len(items)58 59 // Not sorted: same order67- mg := NewMerger(lists, false, false)60+ mg := NewMerger(nil, lists, false, false)61 assert(t, cnt == mg.Length(), "Invalid Length")62 for i := 0; i < cnt; i++ {63 assert(t, items[i] == mg.Get(i), "Invalid Get")64 }3 unmodified lines68 lists, items := buildLists(true)69 cnt := len(items)70 71 // Sorted sorted order79- mg := NewMerger(lists, true, false)72+ mg := NewMerger(nil, lists, true, false)73 assert(t, cnt == mg.Length(), "Invalid Length")74 sort.Sort(ByRelevance(items))75 for i := 0; i < cnt; i++ {76 if items[i] != mg.Get(i) {1 unmodified line78 }79 }80 81 // Inverse order89- mg2 := NewMerger(lists, true, false)82+ mg2 := NewMerger(nil, lists, true, false)83 for i := cnt - 1; i >= 0; i-- {84 if items[i] != mg2.Get(i) {85 t.Error("Not sorted", items[i], mg2.Get(i))86 }2 unmodified linesmodified:src/merger.go+8 added-5 removed
1 unmodified line2 3 import "fmt"4 5 // EmptyMerger is a Merger with no data6-var EmptyMerger = NewMerger([][]*Result{}, false, false)6+var EmptyMerger = NewMerger(nil, [][]*Result{}, false, false)7 8 // Merger holds a set of locally sorted lists of items and provides the view of9 // a single, globally-sorted list10 type Merger struct {11+ pattern *Pattern12 lists [][]*Result13 merged []*Result14 chunks *[]*Chunk15 cursors []int6 unmodified lines22 // PassMerger returns a new Merger that simply returns the items in the23 // original order24 func PassMerger(chunks *[]*Chunk, tac bool) *Merger {25 mg := Merger{26+ pattern: nil,25- chunks: chunks,26- tac: tac,27- count: 0}27+ chunks: chunks,28+ tac: tac,29+ count: 0}30 31 for _, chunk := range *mg.chunks {32 mg.count += len(*chunk)33 }34 return &mg35 }36 37 // NewMerger returns a new Merger36-func NewMerger(lists [][]*Result, sorted bool, tac bool) *Merger {38+func NewMerger(pattern *Pattern, lists [][]*Result, sorted bool, tac bool) *Merger {39 mg := Merger{40+ pattern: pattern,41 lists: lists,42 merged: []*Result{},43 chunks: nil,44 cursors: make([]int, len(lists)),77 unmodified linesmodified:src/pattern_test.go+7 added-1 removed
133 unmodified lines134 }135 pattern.extended = extended136 matches := pattern.matchChunk(&chunk, nil) // No cache137 if matches[0].item.text.ToString() != "junegunn" || string(*matches[0].item.origText) != "junegunn.choi" ||138- matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 ||138 !reflect.DeepEqual(matches[0].item.transformed, trans) {139 t.Error("Invalid match result", matches)140 }141+142+ match, offsets := pattern.MatchItem(chunk[0])143+ if match.item.text.ToString() != "junegunn" || string(*match.item.origText) != "junegunn.choi" ||144+ offsets[0][0] != 0 || offsets[0][1] != 5 ||145+ !reflect.DeepEqual(match.item.transformed, trans) {146+ t.Error("Invalid match result", match)147+ }148 }149 }150 151 func TestCacheKey(t *testing.T) {20 unmodified linesmodified:src/pattern.go+8 added-7 removed
271 unmodified lines272 matches := []*Result{}273 274 if space == nil {275 for _, item := range *chunk {276- if match := p.MatchItem(item); match != nil {276+ if match, _ := p.MatchItem(item); match != nil {277 matches = append(matches, match)278 }279 }280 } else {281 for _, result := range space {282- if match := p.MatchItem(result.item); match != nil {282+ if match, _ := p.MatchItem(result.item); match != nil {283 matches = append(matches, match)284 }285 }286 }287 return matches288 }289 290 // MatchItem returns true if the Item is a match291-func (p *Pattern) MatchItem(item *Item) *Result {291+func (p *Pattern) MatchItem(item *Item) (*Result, []Offset) {292 if p.extended {293 if offsets, bonus, trimLen := p.extendedMatch(item); len(offsets) == len(p.termSets) {294- return buildResult(item, offsets, bonus, trimLen)294+ return buildResult(item, offsets, bonus, trimLen), offsets295 }296- return nil296+ return nil, nil297 }298 offset, bonus, trimLen := p.basicMatch(item)299 if sidx := offset[0]; sidx >= 0 {300- return buildResult(item, []Offset{offset}, bonus, trimLen)300+ offsets := []Offset{offset}301+ return buildResult(item, offsets, bonus, trimLen), offsets302 }302- return nil303+ return nil, nil304 }305 306 func (p *Pattern) basicMatch(item *Item) (Offset, int, int) {307 input := p.prepareInput(item)68 unmodified linesmodified:src/result_test.go+4 added-3 removed
86 unmodified lines87 func TestColorOffset(t *testing.T) {88 // ------------ 20 ---- -- ----89 // ++++++++ ++++++++++90 // --++++++++-- --++++++++++---91+92+ offsets := []Offset{Offset{5, 15}, Offset{25, 35}}93 item := Result{92- offsets: []Offset{Offset{5, 15}, Offset{25, 35}},94 item: &Item{95 colors: &[]ansiOffset{96 ansiOffset{[2]int32{0, 20}, ansiState{1, 5, false}},97 ansiOffset{[2]int32{22, 27}, ansiState{2, 6, true}},98 ansiOffset{[2]int32{30, 32}, ansiState{3, 7, false}},99 ansiOffset{[2]int32{33, 40}, ansiState{4, 8, true}}}}}100 // [{[0 5] 9 false} {[5 15] 99 false} {[15 20] 9 false} {[22 25] 10 true} {[25 35] 99 false} {[35 40] 11 true}]101 101- offsets := item.colorOffsets(99, false, true)102+ colors := item.colorOffsets(offsets, 99, false, true)103 assert := func(idx int, b int32, e int32, c int, bold bool) {103- o := offsets[idx]104+ o := colors[idx]105 if o.offset[0] != b || o.offset[1] != e || o.color != c || o.bold != bold {106 t.Error(o)107 }108 }7 unmodified linesmodified:src/result.go+8 added-9 removed
17 unmodified lines18 index int3219 }20 21 type rank struct {22- index int3222 // byMatchLen, byBonus, ...23 points [5]uint1624+ index int3225 }26 27 type Result struct {28- item *Item29- offsets []Offset30- rank rank28+ item *Item29+ rank rank30 }31 32 func buildResult(item *Item, offsets []Offset, bonus int, trimLen int) *Result {33 if len(offsets) > 1 {34 sort.Sort(ByOrder(offsets))35 }36 38- result := Result{item: item, offsets: offsets, rank: rank{index: item.index}}37+ result := Result{item: item, rank: rank{index: item.index}}38 39 matchlen := 040 prevEnd := 041 minBegin := math.MaxInt3266 unmodified lines108 func minRank() rank {109 return rank{index: 0, points: [5]uint16{0, math.MaxUint16, 0, 0, 0}}110 }111 113-func (result *Result) colorOffsets(color int, bold bool, current bool) []colorOffset {112+func (result *Result) colorOffsets(matchOffsets []Offset, color int, bold bool, current bool) []colorOffset {113 itemColors := result.item.Colors()114 115 if len(itemColors) == 0 {116 var offsets []colorOffset118- for _, off := range result.offsets {117+ for _, off := range matchOffsets {118 119 offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: color, bold: bold})120 }121 return offsets122 }123 124 // Find max column125 var maxCol int32127- for _, off := range result.offsets {126+ for _, off := range matchOffsets {127 if off[1] > maxCol {128 maxCol = off[1]129 }130 }9 unmodified lines140 cols[i] = colorIndex + 1 // XXX141 }142 }143 145- for _, off := range result.offsets {144+ for _, off := range matchOffsets {145 for i := off[0]; i < off[1]; i++ {146 cols[i] = -1147 }148 }110 unmodified linesmodified:src/terminal.go+9 added-5 removed
667 unmodified lines668 }669 670 func (t *Terminal) printHighlighted(result *Result, bold bool, col1 int, col2 int, current bool) {671 item := result.item672- var maxe int673- for _, offset := range result.offsets {674- maxe = util.Max(maxe, int(offset[1]))675- }672 673 // Overflow674 text := make([]rune, item.text.Length())675 copy(text, item.text.ToRunes())676+ matchOffsets := []Offset{}677+ if t.merger.pattern != nil {678+ _, matchOffsets = t.merger.pattern.MatchItem(item)679+ }680+ var maxe int681+ for _, offset := range matchOffsets {682+ maxe = util.Max(maxe, int(offset[1]))683+ }680- offsets := result.colorOffsets(col2, bold, current)684+ offsets := result.colorOffsets(matchOffsets, col2, bold, current)685 maxWidth := t.window.Width - 3686 maxe = util.Constrain(maxe+util.Min(maxWidth/2-2, t.hscrollOff), 0, len(text))687 if overflow(text, maxWidth) {688 if t.hscroll {705 unmodified lines