update from PR comments; cleanup comments in files

This commit is contained in:
aaronknudtson 2024-07-09 23:11:52 -04:00
parent a3a47886c8
commit 043f8cc7d7
5 changed files with 15 additions and 21 deletions

View File

@ -87,11 +87,9 @@ func HighlightSearchResultCode(filename, language string, lineNums []int, code s
return lines return lines
} }
func RawSearchResultCode(filename, language string, lineNums []int, code string) []*ResultLine { func rawSearchResultCode(lineNums []int, code string) []*ResultLine {
// we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting
rawLines := strings.Split(code, "\n") rawLines := strings.Split(code, "\n")
// The lineNums outputted by highlight.Code might not match the original lineNums, because "highlight" removes the last `\n`
lines := make([]*ResultLine, min(len(rawLines), len(lineNums))) lines := make([]*ResultLine, min(len(rawLines), len(lineNums)))
for i := 0; i < len(lines); i++ { for i := 0; i < len(lines); i++ {
lines[i] = &ResultLine{ lines[i] = &ResultLine{
@ -137,7 +135,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int, escap
if escapeHTML { if escapeHTML {
lines = HighlightSearchResultCode(result.Filename, result.Language, lineNums, formattedLinesBuffer.String()) lines = HighlightSearchResultCode(result.Filename, result.Language, lineNums, formattedLinesBuffer.String())
} else { } else {
lines = RawSearchResultCode(result.Filename, result.Language, lineNums, formattedLinesBuffer.String()) lines = rawSearchResultCode(lineNums, formattedLinesBuffer.String())
} }
return &Result{ return &Result{

View File

@ -1,7 +1,7 @@
// Copyright 2024 The Gitea Authors. All rights reserved. // Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
package structs // import "code.gitea.io/gitea/modules/structs" package structs
// ExploreCodeSearchItem A code search match // ExploreCodeSearchItem A code search match
// swagger:model // swagger:model

View File

@ -1,10 +1,11 @@
// Copyright 2021 The Gitea Authors. All rights reserved. // Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
package explore package explore
import ( import (
"net/http" "net/http"
"slices"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
@ -33,7 +34,7 @@ func Code(ctx *context.APIContext) {
// type: integer // type: integer
// - name: fuzzy // - name: fuzzy
// in: query // in: query
// description: whether to search fuzzy or strict // description: whether to search fuzzy or strict (defaults to true)
// type: boolean // type: boolean
// responses: // responses:
// "200": // "200":
@ -50,9 +51,9 @@ func Code(ctx *context.APIContext) {
isFuzzy := ctx.FormOptionalBool("fuzzy").ValueOrDefault(true) isFuzzy := ctx.FormOptionalBool("fuzzy").ValueOrDefault(true)
if keyword == "" { if keyword == "" {
ctx.JSON(http.StatusInternalServerError, api.SearchError{ ctx.JSON(http.StatusOK, api.ExploreCodeResult{
OK: false, Total: 0,
Error: "No keyword provided", Results: make([]api.ExploreCodeSearchItem, 0),
}) })
return return
} }
@ -71,7 +72,6 @@ func Code(ctx *context.APIContext) {
isAdmin = ctx.Doer.IsAdmin isAdmin = ctx.Doer.IsAdmin
} }
// guest user or non-admin user
if ctx.Doer == nil || !isAdmin { if ctx.Doer == nil || !isAdmin {
repoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx, ctx.Doer) repoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx, ctx.Doer)
if err != nil { if err != nil {
@ -112,14 +112,7 @@ func Code(ctx *context.APIContext) {
loadRepoIDs := make([]int64, 0, len(searchResults)) loadRepoIDs := make([]int64, 0, len(searchResults))
for _, result := range searchResults { for _, result := range searchResults {
var find bool if !slices.Contains(loadRepoIDs, result.RepoID) {
for _, id := range loadRepoIDs {
if id == result.RepoID {
find = true
break
}
}
if !find {
loadRepoIDs = append(loadRepoIDs, result.RepoID) loadRepoIDs = append(loadRepoIDs, result.RepoID)
} }
} }

View File

@ -10,7 +10,10 @@ import (
) )
func ToExploreCodeSearchResults(total int, results []*code_indexer.Result, repoMaps map[int64]*repo_model.Repository) api.ExploreCodeResult { func ToExploreCodeSearchResults(total int, results []*code_indexer.Result, repoMaps map[int64]*repo_model.Repository) api.ExploreCodeResult {
out := api.ExploreCodeResult{Total: total} out := api.ExploreCodeResult{
Total: total,
Results: make([]api.ExploreCodeSearchItem, 0, len(results)),
}
for _, res := range results { for _, res := range results {
if repo := repoMaps[res.RepoID]; repo != nil { if repo := repoMaps[res.RepoID]; repo != nil {
for _, r := range res.Lines { for _, r := range res.Lines {

View File

@ -1034,7 +1034,7 @@
}, },
{ {
"type": "boolean", "type": "boolean",
"description": "whether to search fuzzy or strict", "description": "whether to search fuzzy or strict (defaults to true)",
"name": "fuzzy", "name": "fuzzy",
"in": "query" "in": "query"
} }