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 {
RepoID int64 // ignored
Filename string
CommitID string // branch
UpdatedUnix timeutil.TimeStamp // ignored
Language string
Color string
LineNumbers []int64
FormattedLines template.HTML
RepoID int64 // ignored
Filename string
CommitID string // branch
UpdatedUnix timeutil.TimeStamp // ignored
Language string
Color string
Lines []ResultLine
}
type ResultLine struct {
Num int64
FormattedContent template.HTML
}
const pHEAD = "HEAD:"
@ -33,12 +37,12 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
if err != nil {
return nil, err
}
data := []*Result{}
stdout, _, err := git.NewCommand(ctx,
"grep",
"-1", // n before and after lines
"-1", // n before and after lines
"-z",
"--heading",
"--break", // easier parsing
@ -46,7 +50,8 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
"-n", // line nums
"-i", // ignore case
"--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).
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") {
res := Result{CommitID: repo.DefaultBranch}
linenum := []int64{}
code := []string{}
for _, line := range strings.Split(block, "\n") {
@ -71,18 +78,32 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
continue
}
res.LineNumbers = append(res.LineNumbers, i)
linenum = append(linenum, i)
code = append(code, after)
}
}
if res.Filename == "" || len(code) == 0 || len(res.LineNumbers) == 0 {
if res.Filename == "" || len(code) == 0 || len(linenum) == 0 {
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)
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)
}

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>
</h4>
<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>
{{template "shared/searchbottom" dict "root" $ "result" .}}
</div>

View file

@ -4,7 +4,7 @@
{{range .SearchResult.Lines}}
<tr>
<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 class="lines-code chroma"><code class="code-inner">{{.FormattedContent}}</code></td>
</tr>