2019-02-19 19:11:15 -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.
|
|
|
|
|
|
|
|
// this file contains protocol<->span converters
|
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2019-08-14 18:12:18 -06:00
|
|
|
"fmt"
|
2019-03-15 11:19:43 -06:00
|
|
|
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-19 19:11:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type ColumnMapper struct {
|
|
|
|
URI span.URI
|
|
|
|
Converter *span.TokenConverter
|
|
|
|
Content []byte
|
|
|
|
}
|
|
|
|
|
2020-02-12 14:36:46 -07:00
|
|
|
func URIFromSpanURI(uri span.URI) DocumentURI {
|
|
|
|
return DocumentURI(uri)
|
|
|
|
}
|
|
|
|
|
|
|
|
func URIFromPath(path string) DocumentURI {
|
|
|
|
return URIFromSpanURI(span.URIFromPath(path))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u DocumentURI) SpanURI() span.URI {
|
|
|
|
return span.URIFromURI(string(u))
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
func (m *ColumnMapper) Location(s span.Span) (Location, error) {
|
|
|
|
rng, err := m.Range(s)
|
|
|
|
if err != nil {
|
|
|
|
return Location{}, err
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
2020-02-12 14:36:46 -07:00
|
|
|
return Location{URI: URIFromSpanURI(s.URI()), Range: rng}, nil
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
func (m *ColumnMapper) Range(s span.Span) (Range, error) {
|
2019-04-17 15:54:50 -06:00
|
|
|
if span.CompareURI(m.URI, s.URI()) != 0 {
|
2019-08-06 13:13:11 -06:00
|
|
|
return Range{}, errors.Errorf("column mapper is for file %q instead of %q", m.URI, s.URI())
|
2019-03-15 11:19:43 -06:00
|
|
|
}
|
2019-04-01 17:38:11 -06:00
|
|
|
s, err := s.WithAll(m.Converter)
|
2019-03-15 11:19:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
|
|
|
}
|
|
|
|
start, err := m.Position(s.Start())
|
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
end, err := m.Position(s.End())
|
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
|
|
|
}
|
|
|
|
return Range{Start: start, End: end}, nil
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
func (m *ColumnMapper) Position(p span.Point) (Position, error) {
|
|
|
|
chr, err := span.ToUTF16Column(p, m.Content)
|
|
|
|
if err != nil {
|
|
|
|
return Position{}, err
|
|
|
|
}
|
2019-02-19 19:11:15 -07:00
|
|
|
return Position{
|
2019-03-15 11:19:43 -06:00
|
|
|
Line: float64(p.Line() - 1),
|
2019-02-19 19:11:15 -07:00
|
|
|
Character: float64(chr - 1),
|
2019-03-15 11:19:43 -06:00
|
|
|
}, nil
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
func (m *ColumnMapper) Span(l Location) (span.Span, error) {
|
|
|
|
return m.RangeSpan(l.Range)
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
func (m *ColumnMapper) RangeSpan(r Range) (span.Span, error) {
|
|
|
|
start, err := m.Point(r.Start)
|
|
|
|
if err != nil {
|
|
|
|
return span.Span{}, err
|
|
|
|
}
|
|
|
|
end, err := m.Point(r.End)
|
|
|
|
if err != nil {
|
|
|
|
return span.Span{}, err
|
|
|
|
}
|
|
|
|
return span.New(m.URI, start, end).WithAll(m.Converter)
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
func (m *ColumnMapper) PointSpan(p Position) (span.Span, error) {
|
|
|
|
start, err := m.Point(p)
|
|
|
|
if err != nil {
|
|
|
|
return span.Span{}, err
|
|
|
|
}
|
|
|
|
return span.New(m.URI, start, start).WithAll(m.Converter)
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
func (m *ColumnMapper) Point(p Position) (span.Point, error) {
|
|
|
|
line := int(p.Line) + 1
|
|
|
|
offset, err := m.Converter.ToOffset(line, 1)
|
|
|
|
if err != nil {
|
|
|
|
return span.Point{}, err
|
|
|
|
}
|
|
|
|
lineStart := span.NewPoint(line, 1, offset)
|
|
|
|
return span.FromUTF16Column(lineStart, int(p.Character)+1, m.Content)
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
2019-08-14 18:12:18 -06:00
|
|
|
|
|
|
|
func IsPoint(r Range) bool {
|
|
|
|
return r.Start.Line == r.End.Line && r.Start.Character == r.End.Character
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompareRange(a, b Range) int {
|
|
|
|
if r := ComparePosition(a.Start, b.Start); r != 0 {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return ComparePosition(a.End, b.End)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ComparePosition(a, b Position) int {
|
|
|
|
if a.Line < b.Line {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if a.Line > b.Line {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if a.Character < b.Character {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if a.Character > b.Character {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-07-24 08:34:05 -06:00
|
|
|
func Intersect(a, b Range) bool {
|
|
|
|
if a.Start.Line > b.End.Line || a.End.Line < b.Start.Line {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !((a.Start.Line == b.End.Line) && a.Start.Character > b.End.Character ||
|
|
|
|
(a.End.Line == b.Start.Line) && a.End.Character < b.Start.Character)
|
|
|
|
}
|
|
|
|
|
2019-08-14 18:12:18 -06:00
|
|
|
func (r Range) Format(f fmt.State, _ rune) {
|
|
|
|
fmt.Fprintf(f, "%v:%v-%v:%v", r.Start.Line, r.Start.Character, r.End.Line, r.End.Character)
|
|
|
|
}
|