2018-11-02 16:10:49 -06:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
2020-06-11 16:25:17 -06:00
|
|
|
"bytes"
|
2018-12-18 14:18:03 -07:00
|
|
|
"context"
|
2019-07-09 15:52:23 -06:00
|
|
|
"fmt"
|
2018-11-02 16:10:49 -06:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2019-03-06 14:33:47 -07:00
|
|
|
"go/types"
|
2020-03-23 20:47:52 -06:00
|
|
|
"io"
|
2018-11-07 13:21:31 -07:00
|
|
|
|
2019-12-11 11:44:39 -07:00
|
|
|
"golang.org/x/mod/modfile"
|
2019-03-06 14:33:47 -07:00
|
|
|
"golang.org/x/tools/go/analysis"
|
2018-11-07 13:21:31 -07:00
|
|
|
"golang.org/x/tools/go/packages"
|
2019-07-03 13:23:05 -06:00
|
|
|
"golang.org/x/tools/internal/imports"
|
2019-09-03 14:07:13 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2020-05-28 19:21:29 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2018-11-02 16:10:49 -06:00
|
|
|
)
|
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Snapshot represents the current state for the given view.
|
|
|
|
type Snapshot interface {
|
2020-01-13 18:41:03 -07:00
|
|
|
ID() uint64
|
|
|
|
|
2019-11-20 15:57:05 -07:00
|
|
|
// View returns the View associated with this snapshot.
|
|
|
|
View() View
|
2019-05-17 10:15:22 -06:00
|
|
|
|
2020-04-23 21:24:24 -06:00
|
|
|
// FindFile returns the FileHandle for the given URI, if it is already
|
|
|
|
// in the given snapshot.
|
|
|
|
FindFile(uri span.URI) FileHandle
|
|
|
|
|
|
|
|
// GetFile returns the FileHandle for a given URI, initializing it
|
|
|
|
// if it is not already part of the snapshot.
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
GetFile(ctx context.Context, uri span.URI) (FileHandle, error)
|
2019-12-17 16:57:54 -07:00
|
|
|
|
2020-02-14 09:52:17 -07:00
|
|
|
// IsOpen returns whether the editor currently has a file open.
|
2020-02-06 14:20:50 -07:00
|
|
|
IsOpen(uri span.URI) bool
|
|
|
|
|
2020-02-14 09:52:17 -07:00
|
|
|
// IsSaved returns whether the contents are saved on disk or not.
|
|
|
|
IsSaved(uri span.URI) bool
|
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Analyze runs the analyses for the given package at this snapshot.
|
2020-03-31 21:53:42 -06:00
|
|
|
Analyze(ctx context.Context, pkgID string, analyzers ...*analysis.Analyzer) ([]*Error, error)
|
2019-06-13 13:55:53 -06:00
|
|
|
|
2020-06-11 16:25:17 -06:00
|
|
|
// RunGoCommandPiped runs the given `go` command in the view, using the
|
2020-06-21 21:21:15 -06:00
|
|
|
// provided stdout and stderr. It will use the -modfile flag, if possible.
|
2020-06-11 16:25:17 -06:00
|
|
|
RunGoCommandPiped(ctx context.Context, verb string, args []string, stdout, stderr io.Writer) error
|
|
|
|
|
2020-06-21 21:21:15 -06:00
|
|
|
// RunGoCommand runs the given `go` command in the view. It will use the
|
|
|
|
// -modfile flag, if possible.
|
2020-06-11 16:25:17 -06:00
|
|
|
RunGoCommand(ctx context.Context, verb string, args []string) (*bytes.Buffer, error)
|
|
|
|
|
2020-06-21 21:21:15 -06:00
|
|
|
// RunGoCommandDirect runs the given `go` command, never using the
|
|
|
|
// -modfile flag.
|
|
|
|
RunGoCommandDirect(ctx context.Context, verb string, args []string) error
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
// ParseModHandle is used to parse go.mod files.
|
|
|
|
ParseModHandle(ctx context.Context, fh FileHandle) (ParseModHandle, error)
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
// ModWhyHandle is used get the results of `go mod why` for a given module.
|
|
|
|
// It only works for go.mod files that can be parsed, hence it takes a
|
|
|
|
// ParseModHandle.
|
|
|
|
ModWhyHandle(ctx context.Context) (ModWhyHandle, error)
|
|
|
|
|
|
|
|
// ModWhyHandle is used get the possible upgrades for the dependencies of
|
|
|
|
// a given module. It only works for go.mod files that can be parsed, hence
|
|
|
|
// it takes a ParseModHandle.
|
|
|
|
ModUpgradeHandle(ctx context.Context) (ModUpgradeHandle, error)
|
|
|
|
|
|
|
|
// ModWhyHandle is used get the results of `go mod tidy` for a given
|
|
|
|
// module. It only works for go.mod files that can be parsed, hence it
|
|
|
|
// takes a ParseModHandle.
|
|
|
|
ModTidyHandle(ctx context.Context) (ModTidyHandle, error)
|
2020-02-06 12:50:26 -07:00
|
|
|
|
2020-01-14 16:29:21 -07:00
|
|
|
// PackageHandles returns the PackageHandles for the packages that this file
|
|
|
|
// belongs to.
|
2019-11-29 23:17:57 -07:00
|
|
|
PackageHandles(ctx context.Context, fh FileHandle) ([]PackageHandle, error)
|
2019-06-13 13:55:53 -06:00
|
|
|
|
2019-11-20 15:57:05 -07:00
|
|
|
// GetActiveReverseDeps returns the active files belonging to the reverse
|
|
|
|
// dependencies of this file's package.
|
2020-01-10 15:18:59 -07:00
|
|
|
GetReverseDependencies(ctx context.Context, id string) ([]PackageHandle, error)
|
2019-11-20 15:57:05 -07:00
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
// CachedImportPaths returns all the imported packages loaded in this snapshot,
|
2019-11-20 14:38:43 -07:00
|
|
|
// indexed by their import path.
|
2020-01-10 15:18:59 -07:00
|
|
|
CachedImportPaths(ctx context.Context) (map[string]Package, error)
|
2019-08-06 16:51:17 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// KnownPackages returns all the packages loaded in this snapshot.
|
2020-02-07 12:38:36 -07:00
|
|
|
// Workspace packages may be parsed in ParseFull mode, whereas transitive
|
|
|
|
// dependencies will be in ParseExported mode.
|
2019-12-19 12:31:39 -07:00
|
|
|
KnownPackages(ctx context.Context) ([]PackageHandle, error)
|
2020-01-10 15:18:59 -07:00
|
|
|
|
|
|
|
// WorkspacePackages returns the PackageHandles for the snapshot's
|
|
|
|
// top-level packages.
|
|
|
|
WorkspacePackages(ctx context.Context) ([]PackageHandle, error)
|
2019-06-04 20:14:37 -06:00
|
|
|
}
|
|
|
|
|
2019-11-29 23:17:57 -07:00
|
|
|
// PackageHandle represents a handle to a specific version of a package.
|
2019-07-09 15:52:23 -06:00
|
|
|
// It is uniquely defined by the file handles that make up the package.
|
2019-11-29 23:17:57 -07:00
|
|
|
type PackageHandle interface {
|
2020-01-14 16:29:21 -07:00
|
|
|
// ID returns the ID of the package associated with the PackageHandle.
|
2019-09-10 15:33:35 -06:00
|
|
|
ID() string
|
|
|
|
|
2019-11-29 23:17:57 -07:00
|
|
|
// CompiledGoFiles returns the ParseGoHandles composing the package.
|
2019-11-20 14:15:00 -07:00
|
|
|
CompiledGoFiles() []ParseGoHandle
|
2019-07-09 15:52:23 -06:00
|
|
|
|
2020-01-14 16:29:21 -07:00
|
|
|
// Check returns the type-checked Package for the PackageHandle.
|
2019-07-09 15:52:23 -06:00
|
|
|
Check(ctx context.Context) (Package, error)
|
|
|
|
|
2020-01-14 16:29:21 -07:00
|
|
|
// Cached returns the Package for the PackageHandle if it has already been stored.
|
2019-10-24 13:44:41 -06:00
|
|
|
Cached() (Package, error)
|
2019-09-23 18:19:50 -06:00
|
|
|
|
2019-10-04 15:18:43 -06:00
|
|
|
// MissingDependencies reports any unresolved imports.
|
2019-09-23 18:19:50 -06:00
|
|
|
MissingDependencies() []string
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// View represents a single workspace.
|
|
|
|
// This is the level at which we maintain configuration like working directory
|
|
|
|
// and build tags.
|
|
|
|
type View interface {
|
|
|
|
// Session returns the session that created this view.
|
|
|
|
Session() Session
|
2019-05-17 10:15:22 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Name returns the name this view was constructed with.
|
|
|
|
Name() string
|
2019-05-17 08:51:19 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Folder returns the root folder for this view.
|
|
|
|
Folder() span.URI
|
2019-06-04 20:14:37 -06:00
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
// ModFile is the go.mod file at the root of this view. It may not exist.
|
|
|
|
ModFile() span.URI
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-06-05 15:30:52 -06:00
|
|
|
// BuiltinPackage returns the go/ast.Object for the given name in the builtin package.
|
|
|
|
BuiltinPackage(ctx context.Context) (BuiltinPackage, error)
|
2019-11-20 14:38:43 -07:00
|
|
|
|
|
|
|
// BackgroundContext returns a context used for all background processing
|
|
|
|
// on behalf of this view.
|
|
|
|
BackgroundContext() context.Context
|
|
|
|
|
|
|
|
// Shutdown closes this view, and detaches it from it's session.
|
|
|
|
Shutdown(ctx context.Context)
|
|
|
|
|
2020-03-23 20:47:52 -06:00
|
|
|
// WriteEnv writes the view-specific environment to the io.Writer.
|
|
|
|
WriteEnv(ctx context.Context, w io.Writer) error
|
|
|
|
|
2020-02-06 15:49:19 -07:00
|
|
|
// RunProcessEnvFunc runs fn with the process env for this snapshot's view.
|
2019-11-20 14:38:43 -07:00
|
|
|
// Note: the process env contains cached module and filesystem state.
|
2019-12-27 12:07:33 -07:00
|
|
|
RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error) error
|
2019-11-20 14:38:43 -07:00
|
|
|
|
|
|
|
// Options returns a copy of the Options for this view.
|
|
|
|
Options() Options
|
|
|
|
|
|
|
|
// SetOptions sets the options of this view to new values.
|
|
|
|
// Calling this may cause the view to be invalidated and a replacement view
|
|
|
|
// added to the session. If so the new view will be returned, otherwise the
|
|
|
|
// original one will be.
|
|
|
|
SetOptions(context.Context, Options) (View, error)
|
|
|
|
|
|
|
|
// Snapshot returns the current snapshot for the view.
|
|
|
|
Snapshot() Snapshot
|
2020-01-23 11:58:25 -07:00
|
|
|
|
|
|
|
// Rebuild rebuilds the current view, replacing the original view in its session.
|
|
|
|
Rebuild(ctx context.Context) (Snapshot, error)
|
2020-01-23 23:22:47 -07:00
|
|
|
|
|
|
|
// InvalidBuildConfiguration returns true if there is some error in the
|
|
|
|
// user's workspace. In particular, if they are both outside of a module
|
|
|
|
// and their GOPATH.
|
|
|
|
ValidBuildConfiguration() bool
|
2020-05-12 19:56:33 -06:00
|
|
|
|
|
|
|
// IsGoPrivatePath reports whether target is a private import path, as identified
|
|
|
|
// by the GOPRIVATE environment variable.
|
|
|
|
IsGoPrivatePath(path string) bool
|
2020-06-22 12:38:32 -06:00
|
|
|
|
|
|
|
// IgnoredFile reports if a file would be ignored by a `go list` of the whole
|
|
|
|
// workspace.
|
|
|
|
IgnoredFile(uri span.URI) bool
|
2020-06-20 14:11:01 -06:00
|
|
|
|
|
|
|
// WorkspaceDirectories returns any directory known by the view. For views
|
|
|
|
// within a module, this is the module root and any replace targets.
|
|
|
|
WorkspaceDirectories(ctx context.Context) ([]string, error)
|
2019-05-15 10:24:49 -06:00
|
|
|
}
|
|
|
|
|
2020-06-05 15:30:52 -06:00
|
|
|
type BuiltinPackage interface {
|
|
|
|
Package() *ast.Package
|
|
|
|
ParseGoHandle() ParseGoHandle
|
|
|
|
}
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
// Session represents a single connection from a client.
|
|
|
|
// This is the level at which things like open files are maintained on behalf
|
|
|
|
// of the client.
|
|
|
|
// A session may have many active views at any given time.
|
|
|
|
type Session interface {
|
|
|
|
// NewView creates a new View and returns it.
|
2019-11-29 21:51:14 -07:00
|
|
|
NewView(ctx context.Context, name string, folder span.URI, options Options) (View, Snapshot, error)
|
2019-05-15 10:24:49 -06:00
|
|
|
|
|
|
|
// Cache returns the cache that created this session.
|
|
|
|
Cache() Cache
|
|
|
|
|
2019-09-11 00:14:36 -06:00
|
|
|
// View returns a view with a matching name, if the session has one.
|
2019-05-15 10:24:49 -06:00
|
|
|
View(name string) View
|
2019-05-17 08:51:19 -06:00
|
|
|
|
|
|
|
// ViewOf returns a view corresponding to the given URI.
|
2019-11-15 10:43:45 -07:00
|
|
|
ViewOf(uri span.URI) (View, error)
|
2019-05-17 08:51:19 -06:00
|
|
|
|
|
|
|
// Views returns the set of active views built by this session.
|
2019-05-15 10:24:49 -06:00
|
|
|
Views() []View
|
|
|
|
|
2019-05-17 08:51:19 -06:00
|
|
|
// Shutdown the session and all views it has created.
|
2019-05-15 10:24:49 -06:00
|
|
|
Shutdown(ctx context.Context)
|
2019-05-17 10:15:22 -06:00
|
|
|
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
// GetFile returns a handle for the specified file.
|
|
|
|
GetFile(ctx context.Context, uri span.URI) (FileHandle, error)
|
2019-05-17 10:15:22 -06:00
|
|
|
|
2019-12-10 10:29:37 -07:00
|
|
|
// DidModifyFile reports a file modification to the session.
|
2020-01-23 11:58:25 -07:00
|
|
|
// It returns the resulting snapshots, a guaranteed one per view.
|
2020-01-22 19:34:21 -07:00
|
|
|
DidModifyFiles(ctx context.Context, changes []FileModification) ([]Snapshot, error)
|
2019-08-19 10:53:51 -06:00
|
|
|
|
2020-06-02 08:57:20 -06:00
|
|
|
// Overlays returns a slice of file overlays for the session.
|
|
|
|
Overlays() []Overlay
|
2020-05-07 11:22:13 -06:00
|
|
|
|
2019-09-05 22:17:36 -06:00
|
|
|
// Options returns a copy of the SessionOptions for this session.
|
2019-09-11 11:13:44 -06:00
|
|
|
Options() Options
|
2019-09-05 22:17:36 -06:00
|
|
|
|
|
|
|
// SetOptions sets the options of this session to new values.
|
2019-09-11 11:13:44 -06:00
|
|
|
SetOptions(Options)
|
2019-05-15 10:24:49 -06:00
|
|
|
}
|
|
|
|
|
2020-06-02 08:57:20 -06:00
|
|
|
// Overlay is the type for a file held in memory on a session.
|
|
|
|
type Overlay interface {
|
|
|
|
// Session returns the session this overlay belongs to.
|
|
|
|
Session() Session
|
|
|
|
|
|
|
|
// Identity returns the FileIdentity for the overlay.
|
|
|
|
Identity() FileIdentity
|
|
|
|
|
|
|
|
// Saved returns whether this overlay has been saved to disk.
|
|
|
|
Saved() bool
|
|
|
|
|
|
|
|
// Data is the contents of the overlay held in memory.
|
|
|
|
Data() []byte
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
// FileModification represents a modification to a file.
|
|
|
|
type FileModification struct {
|
|
|
|
URI span.URI
|
|
|
|
Action FileAction
|
|
|
|
|
2020-01-09 20:45:06 -07:00
|
|
|
// OnDisk is true if a watched file is changed on disk.
|
|
|
|
// If true, Version will be -1 and Text will be nil.
|
|
|
|
OnDisk bool
|
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
// Version will be -1 and Text will be nil when they are not supplied,
|
2020-01-09 20:45:06 -07:00
|
|
|
// specifically on textDocument/didClose and for on-disk changes.
|
2019-12-04 16:45:25 -07:00
|
|
|
Version float64
|
|
|
|
Text []byte
|
|
|
|
|
|
|
|
// LanguageID is only sent from the language client on textDocument/didOpen.
|
|
|
|
LanguageID string
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:14:21 -07:00
|
|
|
type FileAction int
|
|
|
|
|
|
|
|
const (
|
2020-05-14 12:02:48 -06:00
|
|
|
UnknownFileAction = FileAction(iota)
|
|
|
|
Open
|
2019-11-13 11:14:21 -07:00
|
|
|
Change
|
2019-12-04 16:45:25 -07:00
|
|
|
Close
|
|
|
|
Save
|
2019-11-13 11:14:21 -07:00
|
|
|
Create
|
|
|
|
Delete
|
2020-05-14 12:02:48 -06:00
|
|
|
InvalidateMetadata
|
2019-11-13 11:14:21 -07:00
|
|
|
)
|
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Cache abstracts the core logic of dealing with the environment from the
|
|
|
|
// higher level logic that processes the information to produce results.
|
|
|
|
// The cache provides access to files and their contents, so the source
|
|
|
|
// package does not directly access the file system.
|
|
|
|
// A single cache is intended to be process wide, and is the primary point of
|
|
|
|
// sharing between all consumers.
|
|
|
|
// A cache may have many active sessions at any given time.
|
|
|
|
type Cache interface {
|
|
|
|
// FileSet returns the shared fileset used by all files in the system.
|
|
|
|
FileSet() *token.FileSet
|
2019-05-17 08:51:19 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// ParseGoHandle returns a ParseGoHandle for the given file handle.
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
ParseGoHandle(ctx context.Context, fh FileHandle, mode ParseMode) ParseGoHandle
|
2019-11-20 14:38:43 -07:00
|
|
|
}
|
2019-05-17 08:51:19 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// ParseGoHandle represents a handle to the AST for a file.
|
|
|
|
type ParseGoHandle interface {
|
|
|
|
// File returns a file handle for which to get the AST.
|
|
|
|
File() FileHandle
|
2019-05-17 08:51:19 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Mode returns the parse mode of this handle.
|
|
|
|
Mode() ParseMode
|
2019-05-17 08:51:19 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Parse returns the parsed AST for the file.
|
|
|
|
// If the file is not available, returns nil and an error.
|
2020-02-10 21:10:59 -07:00
|
|
|
Parse(ctx context.Context) (file *ast.File, src []byte, m *protocol.ColumnMapper, parseErr error, err error)
|
2019-06-28 14:21:07 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Cached returns the AST for this handle, if it has already been stored.
|
2020-02-10 21:10:59 -07:00
|
|
|
Cached() (file *ast.File, src []byte, m *protocol.ColumnMapper, parseErr error, err error)
|
2019-11-20 14:38:43 -07:00
|
|
|
}
|
2019-07-03 13:23:05 -06:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
type ParseModHandle interface {
|
|
|
|
// Mod returns the file handle for the go.mod file.
|
|
|
|
Mod() FileHandle
|
2020-02-06 12:50:26 -07:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
// Sum returns the file handle for the analogous go.sum file. It may be nil.
|
|
|
|
Sum() FileHandle
|
2020-02-18 13:47:38 -07:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
// Parse returns the parsed go.mod file, a column mapper, and a list of
|
|
|
|
// parse for the go.mod file.
|
|
|
|
Parse(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, []Error, error)
|
|
|
|
}
|
2020-02-20 09:17:54 -07:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
type ModUpgradeHandle interface {
|
|
|
|
// Upgrades returns the latest versions for each of the module's
|
|
|
|
// dependencies.
|
|
|
|
Upgrades(ctx context.Context) (map[string]string, error)
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
type ModWhyHandle interface {
|
|
|
|
// Why returns the results of `go mod why` for every dependency of the
|
|
|
|
// module.
|
|
|
|
Why(ctx context.Context) (map[string]string, error)
|
|
|
|
}
|
2019-12-11 11:44:39 -07:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
type ModTidyHandle interface {
|
|
|
|
// Tidy returns the results of `go mod tidy` for the module.
|
|
|
|
Tidy(ctx context.Context) (map[string]*modfile.Require, []Error, error)
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
var ErrTmpModfileUnsupported = errors.New("-modfile is unsupported for this Go version")
|
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// ParseMode controls the content of the AST produced when parsing a source file.
|
|
|
|
type ParseMode int
|
2019-09-05 22:17:36 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
const (
|
|
|
|
// ParseHeader specifies that the main package declaration and imports are needed.
|
|
|
|
// This is the mode used when attempting to examine the package graph structure.
|
|
|
|
ParseHeader = ParseMode(iota)
|
2019-09-11 16:21:01 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// ParseExported specifies that the public symbols are needed, but things like
|
|
|
|
// private symbols and function bodies are not.
|
|
|
|
// This mode is used for things where a package is being consumed only as a
|
|
|
|
// dependency.
|
|
|
|
ParseExported
|
2019-09-18 16:19:41 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// ParseFull specifies the full AST is needed.
|
|
|
|
// This is used for files of direct interest where the entire contents must
|
|
|
|
// be considered.
|
|
|
|
ParseFull
|
|
|
|
)
|
2019-09-27 11:17:59 -06:00
|
|
|
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
// FileHandle represents a handle to a specific version of a single file.
|
2019-11-20 14:38:43 -07:00
|
|
|
type FileHandle interface {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
URI() span.URI
|
|
|
|
Kind() FileKind
|
|
|
|
Version() float64
|
2018-12-18 13:46:14 -07:00
|
|
|
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
// Identity returns a FileIdentity for the file, even if there was an error
|
|
|
|
// reading it.
|
|
|
|
// It is a fatal error to call Identity on a file that has not yet been read.
|
2019-11-20 14:38:43 -07:00
|
|
|
Identity() FileIdentity
|
2019-10-29 16:13:19 -06:00
|
|
|
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
// Read reads the contents of a file.
|
2019-11-20 14:38:43 -07:00
|
|
|
// If the file is not available, returns a nil slice and an error.
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
Read() ([]byte, error)
|
2019-05-03 22:04:18 -06:00
|
|
|
}
|
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// FileIdentity uniquely identifies a file at a version from a FileSystem.
|
|
|
|
type FileIdentity struct {
|
|
|
|
URI span.URI
|
2019-11-15 12:47:29 -07:00
|
|
|
|
2020-03-04 11:55:41 -07:00
|
|
|
// SessionID is the ID of the LSP session.
|
|
|
|
SessionID string
|
|
|
|
|
|
|
|
// Version is the version of the file, as specified by the client. It should
|
|
|
|
// only be set in combination with SessionID.
|
2019-11-20 14:38:43 -07:00
|
|
|
Version float64
|
2019-10-04 15:18:43 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Identifier represents a unique identifier for the file.
|
|
|
|
// It could be a file's modification time or its SHA1 hash if it is not on disk.
|
|
|
|
Identifier string
|
2019-10-04 15:18:43 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Kind is the file's kind.
|
|
|
|
Kind FileKind
|
|
|
|
}
|
2019-10-24 13:44:41 -06:00
|
|
|
|
2019-11-20 22:08:58 -07:00
|
|
|
func (fileID FileIdentity) String() string {
|
2019-11-20 12:26:02 -07:00
|
|
|
// Version is not part of the FileIdentity string,
|
|
|
|
// as it can remain change even if the file does not.
|
|
|
|
return fmt.Sprintf("%s%s%s", fileID.URI, fileID.Identifier, fileID.Kind)
|
2019-11-20 14:38:43 -07:00
|
|
|
}
|
2019-10-24 13:44:41 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// FileKind describes the kind of the file in question.
|
|
|
|
// It can be one of Go, mod, or sum.
|
|
|
|
type FileKind int
|
2019-11-11 14:51:47 -07:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
const (
|
2020-06-19 17:07:57 -06:00
|
|
|
// UnknownKind is a file type we don't know about.
|
|
|
|
UnknownKind = FileKind(iota)
|
|
|
|
|
2020-05-06 20:54:50 -06:00
|
|
|
// Go is a normal go source file.
|
2020-06-19 17:07:57 -06:00
|
|
|
Go
|
2020-05-06 20:54:50 -06:00
|
|
|
// Mod is a go.mod file.
|
2019-11-20 14:38:43 -07:00
|
|
|
Mod
|
2020-05-06 20:54:50 -06:00
|
|
|
// Sum is a go.sum file.
|
2019-11-20 14:38:43 -07:00
|
|
|
Sum
|
|
|
|
)
|
2019-05-23 13:03:11 -06:00
|
|
|
|
2020-03-16 09:49:00 -06:00
|
|
|
// Analyzer represents a go/analysis analyzer with some boolean properties
|
|
|
|
// that let the user know how to use the analyzer.
|
|
|
|
type Analyzer struct {
|
|
|
|
Analyzer *analysis.Analyzer
|
2020-04-01 21:22:23 -06:00
|
|
|
enabled bool
|
2020-03-15 13:41:57 -06:00
|
|
|
|
|
|
|
// If this is true, then we can apply the suggested fixes
|
|
|
|
// as part of a source.FixAll codeaction.
|
|
|
|
HighConfidence bool
|
2020-03-31 21:53:42 -06:00
|
|
|
|
|
|
|
// FixesError is only set for type-error analyzers.
|
|
|
|
// It reports true if the message provided indicates an error that could be
|
|
|
|
// fixed by the analyzer.
|
|
|
|
FixesError func(msg string) bool
|
2020-03-16 09:49:00 -06:00
|
|
|
}
|
|
|
|
|
2020-04-01 21:22:23 -06:00
|
|
|
func (a Analyzer) Enabled(snapshot Snapshot) bool {
|
|
|
|
if enabled, ok := snapshot.View().Options().UserEnabledAnalyses[a.Analyzer.Name]; ok {
|
|
|
|
return enabled
|
|
|
|
}
|
|
|
|
return a.enabled
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:33:47 -07:00
|
|
|
// Package represents a Go package that has been type-checked. It maintains
|
|
|
|
// only the relevant fields of a *go/packages.Package.
|
|
|
|
type Package interface {
|
2019-06-11 16:06:27 -06:00
|
|
|
ID() string
|
2019-05-01 20:46:07 -06:00
|
|
|
PkgPath() string
|
2019-11-20 14:15:00 -07:00
|
|
|
CompiledGoFiles() []ParseGoHandle
|
2019-09-17 09:19:11 -06:00
|
|
|
File(uri span.URI) (ParseGoHandle, error)
|
2019-10-24 13:44:41 -06:00
|
|
|
GetSyntax() []*ast.File
|
2019-10-21 15:25:09 -06:00
|
|
|
GetErrors() []*Error
|
2019-03-06 14:33:47 -07:00
|
|
|
GetTypes() *types.Package
|
|
|
|
GetTypesInfo() *types.Info
|
2019-03-29 14:39:22 -06:00
|
|
|
GetTypesSizes() types.Sizes
|
2019-03-11 15:14:55 -06:00
|
|
|
IsIllTyped() bool
|
2020-01-30 17:24:33 -07:00
|
|
|
ForTest() string
|
2019-10-29 16:13:19 -06:00
|
|
|
GetImport(pkgPath string) (Package, error)
|
|
|
|
Imports() []Package
|
2020-05-15 15:13:55 -06:00
|
|
|
Module() *packages.Module
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
2019-09-16 15:17:59 -06:00
|
|
|
|
2019-10-20 17:57:03 -06:00
|
|
|
type Error struct {
|
2020-01-13 17:43:43 -07:00
|
|
|
URI span.URI
|
2019-10-21 15:25:09 -06:00
|
|
|
Range protocol.Range
|
|
|
|
Kind ErrorKind
|
|
|
|
Message string
|
2019-10-24 13:44:41 -06:00
|
|
|
Category string // only used by analysis errors so far
|
2019-10-21 15:25:09 -06:00
|
|
|
SuggestedFixes []SuggestedFix
|
|
|
|
Related []RelatedInformation
|
2019-10-20 17:57:03 -06:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
type ErrorKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
UnknownError = ErrorKind(iota)
|
|
|
|
ListError
|
|
|
|
ParseError
|
|
|
|
TypeError
|
|
|
|
Analysis
|
|
|
|
)
|
|
|
|
|
2019-10-20 17:57:03 -06:00
|
|
|
func (e *Error) Error() string {
|
2020-01-13 17:43:43 -07:00
|
|
|
return fmt.Sprintf("%s:%s: %s", e.URI, e.Range, e.Message)
|
2019-10-20 17:57:03 -06:00
|
|
|
}
|
2020-05-28 19:21:29 -06:00
|
|
|
|
|
|
|
var InconsistentVendoring = errors.New("inconsistent vendoring")
|