Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yp05327 committed Feb 19, 2024
1 parent 5e72526 commit afaa60e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
39 changes: 17 additions & 22 deletions modules/indexer/code/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ 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 map[int]template.HTML
}

type SearchResultLanguages = internal.SearchResultLanguages
Expand Down Expand Up @@ -67,12 +66,11 @@ 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")
lineNumbers := make([]int, len(contentLines))
lines := make(map[int]template.HTML, len(contentLines))
index := startIndex
for i, line := range contentLines {
var formattedLinesBuffer bytes.Buffer
var err error
if index < result.EndIndex &&
result.StartIndex < index+len(line) &&
Expand All @@ -93,21 +91,18 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
return nil, err
}

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

highlighted, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())

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
8 changes: 4 additions & 4 deletions templates/code/searchresults.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
<div class="file-body file-code code-view">
<table>
<tbody>
{{range $k, $line := .Lines}}
<tr>
<td class="lines-num">
{{range .LineNumbers}}
<a href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{$result.Filename | PathEscapeSegments}}#L{{.}}"><span>{{.}}</span></a>
{{end}}
<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">{{.FormattedLines}}</code></td>
<td class="lines-code chroma"><code class="code-inner">{{$line}}</code></td>
</tr>
{{end}}
</tbody>
</table>
</div>
Expand Down
8 changes: 4 additions & 4 deletions templates/repo/search.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@
<div class="file-body file-code code-view">
<table>
<tbody>
{{range $k, $line := .Lines}}
<tr>
<td class="lines-num">
{{range .LineNumbers}}
<a href="{{$.SourcePath}}/src/commit/{{PathEscape $result.CommitID}}/{{PathEscapeSegments $result.Filename}}#L{{.}}"><span>{{.}}</span></a>
{{end}}
<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">{{.FormattedLines}}</code></td>
<td class="lines-code chroma"><code class="code-inner">{{$line}}</code></td>
</tr>
{{end}}
</tbody>
</table>
</div>
Expand Down

0 comments on commit afaa60e

Please sign in to comment.