Skip to content
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

Fix wrong line number in code search result #29260

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
52 changes: 33 additions & 19 deletions modules/indexer/code/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package code
import (
"bytes"
"context"
"fmt"
"html/template"
"strings"

Expand All @@ -16,14 +17,18 @@ 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 []ResultLine
}

type ResultLine struct {
Num int
FormattedContent template.HTML
}

type SearchResultLanguages = internal.SearchResultLanguages
Expand Down Expand Up @@ -70,7 +75,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
var formattedLinesBuffer bytes.Buffer

contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n")
lineNumbers := make([]int, len(contentLines))
lines := make([]ResultLine, 0, len(contentLines))
index := startIndex
for i, line := range contentLines {
var err error
Expand All @@ -93,21 +98,30 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
return nil, err
}

lineNumbers[i] = startLineNum + i
lines = append(lines, ResultLine{Num: startLineNum + i})
index += len(line)
}

highlighted, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())
// we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting
hl, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())
highlightedLines := strings.Split(string(hl), "\n")

if len(highlightedLines) != len(lines) {
return nil, fmt.Errorf("the length of line numbers [%d] don't match the length of highlighted contents [%d]", len(lines), len(highlightedLines))
}

for i := 0; i < len(lines); i++ {
lines[i].FormattedContent = template.HTML(highlightedLines[i])
}

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
15 changes: 1 addition & 14 deletions templates/code/searchresults.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,7 @@
<a role="button" class="ui basic tiny button" rel="nofollow" href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{.Filename | PathEscapeSegments}}">{{ctx.Locale.Tr "repo.diff.view_file"}}</a>
</h4>
<div class="ui attached table segment">
<div class="file-body file-code code-view">
<table>
<tbody>
<tr>
<td class="lines-num">
{{range .LineNumbers}}
<a href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{$result.Filename | PathEscapeSegments}}#L{{.}}"><span>{{.}}</span></a>
{{end}}
</td>
<td class="lines-code chroma"><code class="code-inner">{{.FormattedLines}}</code></td>
</tr>
</tbody>
</table>
</div>
{{template "shared/searchfile" dict "repolink" $repo.Link "result" .}}
</div>
{{template "shared/searchbottom" dict "root" $ "result" .}}
</div>
Expand Down
15 changes: 1 addition & 14 deletions templates/repo/search.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,7 @@
<a role="button" class="ui basic tiny button" rel="nofollow" href="{{$.SourcePath}}/src/commit/{{PathEscape $result.CommitID}}/{{PathEscapeSegments .Filename}}">{{ctx.Locale.Tr "repo.diff.view_file"}}</a>
</h4>
<div class="ui attached table segment">
<div class="file-body file-code code-view">
<table>
<tbody>
<tr>
<td class="lines-num">
{{range .LineNumbers}}
<a href="{{$.SourcePath}}/src/commit/{{PathEscape $result.CommitID}}/{{PathEscapeSegments $result.Filename}}#L{{.}}"><span>{{.}}</span></a>
{{end}}
</td>
<td class="lines-code chroma"><code class="code-inner">{{.FormattedLines}}</code></td>
</tr>
</tbody>
</table>
</div>
{{template "shared/searchfile" dict "repolink" $.SourcePath "result" .}}
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
</div>
{{template "shared/searchbottom" dict "root" $ "result" .}}
</div>
Expand Down
14 changes: 14 additions & 0 deletions templates/shared/searchfile.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="file-body file-code code-view">
<table>
<tbody>
{{range .result.Lines}}
<tr>
<td class="lines-num">
<a href="{{$.repolink}}/src/commit/{{PathEscape $.result.CommitID}}/{{PathEscapeSegments $.result.Filename}}#L{{.Num}}"><span>{{.Num}}</span></a>
</td>
<td class="lines-code chroma"><code class="code-inner">{{.FormattedContent}}</code></td>
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
</tr>
{{end}}
</tbody>
</table>
</div>
Loading