From a0af95a55c89ead4b6c9c18102fa26ef1580fb97 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Mon, 21 Oct 2019 15:10:19 -0400 Subject: [PATCH] internal/lsp: return spanForRange function to fix build This function was removed in CL 202298, but used in CL 200597. Analysis diagnostics should be converted to the source.Error type in the analysis runner as a complete fix, but this is fine for now. Change-Id: Ie5f3f566719073d7df6ab4646f855c9f9ce22ad7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/202539 Run-TryBot: Rebecca Stambler Reviewed-by: Dominik Honnef TryBot-Result: Gobot Gobot --- internal/lsp/source/diagnostics.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/internal/lsp/source/diagnostics.go b/internal/lsp/source/diagnostics.go index 567873505c..ff1c9b8566 100644 --- a/internal/lsp/source/diagnostics.go +++ b/internal/lsp/source/diagnostics.go @@ -5,6 +5,7 @@ package source import ( + "bytes" "context" "fmt" @@ -274,6 +275,35 @@ func relatedInformation(ctx context.Context, view View, diag *analysis.Diagnosti return out, nil } +// spanToRange converts a span.Span to a protocol.Range, +// assuming that the span belongs to the package whose diagnostics are being computed. +func spanToRange(ctx context.Context, view View, pkg Package, spn span.Span, isTypeError bool) (protocol.Range, error) { + ph, err := pkg.File(spn.URI()) + if err != nil { + return protocol.Range{}, err + } + _, m, _, err := ph.Cached(ctx) + if err != nil { + return protocol.Range{}, err + } + data, _, err := ph.File().Read(ctx) + if err != nil { + return protocol.Range{}, err + } + // Try to get a range for the diagnostic. + // TODO: Don't just limit ranges to type errors. + if spn.IsPoint() && isTypeError { + if s, err := spn.WithOffset(m.Converter); err == nil { + start := s.Start() + offset := start.Offset() + if width := bytes.IndexAny(data[offset:], " \n,():;[]"); width > 0 { + spn = span.New(spn.URI(), start, span.NewPoint(start.Line(), start.Column()+width, offset+width)) + } + } + } + return m.Range(spn) +} + func clearReports(v View, reports map[span.URI][]Diagnostic, uri span.URI) { if v.Ignore(uri) { return