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-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"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-10-19 14:03:29 -06:00
|
|
|
)
|
|
|
|
|
2019-02-19 19:11:15 -07:00
|
|
|
func (s *server) cacheAndDiagnose(ctx context.Context, uri span.URI, content string) error {
|
|
|
|
if err := s.setContent(ctx, uri, []byte(content)); err != nil {
|
|
|
|
return err
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
go func() {
|
2019-03-05 15:30:44 -07:00
|
|
|
ctx := s.view.BackgroundContext()
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-19 19:11:15 -07:00
|
|
|
reports, err := source.Diagnostics(ctx, s.view, uri)
|
2018-12-05 15:00:36 -07:00
|
|
|
if err != nil {
|
|
|
|
return // handle error?
|
|
|
|
}
|
2019-02-19 19:11:15 -07:00
|
|
|
for uri, diagnostics := range reports {
|
2019-03-14 14:55:23 -06:00
|
|
|
protocolDiagnostics, err := toProtocolDiagnostics(ctx, s.view, diagnostics)
|
|
|
|
if err != nil {
|
|
|
|
continue // handle errors?
|
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
2019-03-14 14:55:23 -06:00
|
|
|
Diagnostics: protocolDiagnostics,
|
2019-02-19 19:11:15 -07:00
|
|
|
URI: protocol.NewURI(uri),
|
2018-12-05 15:00:36 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
2019-02-19 19:11:15 -07:00
|
|
|
return nil
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-02-19 19:11:15 -07:00
|
|
|
func (s *server) setContent(ctx context.Context, uri span.URI, content []byte) error {
|
2019-03-05 15:30:44 -07:00
|
|
|
return s.view.SetContent(ctx, uri, content)
|
2018-12-18 14:18:03 -07:00
|
|
|
}
|
|
|
|
|
2019-03-14 14:55:23 -06:00
|
|
|
func toProtocolDiagnostics(ctx context.Context, v source.View, diagnostics []source.Diagnostic) ([]protocol.Diagnostic, error) {
|
2018-11-12 12:15:47 -07:00
|
|
|
reports := []protocol.Diagnostic{}
|
|
|
|
for _, diag := range diagnostics {
|
2019-02-19 19:11:15 -07:00
|
|
|
_, m, err := newColumnMap(ctx, v, diag.Span.URI)
|
|
|
|
if err != nil {
|
2019-03-14 14:55:23 -06:00
|
|
|
return nil, err
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
2019-02-27 16:08:04 -07:00
|
|
|
src := diag.Source
|
|
|
|
if src == "" {
|
|
|
|
src = "LSP"
|
|
|
|
}
|
|
|
|
var severity protocol.DiagnosticSeverity
|
|
|
|
switch diag.Severity {
|
|
|
|
case source.SeverityError:
|
|
|
|
severity = protocol.SeverityError
|
|
|
|
case source.SeverityWarning:
|
|
|
|
severity = protocol.SeverityWarning
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
2018-11-12 12:15:47 -07:00
|
|
|
reports = append(reports, protocol.Diagnostic{
|
2018-12-18 13:46:14 -07:00
|
|
|
Message: diag.Message,
|
2019-02-19 19:11:15 -07:00
|
|
|
Range: m.Range(diag.Span),
|
2019-02-27 16:08:04 -07:00
|
|
|
Severity: severity,
|
|
|
|
Source: src,
|
2018-11-12 12:15:47 -07:00
|
|
|
})
|
|
|
|
}
|
2019-03-14 14:55:23 -06:00
|
|
|
return reports, nil
|
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 {
|
2018-12-18 13:46:14 -07:00
|
|
|
if d[i].Range.Start.Character == d[j].Range.Start.Character {
|
2018-11-13 09:13:53 -07:00
|
|
|
return d[i].Message < d[j].Message
|
|
|
|
}
|
2018-12-18 13:46:14 -07:00
|
|
|
return d[i].Range.Start.Character < d[j].Range.Start.Character
|
2018-11-13 09:13:53 -07:00
|
|
|
}
|
|
|
|
return d[i].Range.Start.Line < d[j].Range.Start.Line
|
|
|
|
})
|
|
|
|
}
|