Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/frontend/graphqlbackend/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cmd/frontend/graphqlbackend/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,8 @@ type Symbol {
url: String!
# The canonical URL to this symbol (using an immutable revision specifier).
canonicalURL: String!
# Whether or not the symbol is local to the file it's defined in.
fileLocal: Boolean!
}

# A location inside a resource (in a repository at a specific commit).
Expand Down
2 changes: 1 addition & 1 deletion cmd/frontend/graphqlbackend/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func newSearchResultResolver(result interface{}, score int) *searchSuggestionRes
return &searchSuggestionResolver{result: r, score: score, length: len(r.path), label: r.path}

case *symbolResolver:
return &searchSuggestionResolver{result: r, score: score, length: len(r.symbol.Name + " " + r.symbol.ContainerName), label: r.symbol.Name + " " + r.symbol.ContainerName}
return &searchSuggestionResolver{result: r, score: score, length: len(r.symbol.Name + " " + r.symbol.Parent), label: r.symbol.Name + " " + r.symbol.Parent}

default:
panic("never here")
Expand Down
8 changes: 4 additions & 4 deletions cmd/frontend/graphqlbackend/search_suggestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ func (r *searchResolver) Suggestions(ctx context.Context, args *searchSuggestion
for _, fileMatch := range fileMatches {
for _, sr := range fileMatch.symbols {
score := 20
if sr.symbol.ContainerName == "" {
if sr.symbol.Parent == "" {
score++
}
if len(sr.symbol.Name) < 12 {
score++
}
switch sr.symbol.Kind {
switch ctagsKindToLSPSymbolKind(sr.symbol.Kind) {
case lsp.SKFunction, lsp.SKMethod:
score += 2
case lsp.SKClass:
score += 3
}
if len(sr.symbol.Name) >= 4 && strings.Contains(strings.ToLower(string(sr.symbol.Location.URI)), strings.ToLower(sr.symbol.Name)) {
if len(sr.symbol.Name) >= 4 && strings.Contains(strings.ToLower(sr.uri.String()), strings.ToLower(sr.symbol.Name)) {
score++
}
results = append(results, newSearchResultResolver(sr, score))
Expand Down Expand Up @@ -246,7 +246,7 @@ func (r *searchResolver) Suggestions(ctx context.Context, args *searchSuggestion
k.file = s.path
case *symbolResolver:
k.repoName = s.location.resource.commit.repo.repo.Name
k.symbol = s.symbol.Name + s.symbol.ContainerName
k.symbol = s.symbol.Name + s.symbol.Parent
default:
panic(fmt.Sprintf("unhandled: %#v", s))
}
Expand Down
20 changes: 5 additions & 15 deletions cmd/frontend/graphqlbackend/search_symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func searchSymbolsInRepo(ctx context.Context, repoRevs *search.RepositoryRevisio
if inputRev != "" {
commit.inputRev = &inputRev
}
symbolRes := toSymbolResolver(symbolToLSPSymbolInformation(symbol, baseURI), strings.ToLower(symbol.Language), commit)
symbolRes := toSymbolResolver(symbol, baseURI, strings.ToLower(symbol.Language), commit)
uri := makeFileMatchURIFromSymbol(symbolRes, inputRev)
if fileMatch, ok := fileMatchesByURI[uri]; ok {
fileMatch.symbols = append(fileMatch.symbols, symbolRes)
Expand Down Expand Up @@ -178,21 +178,11 @@ func makeFileMatchURIFromSymbol(symbolResolver *symbolResolver, inputRev string)
return uri
}

// symbolToLSPSymbolInformation converts a symbols service Symbol struct to an LSP SymbolInformation
// baseURI is the git://repo?rev base URI for the symbol that is extended with the file path
func symbolToLSPSymbolInformation(s protocol.Symbol, baseURI *gituri.URI) lsp.SymbolInformation {
func symbolRange(s protocol.Symbol) lsp.Range {
ch := ctagsSymbolCharacter(s)
return lsp.SymbolInformation{
Name: s.Name + s.Signature,
ContainerName: s.Parent,
Kind: ctagsKindToLSPSymbolKind(s.Kind),
Location: lsp.Location{
URI: lsp.DocumentURI(baseURI.WithFilePath(s.Path).String()),
Range: lsp.Range{
Start: lsp.Position{Line: s.Line - 1, Character: ch},
End: lsp.Position{Line: s.Line - 1, Character: ch + len(s.Name)},
},
},
return lsp.Range{
Start: lsp.Position{Line: s.Line - 1, Character: ch},
End: lsp.Position{Line: s.Line - 1, Character: ch + len(s.Name)},
}
}

Expand Down
29 changes: 13 additions & 16 deletions cmd/frontend/graphqlbackend/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
"strings"
"time"

lsp "github.com/sourcegraph/go-lsp"
"github.com/sourcegraph/sourcegraph/cmd/frontend/backend"
"github.com/sourcegraph/sourcegraph/cmd/frontend/graphqlbackend/graphqlutil"
"github.com/sourcegraph/sourcegraph/pkg/api"
"github.com/sourcegraph/sourcegraph/pkg/gituri"
"github.com/sourcegraph/sourcegraph/pkg/symbols/protocol"
log15 "gopkg.in/inconshreveable/log15.v2"
)

type symbolsArgs struct {
Expand Down Expand Up @@ -74,7 +72,7 @@ func computeSymbols(ctx context.Context, commit *gitCommitResolver, query *strin
}
resolvers := make([]*symbolResolver, 0, len(symbols))
for _, symbol := range symbols {
resolver := toSymbolResolver(symbolToLSPSymbolInformation(symbol, baseURI), strings.ToLower(symbol.Language), commit)
resolver := toSymbolResolver(symbol, baseURI, strings.ToLower(symbol.Language), commit)
if resolver == nil {
continue
}
Expand All @@ -83,22 +81,18 @@ func computeSymbols(ctx context.Context, commit *gitCommitResolver, query *strin
return resolvers, err
}

func toSymbolResolver(symbol lsp.SymbolInformation, lang string, commitResolver *gitCommitResolver) *symbolResolver {
func toSymbolResolver(symbol protocol.Symbol, baseURI *gituri.URI, lang string, commitResolver *gitCommitResolver) *symbolResolver {
resolver := &symbolResolver{
symbol: symbol,
language: lang,
uri: baseURI.WithFilePath(symbol.Path),
}
uri, err := gituri.Parse(string(symbol.Location.URI))
if err != nil {
log15.Warn("Omitting symbol with invalid URI from results.", "uri", symbol.Location.URI, "error", err)
return nil
}
symbolRange := symbol.Location.Range // copy
symbolRange := symbolRange(symbol)
resolver.location = &locationResolver{
resource: &gitTreeEntryResolver{
commit: commitResolver,
path: uri.Fragment,
stat: createFileInfo(uri.Fragment, false), // assume the path refers to a file (not dir)
path: resolver.uri.Fragment,
stat: createFileInfo(resolver.uri.Fragment, false), // assume the path refers to a file (not dir)
},
lspRange: &symbolRange,
}
Expand All @@ -118,22 +112,23 @@ func (r *symbolConnectionResolver) PageInfo(ctx context.Context) (*graphqlutil.P
}

type symbolResolver struct {
symbol lsp.SymbolInformation
symbol protocol.Symbol
language string
location *locationResolver
uri *gituri.URI
}

func (r *symbolResolver) Name() string { return r.symbol.Name }

func (r *symbolResolver) ContainerName() *string {
if r.symbol.ContainerName == "" {
if r.symbol.Parent == "" {
return nil
}
return &r.symbol.ContainerName
return &r.symbol.Parent
}

func (r *symbolResolver) Kind() string /* enum SymbolKind */ {
return strings.ToUpper(r.symbol.Kind.String())
return strings.ToUpper(ctagsKindToLSPSymbolKind(r.symbol.Kind).String())
}

func (r *symbolResolver) Language() string { return r.language }
Expand All @@ -143,3 +138,5 @@ func (r *symbolResolver) Location() *locationResolver { return r.location }
func (r *symbolResolver) URL(ctx context.Context) (string, error) { return r.location.URL(ctx) }

func (r *symbolResolver) CanonicalURL() (string, error) { return r.location.CanonicalURL() }

func (r *symbolResolver) FileLocal() bool { return r.symbol.FileLimited }
22 changes: 12 additions & 10 deletions cmd/symbols/internal/pkg/ctags/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewParser(ctagsCommand string) (Parser, error) {

cmd := exec.Command(ctagsCommand, "--_interactive="+opt, "--fields=*",
"--languages=Basic,C,C#,C++,Clojure,Cobol,CSS,CUDA,D,Elixir,elm,Erlang,Go,haskell,Java,JavaScript,kotlin,Lisp,Lua,MatLab,ObjectiveC,OCaml,Perl,Perl6,PHP,Protobuf,Python,R,Ruby,Rust,scala,Scheme,Sh,swift,Tcl,typescript,Verilog,Vim",
"--map-CSS=+.scss", "--map-CSS=+.less", "--map-CSS=+.sass", "--file-scope=no",
"--map-CSS=+.scss", "--map-CSS=+.less", "--map-CSS=+.sass",
"--kinds-Go=-p", // omit because 1 symbol per `package` keyword (1 for each file in a package) is not useful
)
in, err := cmd.StdinPipe()
Expand Down Expand Up @@ -183,6 +183,7 @@ type reply struct {
Scope string `json:"scope"`
ScopeKind string `json:"scopeKind"`
Access string `json:"access"`
File bool `json:"file"`
Signature string `json:"signature"`
Pattern string `json:"pattern"`
}
Expand All @@ -209,15 +210,16 @@ func (p *ctagsProcess) Parse(name string, content []byte) (entries []Entry, err
}

entries = append(entries, Entry{
Name: rep.Name,
Path: rep.Path,
Line: rep.Line,
Kind: rep.Kind,
Language: rep.Language,
Parent: rep.Scope,
ParentKind: rep.ScopeKind,
Pattern: rep.Pattern,
Signature: rep.Signature,
Name: rep.Name,
Path: rep.Path,
Line: rep.Line,
Kind: rep.Kind,
Language: rep.Language,
Parent: rep.Scope,
ParentKind: rep.ScopeKind,
Pattern: rep.Pattern,
Signature: rep.Signature,
FileLimited: rep.File,
})
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/symbols/internal/symbols/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ func filterSymbols(ctx context.Context, db *sqlx.DB, args protocol.SearchArgs) (
}

// The version of the symbols database schema. This is included in the database
// filenames to prevent a newer version of the symbols service attempting to
// read from a database created by an older (and likely incompatible) symbols
// filenames to prevent a newer version of the symbols service from attempting
// to read from a database created by an older (and likely incompatible) symbols
// service. Increment this when you change the database schema.
const symbolsDBVersion = 1
const symbolsDBVersion = 2

// symbolInDB is the same as `protocol.Symbol`, but with two additional columns:
// namelowercase and pathlowercase, which enable indexed case insensitive
Expand Down