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"
|
2020-07-21 13:15:06 -06:00
|
|
|
"golang.org/x/tools/internal/memoize"
|
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-07-28 15:00:10 -06:00
|
|
|
// Fileset returns the Fileset used to parse all the Go files in this snapshot.
|
|
|
|
FileSet() *token.FileSet
|
|
|
|
|
2020-04-23 21:24:24 -06:00
|
|
|
// FindFile returns the FileHandle for the given URI, if it is already
|
|
|
|
// in the given snapshot.
|
2020-07-26 16:01:39 -06:00
|
|
|
FindFile(uri span.URI) VersionedFileHandle
|
2020-04-23 21:24:24 -06:00
|
|
|
|
|
|
|
// GetFile returns the FileHandle for a given URI, initializing it
|
|
|
|
// if it is not already part of the snapshot.
|
2020-07-26 16:01:39 -06:00
|
|
|
GetFile(ctx context.Context, uri span.URI) (VersionedFileHandle, 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
|
|
|
|
|
2020-07-21 13:15:06 -06:00
|
|
|
// ParseGo returns the parsed AST for the file.
|
|
|
|
// If the file is not available, returns nil and an error.
|
|
|
|
ParseGo(ctx context.Context, fh FileHandle, mode ParseMode) (*ParsedGoFile, error)
|
|
|
|
|
|
|
|
// PosToField is a cache of *ast.Fields by token.Pos. This allows us
|
|
|
|
// to quickly find corresponding *ast.Field node given a *types.Var.
|
|
|
|
// We must refer to the AST to render type aliases properly when
|
|
|
|
// formatting signatures and other types.
|
|
|
|
PosToField(ctx context.Context, pgf *ParsedGoFile) (map[token.Pos]*ast.Field, error)
|
|
|
|
|
|
|
|
// PosToDecl maps certain objects' positions to their surrounding
|
|
|
|
// ast.Decl. This mapping is used when building the documentation
|
|
|
|
// string for the objects.
|
|
|
|
PosToDecl(ctx context.Context, pgf *ParsedGoFile) (map[token.Pos]ast.Decl, error)
|
|
|
|
|
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-07-24 11:39:58 -06:00
|
|
|
// ParseMod is used to parse go.mod files.
|
|
|
|
ParseMod(ctx context.Context, fh FileHandle) (*ParsedModule, error)
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
// ModWhy returns the results of `go mod why` for the snapshot's module.
|
|
|
|
ModWhy(ctx context.Context) (map[string]string, error)
|
2020-06-19 17:07:57 -06:00
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
// ModUpgrade returns the possible updates for the snapshot's module.
|
|
|
|
ModUpgrade(ctx context.Context) (map[string]string, error)
|
2020-06-19 17:07:57 -06:00
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
// ModTidy returns the results of `go mod tidy` for the snapshot's module.
|
|
|
|
ModTidy(ctx context.Context) (*TidiedModule, error)
|
2020-02-06 12:50:26 -07:00
|
|
|
|
2020-07-24 15:41:50 -06:00
|
|
|
// BuiltinPackage returns information about the special builtin package.
|
|
|
|
BuiltinPackage(ctx context.Context) (*BuiltinPackage, error)
|
|
|
|
|
2020-07-22 09:32:32 -06:00
|
|
|
// PackagesForFile returns the packages that this file belongs to.
|
|
|
|
PackagesForFile(ctx context.Context, uri span.URI) ([]Package, 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-07-22 09:32:32 -06:00
|
|
|
GetReverseDependencies(ctx context.Context, id string) ([]Package, 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.
|
2020-07-22 09:32:32 -06:00
|
|
|
KnownPackages(ctx context.Context) ([]Package, error)
|
2020-01-10 15:18:59 -07:00
|
|
|
|
2020-07-22 09:32:32 -06:00
|
|
|
// WorkspacePackages returns the snapshot's top-level packages.
|
|
|
|
WorkspacePackages(ctx context.Context) ([]Package, error)
|
2020-07-28 16:18:43 -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) []span.URI
|
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
|
|
|
|
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.
|
2020-07-02 16:34:10 -06:00
|
|
|
Snapshot() (Snapshot, func())
|
2020-01-23 11:58:25 -07:00
|
|
|
|
|
|
|
// Rebuild rebuilds the current view, replacing the original view in its session.
|
2020-07-02 16:34:10 -06:00
|
|
|
Rebuild(ctx context.Context) (Snapshot, func(), 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
|
2019-05-15 10:24:49 -06:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:41:50 -06:00
|
|
|
type BuiltinPackage struct {
|
|
|
|
Package *ast.Package
|
|
|
|
ParsedFile *ParsedGoFile
|
2020-07-21 13:15:06 -06:00
|
|
|
}
|
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
// A ParsedGoFile contains the results of parsing a Go file.
|
2020-07-21 13:15:06 -06:00
|
|
|
type ParsedGoFile struct {
|
|
|
|
memoize.NoCopy
|
|
|
|
|
|
|
|
URI span.URI
|
|
|
|
Mode ParseMode
|
|
|
|
File *ast.File
|
|
|
|
Tok *token.File
|
|
|
|
// Source code used to build the AST. It may be different from the
|
|
|
|
// actual content of the file if we have fixed the AST.
|
|
|
|
Src []byte
|
|
|
|
Mapper *protocol.ColumnMapper
|
|
|
|
ParseErr error
|
2020-06-05 15:30:52 -06:00
|
|
|
}
|
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
// A ParsedModule contains the results of parsing a go.mod file.
|
|
|
|
type ParsedModule struct {
|
|
|
|
memoize.NoCopy
|
|
|
|
|
|
|
|
File *modfile.File
|
|
|
|
Mapper *protocol.ColumnMapper
|
|
|
|
ParseErrors []Error
|
|
|
|
}
|
|
|
|
|
2020-07-29 22:12:55 -06:00
|
|
|
// A TidiedModule contains the results of running `go mod tidy` on a module.
|
2020-07-24 11:39:58 -06:00
|
|
|
type TidiedModule struct {
|
|
|
|
memoize.NoCopy
|
|
|
|
|
|
|
|
// The parsed module, which is guaranteed to have parsed successfully.
|
|
|
|
Parsed *ParsedModule
|
|
|
|
// Diagnostics representing changes made by `go mod tidy`.
|
|
|
|
Errors []Error
|
|
|
|
// The bytes of the go.mod file after it was tidied.
|
|
|
|
TidiedContent []byte
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-07-02 16:34:10 -06:00
|
|
|
// NewView creates a new View, returning it and its first snapshot.
|
|
|
|
NewView(ctx context.Context, name string, folder span.URI, options Options) (View, Snapshot, func(), error)
|
2019-05-15 10:24:49 -06:00
|
|
|
|
2020-07-28 15:00:10 -06:00
|
|
|
// Cache returns the cache that created this session, for debugging only.
|
|
|
|
Cache() interface{}
|
2019-05-15 10:24:49 -06:00
|
|
|
|
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
|
|
|
|
2020-07-28 16:18:43 -06:00
|
|
|
// DidModifyFile reports a file modification to the session. It returns the
|
|
|
|
// resulting snapshots, a guaranteed one per view.
|
2020-07-02 16:34:10 -06:00
|
|
|
DidModifyFiles(ctx context.Context, changes []FileModification) ([]Snapshot, []func(), []span.URI, 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 {
|
2020-07-26 16:01:39 -06:00
|
|
|
VersionedFileHandle
|
2020-06-02 08:57:20 -06:00
|
|
|
|
|
|
|
// Saved returns whether this overlay has been saved to disk.
|
|
|
|
Saved() bool
|
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-07-19 13:11:07 -06:00
|
|
|
func (a FileAction) String() string {
|
|
|
|
switch a {
|
|
|
|
case Open:
|
|
|
|
return "Open"
|
|
|
|
case Change:
|
|
|
|
return "Change"
|
|
|
|
case Close:
|
|
|
|
return "Close"
|
|
|
|
case Save:
|
|
|
|
return "Save"
|
|
|
|
case Create:
|
|
|
|
return "Create"
|
|
|
|
case Delete:
|
|
|
|
return "Delete"
|
|
|
|
case InvalidateMetadata:
|
|
|
|
return "InvalidateMetadata"
|
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-07-26 16:01:39 -06:00
|
|
|
type VersionedFileHandle interface {
|
|
|
|
FileHandle
|
|
|
|
Version() float64
|
|
|
|
Session() string
|
|
|
|
|
|
|
|
// LSPIdentity returns the version identity of a file.
|
|
|
|
VersionedFileIdentity() VersionedFileIdentity
|
|
|
|
}
|
|
|
|
|
|
|
|
type VersionedFileIdentity struct {
|
|
|
|
URI span.URI
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
Version float64
|
|
|
|
}
|
|
|
|
|
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
|
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.
|
2020-07-26 16:01:39 -06:00
|
|
|
FileIdentity() FileIdentity
|
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-07-26 16:01:39 -06:00
|
|
|
// Identifier represents a unique identifier for the file's content.
|
|
|
|
Hash 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 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
|
|
|
|
2020-07-13 17:36:26 -06:00
|
|
|
// Command is the name of the command used to invoke the suggested fixes
|
2020-07-23 21:24:36 -06:00
|
|
|
// for the analyzer. It is non-nil if we expect this analyzer to provide
|
|
|
|
// its fix separately from its diagnostics. That is, we should apply the
|
|
|
|
// analyzer's suggested fixes through a Command, not a TextEdit.
|
|
|
|
Command *Command
|
2020-07-13 17:36:26 -06:00
|
|
|
|
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-07-02 16:34:10 -06:00
|
|
|
func (a Analyzer) Enabled(view View) bool {
|
|
|
|
if enabled, ok := view.Options().UserEnabledAnalyses[a.Analyzer.Name]; ok {
|
2020-04-01 21:22:23 -06:00
|
|
|
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
|
2020-06-07 19:50:35 -06:00
|
|
|
Name() string
|
2019-05-01 20:46:07 -06:00
|
|
|
PkgPath() string
|
2020-07-21 13:15:06 -06:00
|
|
|
CompiledGoFiles() []*ParsedGoFile
|
|
|
|
File(uri span.URI) (*ParsedGoFile, 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)
|
2020-07-15 15:15:09 -06:00
|
|
|
MissingDependencies() []string
|
2019-10-29 16:13:19 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-26 00:55:12 -06:00
|
|
|
// GoModTidy is the source for a diagnostic computed by running `go mod tidy`.
|
|
|
|
const GoModTidy = "go mod tidy"
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
type ErrorKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
UnknownError = ErrorKind(iota)
|
|
|
|
ListError
|
|
|
|
ParseError
|
|
|
|
TypeError
|
2020-07-14 22:19:10 -06:00
|
|
|
ModTidyError
|
2019-10-21 15:25:09 -06:00
|
|
|
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
|
|
|
|
2020-07-13 23:50:57 -06:00
|
|
|
var (
|
|
|
|
InconsistentVendoring = errors.New("inconsistent vendoring")
|
|
|
|
PackagesLoadError = errors.New("packages.Load error")
|
|
|
|
)
|