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 (
|
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"
|
2018-11-07 13:21:31 -07:00
|
|
|
|
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"
|
2018-11-02 16:10:49 -06:00
|
|
|
)
|
|
|
|
|
2019-05-31 17:41:39 -06:00
|
|
|
// FileIdentity uniquely identifies a file at a version from a FileSystem.
|
|
|
|
type FileIdentity struct {
|
|
|
|
URI span.URI
|
|
|
|
Version string
|
2019-09-18 23:21:54 -06:00
|
|
|
Kind FileKind
|
2019-05-31 17:41:39 -06:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
func (identity FileIdentity) String() string {
|
2019-09-18 23:21:54 -06:00
|
|
|
return fmt.Sprintf("%s%s%s", identity.URI, identity.Version, identity.Kind)
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
|
|
|
|
2019-05-31 17:41:39 -06:00
|
|
|
// FileHandle represents a handle to a specific version of a single file from
|
|
|
|
// a specific file system.
|
|
|
|
type FileHandle interface {
|
2019-06-03 23:04:18 -06:00
|
|
|
// FileSystem returns the file system this handle was acquired from.
|
2019-05-31 17:41:39 -06:00
|
|
|
FileSystem() FileSystem
|
2019-06-13 13:55:53 -06:00
|
|
|
|
2019-06-21 15:00:02 -06:00
|
|
|
// Identity returns the FileIdentity for the file.
|
2019-05-31 17:41:39 -06:00
|
|
|
Identity() FileIdentity
|
2019-06-13 13:55:53 -06:00
|
|
|
|
|
|
|
// Read reads the contents of a file and returns it along with its hash value.
|
|
|
|
// If the file is not available, returns a nil slice and an error.
|
2019-06-03 23:04:18 -06:00
|
|
|
Read(ctx context.Context) ([]byte, string, error)
|
2019-05-31 17:41:39 -06:00
|
|
|
}
|
|
|
|
|
2019-05-17 10:15:22 -06:00
|
|
|
// FileSystem is the interface to something that provides file contents.
|
|
|
|
type FileSystem interface {
|
2019-05-31 17:41:39 -06:00
|
|
|
// GetFile returns a handle for the specified file.
|
2019-09-18 23:21:54 -06:00
|
|
|
GetFile(uri span.URI, kind FileKind) FileHandle
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|
|
|
|
|
2019-06-21 15:00:02 -06:00
|
|
|
// FileKind describes the kind of the file in question.
|
|
|
|
// It can be one of Go, mod, or sum.
|
|
|
|
type FileKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Go = FileKind(iota)
|
|
|
|
Mod
|
|
|
|
Sum
|
2019-09-18 23:21:54 -06:00
|
|
|
UnknownKind
|
2019-06-21 15:00:02 -06:00
|
|
|
)
|
|
|
|
|
2019-06-13 13:55:53 -06:00
|
|
|
// ParseGoHandle represents a handle to the AST for a file.
|
2019-06-04 20:14:37 -06:00
|
|
|
type ParseGoHandle interface {
|
2019-06-13 13:55:53 -06:00
|
|
|
// File returns a file handle for which to get the AST.
|
2019-06-04 20:14:37 -06:00
|
|
|
File() FileHandle
|
2019-06-13 13:55:53 -06:00
|
|
|
|
2019-06-04 20:14:37 -06:00
|
|
|
// Mode returns the parse mode of this handle.
|
|
|
|
Mode() ParseMode
|
2019-06-13 13:55:53 -06:00
|
|
|
|
2019-06-04 20:14:37 -06:00
|
|
|
// Parse returns the parsed AST for the file.
|
|
|
|
// If the file is not available, returns nil and an error.
|
2019-09-17 09:19:11 -06:00
|
|
|
Parse(ctx context.Context) (*ast.File, *protocol.ColumnMapper, error, error)
|
2019-08-06 16:51:17 -06:00
|
|
|
|
|
|
|
// Cached returns the AST for this handle, if it has already been stored.
|
2019-10-24 13:44:41 -06:00
|
|
|
Cached() (*ast.File, *protocol.ColumnMapper, error, error)
|
2019-06-04 20:14:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseMode controls the content of the AST produced when parsing a source file.
|
|
|
|
type ParseMode int
|
|
|
|
|
|
|
|
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-06-13 13:55:53 -06:00
|
|
|
|
2019-06-04 20:14:37 -06: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-06-13 13:55:53 -06:00
|
|
|
|
2019-06-04 20:14:37 -06: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-07-09 15:52:23 -06:00
|
|
|
// CheckPackageHandle represents a handle to a specific version of a package.
|
|
|
|
// It is uniquely defined by the file handles that make up the package.
|
|
|
|
type CheckPackageHandle interface {
|
2019-09-10 15:33:35 -06:00
|
|
|
// ID returns the ID of the package associated with the CheckPackageHandle.
|
|
|
|
ID() string
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
// ParseGoHandle returns a ParseGoHandle for which to get the package.
|
|
|
|
Files() []ParseGoHandle
|
|
|
|
|
|
|
|
// Check returns the type-checked Package for the CheckPackageHandle.
|
|
|
|
Check(ctx context.Context) (Package, error)
|
|
|
|
|
|
|
|
// Cached returns the Package for the CheckPackageHandle 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-05-15 10:24:49 -06: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
|
2018-12-18 13:46:14 -07:00
|
|
|
// package does not directly access the file system.
|
2019-05-15 10:24:49 -06:00
|
|
|
// 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 {
|
2019-05-17 10:15:22 -06:00
|
|
|
// A FileSystem that reads file contents from external storage.
|
|
|
|
FileSystem
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
// NewSession creates a new Session manager and returns it.
|
2019-07-10 19:01:12 -06:00
|
|
|
NewSession(ctx context.Context) Session
|
2019-05-17 08:51:19 -06:00
|
|
|
|
|
|
|
// FileSet returns the shared fileset used by all files in the system.
|
|
|
|
FileSet() *token.FileSet
|
2019-06-04 20:14:37 -06:00
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
// ParseGoHandle returns a ParseGoHandle for the given file handle.
|
2019-08-06 16:51:17 -06:00
|
|
|
ParseGoHandle(fh FileHandle, mode ParseMode) 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-09-11 11:13:44 -06:00
|
|
|
NewView(ctx context.Context, name string, folder span.URI, options Options) View
|
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-05-15 10:24:49 -06:00
|
|
|
ViewOf(uri span.URI) View
|
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
|
|
|
|
|
|
|
// A FileSystem prefers the contents from overlays, and falls back to the
|
|
|
|
// content from the underlying cache if no overlay is present.
|
|
|
|
FileSystem
|
|
|
|
|
|
|
|
// DidOpen is invoked each time a file is opened in the editor.
|
2019-10-15 16:07:52 -06:00
|
|
|
DidOpen(ctx context.Context, uri span.URI, kind FileKind, text []byte) error
|
2019-05-17 10:15:22 -06:00
|
|
|
|
|
|
|
// DidSave is invoked each time an open file is saved in the editor.
|
|
|
|
DidSave(uri span.URI)
|
|
|
|
|
|
|
|
// DidClose is invoked each time an open file is closed in the editor.
|
|
|
|
DidClose(uri span.URI)
|
|
|
|
|
2019-08-19 10:53:51 -06:00
|
|
|
// IsOpen returns whether the editor currently has a file open.
|
2019-05-17 10:15:22 -06:00
|
|
|
IsOpen(uri span.URI) bool
|
|
|
|
|
|
|
|
// Called to set the effective contents of a file from this session.
|
2019-09-18 23:21:54 -06:00
|
|
|
SetOverlay(uri span.URI, kind FileKind, data []byte) (wasFirstChange bool)
|
2019-08-19 10:53:51 -06:00
|
|
|
|
2019-10-15 16:07:52 -06:00
|
|
|
// DidChangeOutOfBand is called when a file under the root folder changes.
|
|
|
|
// If the file was open in the editor, it returns true.
|
|
|
|
DidChangeOutOfBand(ctx context.Context, uri span.URI, change protocol.FileChangeType) bool
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// View represents a single workspace.
|
|
|
|
// This is the level at which we maintain configuration like working directory
|
|
|
|
// and build tags.
|
2018-12-18 13:46:14 -07:00
|
|
|
type View interface {
|
2019-05-15 10:24:49 -06:00
|
|
|
// Session returns the session that created this view.
|
|
|
|
Session() Session
|
2019-05-17 08:51:19 -06:00
|
|
|
|
|
|
|
// Name returns the name this view was constructed with.
|
2019-05-14 21:04:23 -06:00
|
|
|
Name() string
|
2019-05-17 08:51:19 -06:00
|
|
|
|
|
|
|
// Folder returns the root folder for this view.
|
2019-05-14 21:04:23 -06:00
|
|
|
Folder() span.URI
|
2019-05-17 08:51:19 -06:00
|
|
|
|
2019-09-16 15:17:59 -06:00
|
|
|
// BuiltinPackage returns the type information for the special "builtin" package.
|
|
|
|
BuiltinPackage() BuiltinPackage
|
2019-05-17 08:51:19 -06:00
|
|
|
|
2019-08-19 10:53:51 -06:00
|
|
|
// GetFile returns the file object for a given URI, initializing it
|
|
|
|
// if it is not already part of the view.
|
2019-02-19 19:11:15 -07:00
|
|
|
GetFile(ctx context.Context, uri span.URI) (File, error)
|
2019-05-17 08:51:19 -06:00
|
|
|
|
2019-08-19 10:53:51 -06:00
|
|
|
// FindFile returns the file object for a given URI if it is
|
|
|
|
// already part of the view.
|
|
|
|
FindFile(ctx context.Context, uri span.URI) File
|
|
|
|
|
2019-05-17 08:51:19 -06:00
|
|
|
// Called to set the effective contents of a file from this view.
|
2019-08-12 16:50:01 -06:00
|
|
|
SetContent(ctx context.Context, uri span.URI, content []byte) (wasFirstChange bool, err error)
|
2019-05-17 08:51:19 -06:00
|
|
|
|
|
|
|
// BackgroundContext returns a context used for all background processing
|
|
|
|
// on behalf of this view.
|
2019-05-14 21:04:23 -06:00
|
|
|
BackgroundContext() context.Context
|
2019-05-17 08:51:19 -06:00
|
|
|
|
|
|
|
// Shutdown closes this view, and detaches it from it's session.
|
2019-05-02 08:55:04 -06:00
|
|
|
Shutdown(ctx context.Context)
|
2019-05-17 08:51:19 -06:00
|
|
|
|
|
|
|
// Ignore returns true if this file should be ignored by this view.
|
2019-05-15 15:58:16 -06:00
|
|
|
Ignore(span.URI) bool
|
2019-06-28 14:21:07 -06:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
// Config returns the configuration for the view.
|
2019-07-14 11:59:24 -06:00
|
|
|
Config(ctx context.Context) *packages.Config
|
2019-07-03 13:23:05 -06:00
|
|
|
|
2019-07-12 16:54:06 -06:00
|
|
|
// RunProcessEnvFunc runs fn with the process env for this view inserted into opts.
|
|
|
|
// Note: the process env contains cached module and filesystem state.
|
|
|
|
RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error, opts *imports.Options) error
|
2019-09-05 22:17:36 -06:00
|
|
|
|
2019-09-11 16:21:01 -06:00
|
|
|
// Options returns a copy of the Options for this view.
|
2019-09-11 11:13:44 -06:00
|
|
|
Options() Options
|
2019-09-11 16:21:01 -06:00
|
|
|
|
|
|
|
// SetOptions sets the options of this view to new values.
|
|
|
|
// Warning: Do not use this, unless in a test.
|
|
|
|
// This function does not correctly invalidate the view when needed.
|
|
|
|
SetOptions(Options)
|
2019-09-18 16:19:41 -06:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
// CheckPackageHandles returns the CheckPackageHandles for the packages
|
|
|
|
// that this file belongs to.
|
|
|
|
CheckPackageHandles(ctx context.Context, f File) (Snapshot, []CheckPackageHandle, error)
|
|
|
|
|
2019-09-23 18:06:15 -06:00
|
|
|
// GetActiveReverseDeps returns the active files belonging to the reverse
|
|
|
|
// dependencies of this file's package.
|
2019-09-27 11:17:59 -06:00
|
|
|
GetActiveReverseDeps(ctx context.Context, f File) []CheckPackageHandle
|
2018-12-18 13:46:14 -07:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
// Snapshot returns the current snapshot for the view.
|
|
|
|
Snapshot() Snapshot
|
2019-05-03 22:04:18 -06:00
|
|
|
}
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
// Snapshot represents the current state for the given view.
|
|
|
|
type Snapshot interface {
|
|
|
|
// Handle returns the FileHandle for the given file.
|
|
|
|
Handle(ctx context.Context, f File) FileHandle
|
2019-10-04 15:18:43 -06:00
|
|
|
|
|
|
|
// View returns the View associated with this snapshot.
|
|
|
|
View() View
|
|
|
|
|
|
|
|
// Analyze runs the analyses for the given package at this snapshot.
|
2019-10-24 16:15:23 -06:00
|
|
|
Analyze(ctx context.Context, id string, analyzers []*analysis.Analyzer) ([]*Error, error)
|
2019-10-24 13:44:41 -06:00
|
|
|
|
|
|
|
// FindAnalysisError returns the analysis error represented by the diagnostic.
|
|
|
|
// This is used to get the SuggestedFixes associated with that error.
|
|
|
|
FindAnalysisError(ctx context.Context, id string, diag protocol.Diagnostic) (*Error, error)
|
|
|
|
|
|
|
|
// CheckPackageHandles returns the CheckPackageHandles for the packages
|
|
|
|
// that this file belongs to.
|
|
|
|
CheckPackageHandles(ctx context.Context, f File) ([]CheckPackageHandle, error)
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
// File represents a source file of any type.
|
|
|
|
type File interface {
|
|
|
|
URI() span.URI
|
|
|
|
Kind() FileKind
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
|
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-09-09 17:26:26 -06:00
|
|
|
Files() []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
|
2019-09-24 14:28:59 -06:00
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
// GetImport returns the CheckPackageHandle for a package imported by this package.
|
|
|
|
GetImport(ctx context.Context, pkgPath string) (Package, error)
|
|
|
|
|
2019-09-09 18:22:42 -06:00
|
|
|
// FindFile returns the AST and type information for a file that may
|
|
|
|
// belong to or be part of a dependency of the given package.
|
2019-09-16 16:17:51 -06:00
|
|
|
FindFile(ctx context.Context, uri span.URI) (ParseGoHandle, Package, error)
|
2019-10-23 14:10:24 -06:00
|
|
|
|
|
|
|
View() View
|
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 {
|
2019-10-21 15:25:09 -06:00
|
|
|
URI span.URI
|
|
|
|
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 {
|
2019-10-21 15:25:09 -06:00
|
|
|
return fmt.Sprintf("%s:%s: %s", e.URI, e.Range, e.Message)
|
2019-10-20 17:57:03 -06:00
|
|
|
}
|
|
|
|
|
2019-09-16 15:17:59 -06:00
|
|
|
type BuiltinPackage interface {
|
|
|
|
Lookup(name string) *ast.Object
|
2019-09-16 16:17:51 -06:00
|
|
|
Files() []ParseGoHandle
|
2019-09-16 15:17:59 -06:00
|
|
|
}
|