From 62138107dfcf98a2a23c0fb5a91983423e39973a Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 20 Dec 2018 17:31:55 -0500 Subject: [PATCH] internal/lsp: fix diagnostics range computation Diagnostics were failing because of https://go-review.googlesource.com/c/tools/+/154742, where I was using the wrong *token.File for the position calculations. This should fix the problem. Change-Id: Ic44e7799da56010b5014d56029fb4e0a8a6bb0e8 Reviewed-on: https://go-review.googlesource.com/c/155479 Reviewed-by: Ian Cottrell --- internal/lsp/diagnostics.go | 16 ++++------------ internal/lsp/lsp_test.go | 2 +- internal/lsp/source/definition.go | 4 ++-- internal/lsp/source/diagnostics.go | 12 ++++++++++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go index 02102867..49743dc0 100644 --- a/internal/lsp/diagnostics.go +++ b/internal/lsp/diagnostics.go @@ -23,10 +23,9 @@ func (s *server) cacheAndDiagnose(ctx context.Context, uri protocol.DocumentURI, return // handle error? } for filename, diagnostics := range reports { - uri := source.ToURI(filename) s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{ - URI: protocol.DocumentURI(uri), - Diagnostics: toProtocolDiagnostics(ctx, s.view, uri, diagnostics), + URI: protocol.DocumentURI(source.ToURI(filename)), + Diagnostics: toProtocolDiagnostics(ctx, s.view, diagnostics), }) } }() @@ -45,17 +44,10 @@ func (s *server) setContent(ctx context.Context, uri source.URI, content []byte) return nil } -func toProtocolDiagnostics(ctx context.Context, v source.View, uri source.URI, diagnostics []source.Diagnostic) []protocol.Diagnostic { +func toProtocolDiagnostics(ctx context.Context, v source.View, diagnostics []source.Diagnostic) []protocol.Diagnostic { reports := []protocol.Diagnostic{} for _, diag := range diagnostics { - f, err := v.GetFile(ctx, uri) - if err != nil { - continue // handle error? - } - tok, err := f.GetToken() - if err != nil { - continue // handle error? - } + tok := v.FileSet().File(diag.Start) reports = append(reports, protocol.Diagnostic{ Message: diag.Message, Range: toProtocolRange(tok, diag.Range), diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index 73c1d4fb..264e4e01 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -157,7 +157,7 @@ func (d diagnostics) test(t *testing.T, exported *packagestest.Exported, v sourc if err != nil { t.Fatal(err) } - got := toProtocolDiagnostics(ctx, v, source.ToURI(filename), sourceDiagnostics[filename]) + got := toProtocolDiagnostics(ctx, v, sourceDiagnostics[filename]) sorted(got) if diff := diffDiagnostics(filename, want, got); diff != "" { t.Error(diff) diff --git a/internal/lsp/source/definition.go b/internal/lsp/source/definition.go index 79eb93ff..778566cb 100644 --- a/internal/lsp/source/definition.go +++ b/internal/lsp/source/definition.go @@ -139,8 +139,8 @@ func checkIdentifier(f *ast.File, pos token.Pos) (ident, error) { func objToRange(ctx context.Context, v View, fset *token.FileSet, obj types.Object) Range { p := obj.Pos() - f := fset.File(p) - pos := f.Position(p) + tok := fset.File(p) + pos := tok.Position(p) if pos.Column == 1 { // We do not have full position information because exportdata does not // store the column. For now, we attempt to read the original source diff --git a/internal/lsp/source/diagnostics.go b/internal/lsp/source/diagnostics.go index 1b84d3c5..7c393aec 100644 --- a/internal/lsp/source/diagnostics.go +++ b/internal/lsp/source/diagnostics.go @@ -70,10 +70,18 @@ func Diagnostics(ctx context.Context, v View, uri URI) (map[string][]Diagnostic, if err != nil || diag.Kind != packages.TypeError { end = 0 } + startPos := fromTokenPosition(diagTok, pos.Line, pos.Column) + if !startPos.IsValid() { + continue + } + endPos := fromTokenPosition(diagTok, pos.Line, pos.Column+end) + if !endPos.IsValid() { + continue + } diagnostic := Diagnostic{ Range: Range{ - Start: fromTokenPosition(diagTok, pos.Line, pos.Column), - End: fromTokenPosition(diagTok, pos.Line, pos.Column+end), + Start: startPos, + End: endPos, }, Message: diag.Msg, }