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

@ -11,12 +11,6 @@
{{if $description}}<span class="description">{{$description | RenderCodeBlock}}</span>{{else}}<span class="no-description text-italic">{{ctx.Locale.Tr "repo.no_desc"}}</span>{{end}}
{{if .Repository.Website}}<a class="link" href="{{.Repository.Website}}">{{.Repository.Website}}</a>{{end}}
</div>
<form class="ignore-dirty" action="{{.RepoLink}}/search/{{if .CodeIndexerDisabled}}{{.BranchNameSubURL}}{{end}}" method="get" data-test-tag="codesearch">
<div class="ui small action input">
<input name="q" value="{{.Keyword}}" placeholder="{{ctx.Locale.Tr "search.code_kind"}}">
{{template "shared/search/button"}}
</div>
</form>
</div>
<div class="tw-flex tw-items-center tw-flex-wrap tw-gap-2 tw-my-2" id="repo-topics">
{{/* it should match the code in issue-home.js */}}
@ -158,6 +152,22 @@
{{else if .IsBlame}}
{{template "repo/blame" .}}
{{else}}{{/* IsViewDirectory */}}
{{/* display the search bar only if */}}
{{$isCommit := StringUtils.HasPrefix .BranchNameSubURL "commit"}}
{{if and (not $isCommit) (or .CodeIndexerDisabled (and (not .TagName) (eq .Repository.DefaultBranch .BranchName)))}}
<div class="code-search tw-w-full tw-py-2 tw-px-2 tw-bg-box-header tw-rounded-t tw-border tw-border-secondary tw-border-b-0">
<form class="ui form ignore-dirty" action="{{.RepoLink}}/search/{{if .CodeIndexerDisabled}}{{.BranchNameSubURL}}{{end}}" method="get" data-test-tag="codesearch">
<input type="hidden" name="path" value="{{.TreePath | PathEscapeSegments}}">
{{template "shared/search/combo_multi"
dict
"Value" .Keyword
"Disabled" .CodeIndexerUnavailable
"Placeholder" (ctx.Locale.Tr "search.code_kind")
"Selected" (index .CodeSearchOptions 0)
"Options" .CodeSearchOptions}}
</form>
</div>
{{end}}
{{template "repo/view_list" .}}
{{end}}
</div>

View file

@ -1,7 +1,7 @@
<div class="flex-text-block tw-flex-wrap">
{{range $term := .SearchResultLanguages}}
<a class="ui {{if eq $.Language $term.Language}}primary{{end}} basic label tw-m-0"
href="?q={{$.Keyword}}{{if ne $.Language $term.Language}}&l={{$term.Language}}{{end}}&mode={{$.CodeSearchMode}}">
href="?q={{$.Keyword}}{{if ne $.Language $term.Language}}&l={{$term.Language}}{{end}}&mode={{$.CodeSearchMode}}&path={{$.CodeSearchPath}}">
<i class="color-icon tw-mr-2" style="background-color: {{$term.Color}}"></i>
{{$term.Language}}
<div class="detail">{{$term.Count}}</div>
@ -13,7 +13,7 @@
{{$repo := or $.Repo (index $.RepoMaps .RepoID)}}
<details class="tw-group diff-file-box diff-box file-content non-diff-file-content repo-search-result" open>
<summary class="tw-list-none">
<h4 class="ui top attached header tw-font-normal tw-flex tw-flex-wrap tw-transform-reset">
<h4 class="ui top attached header tw-font-normal tw-flex tw-items-center tw-flex-wrap tw-transform-reset">
<span class="tw-h-4 tw-transition -tw-rotate-90 group-open:tw-rotate-0">
{{svg "octicon-chevron-down"}}
</span>

View file

@ -1,4 +1,5 @@
<form class="ui form ignore-dirty">
<input type="hidden" name="path" value="{{.CodeSearchPath}}">
{{template "shared/search/combo_multi"
dict
"Value" .Keyword
@ -14,6 +15,23 @@
<p>{{ctx.Locale.Tr "search.code_search_unavailable"}}</p>
</div>
{{else}}
{{if .CodeSearchPath}}
<div class="tw-mb-4">
<span class="breadcrumb">
<a class="section" href="?q={{.Keyword}}&mode={{.CodeSearchMode}}">@</a>
{{$href := ""}}
{{- range $i, $path := StringUtils.Split .CodeSearchPath "/" -}}
{{if eq $i 0}}
{{$href = $path}}
{{else}}
{{$href = StringUtils.Join (StringUtils.Make $href $path) "/"}}
{{end}}
<span class="breadcrumb-divider">/</span>
<span class="section"><a href="?q={{$.Keyword}}&mode={{$.CodeSearchMode}}&path={{$href}}">{{$path}}</a></span>
{{- end -}}
</span>
</div>
{{end}}
{{if .CodeIndexerDisabled}}
<div class="ui message" data-test-tag="grep">
<p>{{ctx.Locale.Tr "search.code_search_by_git_grep"}}</p>