mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-31 20:02:09 +00:00
[PORT] Fix wrong line number in code search result (gitea#29260) (#2619)
Port [Fix wrong line number in code search result (gitea#29260)](https://github.com/go-gitea/gitea/pull/29260) PS: also added [`-e`](https://git-scm.com/docs/git-grep#Documentation/git-grep.txt--e) before passing the keyword (my bad) Co-authored-by: yp05327 <576951401@qq.com> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2619 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Shiny Nematoda <snematoda.751k2@aleeas.com> Co-committed-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
This commit is contained in:
parent
79f8339c74
commit
3b2f28ff1c
6 changed files with 90 additions and 67 deletions
|
@ -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:"
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -25,14 +25,16 @@ func TestNewRepoGrep(t *testing.T) {
|
|||
|
||||
expected := []*Result{
|
||||
{
|
||||
RepoID: 0,
|
||||
Filename: "README.md",
|
||||
CommitID: "master",
|
||||
UpdatedUnix: 0,
|
||||
Language: "Markdown",
|
||||
Color: "#083fa1",
|
||||
LineNumbers: []int64{2, 3},
|
||||
FormattedLines: "\nDescription for repo1",
|
||||
RepoID: 0,
|
||||
Filename: "README.md",
|
||||
CommitID: "master",
|
||||
UpdatedUnix: 0,
|
||||
Language: "Markdown",
|
||||
Color: "#083fa1",
|
||||
Lines: []ResultLine{
|
||||
{Num: 2, FormattedContent: ""},
|
||||
{Num: 3, FormattedContent: "Description for repo1"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue