Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
yp05327 committed Feb 20, 2024
1 parent afaa60e commit aa90c58
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
31 changes: 26 additions & 5 deletions modules/indexer/code/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package code
import (
"bytes"
"context"
"html/template"
"fmt"
"strings"

"code.gitea.io/gitea/modules/highlight"
Expand All @@ -22,7 +22,13 @@ type Result struct {
UpdatedUnix timeutil.TimeStamp
Language string
Color string
Lines map[int]template.HTML
Lines []ResultLine
}

type ResultLine struct {
Num int
FormattedContent string
IsActive bool
}

type SearchResultLanguages = internal.SearchResultLanguages
Expand Down Expand Up @@ -66,12 +72,15 @@ 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")
lines := make(map[int]template.HTML, len(contentLines))
lines := make([]ResultLine, 0, len(contentLines))
index := startIndex
for i, line := range contentLines {
var formattedLinesBuffer bytes.Buffer
var err error
l := ResultLine{Num: startLineNum + i}

if index < result.EndIndex &&
result.StartIndex < index+len(line) &&
result.StartIndex < result.EndIndex {
Expand All @@ -82,6 +91,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
line[openActiveIndex:closeActiveIndex],
line[closeActiveIndex:],
)
l.IsActive = true
} else {
err = writeStrings(&formattedLinesBuffer,
line,
Expand All @@ -91,10 +101,21 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
return nil, err
}

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

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 = highlightedLines[i]
}

return &Result{
RepoID: result.RepoID,
Filename: result.Filename,
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>
{{range $k, $line := .Lines}}
<tr>
<td class="lines-num">
<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">{{$line}}</code></td>
</tr>
{{end}}
</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>
{{range $k, $line := .Lines}}
<tr>
<td class="lines-num">
<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">{{$line}}</code></td>
</tr>
{{end}}
</tbody>
</table>
</div>
{{template "shared/searchfile" dict "repolink" $.SourcePath "result" .}}
</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 {{if .IsActive}}active{{end}}"><code class="code-inner">{{.FormattedContent}}</code></td>
</tr>
{{end}}
</tbody>
</table>
</div>

0 comments on commit aa90c58

Please sign in to comment.