aboutsummaryrefslogtreecommitdiff
path: root/internal/lsp/source/view.go
diff options
context:
space:
mode:
authorDan Willemsen <dwillemsen@google.com>2022-03-29 00:50:59 -0700
committerDan Willemsen <dwillemsen@google.com>2022-03-29 00:52:27 -0700
commitf10932f763d058b0dcb3acfb795c869996fef47b (patch)
tree7e04d345c214f3efac3c4b86c7ec3e831c500437 /internal/lsp/source/view.go
parentd6d1ab63f7e2d16fb9a1f1d29755d12da90aa0bb (diff)
parente693fb417253d14786976bd29a456961aa8b6343 (diff)
downloadgolang-x-tools-f10932f763d058b0dcb3acfb795c869996fef47b.tar.gz
Merge commit 'e693fb417253d14786976bd29a456961aa8b6343'
Change-Id: I65e50880732e718fa2264e47ef7cc19e37cc2f05
Diffstat (limited to 'internal/lsp/source/view.go')
-rw-r--r--internal/lsp/source/view.go47
1 files changed, 32 insertions, 15 deletions
diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go
index 19fca6e8c..4d7d411e0 100644
--- a/internal/lsp/source/view.go
+++ b/internal/lsp/source/view.go
@@ -18,6 +18,7 @@ import (
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/tools/go/analysis"
+ "golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/imports"
"golang.org/x/tools/internal/lsp/progress"
@@ -129,6 +130,12 @@ type Snapshot interface {
// GoModForFile returns the URI of the go.mod file for the given URI.
GoModForFile(uri span.URI) span.URI
+ // WorkFile, if non-empty, is the go.work file for the workspace.
+ WorkFile() span.URI
+
+ // ParseWork is used to parse go.work files.
+ ParseWork(ctx context.Context, fh FileHandle) (*ParsedWorkFile, error)
+
// BuiltinFile returns information about the special builtin package.
BuiltinFile(ctx context.Context) (*ParsedGoFile, error)
@@ -137,7 +144,7 @@ type Snapshot interface {
// PackagesForFile returns the packages that this file belongs to, checked
// in mode.
- PackagesForFile(ctx context.Context, uri span.URI, mode TypecheckMode) ([]Package, error)
+ PackagesForFile(ctx context.Context, uri span.URI, mode TypecheckMode, includeTestVariants bool) ([]Package, error)
// PackageForFile returns a single package that this file belongs to,
// checked in mode and filtered by the package policy.
@@ -201,9 +208,6 @@ const (
// Normal is appropriate for commands that might be run by a user and don't
// deliberately modify go.mod files, e.g. `go test`.
Normal InvocationFlags = iota
- // UpdateUserModFile is for commands that intend to update the user's real
- // go.mod file, e.g. `go mod tidy` in response to a user's request to tidy.
- UpdateUserModFile
// WriteTemporaryModFile is for commands that need information from a
// modified version of the user's go.mod file, e.g. `go mod tidy` used to
// generate diagnostics.
@@ -235,10 +239,6 @@ type View interface {
// Folder returns the folder with which this view was created.
Folder() span.URI
- // TempWorkspace returns the folder this view uses for its temporary
- // workspace module.
- TempWorkspace() span.URI
-
// Shutdown closes this view, and detaches it from its session.
Shutdown(ctx context.Context)
@@ -266,6 +266,9 @@ type View interface {
// RegisterModuleUpgrades registers that upgrades exist for the given modules.
RegisterModuleUpgrades(upgrades map[string]string)
+
+ // FileKind returns the type of a file
+ FileKind(FileHandle) FileKind
}
// A FileSource maps uris to FileHandles. This abstraction exists both for
@@ -297,6 +300,14 @@ type ParsedModule struct {
ParseErrors []*Diagnostic
}
+// A ParsedWorkFile contains the results of parsing a go.work file.
+type ParsedWorkFile struct {
+ URI span.URI
+ File *modfile.WorkFile
+ Mapper *protocol.ColumnMapper
+ ParseErrors []*Diagnostic
+}
+
// A TidiedModule contains the results of running `go mod tidy` on a module.
type TidiedModule struct {
// Diagnostics representing changes made by `go mod tidy`.
@@ -312,6 +323,9 @@ type Metadata interface {
// PackagePath is the package path.
PackagePath() string
+
+ // ModuleInfo returns the go/packages module information for the given package.
+ ModuleInfo() *packages.Module
}
// Session represents a single connection from a client.
@@ -325,7 +339,7 @@ type Session interface {
// non-empty tempWorkspace directory is provided, the View will record a copy
// of its gopls workspace module in that directory, so that client tooling
// can execute in the same main module.
- NewView(ctx context.Context, name string, folder, tempWorkspace span.URI, options *Options) (View, Snapshot, func(), error)
+ NewView(ctx context.Context, name string, folder span.URI, options *Options) (View, Snapshot, func(), error)
// Cache returns the cache that created this session, for debugging only.
Cache() interface{}
@@ -373,8 +387,11 @@ type Session interface {
SetProgressTracker(tracker *progress.Tracker)
}
+var ErrViewExists = errors.New("view already exists for session")
+
// Overlay is the type for a file held in memory on a session.
type Overlay interface {
+ Kind() FileKind
VersionedFileHandle
}
@@ -496,7 +513,6 @@ type VersionedFileIdentity struct {
// FileHandle represents a handle to a specific version of a single file.
type FileHandle interface {
URI() span.URI
- Kind() FileKind
// FileIdentity returns a FileIdentity for the file, even if there was an
// error reading it.
@@ -514,17 +530,14 @@ type FileIdentity struct {
// Identifier represents a unique identifier for the file's content.
Hash string
-
- // Kind is the file's kind.
- Kind FileKind
}
func (id FileIdentity) String() string {
- return fmt.Sprintf("%s%s%s", id.URI, id.Hash, id.Kind)
+ return fmt.Sprintf("%s%s", id.URI, id.Hash)
}
// FileKind describes the kind of the file in question.
-// It can be one of Go, mod, or sum.
+// It can be one of Go,mod, Sum, or Tmpl.
type FileKind int
const (
@@ -539,6 +552,8 @@ const (
Sum
// Tmpl is a template file.
Tmpl
+ // Work is a go.work file.
+ Work
)
// Analyzer represents a go/analysis analyzer with some boolean properties
@@ -643,6 +658,8 @@ const (
ModTidyError DiagnosticSource = "go mod tidy"
OptimizationDetailsError DiagnosticSource = "optimizer details"
UpgradeNotification DiagnosticSource = "upgrade available"
+ TemplateError DiagnosticSource = "template"
+ WorkFileError DiagnosticSource = "go.work file"
)
func AnalyzerErrorKind(name string) DiagnosticSource {