feat: filepath filter for code search (#6143)

Added support for searching content in a specific directory or file.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6143
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
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:
Shiny Nematoda 2024-12-22 12:24:29 +00:00 committed by 0ko
parent bb88e1daf8
commit ee214cb886
19 changed files with 342 additions and 61 deletions

View file

@ -35,6 +35,7 @@ func Code(ctx *context.Context) {
language := ctx.FormTrim("l")
keyword := ctx.FormTrim("q")
path := ctx.FormTrim("path")
isFuzzy := ctx.FormOptionalBool("fuzzy").ValueOrDefault(true)
if mode := ctx.FormTrim("mode"); len(mode) > 0 {
@ -91,6 +92,7 @@ func Code(ctx *context.Context) {
Keyword: keyword,
IsKeywordFuzzy: isFuzzy,
Language: language,
Filename: path,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.UI.RepoSearchPagingNum,

View file

@ -54,6 +54,7 @@ func Search(ctx *context.Context) {
language := ctx.FormTrim("l")
keyword := ctx.FormTrim("q")
path := ctx.FormTrim("path")
mode := ExactSearchMode
if modeStr := ctx.FormString("mode"); len(modeStr) > 0 {
mode = searchModeFromString(modeStr)
@ -63,6 +64,7 @@ func Search(ctx *context.Context) {
ctx.Data["Keyword"] = keyword
ctx.Data["Language"] = language
ctx.Data["CodeSearchPath"] = path
ctx.Data["CodeSearchMode"] = mode.String()
ctx.Data["PageIsViewCode"] = true
@ -86,6 +88,7 @@ func Search(ctx *context.Context) {
Keyword: keyword,
IsKeywordFuzzy: mode == FuzzySearchMode,
Language: language,
Filename: path,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.UI.RepoSearchPagingNum,
@ -100,11 +103,12 @@ func Search(ctx *context.Context) {
} else {
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable(ctx)
}
ctx.Data["CodeSearchOptions"] = []string{"exact", "fuzzy"}
ctx.Data["CodeSearchOptions"] = code_indexer.CodeSearchOptions
} else {
grepOpt := git.GrepOptions{
ContextLineNumber: 1,
RefName: ctx.Repo.RefName,
Filename: path,
}
switch mode {
case FuzzySearchMode:
@ -130,10 +134,12 @@ func Search(ctx *context.Context) {
// UpdatedUnix: not supported yet
// Language: not supported yet
// Color: not supported yet
Lines: code_indexer.HighlightSearchResultCode(r.Filename, r.LineNumbers, r.HighlightedRanges, strings.Join(r.LineCodes, "\n")),
Lines: code_indexer.HighlightSearchResultCode(
r.Filename, r.LineNumbers, r.HighlightedRanges,
strings.Join(r.LineCodes, "\n")),
})
}
ctx.Data["CodeSearchOptions"] = []string{"exact", "union", "regexp"}
ctx.Data["CodeSearchOptions"] = git.GrepSearchOptions
}
ctx.Data["CodeIndexerDisabled"] = !setting.Indexer.RepoIndexerEnabled

View file

@ -39,6 +39,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/highlight"
code_indexer "code.gitea.io/gitea/modules/indexer/code"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
@ -1152,6 +1153,12 @@ PostRecentBranchCheck:
ctx.Data["TreeNames"] = treeNames
ctx.Data["BranchLink"] = branchLink
ctx.Data["CodeIndexerDisabled"] = !setting.Indexer.RepoIndexerEnabled
if setting.Indexer.RepoIndexerEnabled {
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable(ctx)
ctx.Data["CodeSearchOptions"] = code_indexer.CodeSearchOptions
} else {
ctx.Data["CodeSearchOptions"] = git.GrepSearchOptions
}
ctx.HTML(http.StatusOK, tplRepoHome)
}

View file

@ -39,6 +39,7 @@ func CodeSearch(ctx *context.Context) {
language := ctx.FormTrim("l")
keyword := ctx.FormTrim("q")
path := ctx.FormTrim("path")
isFuzzy := ctx.FormOptionalBool("fuzzy").ValueOrDefault(true)
if mode := ctx.FormTrim("mode"); len(mode) > 0 {
@ -88,6 +89,7 @@ func CodeSearch(ctx *context.Context) {
Keyword: keyword,
IsKeywordFuzzy: isFuzzy,
Language: language,
Filename: path,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.UI.RepoSearchPagingNum,