internal/lsp: handle common nil pointer exceptions

We may encounter these nil pointer if go/packages cannot find the
package of the given file, for example, when the user creates a new file
or a new package.

Change-Id: I16993017243a56332dd9f7e0aaf3c1d57f20fc3a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/167462
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Rebecca Stambler 2019-03-13 19:31:41 -04:00
parent 48d47c461c
commit a10017ccd2
3 changed files with 27 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package lsp
import (
"context"
"fmt"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
@ -16,8 +17,12 @@ func formatRange(ctx context.Context, v source.View, s span.Span) ([]protocol.Te
}
rng := s.Range(m.Converter)
if rng.Start == rng.End {
// if we have a single point, then assume the rest of the file
rng.End = f.GetToken(ctx).Pos(f.GetToken(ctx).Size())
// If we have a single point, assume we want the whole file.
tok := f.GetToken(ctx)
if tok == nil {
return nil, fmt.Errorf("no file information for %s", f.URI())
}
rng.End = tok.Pos(tok.Size())
}
edits, err := source.Format(ctx, f, rng)
if err != nil {
@ -45,6 +50,10 @@ func newColumnMap(ctx context.Context, v source.View, uri span.URI) (source.File
if err != nil {
return nil, nil, err
}
m := protocol.NewColumnMapper(f.URI(), f.GetFileSet(ctx), f.GetToken(ctx), f.GetContent(ctx))
tok := f.GetToken(ctx)
if tok == nil {
return nil, nil, fmt.Errorf("no file information for %v", f.URI())
}
m := protocol.NewColumnMapper(f.URI(), f.GetFileSet(ctx), tok, f.GetContent(ctx))
return f, m, nil
}

View File

@ -6,6 +6,7 @@ package lsp
import (
"context"
"fmt"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
@ -19,8 +20,12 @@ func organizeImports(ctx context.Context, v source.View, s span.Span) ([]protoco
}
rng := s.Range(m.Converter)
if rng.Start == rng.End {
// if we have a single point, then assume the rest of the file
rng.End = f.GetToken(ctx).Pos(f.GetToken(ctx).Size())
// If we have a single point, assume we want the whole file.
tok := f.GetToken(ctx)
if tok == nil {
return nil, fmt.Errorf("no file information for %s", f.URI())
}
rng.End = tok.Pos(tok.Size())
}
edits, err := source.Imports(ctx, f, rng)
if err != nil {

View File

@ -56,6 +56,9 @@ func Diagnostics(ctx context.Context, v View, uri span.URI) (map[span.URI][]Diag
return nil, err
}
pkg := f.GetPackage(ctx)
if pkg == nil {
return nil, fmt.Errorf("no package found for %v", f.URI())
}
// Prepare the reports we will send for this package.
reports := make(map[span.URI][]Diagnostic)
for _, filename := range pkg.GetFilenames() {
@ -83,8 +86,12 @@ func Diagnostics(ctx context.Context, v View, uri span.URI) (map[span.URI][]Diag
if spn.IsPoint() && diag.Kind == packages.TypeError {
// Don't set a range if it's anything other than a type error.
if diagFile, err := v.GetFile(ctx, spn.URI); err == nil {
tok := diagFile.GetToken(ctx)
if tok == nil {
continue // ignore errors
}
content := diagFile.GetContent(ctx)
c := span.NewTokenConverter(diagFile.GetFileSet(ctx), diagFile.GetToken(ctx))
c := span.NewTokenConverter(diagFile.GetFileSet(ctx), tok)
s := spn.CleanOffset(c)
if end := bytes.IndexAny(content[s.Start.Offset:], " \n,():;[]"); end > 0 {
spn.End = s.Start