Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
feat: textDocument/implementation (#199)
Browse files Browse the repository at this point in the history
Implements implementations.

Both local and remote are implemented.

Co-authored-by: TJ DeVries <[email protected]>
Co-authored-by: Chris <[email protected]>
  • Loading branch information
chrismwendt and tjdevries authored Nov 4, 2021
1 parent c2ff452 commit f8617a7
Show file tree
Hide file tree
Showing 20 changed files with 1,136 additions and 297 deletions.
4 changes: 0 additions & 4 deletions cmd/lsif-go/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ func validatePaths() error {
//
// Defaults

var defaultProjectRoot = newCachedString(func() string {
return rel(wd.Value())
})

var defaultModuleRoot = newCachedString(func() string {
return searchForGoMod(wd.Value(), toplevel.Value())
})
Expand Down
3 changes: 2 additions & 1 deletion cmd/lsif-go/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/sourcegraph/sourcegraph/lib/codeintel/lsif/protocol/writer"
)

func writeIndex(repositoryRoot, repositoryRemote, projectRoot, moduleName, moduleVersion string, dependencies map[string]gomod.GoModule, outFile string, outputOptions output.Options) error {
func writeIndex(repositoryRoot, repositoryRemote, projectRoot, moduleName, moduleVersion string, dependencies map[string]gomod.GoModule, projectDependencies []string, outFile string, outputOptions output.Options) error {
start := time.Now()

out, err := os.Create(outFile)
Expand Down Expand Up @@ -40,6 +40,7 @@ func writeIndex(repositoryRoot, repositoryRemote, projectRoot, moduleName, modul
moduleName,
moduleVersion,
dependencies,
projectDependencies,
writer.NewJSONWriter(out),
packageDataCache,
outputOptions,
Expand Down
6 changes: 6 additions & 0 deletions cmd/lsif-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ func mainErr() (err error) {
return fmt.Errorf("failed to list dependencies: %v", err)
}

projectDependencies, err := gomod.ListProjectDependencies(moduleRoot)
if err != nil {
return fmt.Errorf("failed to list project dependencies: %v", err)
}

if err := writeIndex(
repositoryRoot,
repositoryRemote,
projectRoot,
moduleName,
moduleVersion,
dependencies,
projectDependencies,
outFile,
outputOptions,
); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/sourcegraph/lsif-go
go 1.15

require (
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/agnivade/levenshtein v1.1.1
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/efritz/pentimento v0.0.0-20190429011147-ade47d831101
github.com/google/go-cmp v0.5.6
Expand All @@ -12,7 +12,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/slimsag/godocmd v0.0.0-20161025000126-a1005ad29fe3
github.com/sourcegraph/lsif-static-doc v0.0.0-20210831232443-e74f711cdf06
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210831223740-2eb532a72f6c
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210914223954-cff3e4aaa732
golang.org/x/tools v0.1.3
mvdan.cc/gofumpt v0.1.1 // indirect
)
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafo
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRYS3XieqF+Z9B9gNxo/eANAJCF2eiN4=
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
Expand Down Expand Up @@ -51,6 +52,7 @@ github.com/derision-test/go-mockgen v1.1.2/go.mod h1:9H3VGTWYnL1VJoHHCuPKDpPFmNQ
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/efritz/pentimento v0.0.0-20190429011147-ade47d831101 h1:RylpU+KNJJNEJIk3o8gZ70uPTlutxaYnikKNPko39LA=
Expand Down Expand Up @@ -244,6 +246,7 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/shurcooL/go-goon v0.0.0-20210110234559-7585751d9a17 h1:lRAUE0dIvigSSFAmaM2dfg7OH8T+a8zJ5smEh09a/GI=
github.com/shurcooL/go-goon v0.0.0-20210110234559-7585751d9a17/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
Expand All @@ -253,11 +256,13 @@ github.com/slimsag/godocmd v0.0.0-20161025000126-a1005ad29fe3/go.mod h1:AIBPxLCk
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/sourcegraph/batch-change-utils v0.0.0-20210708162152-c9f35b905d94/go.mod h1:kKNRPN6dxuXwC4UUhPVcAJsvz4EVEfI0jyN5HUJsazA=
github.com/sourcegraph/go-diff v0.6.1/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs=
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf/go.mod h1:ppFaPm6kpcHnZGqQTFhUIAQRIEhdQDWP1PCv4/ON354=
github.com/sourcegraph/lsif-static-doc v0.0.0-20210831232443-e74f711cdf06 h1:MoYw3z6QAnSRFw9IvUylhfHGxxs1V95iZqDhJzIQ2fM=
github.com/sourcegraph/lsif-static-doc v0.0.0-20210831232443-e74f711cdf06/go.mod h1:8znXDIrOCwaA0q2v7BfPjQ+SD6BtKi6iB3B2I18zu1c=
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210831223740-2eb532a72f6c h1:vGUsHY3gwdZ3nfw6KyNMs9sA5JJTBPSjsa8VIbhPhhU=
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210831223740-2eb532a72f6c/go.mod h1:aH/URdfJ92i5Ec/5u2GSlliMZtKzsD91oTJNwZpH8Hc=
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210914223954-cff3e4aaa732 h1:mgoL7S4E44mm8qycqb0/pEKw/y1mi3lptAH01Iyz330=
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210914223954-cff3e4aaa732/go.mod h1:9vLg5fQL9Q3iktfqu96OrswbOq7yqMesMJER9DCy8vk=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
Expand Down
38 changes: 36 additions & 2 deletions internal/gomod/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func ListDependencies(dir, rootModule, rootVersion string, outputOptions output.
}

resolve := func() {
output, err := command.Run(dir, "go", "list", "-mod=readonly", "-m", "-json", "all")
var output, modOutput string

output, err = command.Run(dir, "go", "list", "-mod=readonly", "-m", "-json", "all")
if err != nil {
err = fmt.Errorf("failed to list modules: %v\n%s", err, output)
return
Expand All @@ -48,7 +50,7 @@ func ListDependencies(dir, rootModule, rootVersion string, outputOptions output.
// Because of that, we do a separate execution to guarantee we get only
// this package information to use to determine the corresponding
// goVersion.
modOutput, err := command.Run(dir, "go", "list", "-mod=readonly", "-m", "-json")
modOutput, err = command.Run(dir, "go", "list", "-mod=readonly", "-m", "-json")
if err != nil {
err = fmt.Errorf("failed to list module info: %v\n%s", err, output)
return
Expand All @@ -72,6 +74,38 @@ func ListDependencies(dir, rootModule, rootVersion string, outputOptions output.
return dependencies, err
}

// listProjectDependencies finds any packages from "$ go list all" that are NOT declared
// as part of the current project.
//
// NOTE: This is different from the other dependencies stored in the indexer because it
// does not modules, but packages.
func ListProjectDependencies(projectRoot string) ([]string, error) {
projectPackageOutput, err := command.Run(projectRoot, "go", "list", "./...")
if err != nil {
return nil, fmt.Errorf("failed to list project packages: %v\n%s", err, projectPackageOutput)
}

projectPackages := map[string]struct{}{}
for _, pkg := range strings.Split(projectPackageOutput, "\n") {
projectPackages[pkg] = struct{}{}
}

output, err := command.Run(projectRoot, "go", "list", "all")
if err != nil {
return nil, fmt.Errorf("failed to list dependecy packages: %v\n%s", err, output)
}

dependencyPackages := []string{"std"}
for _, dep := range strings.Split(output, "\n") {
// It's a dependency if it's not in the projectPackages
if _, ok := projectPackages[dep]; !ok {
dependencyPackages = append(dependencyPackages, dep)
}
}

return dependencyPackages, nil
}

type jsonModule struct {
Name string `json:"Path"`
Version string `json:"Version"`
Expand Down
10 changes: 10 additions & 0 deletions internal/gomod/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ package gomod
// or not. At this point, it checks whether the package name is one of those
// that is found from running "go list std" in the latest released go version.
func isStandardlibPackge(pkg string) bool {
// For certain items in the Universe scope, they have no package.
// Because of this, we put them in the "builtin" package, because
// that's the name of the file in the go standard library that defines
// these items.
//
// An example would be `error` or `error.Error()`
if pkg == "builtin" {
return true
}

_, ok := standardLibraryMap[pkg]
return ok
}
Expand Down
11 changes: 3 additions & 8 deletions internal/indexer/documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,16 +1082,11 @@ func sortExportedNameFirst(a, b string) bool {
bUnderscore = b[0] == '_'
}
if aUnderscore != bUnderscore {
if !aUnderscore {
return true // Names not starting with "_" have higher importance
}
return false
// Names not starting with "_" have higher importance
return !aUnderscore
}
if aExported != bExported {
if aExported {
return true
}
return false
return aExported
}
return a < b
}
Expand Down
Loading

0 comments on commit f8617a7

Please sign in to comment.