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
}
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{

View File

@ -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

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
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)
}
}

View File

@ -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 {

View File

@ -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"
}