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"
|
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-02-06 15:49:19 -07:00
|
|
|
// Config returns the configuration for the view.
|
|
|
|
Config(ctx context.Context) *packages.Config
|
|
|
|
|
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.
|
2020-01-10 15:37:29 -07:00
|
|
|
GetFile(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-01-16 12:32:09 -07:00
|
|
|
// ModTidyHandle returns a ModTidyHandle for the given go.mod file handle.
|
2020-01-16 12:32:09 -07:00
|
|
|
// This function can have no data or error if there is no modfile detected.
|
2020-02-06 11:59:27 -07:00
|
|
|
ModTidyHandle(ctx context.Context, fh FileHandle) (ModTidyHandle, error)
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
// ModHandle returns a ModHandle for the passed in go.mod file handle.
|
|
|
|
// This function can have no data if there is no modfile detected.
|
|
|
|
ModHandle(ctx context.Context, fh FileHandle) ModHandle
|
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-01-16 12:32:09 -07:00
|
|
|
// ModFiles returns the URIs of the go.mod files attached to the view associated with this snapshot.
|
2020-01-23 14:54:21 -07:00
|
|
|
ModFiles() (span.URI, span.URI)
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-01-11 18:29:13 -07:00
|
|
|
// LookupBuiltin returns the go/ast.Object for the given name in the builtin package.
|
2020-01-15 17:11:40 -07:00
|
|
|
LookupBuiltin(ctx context.Context, name string) (*ast.Object, 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)
|
|
|
|
|
|
|
|
// Ignore returns true if this file should be ignored by this view.
|
|
|
|
Ignore(span.URI) bool
|
|
|
|
|
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
|
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
|
|
|
|
|
|
|
// A FileSystem prefers the contents from overlays, and falls back to the
|
|
|
|
// content from the underlying cache if no overlay is present.
|
|
|
|
FileSystem
|
|
|
|
|
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-05-07 11:22:13 -06:00
|
|
|
// UnsavedFiles returns a slice of open but unsaved files in the session.
|
|
|
|
UnsavedFiles() []span.URI
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
// A FileSystem that reads file contents from external storage.
|
|
|
|
FileSystem
|
2019-05-17 08:51:19 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// 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.
|
|
|
|
ParseGoHandle(fh FileHandle, mode ParseMode) ParseGoHandle
|
|
|
|
}
|
2019-08-19 10:53:51 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// FileSystem is the interface to something that provides file contents.
|
|
|
|
type FileSystem interface {
|
|
|
|
// GetFile returns a handle for the specified file.
|
2020-01-06 16:08:39 -07:00
|
|
|
GetFile(uri span.URI) FileHandle
|
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-02-18 13:47:38 -07:00
|
|
|
// ModHandle represents a handle to the modfile for a go.mod.
|
|
|
|
type ModHandle interface {
|
2020-02-06 12:50:26 -07:00
|
|
|
// File returns a file handle for which to get the modfile.
|
|
|
|
File() FileHandle
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
// Parse returns the parsed modfile and a mapper for the go.mod file.
|
|
|
|
// If the file is not available, returns nil and an error.
|
|
|
|
Parse(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, error)
|
|
|
|
|
2020-02-06 12:50:26 -07:00
|
|
|
// Upgrades returns the parsed modfile, a mapper, and any dependency upgrades
|
2020-02-18 13:47:38 -07:00
|
|
|
// for the go.mod file. Note that this will only work if the go.mod is the view's go.mod.
|
|
|
|
// If the file is not available, returns nil and an error.
|
2020-02-06 12:50:26 -07:00
|
|
|
Upgrades(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, map[string]string, error)
|
2020-02-20 09:17:54 -07:00
|
|
|
|
|
|
|
// Why returns the parsed modfile, a mapper, and any explanations why a dependency should be
|
|
|
|
// in the go.mod file. Note that this will only work if the go.mod is the view's go.mod.
|
|
|
|
// If the file is not available, returns nil and an error.
|
|
|
|
Why(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, map[string]string, error)
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
// ModTidyHandle represents a handle to the modfile for the view.
|
|
|
|
// Specifically for the purpose of getting diagnostics by running "go mod tidy".
|
2020-01-16 12:32:09 -07:00
|
|
|
type ModTidyHandle interface {
|
2019-12-11 11:44:39 -07:00
|
|
|
// File returns a file handle for which to get the modfile.
|
|
|
|
File() FileHandle
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// Tidy returns the parsed modfile, a mapper, and "go mod tidy" errors
|
|
|
|
// for the go.mod file. If the file is not available, returns nil and an error.
|
2020-01-14 14:53:48 -07:00
|
|
|
Tidy(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, map[string]*modfile.Require, []Error, error)
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// FileHandle represents a handle to a specific version of a single file from
|
|
|
|
// a specific file system.
|
|
|
|
type FileHandle interface {
|
|
|
|
// FileSystem returns the file system this handle was acquired from.
|
|
|
|
FileSystem() FileSystem
|
2018-12-18 13:46:14 -07:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
// Identity returns the FileIdentity for the file.
|
|
|
|
Identity() FileIdentity
|
2019-10-29 16:13:19 -06:00
|
|
|
|
2019-11-20 14:38:43 -07: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.
|
|
|
|
Read(ctx context.Context) ([]byte, string, 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-05-06 20:54:50 -06:00
|
|
|
// Go is a normal go source file.
|
2019-11-20 14:38:43 -07:00
|
|
|
Go = FileKind(iota)
|
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
|
2020-05-06 20:54:50 -06:00
|
|
|
// UnknownKind is a file type we don't know about.
|
2019-11-20 14:38:43 -07:00
|
|
|
UnknownKind
|
|
|
|
)
|
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")
|