internal/lsp: remove the source.Error type
source.Error and source.Diagnostic are almost identical types, used
arbitrarily in different parts of the code. This CL is the first step in
cleaning up that redundancy: it deletes the source.Error type.
To do that, I added the fields from source.Error to source.Diagnostic,
and made absolutely no other semantic code changes -- I just renamed
things that were named Error to Diagnostic. With only aesthetic concerns
in play, I hope this CL will be easy to review. The next CL will clean
up all the stupid-looking code that converts a Diagnostic to a
Diagnostic, etc.
Change-Id: I1298cc8144c686b8a37fc2cc106930105e511353
Reviewed-on: https://go-review.googlesource.com/c/tools/+/288214
Trust: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/cache/analysis.go b/internal/lsp/cache/analysis.go
index c51094d..0c3f87b 100644
--- a/internal/lsp/cache/analysis.go
+++ b/internal/lsp/cache/analysis.go
@@ -23,7 +23,7 @@
errors "golang.org/x/xerrors"
)
-func (s *snapshot) Analyze(ctx context.Context, id string, analyzers ...*analysis.Analyzer) ([]*source.Error, error) {
+func (s *snapshot) Analyze(ctx context.Context, id string, analyzers ...*analysis.Analyzer) ([]*source.Diagnostic, error) {
var roots []*actionHandle
for _, a := range analyzers {
@@ -39,7 +39,7 @@
return nil, ctx.Err()
}
- var results []*source.Error
+ var results []*source.Diagnostic
for _, ah := range roots {
diagnostics, _, err := ah.analyze(ctx, s)
if err != nil {
@@ -64,7 +64,7 @@
}
type actionData struct {
- diagnostics []*source.Error
+ diagnostics []*source.Diagnostic
result interface{}
objectFacts map[objectFactKey]analysis.Fact
packageFacts map[packageFactKey]analysis.Fact
@@ -150,7 +150,7 @@
return act, nil
}
-func (act *actionHandle) analyze(ctx context.Context, snapshot *snapshot) ([]*source.Error, interface{}, error) {
+func (act *actionHandle) analyze(ctx context.Context, snapshot *snapshot) ([]*source.Diagnostic, interface{}, error) {
d, err := act.handle.Get(ctx, snapshot.generation, snapshot)
if err != nil {
return nil, nil, err
@@ -323,7 +323,7 @@
analysisinternal.SetTypeErrors(pass, pkg.typeErrors)
if pkg.IsIllTyped() {
- data.err = errors.Errorf("analysis skipped due to errors in package: %v", pkg.GetErrors())
+ data.err = errors.Errorf("analysis skipped due to errors in package: %v", pkg.GetDiagnostics())
return data
}
data.result, data.err = pass.Analyzer.Run(pass)
@@ -347,7 +347,7 @@
}
for _, diag := range diagnostics {
- srcErr, err := sourceError(ctx, snapshot, pkg, diag)
+ srcErr, err := sourceDiagnostic(ctx, snapshot, pkg, diag)
if err != nil {
event.Error(ctx, "unable to compute analysis error position", err, tag.Category.Of(diag.Category), tag.Package.Of(pkg.ID()))
continue
diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go
index 1760d38..0842825 100644
--- a/internal/lsp/cache/check.go
+++ b/internal/lsp/cache/check.go
@@ -377,12 +377,12 @@
// Try to attach errors messages to the file as much as possible.
var found bool
for _, e := range rawErrors {
- srcErr, err := sourceError(ctx, snapshot, pkg, e)
+ srcErr, err := sourceDiagnostic(ctx, snapshot, pkg, e)
if err != nil {
continue
}
found = true
- pkg.errors = append(pkg.errors, srcErr)
+ pkg.diagnostics = append(pkg.diagnostics, srcErr)
}
if found {
return pkg, nil
@@ -439,12 +439,12 @@
if mode == source.ParseFull {
expandErrors(rawErrors)
for _, e := range rawErrors {
- srcErr, err := sourceError(ctx, snapshot, pkg, e)
+ srcErr, err := sourceDiagnostic(ctx, snapshot, pkg, e)
if err != nil {
event.Error(ctx, "unable to compute error positions", err, tag.Package.Of(pkg.ID()))
continue
}
- pkg.errors = append(pkg.errors, srcErr)
+ pkg.diagnostics = append(pkg.diagnostics, srcErr)
if err, ok := e.(extendedError); ok {
pkg.typeErrors = append(pkg.typeErrors, err.primary)
}
diff --git a/internal/lsp/cache/errors.go b/internal/lsp/cache/errors.go
index 10f7d1f..c14d4c2 100644
--- a/internal/lsp/cache/errors.go
+++ b/internal/lsp/cache/errors.go
@@ -26,7 +26,7 @@
errors "golang.org/x/xerrors"
)
-func sourceError(ctx context.Context, snapshot *snapshot, pkg *pkg, e interface{}) (*source.Error, error) {
+func sourceDiagnostic(ctx context.Context, snapshot *snapshot, pkg *pkg, e interface{}) (*source.Diagnostic, error) {
fset := snapshot.view.session.cache.fset
var (
spn span.Span
@@ -50,7 +50,7 @@
// We may not have been able to parse a valid span.
if _, err := spanToRange(snapshot, pkg, spn); err != nil {
- return &source.Error{
+ return &source.Diagnostic{
URI: spn.URI(),
Message: msg,
Kind: kind,
@@ -145,7 +145,7 @@
if err != nil {
return nil, err
}
- se := &source.Error{
+ sd := &source.Diagnostic{
URI: spn.URI(),
Range: rng,
Message: msg,
@@ -155,10 +155,10 @@
Related: related,
}
if code != 0 {
- se.Code = code.String()
- se.CodeHref = typesCodeHref(snapshot, code)
+ sd.Code = code.String()
+ sd.CodeHref = typesCodeHref(snapshot, code)
}
- return se, nil
+ return sd, nil
}
func typesCodeHref(snapshot *snapshot, code typesinternal.ErrorCode) string {
diff --git a/internal/lsp/cache/load.go b/internal/lsp/cache/load.go
index 41bc79d..0701614 100644
--- a/internal/lsp/cache/load.go
+++ b/internal/lsp/cache/load.go
@@ -207,7 +207,7 @@
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.`
return &source.CriticalError{
MainError: errors.Errorf(msg),
- ErrorList: s.applyCriticalErrorToFiles(ctx, msg, openFiles),
+ DiagList: s.applyCriticalErrorToFiles(ctx, msg, openFiles),
}
}
@@ -231,29 +231,29 @@
// Add a diagnostic to each file in a nested module to mark it as
// "orphaned". Don't show a general diagnostic in the progress bar,
// because the user may still want to edit a file in a nested module.
- var srcErrs []*source.Error
+ var srcDiags []*source.Diagnostic
for modDir, uris := range nestedModules {
msg := fmt.Sprintf(`This file is in %s, which is a nested module in the %s module.
gopls currently requires one module per workspace folder.
Please open %s as a separate workspace folder.
You can learn more here: https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.
`, modDir, filepath.Dir(rootModURI.Filename()), modDir)
- srcErrs = append(srcErrs, s.applyCriticalErrorToFiles(ctx, msg, uris)...)
+ srcDiags = append(srcDiags, s.applyCriticalErrorToFiles(ctx, msg, uris)...)
}
- if len(srcErrs) != 0 {
+ if len(srcDiags) != 0 {
return &source.CriticalError{
MainError: errors.Errorf(`You are working in a nested module.
Please open it as a separate workspace folder. Learn more:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.`),
- ErrorList: srcErrs,
+ DiagList: srcDiags,
}
}
}
return nil
}
-func (s *snapshot) applyCriticalErrorToFiles(ctx context.Context, msg string, files []source.VersionedFileHandle) []*source.Error {
- var srcErrs []*source.Error
+func (s *snapshot) applyCriticalErrorToFiles(ctx context.Context, msg string, files []source.VersionedFileHandle) []*source.Diagnostic {
+ var srcDiags []*source.Diagnostic
for _, fh := range files {
// Place the diagnostics on the package or module declarations.
var rng protocol.Range
@@ -272,14 +272,14 @@
}
}
}
- srcErrs = append(srcErrs, &source.Error{
+ srcDiags = append(srcDiags, &source.Diagnostic{
URI: fh.URI(),
Range: rng,
Kind: source.ListError,
Message: msg,
})
}
- return srcErrs
+ return srcDiags
}
type workspaceDirKey string
diff --git a/internal/lsp/cache/mod.go b/internal/lsp/cache/mod.go
index 7949918..27d4496 100644
--- a/internal/lsp/cache/mod.go
+++ b/internal/lsp/cache/mod.go
@@ -67,10 +67,10 @@
file, err := modfile.Parse(modFH.URI().Filename(), contents, nil)
// Attempt to convert the error to a standardized parse error.
- var parseErrors []*source.Error
+ var parseErrors []*source.Diagnostic
if err != nil {
if parseErr := extractErrorWithPosition(ctx, err.Error(), s); parseErr != nil {
- parseErrors = []*source.Error{parseErr}
+ parseErrors = []*source.Diagnostic{parseErr}
}
}
return &parseModData{
@@ -218,8 +218,8 @@
// extractGoCommandError tries to parse errors that come from the go command
// and shape them into go.mod diagnostics.
-func (s *snapshot) extractGoCommandErrors(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle, goCmdError string) []*source.Error {
- var srcErrs []*source.Error
+func (s *snapshot) extractGoCommandErrors(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle, goCmdError string) []*source.Diagnostic {
+ var srcErrs []*source.Diagnostic
if srcErr := s.parseModError(ctx, fh, goCmdError); srcErr != nil {
srcErrs = append(srcErrs, srcErr)
}
@@ -245,7 +245,7 @@
// We search for module@version, starting from the end to find the most
// relevant module, e.g. random.org@v1.2.3 above. Then we associate the error
// with a directive that references any of the modules mentioned.
-func (s *snapshot) matchErrorToModule(ctx context.Context, fh source.FileHandle, goCmdError string) *source.Error {
+func (s *snapshot) matchErrorToModule(ctx context.Context, fh source.FileHandle, goCmdError string) *source.Diagnostic {
pm, err := s.ParseMod(ctx, fh)
if err != nil {
return nil
@@ -314,7 +314,7 @@
if disabledByGOPROXY {
msg = fmt.Sprintf("%v@%v has not been downloaded", innermost.Path, innermost.Version)
}
- return &source.Error{
+ return &source.Diagnostic{
Message: msg,
Kind: source.ListError,
Range: rng,
@@ -329,7 +329,7 @@
}},
}
}
- return &source.Error{
+ return &source.Diagnostic{
Message: goCmdError,
Range: rng,
URI: fh.URI(),
@@ -345,7 +345,7 @@
// information for the given unstructured error. If a file handle is provided,
// the error position will be on that file. This is useful for parse errors,
// where we already know the file with the error.
-func extractErrorWithPosition(ctx context.Context, goCmdError string, src source.FileSource) *source.Error {
+func extractErrorWithPosition(ctx context.Context, goCmdError string, src source.FileSource) *source.Diagnostic {
matches := errorPositionRe.FindStringSubmatch(strings.TrimSpace(goCmdError))
if len(matches) == 0 {
return nil
@@ -381,7 +381,7 @@
if fh != nil {
category = SyntaxError
}
- return &source.Error{
+ return &source.Diagnostic{
Category: category,
Message: msg,
Range: rng,
diff --git a/internal/lsp/cache/mod_tidy.go b/internal/lsp/cache/mod_tidy.go
index 1650efa..85aa7cc 100644
--- a/internal/lsp/cache/mod_tidy.go
+++ b/internal/lsp/cache/mod_tidy.go
@@ -73,7 +73,7 @@
}
if criticalErr := s.GetCriticalError(ctx); criticalErr != nil {
return &source.TidiedModule{
- Errors: criticalErr.ErrorList,
+ Diagnostics: criticalErr.DiagList,
}, nil
}
workspacePkgs, err := s.WorkspacePackages(ctx)
@@ -131,13 +131,13 @@
}
// Compare the original and tidied go.mod files to compute errors and
// suggested fixes.
- errors, err := modTidyErrors(ctx, snapshot, pm, ideal, workspacePkgs)
+ diagnostics, err := modTidyDiagnostics(ctx, snapshot, pm, ideal, workspacePkgs)
if err != nil {
return &modTidyData{err: err}
}
return &modTidyData{
tidied: &source.TidiedModule{
- Errors: errors,
+ Diagnostics: diagnostics,
TidiedContent: tempContents,
},
}
@@ -151,7 +151,7 @@
return mth.tidy(ctx, s)
}
-func (s *snapshot) parseModError(ctx context.Context, fh source.FileHandle, errText string) *source.Error {
+func (s *snapshot) parseModError(ctx context.Context, fh source.FileHandle, errText string) *source.Diagnostic {
// Match on common error messages. This is really hacky, but I'm not sure
// of any better way. This can be removed when golang/go#39164 is resolved.
isInconsistentVendor := strings.Contains(errText, "inconsistent vendoring")
@@ -179,7 +179,7 @@
switch {
case isInconsistentVendor:
- return &source.Error{
+ return &source.Diagnostic{
URI: fh.URI(),
Range: rng,
Kind: source.ListError,
@@ -196,7 +196,7 @@
}
case isGoSumUpdates:
- return &source.Error{
+ return &source.Diagnostic{
URI: fh.URI(),
Range: rng,
Kind: source.ListError,
@@ -242,10 +242,10 @@
return hashContents([]byte(hashed)), nil
}
-// modTidyErrors computes the differences between the original and tidied
+// modTidyDiagnostics computes the differences between the original and tidied
// go.mod files to produce diagnostic and suggested fixes. Some diagnostics
// may appear on the Go files that import packages from missing modules.
-func modTidyErrors(ctx context.Context, snapshot source.Snapshot, pm *source.ParsedModule, ideal *modfile.File, workspacePkgs []source.Package) (errors []*source.Error, err error) {
+func modTidyDiagnostics(ctx context.Context, snapshot source.Snapshot, pm *source.ParsedModule, ideal *modfile.File, workspacePkgs []source.Package) (diagnostics []*source.Diagnostic, err error) {
// First, determine which modules are unused and which are missing from the
// original go.mod file.
var (
@@ -269,11 +269,11 @@
for _, req := range wrongDirectness {
// Handle dependencies that are incorrectly labeled indirect and
// vice versa.
- srcErr, err := directnessError(pm.Mapper, req, snapshot.View().Options().ComputeEdits)
+ srcDiag, err := directnessDiagnostic(pm.Mapper, req, snapshot.View().Options().ComputeEdits)
if err != nil {
return nil, err
}
- errors = append(errors, srcErr)
+ diagnostics = append(diagnostics, srcDiag)
}
// Next, compute any diagnostics for modules that are missing from the
// go.mod file. The fixes will be for the go.mod file, but the
@@ -281,12 +281,12 @@
// statements in the Go files in which the dependencies are used.
missingModuleFixes := map[*modfile.Require][]source.SuggestedFix{}
for _, req := range missing {
- srcErr, err := missingModuleError(snapshot, pm, req)
+ srcDiag, err := missingModuleDiagnostic(snapshot, pm, req)
if err != nil {
return nil, err
}
- missingModuleFixes[req] = srcErr.SuggestedFixes
- errors = append(errors, srcErr)
+ missingModuleFixes[req] = srcDiag.SuggestedFixes
+ diagnostics = append(diagnostics, srcDiag)
}
// Add diagnostics for missing modules anywhere they are imported in the
// workspace.
@@ -361,33 +361,33 @@
if err != nil {
return nil, err
}
- errors = append(errors, srcErr)
+ diagnostics = append(diagnostics, srcErr)
}
}
}
// Finally, add errors for any unused dependencies.
- onlyError := len(errors) == 0 && len(unused) == 1
+ onlyDiagnostic := len(diagnostics) == 0 && len(unused) == 1
for _, req := range unused {
- srcErr, err := unusedError(pm.Mapper, req, onlyError, snapshot.View().Options().ComputeEdits)
+ srcErr, err := unusedDiagnostic(pm.Mapper, req, onlyDiagnostic, snapshot.View().Options().ComputeEdits)
if err != nil {
return nil, err
}
- errors = append(errors, srcErr)
+ diagnostics = append(diagnostics, srcErr)
}
- return errors, nil
+ return diagnostics, nil
}
-// unusedError returns a source.Error for an unused require.
-func unusedError(m *protocol.ColumnMapper, req *modfile.Require, onlyError bool, computeEdits diff.ComputeEdits) (*source.Error, error) {
+// unusedDiagnostic returns a source.Diagnostic for an unused require.
+func unusedDiagnostic(m *protocol.ColumnMapper, req *modfile.Require, onlyDiagnostic bool, computeEdits diff.ComputeEdits) (*source.Diagnostic, error) {
rng, err := rangeFromPositions(m, req.Syntax.Start, req.Syntax.End)
if err != nil {
return nil, err
}
- args, err := source.MarshalArgs(m.URI, onlyError, req.Mod.Path)
+ args, err := source.MarshalArgs(m.URI, onlyDiagnostic, req.Mod.Path)
if err != nil {
return nil, err
}
- return &source.Error{
+ return &source.Diagnostic{
Category: source.GoModTidy,
Message: fmt.Sprintf("%s is not used in this module", req.Mod.Path),
Range: rng,
@@ -403,9 +403,9 @@
}, nil
}
-// directnessError extracts errors when a dependency is labeled indirect when
+// directnessDiagnostic extracts errors when a dependency is labeled indirect when
// it should be direct and vice versa.
-func directnessError(m *protocol.ColumnMapper, req *modfile.Require, computeEdits diff.ComputeEdits) (*source.Error, error) {
+func directnessDiagnostic(m *protocol.ColumnMapper, req *modfile.Require, computeEdits diff.ComputeEdits) (*source.Diagnostic, error) {
rng, err := rangeFromPositions(m, req.Syntax.Start, req.Syntax.End)
if err != nil {
return nil, err
@@ -430,7 +430,7 @@
if err != nil {
return nil, err
}
- return &source.Error{
+ return &source.Diagnostic{
Message: fmt.Sprintf("%s should be %s", req.Mod.Path, direction),
Range: rng,
URI: m.URI,
@@ -444,7 +444,7 @@
}, nil
}
-func missingModuleError(snapshot source.Snapshot, pm *source.ParsedModule, req *modfile.Require) (*source.Error, error) {
+func missingModuleDiagnostic(snapshot source.Snapshot, pm *source.ParsedModule, req *modfile.Require) (*source.Diagnostic, error) {
var rng protocol.Range
// Default to the start of the file if there is no module declaration.
if pm.File != nil && pm.File.Module != nil && pm.File.Module.Syntax != nil {
@@ -459,7 +459,7 @@
if err != nil {
return nil, err
}
- return &source.Error{
+ return &source.Diagnostic{
URI: pm.Mapper.URI,
Range: rng,
Message: fmt.Sprintf("%s is not in your go.mod file", req.Mod.Path),
@@ -514,7 +514,7 @@
// missingModuleForImport creates an error for a given import path that comes
// from a missing module.
-func missingModuleForImport(snapshot source.Snapshot, m *protocol.ColumnMapper, imp *ast.ImportSpec, req *modfile.Require, fixes []source.SuggestedFix) (*source.Error, error) {
+func missingModuleForImport(snapshot source.Snapshot, m *protocol.ColumnMapper, imp *ast.ImportSpec, req *modfile.Require, fixes []source.SuggestedFix) (*source.Diagnostic, error) {
if req.Syntax == nil {
return nil, fmt.Errorf("no syntax for %v", req)
}
@@ -526,7 +526,7 @@
if err != nil {
return nil, err
}
- return &source.Error{
+ return &source.Diagnostic{
Category: source.GoModTidy,
URI: m.URI,
Range: rng,
diff --git a/internal/lsp/cache/pkg.go b/internal/lsp/cache/pkg.go
index 086c866..66f3e1a 100644
--- a/internal/lsp/cache/pkg.go
+++ b/internal/lsp/cache/pkg.go
@@ -20,7 +20,7 @@
mode source.ParseMode
goFiles []*source.ParsedGoFile
compiledGoFiles []*source.ParsedGoFile
- errors []*source.Error
+ diagnostics []*source.Diagnostic
imports map[packagePath]*pkg
version *module.Version
typeErrors []types.Error
@@ -84,8 +84,8 @@
return syntax
}
-func (p *pkg) GetErrors() []*source.Error {
- return p.errors
+func (p *pkg) GetDiagnostics() []*source.Diagnostic {
+ return p.diagnostics
}
func (p *pkg) GetTypes() *types.Package {
diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go
index 6a3995a..d8b2462 100644
--- a/internal/lsp/cache/snapshot.go
+++ b/internal/lsp/cache/snapshot.go
@@ -1037,7 +1037,7 @@
if err != nil {
continue
}
- criticalErr.ErrorList = append(criticalErr.ErrorList, s.extractGoCommandErrors(ctx, s, fh, loadErr.Error())...)
+ criticalErr.DiagList = append(criticalErr.DiagList, s.extractGoCommandErrors(ctx, s, fh, loadErr.Error())...)
}
return criticalErr
}
diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go
index 55c52eb..47d5758 100644
--- a/internal/lsp/cache/view.go
+++ b/internal/lsp/cache/view.go
@@ -539,9 +539,9 @@
// If we have multiple modules, we need to load them by paths.
var scopes []interface{}
- var modErrors []*source.Error
+ var modDiagnostics []*source.Diagnostic
addError := func(uri span.URI, err error) {
- modErrors = append(modErrors, &source.Error{
+ modDiagnostics = append(modDiagnostics, &source.Diagnostic{
URI: uri,
Category: "compiler",
Kind: source.ListError,
@@ -575,10 +575,10 @@
}
if err != nil {
event.Error(ctx, "initial workspace load failed", err)
- if modErrors != nil {
+ if modDiagnostics != nil {
s.initializedErr = &source.CriticalError{
- MainError: errors.Errorf("errors loading modules: %v: %w", err, modErrors),
- ErrorList: modErrors,
+ MainError: errors.Errorf("errors loading modules: %v: %w", err, modDiagnostics),
+ DiagList: modDiagnostics,
}
} else {
s.initializedErr = &source.CriticalError{
diff --git a/internal/lsp/code_action.go b/internal/lsp/code_action.go
index 1f4a1a1..a2cdbfe 100644
--- a/internal/lsp/code_action.go
+++ b/internal/lsp/code_action.go
@@ -262,7 +262,7 @@
sourceFixAllEdits []protocol.TextDocumentEdit
)
for _, diag := range diagnostics {
- srcErr, analyzer, ok := findSourceError(ctx, snapshot, pkg.ID(), diag)
+ srcErr, analyzer, ok := findDiagnostic(ctx, snapshot, pkg.ID(), diag)
if !ok {
continue
}
@@ -300,7 +300,7 @@
return codeActions, sourceFixAllEdits, nil
}
-func findSourceError(ctx context.Context, snapshot source.Snapshot, pkgID string, diag protocol.Diagnostic) (*source.Error, source.Analyzer, bool) {
+func findDiagnostic(ctx context.Context, snapshot source.Snapshot, pkgID string, diag protocol.Diagnostic) (*source.Diagnostic, source.Analyzer, bool) {
analyzer := diagnosticToAnalyzer(snapshot, diag.Source, diag.Message)
if analyzer == nil {
return nil, source.Analyzer{}, false
@@ -428,31 +428,31 @@
return codeActions, nil
}
-func diagnosticToCommandCodeAction(ctx context.Context, snapshot source.Snapshot, e *source.Error, d *protocol.Diagnostic, kind protocol.CodeActionKind) (*protocol.CodeAction, error) {
+func diagnosticToCommandCodeAction(ctx context.Context, snapshot source.Snapshot, sd *source.Diagnostic, pd *protocol.Diagnostic, kind protocol.CodeActionKind) (*protocol.CodeAction, error) {
// The fix depends on the category of the analyzer. The diagnostic may be
// nil, so use the error's category.
- analyzer := diagnosticToAnalyzer(snapshot, e.Category, e.Message)
+ analyzer := diagnosticToAnalyzer(snapshot, sd.Category, sd.Message)
if analyzer == nil {
- return nil, fmt.Errorf("no convenience analyzer for category %s", e.Category)
+ return nil, fmt.Errorf("no convenience analyzer for category %s", sd.Category)
}
if analyzer.Command == nil {
return nil, fmt.Errorf("no command for convenience analyzer %s", analyzer.Analyzer.Name)
}
- jsonArgs, err := source.MarshalArgs(e.URI, e.Range)
+ jsonArgs, err := source.MarshalArgs(sd.URI, sd.Range)
if err != nil {
return nil, err
}
var diagnostics []protocol.Diagnostic
- if d != nil {
- diagnostics = append(diagnostics, *d)
+ if pd != nil {
+ diagnostics = append(diagnostics, *pd)
}
return &protocol.CodeAction{
- Title: e.Message,
+ Title: sd.Message,
Kind: kind,
Diagnostics: diagnostics,
Command: &protocol.Command{
Command: analyzer.Command.ID(),
- Title: e.Message,
+ Title: sd.Message,
Arguments: jsonArgs,
},
}, nil
@@ -520,7 +520,7 @@
return nil, err
}
}
- errors, err := mod.ErrorsForMod(ctx, snapshot, modFH)
+ errors, err := mod.DiagnosticsForMod(ctx, snapshot, modFH)
if err != nil {
return nil, err
}
@@ -564,8 +564,8 @@
return quickFixes, nil
}
-func sameDiagnostic(d protocol.Diagnostic, e *source.Error) bool {
- return d.Message == e.Message && protocol.CompareRange(d.Range, e.Range) == 0 && d.Source == e.Category
+func sameDiagnostic(pd protocol.Diagnostic, sd *source.Diagnostic) bool {
+ return pd.Message == sd.Message && protocol.CompareRange(pd.Range, sd.Range) == 0 && pd.Source == sd.Category
}
func goTest(ctx context.Context, snapshot source.Snapshot, uri span.URI, rng protocol.Range) ([]protocol.CodeAction, error) {
diff --git a/internal/lsp/command.go b/internal/lsp/command.go
index 997a20d..b1d9264 100644
--- a/internal/lsp/command.go
+++ b/internal/lsp/command.go
@@ -252,8 +252,8 @@
case source.CommandRemoveDependency:
var uri protocol.DocumentURI
var modulePath string
- var onlyError bool
- if err := source.UnmarshalArgs(args, &uri, &onlyError, &modulePath); err != nil {
+ var onlyDiagnostic bool
+ if err := source.UnmarshalArgs(args, &uri, &onlyDiagnostic, &modulePath); err != nil {
return err
}
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, uri, source.UnknownKind)
@@ -266,7 +266,7 @@
// must make textual edits.
// TODO(rstambler): In Go 1.17+, we will be able to use the go command
// without checking if the module is tidy.
- if onlyError {
+ if onlyDiagnostic {
if err := s.runGoGetModule(ctx, snapshot, uri.SpanURI(), false, []string{modulePath + "@none"}); err != nil {
return err
}
diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go
index 6f838f0..12a3378 100644
--- a/internal/lsp/diagnostics.go
+++ b/internal/lsp/diagnostics.go
@@ -351,7 +351,7 @@
var errMsg string
if err != nil {
event.Error(ctx, "errors loading workspace", err.MainError, tag.Snapshot.Of(snapshot.ID()), tag.Directory.Of(snapshot.View().Folder()))
- s.storeErrorDiagnostics(ctx, snapshot, modSource, err.ErrorList)
+ s.storeErrorDiagnostics(ctx, snapshot, modSource, err.DiagList)
errMsg = strings.Replace(err.MainError.Error(), "\n", " ", -1)
}
@@ -409,18 +409,18 @@
}
}
-func (s *Server) storeErrorDiagnostics(ctx context.Context, snapshot source.Snapshot, dsource diagnosticSource, errors []*source.Error) {
- for _, e := range errors {
+func (s *Server) storeErrorDiagnostics(ctx context.Context, snapshot source.Snapshot, dsource diagnosticSource, diagnostics []*source.Diagnostic) {
+ for _, d := range diagnostics {
diagnostic := &source.Diagnostic{
- Range: e.Range,
- Message: e.Message,
- Related: e.Related,
+ Range: d.Range,
+ Message: d.Message,
+ Related: d.Related,
Severity: protocol.SeverityError,
- Source: e.Category,
- Code: e.Code,
- CodeHref: e.CodeHref,
+ Source: d.Category,
+ Code: d.Code,
+ CodeHref: d.CodeHref,
}
- s.storeDiagnostics(snapshot, e.URI, dsource, []*source.Diagnostic{diagnostic})
+ s.storeDiagnostics(snapshot, d.URI, dsource, []*source.Diagnostic{diagnostic})
}
}
diff --git a/internal/lsp/mod/diagnostics.go b/internal/lsp/mod/diagnostics.go
index b5da560..9716aa7 100644
--- a/internal/lsp/mod/diagnostics.go
+++ b/internal/lsp/mod/diagnostics.go
@@ -27,7 +27,7 @@
return nil, err
}
reports[fh.VersionedFileIdentity()] = []*source.Diagnostic{}
- errors, err := ErrorsForMod(ctx, snapshot, fh)
+ errors, err := DiagnosticsForMod(ctx, snapshot, fh)
if err != nil {
return nil, err
}
@@ -55,7 +55,7 @@
return reports, nil
}
-func ErrorsForMod(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]*source.Error, error) {
+func DiagnosticsForMod(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]*source.Diagnostic, error) {
pm, err := snapshot.ParseMod(ctx, fh)
if err != nil {
if pm == nil || len(pm.ParseErrors) == 0 {
@@ -64,7 +64,7 @@
return pm.ParseErrors, nil
}
- var errors []*source.Error
+ var diagnostics []*source.Diagnostic
// Add upgrade quick fixes for individual modules if we know about them.
upgrades := snapshot.View().ModuleUpgrades()
@@ -82,7 +82,7 @@
if err != nil {
return nil, err
}
- errors = append(errors, &source.Error{
+ diagnostics = append(diagnostics, &source.Diagnostic{
URI: fh.URI(),
Range: rng,
Kind: source.UpgradeNotification,
@@ -101,11 +101,11 @@
tidied, err := snapshot.ModTidy(ctx, pm)
if source.IsNonFatalGoModError(err) {
- return errors, nil
+ return diagnostics, nil
}
if err != nil {
return nil, err
}
- errors = append(errors, tidied.Errors...)
- return errors, nil
+ diagnostics = append(diagnostics, tidied.Diagnostics...)
+ return diagnostics, nil
}
diff --git a/internal/lsp/source/diagnostics.go b/internal/lsp/source/diagnostics.go
index 1dd937a..b426a04 100644
--- a/internal/lsp/source/diagnostics.go
+++ b/internal/lsp/source/diagnostics.go
@@ -14,18 +14,6 @@
"golang.org/x/tools/internal/span"
)
-type Diagnostic struct {
- Range protocol.Range
- Message string
- Source string
- Code string
- CodeHref string
- Severity protocol.DiagnosticSeverity
- Tags []protocol.DiagnosticTag
-
- Related []RelatedInformation
-}
-
type SuggestedFix struct {
Title string
Edits map[span.URI][]protocol.TextEdit
@@ -48,7 +36,7 @@
}
// Prepare any additional reports for the errors in this package.
- for _, e := range pkg.GetErrors() {
+ for _, e := range pkg.GetDiagnostics() {
// We only need to handle lower-level errors.
if e.Kind != ListError {
continue
@@ -173,12 +161,12 @@
}
func typeCheckDiagnostics(ctx context.Context, snapshot Snapshot, pkg Package) TypeCheckDiagnostics {
- ctx, done := event.Start(ctx, "source.diagnostics", tag.Package.Of(pkg.ID()))
+ ctx, done := event.Start(ctx, "source.typeCheckDiagnostics", tag.Package.Of(pkg.ID()))
_ = ctx // circumvent SA4006
defer done()
diagSets := make(map[span.URI]*diagnosticSet)
- for _, e := range pkg.GetErrors() {
+ for _, e := range pkg.GetDiagnostics() {
diag := &Diagnostic{
Message: e.Message,
Range: e.Range,
diff --git a/internal/lsp/source/rename_check.go b/internal/lsp/source/rename_check.go
index 7986ea1..0f1bf92 100644
--- a/internal/lsp/source/rename_check.go
+++ b/internal/lsp/source/rename_check.go
@@ -812,7 +812,7 @@
// type-checker.
//
// Only proceed if all packages have no errors.
- if errs := pkg.GetErrors(); len(errs) > 0 {
+ if diags := pkg.GetDiagnostics(); len(diags) > 0 {
r.errorf(token.NoPos, // we don't have a position for this error.
"renaming %q to %q not possible because %q has errors",
r.from, r.to, pkg.PkgPath())
diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go
index 4eb3a59..bd4875a 100644
--- a/internal/lsp/source/view.go
+++ b/internal/lsp/source/view.go
@@ -84,7 +84,7 @@
PosToDecl(ctx context.Context, pgf *ParsedGoFile) (map[token.Pos]ast.Decl, error)
// Analyze runs the analyses for the given package at this snapshot.
- Analyze(ctx context.Context, pkgID string, analyzers ...*analysis.Analyzer) ([]*Error, error)
+ Analyze(ctx context.Context, pkgID string, analyzers ...*analysis.Analyzer) ([]*Diagnostic, error)
// RunGoCommandPiped runs the given `go` command, writing its output
// to stdout and stderr. Verb, Args, and WorkingDir must be specified.
@@ -266,13 +266,13 @@
URI span.URI
File *modfile.File
Mapper *protocol.ColumnMapper
- ParseErrors []*Error
+ ParseErrors []*Diagnostic
}
// A TidiedModule contains the results of running `go mod tidy` on a module.
type TidiedModule struct {
// Diagnostics representing changes made by `go mod tidy`.
- Errors []*Error
+ Diagnostics []*Diagnostic
// The bytes of the go.mod file after it was tidied.
TidiedContent []byte
}
@@ -543,7 +543,7 @@
CompiledGoFiles() []*ParsedGoFile
File(uri span.URI) (*ParsedGoFile, error)
GetSyntax() []*ast.File
- GetErrors() []*Error
+ GetDiagnostics() []*Diagnostic
GetTypes() *types.Package
GetTypesInfo() *types.Info
GetTypesSizes() types.Sizes
@@ -558,24 +558,28 @@
type CriticalError struct {
// MainError is the primary error. Must be non-nil.
MainError error
- // ErrorList contains any supplemental (structured) errors.
- ErrorList []*Error
+ // DiagList contains any supplemental (structured) diagnostics.
+ DiagList []*Diagnostic
}
-// An Error corresponds to an LSP Diagnostic.
+// An Diagnostic corresponds to an LSP Diagnostic.
// https://microsoft.github.io/language-server-protocol/specification#diagnostic
-type Error struct {
+type Diagnostic struct {
URI span.URI
Range protocol.Range
- Kind ErrorKind
- Message string
- Category string // only used by analysis errors so far
-
- Related []RelatedInformation
-
+ Severity protocol.DiagnosticSeverity
Code string
CodeHref string
+ Source string
+ Kind ErrorKind
+ Category string // only used by analysis errors so far
+
+ Message string
+
+ Tags []protocol.DiagnosticTag
+ Related []RelatedInformation
+
// SuggestedFixes is used to generate quick fixes for a CodeAction request.
// It isn't part of the Diagnostic type.
SuggestedFixes []SuggestedFix