-
Notifications
You must be signed in to change notification settings - Fork 329
/
body.go
97 lines (82 loc) · 2.41 KB
/
body.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package langsvr
import (
"sort"
"github.com/google/gapid/core/langsvr/protocol"
"github.com/google/gapid/core/math/sint"
)
// Body represents the body of a document.
type Body struct {
runes []rune
eol []int
}
// NewBody returns a body containing the specified text.
func NewBody(text string) Body {
return NewBodyFromRunes([]rune(text))
}
// NewBodyFromRunes returns a body containing the specified text in runes.
func NewBodyFromRunes(runes []rune) Body {
b := Body{runes: runes}
b.eol = []int{-1}
for i, r := range runes {
if r == '\n' {
b.eol = append(b.eol, i)
}
}
b.eol = append(b.eol, len(runes))
return b
}
// Runes returns the body's runes.
func (b Body) Runes() []rune {
return b.runes
}
// Text returns the body's text.
func (b Body) Text() string {
return string(b.runes)
}
// FullRange returns the entire range of the document.
func (b Body) FullRange() Range {
return Range{b.Position(0), b.Position(len(b.runes))}
}
// GetRange returns a chunk of the body.
func (b Body) GetRange(rng Range) string {
s, e := b.Offset(rng.Start), b.Offset(rng.End)
return string(b.runes[s:e])
}
// Position returns the position at the specified offset
func (b Body) Position(offset int) Position {
offset = sint.Min(offset, len(b.runes))
l := sort.SearchInts(b.eol, offset)
c := offset - b.eol[l-1]
return Position{l, c}
}
// Range returns the range for the given start and end offsets.
func (b Body) Range(start, end int) Range {
return Range{b.Position(start), b.Position(end)}
}
// Offset returns the byte offset of p.
func (b Body) Offset(p Position) int {
return b.offset(p.toProtocol())
}
func (b Body) offset(p protocol.Position) int {
nextEOL := len(b.runes)
if p.Line >= len(b.eol) {
return nextEOL
}
if p.Line+1 < len(b.eol) {
nextEOL = b.eol[p.Line+1]
}
return sint.Min(b.eol[p.Line]+p.Column+1, nextEOL)
}