1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:04:43 -07:00

internal/lsp: add compare functions for spans

Needed in order to sort lists of spans for stable tests, also used for span
equality tests.

Change-Id: Id2bedff4d7136494e2302c02bcb9bdc662ccfeb1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/172660
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Ian Cottrell 2019-04-17 18:01:48 -04:00
parent f6abc2cac8
commit 183b688ac4

View File

@ -7,6 +7,7 @@ package span
import (
"encoding/json"
"fmt"
"strings"
)
// Span represents a source code range in standardized form.
@ -56,6 +57,45 @@ func NewPoint(line, col, offset int) Point {
return p
}
func Compare(a, b Span) int {
if r := strings.Compare(string(a.v.URI), string(b.v.URI)); r != 0 {
return r
}
if r := comparePoint(a.v.Start, b.v.Start); r != 0 {
return r
}
return comparePoint(a.v.End, b.v.End)
}
func ComparePoint(a, b Point) int {
return comparePoint(a.v, b.v)
}
func comparePoint(a, b point) int {
if !a.hasPosition() {
if a.Offset < b.Offset {
return -1
}
if a.Offset > b.Offset {
return 1
}
return 0
}
if a.Line < b.Line {
return -1
}
if a.Line > b.Line {
return 1
}
if a.Column < b.Column {
return -1
}
if a.Column > b.Column {
return 1
}
return 0
}
func (s Span) HasPosition() bool { return s.v.Start.hasPosition() }
func (s Span) HasOffset() bool { return s.v.Start.hasOffset() }
func (s Span) IsValid() bool { return s.v.Start.isValid() }