Skip to content

Fix wrong line number in code search result #29260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions modules/indexer/code/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ import (

// Result a search result to display
type Result struct {
RepoID int64
Filename string
CommitID string
UpdatedUnix timeutil.TimeStamp
Language string
Color string
LineNumbers []int
FormattedLines template.HTML
RepoID int64
Filename string
CommitID string
UpdatedUnix timeutil.TimeStamp
Language string
Color string
Lines map[int]template.HTML
}

type SearchResultLanguages = internal.SearchResultLanguages
Expand Down Expand Up @@ -67,12 +66,11 @@ func writeStrings(buf *bytes.Buffer, strs ...string) error {
func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Result, error) {
startLineNum := 1 + strings.Count(result.Content[:startIndex], "\n")

var formattedLinesBuffer bytes.Buffer

contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n")
lineNumbers := make([]int, len(contentLines))
lines := make(map[int]template.HTML, len(contentLines))
index := startIndex
for i, line := range contentLines {
var formattedLinesBuffer bytes.Buffer
var err error
if index < result.EndIndex &&
result.StartIndex < index+len(line) &&
Expand All @@ -93,21 +91,18 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
return nil, err
}

lineNumbers[i] = startLineNum + i
lines[startLineNum+i], _ = highlight.Code(result.Filename, "", formattedLinesBuffer.String())
index += len(line)
}

highlighted, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())

return &Result{
RepoID: result.RepoID,
Filename: result.Filename,
CommitID: result.CommitID,
UpdatedUnix: result.UpdatedUnix,
Language: result.Language,
Color: result.Color,
LineNumbers: lineNumbers,
FormattedLines: highlighted,
RepoID: result.RepoID,
Filename: result.Filename,
CommitID: result.CommitID,
UpdatedUnix: result.UpdatedUnix,
Language: result.Language,
Color: result.Color,
Lines: lines,
}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions templates/code/searchresults.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
<div class="file-body file-code code-view">
<table>
<tbody>
{{range $k, $line := .Lines}}
<tr>
<td class="lines-num">
{{range .LineNumbers}}
<a href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{$result.Filename | PathEscapeSegments}}#L{{.}}"><span>{{.}}</span></a>
{{end}}
<a href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{$result.Filename | PathEscapeSegments}}#L{{$k}}"><span>{{$k}}</span></a>
</td>
<td class="lines-code chroma"><code class="code-inner">{{.FormattedLines}}</code></td>
<td class="lines-code chroma"><code class="code-inner">{{$line}}</code></td>
</tr>
{{end}}
</tbody>
</table>
</div>
Expand Down
8 changes: 4 additions & 4 deletions templates/repo/search.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@
<div class="file-body file-code code-view">
<table>
<tbody>
{{range $k, $line := .Lines}}
<tr>
<td class="lines-num">
{{range .LineNumbers}}
<a href="{{$.SourcePath}}/src/commit/{{PathEscape $result.CommitID}}/{{PathEscapeSegments $result.Filename}}#L{{.}}"><span>{{.}}</span></a>
{{end}}
<a href="{{$.SourcePath}}/src/commit/{{PathEscape $result.CommitID}}/{{PathEscapeSegments $result.Filename}}#L{{$k}}"><span>{{$k}}</span></a>
</td>
<td class="lines-code chroma"><code class="code-inner">{{.FormattedLines}}</code></td>
<td class="lines-code chroma"><code class="code-inner">{{$line}}</code></td>
</tr>
{{end}}
</tbody>
</table>
</div>
Expand Down