feat: highlighted code search results (#4749)
closes #4534 <details> <summary>Screenshots</summary>  </details> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4749 Reviewed-by: 0ko <0ko@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
517637137c
commit
06d2e90fa4
10 changed files with 214 additions and 75 deletions
|
@ -1,4 +1,5 @@
|
|||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package git
|
||||
|
@ -19,9 +20,10 @@ import (
|
|||
)
|
||||
|
||||
type GrepResult struct {
|
||||
Filename string
|
||||
LineNumbers []int
|
||||
LineCodes []string
|
||||
Filename string
|
||||
LineNumbers []int
|
||||
LineCodes []string
|
||||
HighlightedRanges [][3]int
|
||||
}
|
||||
|
||||
type GrepOptions struct {
|
||||
|
@ -33,6 +35,13 @@ type GrepOptions struct {
|
|||
PathSpec []setting.Glob
|
||||
}
|
||||
|
||||
func hasPrefixFold(s, t string) bool {
|
||||
if len(s) < len(t) {
|
||||
return false
|
||||
}
|
||||
return strings.EqualFold(s[:len(t)], t)
|
||||
}
|
||||
|
||||
func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepOptions) ([]*GrepResult, error) {
|
||||
stdoutReader, stdoutWriter, err := os.Pipe()
|
||||
if err != nil {
|
||||
|
@ -53,18 +62,19 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
|
|||
2^@repo: go-gitea/gitea
|
||||
*/
|
||||
var results []*GrepResult
|
||||
cmd := NewCommand(ctx, "grep", "--null", "--break", "--heading", "--fixed-strings", "--line-number", "--ignore-case", "--full-name")
|
||||
cmd := NewCommand(ctx, "grep",
|
||||
"--null", "--break", "--heading", "--column",
|
||||
"--fixed-strings", "--line-number", "--ignore-case", "--full-name")
|
||||
cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber))
|
||||
if opts.MatchesPerFile > 0 {
|
||||
cmd.AddOptionValues("--max-count", fmt.Sprint(opts.MatchesPerFile))
|
||||
}
|
||||
words := []string{search}
|
||||
if opts.IsFuzzy {
|
||||
words := strings.Fields(search)
|
||||
for _, word := range words {
|
||||
cmd.AddOptionValues("-e", strings.TrimLeft(word, "-"))
|
||||
}
|
||||
} else {
|
||||
cmd.AddOptionValues("-e", strings.TrimLeft(search, "-"))
|
||||
words = strings.Fields(search)
|
||||
}
|
||||
for _, word := range words {
|
||||
cmd.AddOptionValues("-e", strings.TrimLeft(word, "-"))
|
||||
}
|
||||
|
||||
// pathspec
|
||||
|
@ -128,6 +138,24 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
|
|||
if lineNum, lineCode, ok := strings.Cut(line, "\x00"); ok {
|
||||
lineNumInt, _ := strconv.Atoi(lineNum)
|
||||
res.LineNumbers = append(res.LineNumbers, lineNumInt)
|
||||
if lineCol, lineCode2, ok := strings.Cut(lineCode, "\x00"); ok {
|
||||
lineColInt, _ := strconv.Atoi(lineCol)
|
||||
start := lineColInt - 1
|
||||
matchLen := len(lineCode2)
|
||||
for _, word := range words {
|
||||
if hasPrefixFold(lineCode2[start:], word) {
|
||||
matchLen = len(word)
|
||||
break
|
||||
}
|
||||
}
|
||||
res.HighlightedRanges = append(res.HighlightedRanges, [3]int{
|
||||
len(res.LineCodes),
|
||||
start,
|
||||
start + matchLen,
|
||||
})
|
||||
res.LineCodes = append(res.LineCodes, lineCode2)
|
||||
continue
|
||||
}
|
||||
res.LineCodes = append(res.LineCodes, lineCode)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,28 +20,43 @@ func TestGrepSearch(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
defer repo.Close()
|
||||
|
||||
res, err := GrepSearch(context.Background(), repo, "void", GrepOptions{})
|
||||
res, err := GrepSearch(context.Background(), repo, "public", GrepOptions{})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []*GrepResult{
|
||||
{
|
||||
Filename: "java-hello/main.java",
|
||||
LineNumbers: []int{3},
|
||||
LineCodes: []string{" public static void main(String[] args)"},
|
||||
LineNumbers: []int{1, 3},
|
||||
LineCodes: []string{
|
||||
"public class HelloWorld",
|
||||
" public static void main(String[] args)",
|
||||
},
|
||||
HighlightedRanges: [][3]int{{0, 0, 6}, {1, 1, 7}},
|
||||
},
|
||||
{
|
||||
Filename: "main.vendor.java",
|
||||
LineNumbers: []int{3},
|
||||
LineCodes: []string{" public static void main(String[] args)"},
|
||||
LineNumbers: []int{1, 3},
|
||||
LineCodes: []string{
|
||||
"public class HelloWorld",
|
||||
" public static void main(String[] args)",
|
||||
},
|
||||
HighlightedRanges: [][3]int{{0, 0, 6}, {1, 1, 7}},
|
||||
},
|
||||
}, res)
|
||||
|
||||
res, err = GrepSearch(context.Background(), repo, "void", GrepOptions{MaxResultLimit: 1})
|
||||
res, err = GrepSearch(context.Background(), repo, "void", GrepOptions{MaxResultLimit: 1, ContextLineNumber: 2})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []*GrepResult{
|
||||
{
|
||||
Filename: "java-hello/main.java",
|
||||
LineNumbers: []int{3},
|
||||
LineCodes: []string{" public static void main(String[] args)"},
|
||||
LineNumbers: []int{1, 2, 3, 4, 5},
|
||||
LineCodes: []string{
|
||||
"public class HelloWorld",
|
||||
"{",
|
||||
" public static void main(String[] args)",
|
||||
" {",
|
||||
" System.out.println(\"Hello world!\");",
|
||||
},
|
||||
HighlightedRanges: [][3]int{{2, 15, 19}},
|
||||
},
|
||||
}, res)
|
||||
|
||||
|
@ -49,24 +64,28 @@ func TestGrepSearch(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
assert.Equal(t, []*GrepResult{
|
||||
{
|
||||
Filename: "i-am-a-python.p",
|
||||
LineNumbers: []int{1},
|
||||
LineCodes: []string{"## This is a simple file to do a hello world"},
|
||||
Filename: "i-am-a-python.p",
|
||||
LineNumbers: []int{1},
|
||||
LineCodes: []string{"## This is a simple file to do a hello world"},
|
||||
HighlightedRanges: [][3]int{{0, 39, 44}},
|
||||
},
|
||||
{
|
||||
Filename: "java-hello/main.java",
|
||||
LineNumbers: []int{1},
|
||||
LineCodes: []string{"public class HelloWorld"},
|
||||
Filename: "java-hello/main.java",
|
||||
LineNumbers: []int{1},
|
||||
LineCodes: []string{"public class HelloWorld"},
|
||||
HighlightedRanges: [][3]int{{0, 18, 23}},
|
||||
},
|
||||
{
|
||||
Filename: "main.vendor.java",
|
||||
LineNumbers: []int{1},
|
||||
LineCodes: []string{"public class HelloWorld"},
|
||||
Filename: "main.vendor.java",
|
||||
LineNumbers: []int{1},
|
||||
LineCodes: []string{"public class HelloWorld"},
|
||||
HighlightedRanges: [][3]int{{0, 18, 23}},
|
||||
},
|
||||
{
|
||||
Filename: "python-hello/hello.py",
|
||||
LineNumbers: []int{1},
|
||||
LineCodes: []string{"## This is a simple file to do a hello world"},
|
||||
Filename: "python-hello/hello.py",
|
||||
LineNumbers: []int{1},
|
||||
LineCodes: []string{"## This is a simple file to do a hello world"},
|
||||
HighlightedRanges: [][3]int{{0, 39, 44}},
|
||||
},
|
||||
}, res)
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"code.gitea.io/gitea/modules/highlight"
|
||||
"code.gitea.io/gitea/modules/indexer/code/internal"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/services/gitdiff"
|
||||
)
|
||||
|
||||
// Result a search result to display
|
||||
|
@ -70,11 +71,85 @@ func writeStrings(buf *bytes.Buffer, strs ...string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func HighlightSearchResultCode(filename string, lineNums []int, code string) []ResultLine {
|
||||
const (
|
||||
highlightTagStart = "<span class=\"search-highlight\">"
|
||||
highlightTagEnd = "</span>"
|
||||
)
|
||||
|
||||
func HighlightSearchResultCode(filename string, lineNums []int, highlightRanges [][3]int, code string) []ResultLine {
|
||||
hcd := gitdiff.NewHighlightCodeDiff()
|
||||
hcd.CollectUsedRunes(code)
|
||||
startTag, endTag := hcd.NextPlaceholder(), hcd.NextPlaceholder()
|
||||
hcd.PlaceholderTokenMap[startTag] = highlightTagStart
|
||||
hcd.PlaceholderTokenMap[endTag] = highlightTagEnd
|
||||
|
||||
// we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting
|
||||
hl, _ := highlight.Code(filename, "", code)
|
||||
highlightedLines := strings.Split(string(hl), "\n")
|
||||
conv := hcd.ConvertToPlaceholders(string(hl))
|
||||
convLines := strings.Split(conv, "\n")
|
||||
|
||||
// each highlightRange is of the form [line number, start pos, end pos]
|
||||
for _, highlightRange := range highlightRanges {
|
||||
ln, start, end := highlightRange[0], highlightRange[1], highlightRange[2]
|
||||
line := convLines[ln]
|
||||
if line == "" || len(line) <= start || len(line) < end {
|
||||
continue
|
||||
}
|
||||
|
||||
sb := strings.Builder{}
|
||||
count := -1
|
||||
isOpen := false
|
||||
for _, r := range line {
|
||||
if token, ok := hcd.PlaceholderTokenMap[r];
|
||||
// token was not found
|
||||
!ok ||
|
||||
// token was marked as used
|
||||
token == "" ||
|
||||
// the token is not an valid html tag emited by chroma
|
||||
!(len(token) > 6 && (token[0:5] == "<span" || token[0:6] == "</span")) {
|
||||
count++
|
||||
} else if !isOpen {
|
||||
// open the tag only after all other placeholders
|
||||
sb.WriteRune(r)
|
||||
continue
|
||||
} else if isOpen && count < end {
|
||||
// if the tag is open, but a placeholder exists in between
|
||||
// close the tag
|
||||
sb.WriteRune(endTag)
|
||||
// write the placeholder
|
||||
sb.WriteRune(r)
|
||||
// reopen the tag
|
||||
sb.WriteRune(startTag)
|
||||
continue
|
||||
}
|
||||
|
||||
switch count {
|
||||
case end:
|
||||
// if tag is not open, no need to close
|
||||
if !isOpen {
|
||||
break
|
||||
}
|
||||
sb.WriteRune(endTag)
|
||||
isOpen = false
|
||||
case start:
|
||||
// if tag is open, do not open again
|
||||
if isOpen {
|
||||
break
|
||||
}
|
||||
isOpen = true
|
||||
sb.WriteRune(startTag)
|
||||
}
|
||||
|
||||
sb.WriteRune(r)
|
||||
}
|
||||
if isOpen {
|
||||
sb.WriteRune(endTag)
|
||||
}
|
||||
convLines[ln] = sb.String()
|
||||
}
|
||||
conv = strings.Join(convLines, "\n")
|
||||
|
||||
highlightedLines := strings.Split(hcd.Recover(conv), "\n")
|
||||
// The lineNums outputted by highlight.Code might not match the original lineNums, because "highlight" removes the last `\n`
|
||||
lines := make([]ResultLine, min(len(highlightedLines), len(lineNums)))
|
||||
for i := 0; i < len(lines); i++ {
|
||||
|
@ -92,6 +167,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
|
|||
contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n")
|
||||
lineNums := make([]int, 0, len(contentLines))
|
||||
index := startIndex
|
||||
var highlightRanges [][3]int
|
||||
for i, line := range contentLines {
|
||||
var err error
|
||||
if index < result.EndIndex &&
|
||||
|
@ -99,6 +175,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
|
|||
result.StartIndex < result.EndIndex {
|
||||
openActiveIndex := max(result.StartIndex-index, 0)
|
||||
closeActiveIndex := min(result.EndIndex-index, len(line))
|
||||
highlightRanges = append(highlightRanges, [3]int{i, openActiveIndex, closeActiveIndex})
|
||||
err = writeStrings(&formattedLinesBuffer,
|
||||
line[:openActiveIndex],
|
||||
line[openActiveIndex:closeActiveIndex],
|
||||
|
@ -122,7 +199,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
|
|||
UpdatedUnix: result.UpdatedUnix,
|
||||
Language: result.Language,
|
||||
Color: result.Color,
|
||||
Lines: HighlightSearchResultCode(result.Filename, lineNums, formattedLinesBuffer.String()),
|
||||
Lines: HighlightSearchResultCode(result.Filename, lineNums, highlightRanges, formattedLinesBuffer.String()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue