1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:04:44 -07:00

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 <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2018-12-20 17:31:55 -05:00
parent 85a09cd5ed
commit 62138107df
4 changed files with 17 additions and 17 deletions

View File

@ -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),

View File

@ -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)

View File

@ -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

View File

@ -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,
}