Skip to content

Commit a2b0fb1

Browse files
yp05327wxiaoguang
andauthored
Fix wrong line number in code search result (#29260)
Fix #29136 Before: The result is a table and all line numbers are all in one row. After: Use a separate table column for the line numbers. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 5cddab4 commit a2b0fb1

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

modules/indexer/code/search.go

+31-19
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ import (
1616

1717
// Result a search result to display
1818
type Result struct {
19-
RepoID int64
20-
Filename string
21-
CommitID string
22-
UpdatedUnix timeutil.TimeStamp
23-
Language string
24-
Color string
25-
LineNumbers []int
26-
FormattedLines template.HTML
19+
RepoID int64
20+
Filename string
21+
CommitID string
22+
UpdatedUnix timeutil.TimeStamp
23+
Language string
24+
Color string
25+
Lines []ResultLine
26+
}
27+
28+
type ResultLine struct {
29+
Num int
30+
FormattedContent template.HTML
2731
}
2832

2933
type SearchResultLanguages = internal.SearchResultLanguages
@@ -70,7 +74,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
7074
var formattedLinesBuffer bytes.Buffer
7175

7276
contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n")
73-
lineNumbers := make([]int, len(contentLines))
77+
lines := make([]ResultLine, 0, len(contentLines))
7478
index := startIndex
7579
for i, line := range contentLines {
7680
var err error
@@ -93,21 +97,29 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
9397
return nil, err
9498
}
9599

96-
lineNumbers[i] = startLineNum + i
100+
lines = append(lines, ResultLine{Num: startLineNum + i})
97101
index += len(line)
98102
}
99103

100-
highlighted, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())
104+
// we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting
105+
hl, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())
106+
highlightedLines := strings.Split(string(hl), "\n")
107+
108+
// The lines outputted by highlight.Code might not match the original lines, because "highlight" removes the last `\n`
109+
lines = lines[:min(len(highlightedLines), len(lines))]
110+
highlightedLines = highlightedLines[:len(lines)]
111+
for i := 0; i < len(lines); i++ {
112+
lines[i].FormattedContent = template.HTML(highlightedLines[i])
113+
}
101114

102115
return &Result{
103-
RepoID: result.RepoID,
104-
Filename: result.Filename,
105-
CommitID: result.CommitID,
106-
UpdatedUnix: result.UpdatedUnix,
107-
Language: result.Language,
108-
Color: result.Color,
109-
LineNumbers: lineNumbers,
110-
FormattedLines: highlighted,
116+
RepoID: result.RepoID,
117+
Filename: result.Filename,
118+
CommitID: result.CommitID,
119+
UpdatedUnix: result.UpdatedUnix,
120+
Language: result.Language,
121+
Color: result.Color,
122+
Lines: lines,
111123
}, nil
112124
}
113125

templates/code/searchresults.tmpl

+1-14
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,7 @@
2222
<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>
2323
</h4>
2424
<div class="ui attached table segment">
25-
<div class="file-body file-code code-view">
26-
<table>
27-
<tbody>
28-
<tr>
29-
<td class="lines-num">
30-
{{range .LineNumbers}}
31-
<a href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{$result.Filename | PathEscapeSegments}}#L{{.}}"><span>{{.}}</span></a>
32-
{{end}}
33-
</td>
34-
<td class="lines-code chroma"><code class="code-inner">{{.FormattedLines}}</code></td>
35-
</tr>
36-
</tbody>
37-
</table>
38-
</div>
25+
{{template "shared/searchfile" dict "RepoLink" $repo.Link "SearchResult" .}}
3926
</div>
4027
{{template "shared/searchbottom" dict "root" $ "result" .}}
4128
</div>

templates/repo/search.tmpl

+1-14
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,7 @@
4444
<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>
4545
</h4>
4646
<div class="ui attached table segment">
47-
<div class="file-body file-code code-view">
48-
<table>
49-
<tbody>
50-
<tr>
51-
<td class="lines-num">
52-
{{range .LineNumbers}}
53-
<a href="{{$.SourcePath}}/src/commit/{{PathEscape $result.CommitID}}/{{PathEscapeSegments $result.Filename}}#L{{.}}"><span>{{.}}</span></a>
54-
{{end}}
55-
</td>
56-
<td class="lines-code chroma"><code class="code-inner">{{.FormattedLines}}</code></td>
57-
</tr>
58-
</tbody>
59-
</table>
60-
</div>
47+
{{template "shared/searchfile" dict "RepoLink" $.SourcePath "SearchResult" .}}
6148
</div>
6249
{{template "shared/searchbottom" dict "root" $ "result" .}}
6350
</div>

templates/shared/searchfile.tmpl

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div class="file-body file-code code-view">
2+
<table>
3+
<tbody>
4+
{{range .SearchResult.Lines}}
5+
<tr>
6+
<td class="lines-num">
7+
<a href="{{$.RepoLink}}/src/commit/{{PathEscape $.SearchResult.CommitID}}/{{PathEscapeSegments $.SearchResult.Filename}}#L{{.Num}}"><span>{{.Num}}</span></a>
8+
</td>
9+
<td class="lines-code chroma"><code class="code-inner">{{.FormattedContent}}</code></td>
10+
</tr>
11+
{{end}}
12+
</tbody>
13+
</table>
14+
</div>

0 commit comments

Comments
 (0)