diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go index fbce8b189d2..9f18d633217 100644 --- a/modules/indexer/code/search.go +++ b/modules/indexer/code/search.go @@ -87,11 +87,9 @@ func HighlightSearchResultCode(filename, language string, lineNums []int, code s return lines } -func RawSearchResultCode(filename, language string, lineNums []int, code string) []*ResultLine { - // we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting +func rawSearchResultCode(lineNums []int, code string) []*ResultLine { 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))) for i := 0; i < len(lines); i++ { lines[i] = &ResultLine{ @@ -137,7 +135,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int, escap if escapeHTML { lines = HighlightSearchResultCode(result.Filename, result.Language, lineNums, formattedLinesBuffer.String()) } else { - lines = RawSearchResultCode(result.Filename, result.Language, lineNums, formattedLinesBuffer.String()) + lines = rawSearchResultCode(lineNums, formattedLinesBuffer.String()) } return &Result{ diff --git a/modules/structs/explore.go b/modules/structs/explore.go index abb5727baa6..6fc708da089 100644 --- a/modules/structs/explore.go +++ b/modules/structs/explore.go @@ -1,7 +1,7 @@ // Copyright 2024 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -package structs // import "code.gitea.io/gitea/modules/structs" +package structs // ExploreCodeSearchItem A code search match // swagger:model diff --git a/routers/api/v1/explore/code.go b/routers/api/v1/explore/code.go index 48a83f4b9c4..6a8c21271bd 100644 --- a/routers/api/v1/explore/code.go +++ b/routers/api/v1/explore/code.go @@ -1,10 +1,11 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. +// Copyright 2024 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package explore import ( "net/http" + "slices" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" @@ -33,7 +34,7 @@ func Code(ctx *context.APIContext) { // type: integer // - name: fuzzy // in: query - // description: whether to search fuzzy or strict + // description: whether to search fuzzy or strict (defaults to true) // type: boolean // responses: // "200": @@ -50,9 +51,9 @@ func Code(ctx *context.APIContext) { isFuzzy := ctx.FormOptionalBool("fuzzy").ValueOrDefault(true) if keyword == "" { - ctx.JSON(http.StatusInternalServerError, api.SearchError{ - OK: false, - Error: "No keyword provided", + ctx.JSON(http.StatusOK, api.ExploreCodeResult{ + Total: 0, + Results: make([]api.ExploreCodeSearchItem, 0), }) return } @@ -71,7 +72,6 @@ func Code(ctx *context.APIContext) { isAdmin = ctx.Doer.IsAdmin } - // guest user or non-admin user if ctx.Doer == nil || !isAdmin { repoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx, ctx.Doer) if err != nil { @@ -112,14 +112,7 @@ func Code(ctx *context.APIContext) { loadRepoIDs := make([]int64, 0, len(searchResults)) for _, result := range searchResults { - var find bool - for _, id := range loadRepoIDs { - if id == result.RepoID { - find = true - break - } - } - if !find { + if !slices.Contains(loadRepoIDs, result.RepoID) { loadRepoIDs = append(loadRepoIDs, result.RepoID) } } diff --git a/services/convert/explore.go b/services/convert/explore.go index 5a2eeb08ed8..9983f6a93a8 100644 --- a/services/convert/explore.go +++ b/services/convert/explore.go @@ -10,7 +10,10 @@ import ( ) 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 { if repo := repoMaps[res.RepoID]; repo != nil { for _, r := range res.Lines { diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 011836925b0..7bcb096aaf0 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -1034,7 +1034,7 @@ }, { "type": "boolean", - "description": "whether to search fuzzy or strict", + "description": "whether to search fuzzy or strict (defaults to true)", "name": "fuzzy", "in": "query" }