diff --git a/contentprovider.go b/contentprovider.go index 1c2809dd2..c3c35fa65 100644 --- a/contentprovider.go +++ b/contentprovider.go @@ -221,7 +221,7 @@ func (p *contentProvider) fillContentMatches(ms []*candidateMatch, numContextLin m := ms[0] num := p.newlines().atOffset(m.byteOffset) lineStart := int(p.newlines().lineStart(num)) - lineEnd := int(p.newlines().lineEnd(num, true)) + lineEnd := int(p.newlines().lineEnd(num)) var lineCands []*candidateMatch @@ -229,7 +229,7 @@ func (p *contentProvider) fillContentMatches(ms []*candidateMatch, numContextLin for len(ms) > 0 { m := ms[0] - if int(m.byteOffset) <= lineEnd { + if int(m.byteOffset) < lineEnd { endMatch = m.byteOffset + m.byteMatchSz lineCands = append(lineCands, m) ms = ms[1:] @@ -252,7 +252,7 @@ func (p *contentProvider) fillContentMatches(ms []*candidateMatch, numContextLin // crosses a line boundary. Prevent confusion by // taking lines until we pass the last match for lineEnd < len(data) && endMatch > uint32(lineEnd) { - next := bytes.IndexByte(data[lineEnd+1:], '\n') + next := bytes.IndexByte(data[lineEnd:], '\n') if next == -1 { lineEnd = len(data) } else { @@ -487,11 +487,7 @@ func (nls newlines) lineStart(lineNumber int) uint32 { // last byte of the line. lineNumber is 1-based. If lineNumber is out // of range of the lines in the file, the return value will be clamped to // [0,fileSize]. -// -// If trimNewline is set, the offset exclude any line-terminating newline. -// Note: this does _not_ trim any carriage returns and should probably not be -// used by default. It only exists for backwards compatibility. -func (nls newlines) lineEnd(lineNumber int, trimNewline bool) uint32 { +func (nls newlines) lineEnd(lineNumber int) uint32 { // nls.locs[0] + 1 is the start of the 2nd line of data. endIdx := lineNumber - 1 @@ -499,8 +495,6 @@ func (nls newlines) lineEnd(lineNumber int, trimNewline bool) uint32 { return 0 } else if endIdx >= len(nls.locs) { return nls.fileSize - } else if trimNewline { - return nls.locs[endIdx] } else { return nls.locs[endIdx] + 1 } @@ -514,7 +508,7 @@ func (nls newlines) getLines(data []byte, low, high int) []byte { } lowStart := nls.lineStart(low) - highEnd := nls.lineEnd(high-1, false) + highEnd := nls.lineEnd(high - 1) return data[lowStart:highEnd] } diff --git a/contentprovider_test.go b/contentprovider_test.go index 47d63b425..f237c82bc 100644 --- a/contentprovider_test.go +++ b/contentprovider_test.go @@ -137,7 +137,7 @@ func TestAtOffset(t *testing.T) { if gotLineStart := nls.lineStart(gotLineNumber); gotLineStart != tt.lineStart { t.Fatalf("expected line start %d, got %d", tt.lineStart, gotLineStart) } - if gotLineEnd := nls.lineEnd(gotLineNumber, false); gotLineEnd != tt.lineEnd { + if gotLineEnd := nls.lineEnd(gotLineNumber); gotLineEnd != tt.lineEnd { t.Fatalf("expected line end %d, got %d", tt.lineEnd, gotLineEnd) } }) @@ -191,7 +191,7 @@ func TestLineBounds(t *testing.T) { if gotStart != tt.start { t.Fatalf("expected line start %d, got %d", tt.start, gotStart) } - gotEnd := nls.lineEnd(tt.lineNumber, false) + gotEnd := nls.lineEnd(tt.lineNumber) if gotEnd != tt.end { t.Fatalf("expected line end %d, got %d", tt.end, gotEnd) } diff --git a/index_test.go b/index_test.go index 6e6514a27..5a2b0091a 100644 --- a/index_test.go +++ b/index_test.go @@ -202,7 +202,7 @@ func (s *memSeeker) Size() (uint32, error) { func TestNewlines(t *testing.T) { b := testIndexBuilder(t, nil, Document{Name: "filename", Content: []byte("line1\nline2\nbla")}) - // ---------------------------------------------012345-678901-234 + // -------------------------------------------012345-678901-234 t.Run("LineMatches", func(t *testing.T) { sres := searchForTest(t, b, &query.Substring{Pattern: "ne2"}) @@ -216,15 +216,15 @@ func TestNewlines(t *testing.T) { LineOffset: 2, MatchLength: 3, }}, - Line: []byte("line2"), + Line: []byte("line2\n"), LineStart: 6, - LineEnd: 11, + LineEnd: 12, LineNumber: 2, }}, }} - if !reflect.DeepEqual(matches, want) { - t.Errorf("got %v, want %v", matches, want) + if diff := cmp.Diff(matches, want); diff != "" { + t.Fatal(diff) } }) @@ -269,7 +269,7 @@ func TestQueryNewlines(t *testing.T) { } m := matches[0] if len(m.LineMatches) != 2 { - t.Fatalf("got %d line matches, want exactly two", len(m.LineMatches)) + t.Fatalf("got %d line matches, want exactly two %#v", len(m.LineMatches), m.LineMatches) } }) diff --git a/matchtree.go b/matchtree.go index e22b02cd5..81490266e 100644 --- a/matchtree.go +++ b/matchtree.go @@ -700,7 +700,7 @@ func (t *andLineMatchTree) matches(cp *contentProvider, cost int, known map[matc } prev = line byteStart := int(cp.newlines().lineStart(line)) - byteEnd := int(cp.newlines().lineEnd(line, false)) + byteEnd := int(cp.newlines().lineEnd(line)) lines = append(lines, lineRange{byteStart, byteEnd}) } diff --git a/testdata/backcompat/new_v16.00000.zoekt b/testdata/backcompat/new_v16.00000.zoekt new file mode 100644 index 000000000..cd69358f6 Binary files /dev/null and b/testdata/backcompat/new_v16.00000.zoekt differ diff --git a/testdata/golden/TestReadSearch/ctagsrepo_v16.00000.golden b/testdata/golden/TestReadSearch/ctagsrepo_v16.00000.golden index 2ec38c772..6c5faaacd 100644 --- a/testdata/golden/TestReadSearch/ctagsrepo_v16.00000.golden +++ b/testdata/golden/TestReadSearch/ctagsrepo_v16.00000.golden @@ -9,9 +9,9 @@ "Language": "go", "LineMatches": [ { - "Line": "ZnVuYyBtYWluKCkgew==", + "Line": "ZnVuYyBtYWluKCkgewo=", "LineStart": 69, - "LineEnd": 82, + "LineEnd": 83, "LineNumber": 10, "Before": null, "After": null, @@ -39,9 +39,9 @@ "Language": "go", "LineMatches": [ { - "Line": "cGFja2FnZSBtYWlu", + "Line": "cGFja2FnZSBtYWluCg==", "LineStart": 0, - "LineEnd": 12, + "LineEnd": 13, "LineNumber": 1, "Before": null, "After": null, @@ -69,9 +69,9 @@ "Language": "go", "LineMatches": [ { - "Line": "CW51bSAgICAgPSA1", + "Line": "CW51bSAgICAgPSA1Cg==", "LineStart": 34, - "LineEnd": 46, + "LineEnd": 47, "LineNumber": 6, "Before": null, "After": null, @@ -104,9 +104,9 @@ "Language": "go", "LineMatches": [ { - "Line": "CW1lc3NhZ2UgPSAiaGVsbG8i", + "Line": "CW1lc3NhZ2UgPSAiaGVsbG8iCg==", "LineStart": 47, - "LineEnd": 65, + "LineEnd": 66, "LineNumber": 7, "Before": null, "After": null, diff --git a/testdata/golden/TestReadSearch/ctagsrepo_v17.00000.golden b/testdata/golden/TestReadSearch/ctagsrepo_v17.00000.golden index de1f98e2f..d8054cc34 100644 --- a/testdata/golden/TestReadSearch/ctagsrepo_v17.00000.golden +++ b/testdata/golden/TestReadSearch/ctagsrepo_v17.00000.golden @@ -9,9 +9,9 @@ "Language": "go", "LineMatches": [ { - "Line": "ZnVuYyBtYWluKCkgew==", + "Line": "ZnVuYyBtYWluKCkgewo=", "LineStart": 69, - "LineEnd": 82, + "LineEnd": 83, "LineNumber": 10, "Before": null, "After": null, @@ -39,9 +39,9 @@ "Language": "go", "LineMatches": [ { - "Line": "cGFja2FnZSBtYWlu", + "Line": "cGFja2FnZSBtYWluCg==", "LineStart": 0, - "LineEnd": 12, + "LineEnd": 13, "LineNumber": 1, "Before": null, "After": null, @@ -69,9 +69,9 @@ "Language": "go", "LineMatches": [ { - "Line": "CW51bSAgICAgPSA1", + "Line": "CW51bSAgICAgPSA1Cg==", "LineStart": 34, - "LineEnd": 46, + "LineEnd": 47, "LineNumber": 6, "Before": null, "After": null, @@ -104,9 +104,9 @@ "Language": "go", "LineMatches": [ { - "Line": "CW1lc3NhZ2UgPSAiaGVsbG8i", + "Line": "CW1lc3NhZ2UgPSAiaGVsbG8iCg==", "LineStart": 47, - "LineEnd": 65, + "LineEnd": 66, "LineNumber": 7, "Before": null, "After": null, diff --git a/testdata/golden/TestReadSearch/repo17_v17.00000.golden b/testdata/golden/TestReadSearch/repo17_v17.00000.golden index 2d11f1f0f..452429bcf 100644 --- a/testdata/golden/TestReadSearch/repo17_v17.00000.golden +++ b/testdata/golden/TestReadSearch/repo17_v17.00000.golden @@ -9,9 +9,9 @@ "Language": "Go", "LineMatches": [ { - "Line": "ZnVuYyBtYWluKCkgew==", + "Line": "ZnVuYyBtYWluKCkgewo=", "LineStart": 69, - "LineEnd": 82, + "LineEnd": 83, "LineNumber": 10, "Before": null, "After": null, @@ -39,9 +39,9 @@ "Language": "Go", "LineMatches": [ { - "Line": "cGFja2FnZSBtYWlu", + "Line": "cGFja2FnZSBtYWluCg==", "LineStart": 0, - "LineEnd": 12, + "LineEnd": 13, "LineNumber": 1, "Before": null, "After": null, diff --git a/testdata/golden/TestReadSearch/repo2_v16.00000.golden b/testdata/golden/TestReadSearch/repo2_v16.00000.golden index 82a6112d8..cf70c4e0d 100644 --- a/testdata/golden/TestReadSearch/repo2_v16.00000.golden +++ b/testdata/golden/TestReadSearch/repo2_v16.00000.golden @@ -9,9 +9,9 @@ "Language": "Go", "LineMatches": [ { - "Line": "ZnVuYyBtYWluKCkgew==", + "Line": "ZnVuYyBtYWluKCkgewo=", "LineStart": 33, - "LineEnd": 46, + "LineEnd": 47, "LineNumber": 7, "Before": null, "After": null, @@ -39,9 +39,9 @@ "Language": "Go", "LineMatches": [ { - "Line": "cGFja2FnZSBtYWlu", + "Line": "cGFja2FnZSBtYWluCg==", "LineStart": 0, - "LineEnd": 12, + "LineEnd": 13, "LineNumber": 1, "Before": null, "After": null, diff --git a/testdata/golden/TestReadSearch/repo_v16.00000.golden b/testdata/golden/TestReadSearch/repo_v16.00000.golden index ed2532855..7f5f3c41c 100644 --- a/testdata/golden/TestReadSearch/repo_v16.00000.golden +++ b/testdata/golden/TestReadSearch/repo_v16.00000.golden @@ -9,9 +9,9 @@ "Language": "Go", "LineMatches": [ { - "Line": "ZnVuYyBtYWluKCkgew==", + "Line": "ZnVuYyBtYWluKCkgewo=", "LineStart": 69, - "LineEnd": 82, + "LineEnd": 83, "LineNumber": 10, "Before": null, "After": null, @@ -39,9 +39,9 @@ "Language": "Go", "LineMatches": [ { - "Line": "cGFja2FnZSBtYWlu", + "Line": "cGFja2FnZSBtYWluCg==", "LineStart": 0, - "LineEnd": 12, + "LineEnd": 13, "LineNumber": 1, "Before": null, "After": null, diff --git a/web/e2e_test.go b/web/e2e_test.go index 1566e219b..e0be04a71 100644 --- a/web/e2e_test.go +++ b/web/e2e_test.go @@ -393,7 +393,7 @@ func TestContextLines(t *testing.T) { { Pre: "f", Match: "our", - Post: "th", + Post: "th\n", }, }, }, @@ -431,7 +431,7 @@ func TestContextLines(t *testing.T) { { Pre: "f", Match: "our", - Post: "th", + Post: "th\n", }, }, Before: "second snippet\nthird thing\n", @@ -453,7 +453,7 @@ func TestContextLines(t *testing.T) { { Pre: "", Match: "one", - Post: " line", + Post: " line\n", }, }, After: "second snippet\nthird thing\n", @@ -516,7 +516,7 @@ func TestContextLines(t *testing.T) { { Pre: "", Match: "one", - Post: " line", + Post: " line\n", }, }, After: "second snippet\nthird thing\nfourth\nfifth block\nsixth example\nseventh", @@ -537,6 +537,7 @@ func TestContextLines(t *testing.T) { { Pre: "\t", Match: "trois", + Post: "\n", }, }, Before: "un \n \n", @@ -558,7 +559,7 @@ func TestContextLines(t *testing.T) { { Pre: "to carry ", Match: "water", - Post: " in the no later bla", + Post: " in the no later bla\n", }, }, Before: "\n\n\n\n", @@ -580,6 +581,7 @@ func TestContextLines(t *testing.T) { { Pre: "", Match: "pastures", + Post: "\n", }, }, Before: "green\n",