You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.2 KiB
131 lines
3.2 KiB
7 years ago
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
5 years ago
|
package code
|
||
7 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
7 years ago
|
"html"
|
||
7 years ago
|
gotemplate "html/template"
|
||
|
"strings"
|
||
|
|
||
|
"code.gitea.io/gitea/modules/highlight"
|
||
|
"code.gitea.io/gitea/modules/util"
|
||
|
)
|
||
|
|
||
|
// Result a search result to display
|
||
|
type Result struct {
|
||
7 years ago
|
RepoID int64
|
||
7 years ago
|
Filename string
|
||
|
HighlightClass string
|
||
|
LineNumbers []int
|
||
|
FormattedLines gotemplate.HTML
|
||
|
}
|
||
|
|
||
|
func indices(content string, selectionStartIndex, selectionEndIndex int) (int, int) {
|
||
|
startIndex := selectionStartIndex
|
||
|
numLinesBefore := 0
|
||
|
for ; startIndex > 0; startIndex-- {
|
||
|
if content[startIndex-1] == '\n' {
|
||
|
if numLinesBefore == 1 {
|
||
|
break
|
||
|
}
|
||
|
numLinesBefore++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
endIndex := selectionEndIndex
|
||
|
numLinesAfter := 0
|
||
|
for ; endIndex < len(content); endIndex++ {
|
||
|
if content[endIndex] == '\n' {
|
||
|
if numLinesAfter == 1 {
|
||
|
break
|
||
|
}
|
||
|
numLinesAfter++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return startIndex, endIndex
|
||
|
}
|
||
|
|
||
|
func writeStrings(buf *bytes.Buffer, strs ...string) error {
|
||
|
for _, s := range strs {
|
||
|
_, err := buf.WriteString(s)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
5 years ago
|
func searchResult(result *SearchResult, startIndex, endIndex int) (*Result, error) {
|
||
7 years ago
|
startLineNum := 1 + strings.Count(result.Content[:startIndex], "\n")
|
||
|
|
||
|
var formattedLinesBuffer bytes.Buffer
|
||
|
|
||
|
contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n")
|
||
|
lineNumbers := make([]int, len(contentLines))
|
||
|
index := startIndex
|
||
|
for i, line := range contentLines {
|
||
|
var err error
|
||
|
if index < result.EndIndex &&
|
||
|
result.StartIndex < index+len(line) &&
|
||
|
result.StartIndex < result.EndIndex {
|
||
|
openActiveIndex := util.Max(result.StartIndex-index, 0)
|
||
|
closeActiveIndex := util.Min(result.EndIndex-index, len(line))
|
||
|
err = writeStrings(&formattedLinesBuffer,
|
||
|
`<li>`,
|
||
7 years ago
|
html.EscapeString(line[:openActiveIndex]),
|
||
7 years ago
|
`<span class='active'>`,
|
||
7 years ago
|
html.EscapeString(line[openActiveIndex:closeActiveIndex]),
|
||
7 years ago
|
`</span>`,
|
||
7 years ago
|
html.EscapeString(line[closeActiveIndex:]),
|
||
7 years ago
|
`</li>`,
|
||
|
)
|
||
|
} else {
|
||
|
err = writeStrings(&formattedLinesBuffer,
|
||
|
`<li>`,
|
||
7 years ago
|
html.EscapeString(line),
|
||
7 years ago
|
`</li>`,
|
||
|
)
|
||
|
}
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
lineNumbers[i] = startLineNum + i
|
||
|
index += len(line)
|
||
|
}
|
||
|
return &Result{
|
||
7 years ago
|
RepoID: result.RepoID,
|
||
7 years ago
|
Filename: result.Filename,
|
||
|
HighlightClass: highlight.FileNameToHighlightClass(result.Filename),
|
||
|
LineNumbers: lineNumbers,
|
||
|
FormattedLines: gotemplate.HTML(formattedLinesBuffer.String()),
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// PerformSearch perform a search on a repository
|
||
7 years ago
|
func PerformSearch(repoIDs []int64, keyword string, page, pageSize int) (int, []*Result, error) {
|
||
7 years ago
|
if len(keyword) == 0 {
|
||
|
return 0, nil, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
total, results, err := indexer.Search(repoIDs, keyword, page, pageSize)
|
||
7 years ago
|
if err != nil {
|
||
|
return 0, nil, err
|
||
|
}
|
||
|
|
||
|
displayResults := make([]*Result, len(results))
|
||
|
|
||
|
for i, result := range results {
|
||
|
startIndex, endIndex := indices(result.Content, result.StartIndex, result.EndIndex)
|
||
|
displayResults[i], err = searchResult(result, startIndex, endIndex)
|
||
|
if err != nil {
|
||
|
return 0, nil, err
|
||
|
}
|
||
|
}
|
||
|
return int(total), displayResults, nil
|
||
|
}
|