Skip to content

Commit

Permalink
Merge pull request #11 from xushiwei/q
Browse files Browse the repository at this point in the history
golang.org/x/tools/go/packages => goxls/packages
  • Loading branch information
xushiwei authored Oct 9, 2023
2 parents 9ffff11 + e866635 commit fa7df93
Show file tree
Hide file tree
Showing 18 changed files with 282 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gopls/doc/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

"github.com/jba/printsrc"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/gopls/internal/goxls/packages"
"golang.org/x/tools/gopls/internal/lsp/command"
"golang.org/x/tools/gopls/internal/lsp/command/commandmeta"
"golang.org/x/tools/gopls/internal/lsp/mod"
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/astutil/purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"reflect"
"testing"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/gopls/internal/astutil"
"golang.org/x/tools/gopls/internal/goxls/packages"
"golang.org/x/tools/internal/testenv"
)

Expand Down
213 changes: 213 additions & 0 deletions gopls/internal/goxls/packages/packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
// Copyright 2023 The GoPlus Authors (goplus.org). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package packages

import (
"sort"

"golang.org/x/tools/go/packages"
internal "golang.org/x/tools/internal/packagesinternal"

"golang.org/x/tools/gopls/internal/goxls/packagesinternal"
)

// An Error describes a problem with a package's metadata, syntax, or types.
type Error = packages.Error

// ErrorKind describes the source of the error, allowing the user to
// differentiate between errors generated by the driver, the parser, or the
// type-checker.
type ErrorKind = packages.ErrorKind

const (
UnknownError = packages.UnknownError
ListError = packages.ListError
ParseError = packages.ParseError
TypeError = packages.TypeError
)

// A LoadMode controls the amount of detail to return when loading.
// The bits below can be combined to specify which fields should be
// filled in the result packages.
// The zero value is a special case, equivalent to combining
// the NeedName, NeedFiles, and NeedCompiledGoFiles bits.
// ID and Errors (if present) will always be filled.
// Load may return more information than requested.
type LoadMode = packages.LoadMode

const (
// NeedName adds Name and PkgPath.
NeedName = packages.NeedName

// NeedFiles adds GoFiles, GopFiles and OtherFiles.
NeedFiles = packages.NeedFiles

// NeedCompiledGoFiles adds CompiledGoFiles/CompiledGopFiles.
NeedCompiledGoFiles = packages.NeedCompiledGoFiles

// NeedCompiledGopFiles adds CompiledGoFiles/CompiledGopFiles.
NeedCompiledGopFiles = packages.NeedCompiledGoFiles

// NeedImports adds Imports. If NeedDeps is not set, the Imports field will contain
// "placeholder" Packages with only the ID set.
NeedImports = packages.NeedImports

// NeedDeps adds the fields requested by the LoadMode in the packages in Imports.
NeedDeps = packages.NeedDeps

// NeedExportFile adds ExportFile.
NeedExportFile = packages.NeedExportFile

// NeedTypes adds Types, Fset, and IllTyped.
NeedTypes = packages.NeedTypes

// NeedSyntax adds Syntax.
NeedSyntax = packages.NeedSyntax

// NeedTypesInfo adds TypesInfo.
NeedTypesInfo = packages.NeedTypesInfo

// NeedTypesSizes adds TypesSizes.
NeedTypesSizes = packages.NeedTypesSizes

// NeedModule adds Module.
NeedModule = packages.NeedModule

// NeedEmbedFiles adds EmbedFiles.
NeedEmbedFiles = packages.NeedEmbedFiles

// NeedEmbedPatterns adds EmbedPatterns.
NeedEmbedPatterns = packages.NeedEmbedPatterns
)

// A Config specifies details about how packages should be loaded.
// The zero value is a valid configuration.
// Calls to Load do not modify this struct.
type Config = packages.Config

type Package = packages.Package

/*
// A Package describes a loaded Go+ package.
type Package struct {
*packages.Package
// GopFiles lists the absolute file paths of the package's Go source files.
// It may include files that should not be compiled, for example because
// they contain non-matching build tags, are documentary pseudo-files such as
// unsafe/unsafe.go or builtin/builtin.go, or are subject to cgo preprocessing.
GopFiles []string
// CompiledGopFiles lists the absolute file paths of the package's source
// files that are suitable for type checking.
// This may differ from GoFiles if files are processed before compilation.
CompiledGopFiles []string
// Imports maps import paths appearing in the package's Go source files
// to corresponding loaded Packages.
Imports map[string]*Package
// TypesInfo provides type information about the package's syntax trees.
// It is set only when Syntax is set.
GopTypesInfo *typesutil.Info
}
*/

// Module provides module information for a package.
type Module = packages.Module

// Load loads and returns the Go packages named by the given patterns.
//
// Config specifies loading options;
// nil behaves the same as an empty Config.
//
// Load returns an error if any of the patterns was invalid
// as defined by the underlying build system.
// It may return an empty list of packages without an error,
// for instance for an empty expansion of a valid wildcard.
// Errors associated with a particular package are recorded in the
// corresponding Package's Errors list, and do not cause Load to
// return an error. Clients may need to handle such errors before
// proceeding with further analysis. The PrintErrors function is
// provided for convenient display of all errors.
func Load(cfg *Config, patterns ...string) ([]*Package, error) {
return packages.Load(cfg, patterns...)
}

/*
pkgs, err := packages.Load(cfg, patterns...)
if err != nil {
return nil, err
}
ret := make([]*Package, len(pkgs))
for i, pkg := range pkgs {
ret[i] = &Package{Package: pkg, Imports: importPkgs(pkg.Imports)}
}
return ret, nil
}
func importPkgs(pkgs map[string]*packages.Package) map[string]*Package {
if len(pkgs) == 0 {
return nil
}
ret := make(map[string]*Package, len(pkgs))
for path, pkg := range pkgs {
ret[path] = &Package{Package: pkg}
}
return ret
}
*/

// Visit visits all the packages in the import graph whose roots are
// pkgs, calling the optional pre function the first time each package
// is encountered (preorder), and the optional post function after a
// package's dependencies have been visited (postorder).
// The boolean result of pre(pkg) determines whether
// the imports of package pkg are visited.
func Visit(pkgs []*Package, pre func(*Package) bool, post func(*Package)) {
seen := make(map[*Package]bool)
var visit func(*Package)
visit = func(pkg *Package) {
if !seen[pkg] {
seen[pkg] = true

if pre == nil || pre(pkg) {
paths := make([]string, 0, len(pkg.Imports))
for path := range pkg.Imports {
paths = append(paths, path)
}
sort.Strings(paths) // Imports is a map, this makes visit stable
for _, path := range paths {
visit(pkg.Imports[path])
}
}

if post != nil {
post(pkg)
}
}
}
for _, pkg := range pkgs {
visit(pkg)
}
}

func init() {
packagesinternal.GetForTest = func(p interface{}) string {
return internal.GetForTest(p.(*Package))
// return internal.GetForTest(p.(*Package).Package)
}
packagesinternal.GetDepsErrors = func(p interface{}) []*packagesinternal.PackageError {
return internal.GetDepsErrors(p.(*Package))
// return internal.GetDepsErrors(p.(*Package).Package)
}
packagesinternal.GetGoCmdRunner = internal.GetGoCmdRunner
packagesinternal.SetGoCmdRunner = internal.SetGoCmdRunner
packagesinternal.SetModFile = internal.SetModFile
packagesinternal.SetModFlag = internal.SetModFlag
packagesinternal.TypecheckCgo = internal.TypecheckCgo
packagesinternal.DepsErrors = internal.DepsErrors
packagesinternal.ForTest = internal.ForTest
}
27 changes: 27 additions & 0 deletions gopls/internal/goxls/packagesinternal/pkgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023 The GoPlus Authors (goplus.org). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package packagesinternal exposes internal-only fields from goxls/packages.
package packagesinternal

import (
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/packagesinternal"
)

var GetForTest = func(p interface{}) string { return "" }
var GetDepsErrors = func(p interface{}) []*PackageError { return nil }

type PackageError = packagesinternal.PackageError

var GetGoCmdRunner = func(config interface{}) *gocommand.Runner { return nil }

var SetGoCmdRunner = func(config interface{}, runner *gocommand.Runner) {}

var TypecheckCgo int
var DepsErrors int // must be set as a LoadMode to call GetDepsErrors
var ForTest int // must be set as a LoadMode to call GetForTest

var SetModFlag = func(config interface{}, value string) {}
var SetModFile = func(config interface{}, value string) {}
2 changes: 1 addition & 1 deletion gopls/internal/lsp/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/gopls/internal/bug"
"golang.org/x/tools/gopls/internal/goxls/packagesinternal"
"golang.org/x/tools/gopls/internal/goxls/parserutil"
"golang.org/x/tools/gopls/internal/lsp/filecache"
"golang.org/x/tools/gopls/internal/lsp/protocol"
Expand All @@ -32,7 +33,6 @@ import (
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/tag"
"golang.org/x/tools/internal/gcimporter"
"golang.org/x/tools/internal/packagesinternal"
"golang.org/x/tools/internal/tokeninternal"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/typesinternal"
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/cache/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"strconv"
"strings"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/gopls/internal/bug"
"golang.org/x/tools/gopls/internal/goxls/packages"
"golang.org/x/tools/gopls/internal/lsp/command"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/safetoken"
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/cache/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package cache
import (
"sort"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/gopls/internal/bug"
"golang.org/x/tools/gopls/internal/goxls/packages"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/gopls/internal/span"
)
Expand Down
23 changes: 13 additions & 10 deletions gopls/internal/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ import (
"sync/atomic"
"time"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/gopls/internal/bug"
"golang.org/x/tools/gopls/internal/goxls/goputil"
"golang.org/x/tools/gopls/internal/goxls/packages" // goxls: replace golang.org/x/tools/go/packages
"golang.org/x/tools/gopls/internal/goxls/packagesinternal" // goxls: replace golang.org/x/tools/internal/packagesinternal
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/gopls/internal/span"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/tag"
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/packagesinternal"
)

var loadID uint64 // atomic identifier for loads
Expand Down Expand Up @@ -438,14 +437,18 @@ func buildMetadata(updates map[PackageID]*source.Metadata, pkg *packages.Package
uri := span.URIFromPath(filename)
m.GoFiles = append(m.GoFiles, uri)
}
// goxls: Go+ files: we don't change codes out of gopls
for _, filename := range pkg.OtherFiles {
fext := filepath.Ext(filename)
if goputil.FileKind(fext) != 0 { // Go+ file
uri := span.URIFromPath(filename)
m.GopFiles = append(m.GopFiles, uri)
/*
if true { // goxls: Go+ files
for _, filename := range pkg.GopFiles {
uri := span.URIFromPath(filename)
m.GopFiles = append(m.GopFiles, uri)
}
for _, filename := range pkg.CompiledGopFiles {
uri := span.URIFromPath(filename)
m.CompiledGopFiles = append(m.CompiledGopFiles, uri)
}
}
}
*/
for _, filename := range pkg.IgnoredFiles {
uri := span.URIFromPath(filename)
m.IgnoredFiles = append(m.IgnoredFiles, uri)
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/cache/mod_tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func missingModuleDiagnostics(ctx context.Context, snapshot *snapshot, pm *sourc
//
// import (
// "golang.org/x/tools/go/expect"
// "golang.org/x/tools/go/packages"
// "golang.org/x/tools/gopls/internal/goxls/packages"
// )
// They both are related to the same module: "golang.org/x/tools".
var match string
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
"unsafe"

"golang.org/x/sync/errgroup"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/types/objectpath"
"golang.org/x/tools/gopls/internal/bug"
"golang.org/x/tools/gopls/internal/goxls/packages"
"golang.org/x/tools/gopls/internal/goxls/packagesinternal"
"golang.org/x/tools/gopls/internal/lsp/command"
"golang.org/x/tools/gopls/internal/lsp/filecache"
"golang.org/x/tools/gopls/internal/lsp/protocol"
Expand All @@ -42,7 +43,6 @@ import (
"golang.org/x/tools/internal/event/tag"
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/memoize"
"golang.org/x/tools/internal/packagesinternal"
"golang.org/x/tools/internal/persistent"
"golang.org/x/tools/internal/typesinternal"
)
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/command/commandmeta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"unicode"

"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/gopls/internal/goxls/packages"
"golang.org/x/tools/gopls/internal/lsp/command"
)

Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/safetoken/safetoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os"
"testing"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/gopls/internal/goxls/packages"
"golang.org/x/tools/gopls/internal/lsp/safetoken"
"golang.org/x/tools/internal/testenv"
)
Expand Down
Loading

0 comments on commit fa7df93

Please sign in to comment.