diff --git a/internal/lsp/format.go b/internal/lsp/format.go index fc239752..5ce6a346 100644 --- a/internal/lsp/format.go +++ b/internal/lsp/format.go @@ -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 } diff --git a/internal/lsp/imports.go b/internal/lsp/imports.go index 21fb98e1..c20f1a61 100644 --- a/internal/lsp/imports.go +++ b/internal/lsp/imports.go @@ -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 { diff --git a/internal/lsp/source/diagnostics.go b/internal/lsp/source/diagnostics.go index f3945506..f3c34758 100644 --- a/internal/lsp/source/diagnostics.go +++ b/internal/lsp/source/diagnostics.go @@ -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