This commit is contained in:
Shiny Nematoda 2024-03-10 10:21:11 +00:00
parent 95065e2ad1
commit 71a72d8a22
3 changed files with 37 additions and 16 deletions

View file

@ -16,14 +16,18 @@ import (
) )
type Result struct { type Result struct {
RepoID int64 // ignored RepoID int64 // ignored
Filename string Filename string
CommitID string // branch CommitID string // branch
UpdatedUnix timeutil.TimeStamp // ignored UpdatedUnix timeutil.TimeStamp // ignored
Language string Language string
Color string Color string
LineNumbers []int64 Lines []ResultLine
FormattedLines template.HTML }
type ResultLine struct {
Num int64
FormattedContent template.HTML
} }
const pHEAD = "HEAD:" const pHEAD = "HEAD:"
@ -33,12 +37,12 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
if err != nil { if err != nil {
return nil, err return nil, err
} }
data := []*Result{} data := []*Result{}
stdout, _, err := git.NewCommand(ctx, stdout, _, err := git.NewCommand(ctx,
"grep", "grep",
"-1", // n before and after lines "-1", // n before and after lines
"-z", "-z",
"--heading", "--heading",
"--break", // easier parsing "--break", // easier parsing
@ -46,7 +50,8 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
"-n", // line nums "-n", // line nums
"-i", // ignore case "-i", // ignore case
"--full-name", // full file path, rel to repo "--full-name", // full file path, rel to repo
//"--column", // for adding better highlighting support //"--column", // for adding better highlighting support
"-e", // for queries starting with "-"
). ).
AddDynamicArguments(keyword). AddDynamicArguments(keyword).
AddArguments("HEAD"). AddArguments("HEAD").
@ -57,6 +62,8 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
for _, block := range strings.Split(stdout, "\n\n") { for _, block := range strings.Split(stdout, "\n\n") {
res := Result{CommitID: repo.DefaultBranch} res := Result{CommitID: repo.DefaultBranch}
linenum := []int64{}
code := []string{} code := []string{}
for _, line := range strings.Split(block, "\n") { for _, line := range strings.Split(block, "\n") {
@ -71,18 +78,32 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
continue continue
} }
res.LineNumbers = append(res.LineNumbers, i) linenum = append(linenum, i)
code = append(code, after) code = append(code, after)
} }
} }
if res.Filename == "" || len(code) == 0 || len(res.LineNumbers) == 0 { if res.Filename == "" || len(code) == 0 || len(linenum) == 0 {
continue continue
} }
res.FormattedLines, res.Language = highlight.Code(res.Filename, "", strings.Join(code, "\n")) var hl template.HTML
hl, res.Language = highlight.Code(res.Filename, "", strings.Join(code, "\n"))
res.Color = enry.GetColor(res.Language) res.Color = enry.GetColor(res.Language)
hlCode := strings.Split(string(hl), "\n")
n := min(len(hlCode), len(linenum))
res.Lines = make([]ResultLine, n)
for i := 0; i < n; i++ {
res.Lines[i] = ResultLine{
Num: linenum[i],
FormattedContent: template.HTML(hlCode[i]),
}
}
data = append(data, &res) data = append(data, &res)
} }

View file

@ -22,7 +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> <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> </h4>
<div class="ui attached table segment"> <div class="ui attached table segment">
{{template "shared/searchfile" dict "RepoLink" $repo.Link "SearchResult" .}} {{template "shared/searchfile" dict "RepoLink" $repo.Link "IsIndexer" true "SearchResult" .}}
</div> </div>
{{template "shared/searchbottom" dict "root" $ "result" .}} {{template "shared/searchbottom" dict "root" $ "result" .}}
</div> </div>

View file

@ -4,7 +4,7 @@
{{range .SearchResult.Lines}} {{range .SearchResult.Lines}}
<tr> <tr>
<td class="lines-num"> <td class="lines-num">
<a href="{{$.RepoLink}}/src/commit/{{PathEscape $.SearchResult.CommitID}}/{{PathEscapeSegments $.SearchResult.Filename}}#L{{.Num}}"><span>{{.Num}}</span></a> <a href="{{$.RepoLink}}/src/{{if not $.IsIndexer}}branch{{else}}commit{{end}}/{{PathEscape $.SearchResult.CommitID}}/{{PathEscapeSegments $.SearchResult.Filename}}#L{{.Num}}"><span>{{.Num}}</span></a>
</td> </td>
<td class="lines-code chroma"><code class="code-inner">{{.FormattedContent}}</code></td> <td class="lines-code chroma"><code class="code-inner">{{.FormattedContent}}</code></td>
</tr> </tr>