Skip to content

Commit dd3aaae

Browse files
committed
improve
1 parent afaa60e commit dd3aaae

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

modules/indexer/code/search.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package code
66
import (
77
"bytes"
88
"context"
9-
"html/template"
9+
"fmt"
1010
"strings"
1111

1212
"code.gitea.io/gitea/modules/highlight"
@@ -22,7 +22,12 @@ type Result struct {
2222
UpdatedUnix timeutil.TimeStamp
2323
Language string
2424
Color string
25-
Lines map[int]template.HTML
25+
Lines []ResultLine
26+
}
27+
28+
type ResultLine struct {
29+
Num int
30+
FormattedContent string
2631
}
2732

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

74+
var formattedLinesBuffer bytes.Buffer
75+
6976
contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n")
70-
lines := make(map[int]template.HTML, len(contentLines))
77+
lines := make([]ResultLine, 0, len(contentLines))
7178
index := startIndex
7279
for i, line := range contentLines {
73-
var formattedLinesBuffer bytes.Buffer
7480
var err error
7581
if index < result.EndIndex &&
7682
result.StartIndex < index+len(line) &&
@@ -91,10 +97,21 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
9197
return nil, err
9298
}
9399

94-
lines[startLineNum+i], _ = highlight.Code(result.Filename, "", formattedLinesBuffer.String())
100+
lines = append(lines, ResultLine{Num: startLineNum + i})
95101
index += len(line)
96102
}
97103

104+
hl, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())
105+
highlightedLines := strings.Split(string(hl), "\n")
106+
107+
if len(highlightedLines) != len(lines) {
108+
return nil, fmt.Errorf("the length of line numbers [%d] don't match the length of highlighted contents [%d]", len(lines), len(highlightedLines))
109+
}
110+
111+
for i := 0; i < len(lines); i++ {
112+
lines[i].FormattedContent = highlightedLines[i]
113+
}
114+
98115
return &Result{
99116
RepoID: result.RepoID,
100117
Filename: result.Filename,

templates/code/searchresults.tmpl

Lines changed: 1 addition & 14 deletions
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-
{{range $k, $line := .Lines}}
29-
<tr>
30-
<td class="lines-num">
31-
<a href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{$result.Filename | PathEscapeSegments}}#L{{$k}}"><span>{{$k}}</span></a>
32-
</td>
33-
<td class="lines-code chroma"><code class="code-inner">{{$line}}</code></td>
34-
</tr>
35-
{{end}}
36-
</tbody>
37-
</table>
38-
</div>
25+
{{template "shared/searchfile" dict "repolink" $repo.Link "result" .}}
3926
</div>
4027
{{template "shared/searchbottom" dict "root" $ "result" .}}
4128
</div>

templates/repo/search.tmpl

Lines changed: 1 addition & 14 deletions
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-
{{range $k, $line := .Lines}}
51-
<tr>
52-
<td class="lines-num">
53-
<a href="{{$.SourcePath}}/src/commit/{{PathEscape $result.CommitID}}/{{PathEscapeSegments $result.Filename}}#L{{$k}}"><span>{{$k}}</span></a>
54-
</td>
55-
<td class="lines-code chroma"><code class="code-inner">{{$line}}</code></td>
56-
</tr>
57-
{{end}}
58-
</tbody>
59-
</table>
60-
</div>
47+
{{template "shared/searchfile" dict "repolink" $.SourcePath "result" .}}
6148
</div>
6249
{{template "shared/searchbottom" dict "root" $ "result" .}}
6350
</div>

templates/shared/searchfile.tmpl

Lines changed: 14 additions & 0 deletions
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 .result.Lines}}
5+
<tr>
6+
<td class="lines-num">
7+
<a href="{{$.repolink}}/src/commit/{{PathEscape $.result.CommitID}}/{{PathEscapeSegments $.result.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)