Skip to content

Commit 853a39b

Browse files
committed
update from PR comments; cleanup comments in files
1 parent a3a4788 commit 853a39b

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

modules/indexer/code/search.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,9 @@ func HighlightSearchResultCode(filename, language string, lineNums []int, code s
8787
return lines
8888
}
8989

90-
func RawSearchResultCode(filename, language string, lineNums []int, code string) []*ResultLine {
91-
// we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting
90+
func rawSearchResultCode(lineNums []int, code string) []*ResultLine {
9291
rawLines := strings.Split(code, "\n")
9392

94-
// The lineNums outputted by highlight.Code might not match the original lineNums, because "highlight" removes the last `\n`
9593
lines := make([]*ResultLine, min(len(rawLines), len(lineNums)))
9694
for i := 0; i < len(lines); i++ {
9795
lines[i] = &ResultLine{
@@ -137,7 +135,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int, escap
137135
if escapeHTML {
138136
lines = HighlightSearchResultCode(result.Filename, result.Language, lineNums, formattedLinesBuffer.String())
139137
} else {
140-
lines = RawSearchResultCode(result.Filename, result.Language, lineNums, formattedLinesBuffer.String())
138+
lines = rawSearchResultCode(lineNums, formattedLinesBuffer.String())
141139
}
142140

143141
return &Result{

modules/structs/explore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2024 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
package structs // import "code.gitea.io/gitea/modules/structs"
4+
package structs
55

66
// ExploreCodeSearchItem A code search match
77
// swagger:model

routers/api/v1/explore/code.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright 2021 The Gitea Authors. All rights reserved.
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package explore
55

66
import (
77
"net/http"
8+
"slices"
89

910
"code.gitea.io/gitea/models/db"
1011
repo_model "code.gitea.io/gitea/models/repo"
@@ -33,7 +34,7 @@ func Code(ctx *context.APIContext) {
3334
// type: integer
3435
// - name: fuzzy
3536
// in: query
36-
// description: whether to search fuzzy or strict
37+
// description: whether to search fuzzy or strict (defaults to true)
3738
// type: boolean
3839
// responses:
3940
// "200":
@@ -50,9 +51,9 @@ func Code(ctx *context.APIContext) {
5051
isFuzzy := ctx.FormOptionalBool("fuzzy").ValueOrDefault(true)
5152

5253
if keyword == "" {
53-
ctx.JSON(http.StatusInternalServerError, api.SearchError{
54-
OK: false,
55-
Error: "No keyword provided",
54+
ctx.JSON(http.StatusOK, api.ExploreCodeResult{
55+
Total: 0,
56+
Results: make([]api.ExploreCodeSearchItem, 0),
5657
})
5758
return
5859
}
@@ -71,7 +72,6 @@ func Code(ctx *context.APIContext) {
7172
isAdmin = ctx.Doer.IsAdmin
7273
}
7374

74-
// guest user or non-admin user
7575
if ctx.Doer == nil || !isAdmin {
7676
repoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx, ctx.Doer)
7777
if err != nil {
@@ -112,14 +112,7 @@ func Code(ctx *context.APIContext) {
112112

113113
loadRepoIDs := make([]int64, 0, len(searchResults))
114114
for _, result := range searchResults {
115-
var find bool
116-
for _, id := range loadRepoIDs {
117-
if id == result.RepoID {
118-
find = true
119-
break
120-
}
121-
}
122-
if !find {
115+
if !slices.Contains(loadRepoIDs, result.RepoID) {
123116
loadRepoIDs = append(loadRepoIDs, result.RepoID)
124117
}
125118
}

services/convert/explore.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010
)
1111

1212
func ToExploreCodeSearchResults(total int, results []*code_indexer.Result, repoMaps map[int64]*repo_model.Repository) api.ExploreCodeResult {
13-
out := api.ExploreCodeResult{Total: total}
13+
out := api.ExploreCodeResult{
14+
Total: total,
15+
Results: make([]api.ExploreCodeSearchItem, 0, len(results)),
16+
}
1417
for _, res := range results {
1518
if repo := repoMaps[res.RepoID]; repo != nil {
1619
for _, r := range res.Lines {

0 commit comments

Comments
 (0)