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-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"
|
2018-10-19 14:03:29 -06:00
|
|
|
)
|
|
|
|
|
2018-11-12 12:15:47 -07:00
|
|
|
func toProtocolDiagnostics(v *source.View, diagnostics []source.Diagnostic) []protocol.Diagnostic {
|
|
|
|
reports := []protocol.Diagnostic{}
|
|
|
|
for _, diag := range diagnostics {
|
|
|
|
tok := v.Config.Fset.File(diag.Range.Start)
|
|
|
|
reports = append(reports, protocol.Diagnostic{
|
|
|
|
Message: diag.Message,
|
|
|
|
Range: toProtocolRange(tok, diag.Range),
|
|
|
|
Severity: toProtocolSeverity(diag.Severity),
|
|
|
|
Source: "LSP",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return reports
|
2018-10-29 16:12:41 -06:00
|
|
|
}
|
2018-10-19 14:03:29 -06:00
|
|
|
|
2018-11-12 12:15:47 -07:00
|
|
|
func toProtocolSeverity(severity source.DiagnosticSeverity) protocol.DiagnosticSeverity {
|
|
|
|
switch severity {
|
|
|
|
case source.SeverityError:
|
|
|
|
return protocol.SeverityError
|
|
|
|
case source.SeverityWarning:
|
|
|
|
return protocol.SeverityWarning
|
|
|
|
case source.SeverityHint:
|
|
|
|
return protocol.SeverityHint
|
|
|
|
case source.SeverityInformation:
|
|
|
|
return protocol.SeverityInformation
|
|
|
|
}
|
|
|
|
return protocol.SeverityError // default
|
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
|
|
|
|
})
|
|
|
|
}
|