Debian Codegraph @

gopls/internal/protocol/span.go

raw symbols SCIP
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package protocol
6
7 import (
8 "fmt"
9 "unicode/utf8"
10 )
11
12 // Empty reports whether the Range is an empty selection.
13 func (rng Range) Empty() bool { return rng.Start == rng.End }
14
15 // Contains reports whether the position is within the range, inclusive of the
16 // start and end positions.
17 func (rng Range) Contains(pos Position) bool {
18 return ComparePosition(rng.Start, pos) <= 0 &&
19 ComparePosition(pos, rng.End) <= 0
20 }
21
22 // Empty reports whether the Location is an empty selection.
23 func (loc Location) Empty() bool { return loc.Range.Empty() }
24
25 // CompareLocation defines a three-valued comparison over locations,
26 // lexicographically ordered by (URI, Range).
27 func CompareLocation(x, y Location) int {
28 if x.URI != y.URI {
29 if x.URI < y.URI {
30 return -1
31 } else {
32 return +1
33 }
34 }
35 return CompareRange(x.Range, y.Range)
36 }
37
38 // CompareRange returns -1 if a is before b, 0 if a == b, and 1 if a is after b.
39 //
40 // A range a is defined to be 'before' b if a.Start is before b.Start, or
41 // a.Start == b.Start and a.End is before b.End.
42 func CompareRange(a, b Range) int {
43 if r := ComparePosition(a.Start, b.Start); r != 0 {
44 return r
45 }
46 return ComparePosition(a.End, b.End)
47 }
48
49 // ComparePosition returns -1 if a is before b, 0 if a == b, and 1 if a is after b.
50 func ComparePosition(a, b Position) int {
51 if a.Line != b.Line {
52 if a.Line < b.Line {
53 return -1
54 } else {
55 return +1
56 }
57 }
58 if a.Character != b.Character {
59 if a.Character < b.Character {
60 return -1
61 } else {
62 return +1
63 }
64 }
65 return 0
66 }
67
68 // Intersect reports whether x and y intersect.
69 //
70 // Two non-empty half-open integer intervals intersect iff:
71 //
72 // y.start < x.end && x.start < y.end
73 //
74 // Mathematical conventional views an interval as a set of integers.
75 // An empty interval is the empty set, so its intersection with any
76 // other interval is empty, and thus an empty interval does not
77 // intersect any other interval.
78 //
79 // However, this function uses a looser definition appropriate for
80 // text selections: if either x or y is empty, it uses <= operators
81 // instead, so an empty range within or abutting a non-empty range is
82 // considered to overlap it, and an empty range overlaps itself.
83 //
84 // This handles the common case in which there is no selection, but
85 // the cursor is at the start or end of an expression and the caller
86 // wants to know whether the cursor intersects the range of the
87 // expression. The answer in this case should be yes, even though the
88 // selection is empty. Similarly the answer should also be yes if the
89 // cursor is properly within the range of the expression. But a
90 // non-empty selection abutting the expression should not be
91 // considered to intersect it.
92 func Intersect(x, y Range) bool {
93 r1 := ComparePosition(x.Start, y.End)
94 r2 := ComparePosition(y.Start, x.End)
95 if r1 < 0 && r2 < 0 {
96 return true // mathematical intersection
97 }
98 return (x.Empty() || y.Empty()) && r1 <= 0 && r2 <= 0
99 }
100
101 // Format implements fmt.Formatter.
102 //
103 // Note: Formatter is implemented instead of Stringer (presumably) for
104 // performance reasons, though it is not clear that it matters in practice.
105 func (r Range) Format(f fmt.State, _ rune) {
106 fmt.Fprintf(f, "%v-%v", r.Start, r.End)
107 }
108
109 // Format implements fmt.Formatter.
110 //
111 // See Range.Format for discussion of why the Formatter interface is
112 // implemented rather than Stringer.
113 func (p Position) Format(f fmt.State, _ rune) {
114 fmt.Fprintf(f, "%v:%v", p.Line, p.Character)
115 }
116
117 // -- implementation helpers --
118
119 // UTF16Len returns the number of codes in the UTF-16 transcoding of s.
120 func UTF16Len(s []byte) int {
121 var n int
122 for len(s) > 0 {
123 n++
124
125 // Fast path for ASCII.
126 if s[0] < 0x80 {
127 s = s[1:]
128 continue
129 }
130
131 r, size := utf8.DecodeRune(s)
132 if r >= 0x10000 {
133 n++ // surrogate pair
134 }
135 s = s[size:]
136 }
137 return n
138 }