2018-11-05 12:48:08 -07:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-10-19 14:03:29 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
2018-12-05 15:00:36 -07:00
|
|
|
"context"
|
2018-11-13 09:13:53 -07:00
|
|
|
"sort"
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
2018-10-19 14:03:29 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2018-11-02 14:15:31 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2018-10-19 14:03:29 -06:00
|
|
|
)
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
func (s *server) CacheAndDiagnose(ctx context.Context, uri protocol.DocumentURI, text string) {
|
|
|
|
f := s.view.GetFile(source.URI(uri))
|
|
|
|
f.SetContent([]byte(text))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
reports, err := source.Diagnostics(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return // handle error?
|
|
|
|
}
|
|
|
|
for filename, diagnostics := range reports {
|
|
|
|
s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
|
|
|
URI: protocol.DocumentURI(source.ToURI(filename)),
|
|
|
|
Diagnostics: toProtocolDiagnostics(s.view, diagnostics),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func toProtocolDiagnostics(v *cache.View, diagnostics []source.Diagnostic) []protocol.Diagnostic {
|
2018-11-12 12:15:47 -07:00
|
|
|
reports := []protocol.Diagnostic{}
|
|
|
|
for _, diag := range diagnostics {
|
2018-12-05 15:00:36 -07:00
|
|
|
f := v.GetFile(source.ToURI(diag.Filename))
|
|
|
|
tok, err := f.GetToken()
|
|
|
|
if err != nil {
|
|
|
|
continue // handle error?
|
|
|
|
}
|
|
|
|
pos := fromTokenPosition(tok, diag.Position)
|
|
|
|
if !pos.IsValid() {
|
|
|
|
continue // handle error?
|
|
|
|
}
|
2018-11-12 12:15:47 -07:00
|
|
|
reports = append(reports, protocol.Diagnostic{
|
2018-12-05 15:00:36 -07:00
|
|
|
Message: diag.Message,
|
|
|
|
Range: toProtocolRange(tok, source.Range{
|
|
|
|
Start: pos,
|
|
|
|
End: pos,
|
|
|
|
}),
|
|
|
|
Severity: protocol.SeverityError, // all diagnostics have error severity for now
|
2018-11-12 12:15:47 -07:00
|
|
|
Source: "LSP",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return reports
|
2018-10-29 16:12:41 -06:00
|
|
|
}
|
2018-10-19 14:03:29 -06:00
|
|
|
|
2018-11-13 09:13:53 -07:00
|
|
|
func sorted(d []protocol.Diagnostic) {
|
|
|
|
sort.Slice(d, func(i int, j int) bool {
|
|
|
|
if d[i].Range.Start.Line == d[j].Range.Start.Line {
|
|
|
|
if d[i].Range.Start.Character == d[j].Range.End.Character {
|
|
|
|
return d[i].Message < d[j].Message
|
|
|
|
}
|
|
|
|
return d[i].Range.Start.Character < d[j].Range.End.Character
|
|
|
|
}
|
|
|
|
return d[i].Range.Start.Line < d[j].Range.Start.Line
|
|
|
|
})
|
|
|
|
}
|