Skip to content

Commit afaa60e

Browse files
committed
fix
1 parent 5e72526 commit afaa60e

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

modules/indexer/code/search.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ 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 map[int]template.HTML
2726
}
2827

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

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

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

100-
highlighted, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())
101-
10298
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,
99+
RepoID: result.RepoID,
100+
Filename: result.Filename,
101+
CommitID: result.CommitID,
102+
UpdatedUnix: result.UpdatedUnix,
103+
Language: result.Language,
104+
Color: result.Color,
105+
Lines: lines,
111106
}, nil
112107
}
113108

templates/code/searchresults.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
<div class="file-body file-code code-view">
2626
<table>
2727
<tbody>
28+
{{range $k, $line := .Lines}}
2829
<tr>
2930
<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}}
31+
<a href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{$result.Filename | PathEscapeSegments}}#L{{$k}}"><span>{{$k}}</span></a>
3332
</td>
34-
<td class="lines-code chroma"><code class="code-inner">{{.FormattedLines}}</code></td>
33+
<td class="lines-code chroma"><code class="code-inner">{{$line}}</code></td>
3534
</tr>
35+
{{end}}
3636
</tbody>
3737
</table>
3838
</div>

templates/repo/search.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@
4747
<div class="file-body file-code code-view">
4848
<table>
4949
<tbody>
50+
{{range $k, $line := .Lines}}
5051
<tr>
5152
<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}}
53+
<a href="{{$.SourcePath}}/src/commit/{{PathEscape $result.CommitID}}/{{PathEscapeSegments $result.Filename}}#L{{$k}}"><span>{{$k}}</span></a>
5554
</td>
56-
<td class="lines-code chroma"><code class="code-inner">{{.FormattedLines}}</code></td>
55+
<td class="lines-code chroma"><code class="code-inner">{{$line}}</code></td>
5756
</tr>
57+
{{end}}
5858
</tbody>
5959
</table>
6060
</div>

0 commit comments

Comments
 (0)