| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package protocol |
| 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 |
import ( |
| 60 |
"bytes" |
| 61 |
"fmt" |
| 62 |
"go/ast" |
| 63 |
"go/token" |
| 64 |
"sort" |
| 65 |
"sync" |
| 66 |
"unicode/utf8" |
| 67 |
|
| 68 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 69 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
type Mapper struct { |
| 91 |
URI DocumentURI |
| 92 |
Content []byte |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
linesOnce sync.Once |
| 98 |
lineStart []int |
| 99 |
nonASCII bool |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func NewMapper(uri DocumentURI, content []byte) *Mapper { |
| 107 |
return &Mapper{URI: uri, Content: content} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
func (m *Mapper) initLines() { |
| 112 |
m.linesOnce.Do(func() { |
| 113 |
nlines := bytes.Count(m.Content, []byte("\n")) |
| 114 |
m.lineStart = make([]int, 1, nlines+1) |
| 115 |
for offset, b := range m.Content { |
| 116 |
if b == '\n' { |
| 117 |
m.lineStart = append(m.lineStart, offset+1) |
| 118 |
} |
| 119 |
if b >= utf8.RuneSelf { |
| 120 |
m.nonASCII = true |
| 121 |
} |
| 122 |
} |
| 123 |
}) |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
func (m *Mapper) LineCol8Position(line, col8 int) (Position, error) { |
| 129 |
|
| 130 |
if line < 1 { |
| 131 |
return Position{}, bug.Errorf("invalid 1-based line number: %d", line) |
| 132 |
} |
| 133 |
if col8 < 1 { |
| 134 |
return Position{}, bug.Errorf("invalid 1-based column number: %d", col8) |
| 135 |
} |
| 136 |
|
| 137 |
m.initLines() |
| 138 |
line0 := line - 1 |
| 139 |
if !(0 <= line0 && line0 < len(m.lineStart)) { |
| 140 |
return Position{}, fmt.Errorf("line number %d out of range (max %d)", line, len(m.lineStart)) |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
start := m.lineStart[line0] |
| 145 |
end := start + col8 - 1 |
| 146 |
|
| 147 |
|
| 148 |
if end > len(m.Content) { |
| 149 |
return Position{}, fmt.Errorf("column is beyond end of file") |
| 150 |
} else if line0+1 < len(m.lineStart) && end >= m.lineStart[line0+1] { |
| 151 |
return Position{}, fmt.Errorf("column is beyond end of line") |
| 152 |
} |
| 153 |
|
| 154 |
char := UTF16Len(m.Content[start:end]) |
| 155 |
return Position{Line: uint32(line0), Character: uint32(char)}, nil |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
func (m *Mapper) OffsetLocation(start, end int) (Location, error) { |
| 162 |
rng, err := m.OffsetRange(start, end) |
| 163 |
if err != nil { |
| 164 |
return Location{}, err |
| 165 |
} |
| 166 |
return m.URI.Location(rng), nil |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
func (m *Mapper) OffsetRange(start, end int) (Range, error) { |
| 171 |
if start > end { |
| 172 |
return Range{}, fmt.Errorf("start offset (%d) > end (%d)", start, end) |
| 173 |
} |
| 174 |
startPosition, err := m.OffsetPosition(start) |
| 175 |
if err != nil { |
| 176 |
return Range{}, fmt.Errorf("start: %v", err) |
| 177 |
} |
| 178 |
endPosition, err := m.OffsetPosition(end) |
| 179 |
if err != nil { |
| 180 |
return Range{}, fmt.Errorf("end: %v", err) |
| 181 |
} |
| 182 |
return Range{Start: startPosition, End: endPosition}, nil |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
func (m *Mapper) OffsetPosition(offset int) (Position, error) { |
| 187 |
if !(0 <= offset && offset <= len(m.Content)) { |
| 188 |
return Position{}, fmt.Errorf("invalid offset %d (want 0-%d)", offset, len(m.Content)) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
line, col16 := m.lineCol16(offset) |
| 194 |
return Position{Line: uint32(line), Character: uint32(col16)}, nil |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
func (m *Mapper) lineCol16(offset int) (int, int) { |
| 199 |
line, start, cr := m.line(offset) |
| 200 |
var col16 int |
| 201 |
if m.nonASCII { |
| 202 |
col16 = UTF16Len(m.Content[start:offset]) |
| 203 |
} else { |
| 204 |
col16 = offset - start |
| 205 |
} |
| 206 |
if cr { |
| 207 |
col16-- |
| 208 |
} |
| 209 |
return line, col16 |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
func (m *Mapper) OffsetLineCol8(offset int) (int, int) { |
| 214 |
line, start, cr := m.line(offset) |
| 215 |
col8 := offset - start |
| 216 |
if cr { |
| 217 |
col8-- |
| 218 |
} |
| 219 |
return line + 1, col8 + 1 |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
func (m *Mapper) line(offset int) (int, int, bool) { |
| 227 |
m.initLines() |
| 228 |
|
| 229 |
line := sort.Search(len(m.lineStart), func(i int) bool { |
| 230 |
return offset < m.lineStart[i] |
| 231 |
}) |
| 232 |
|
| 233 |
|
| 234 |
var eol int |
| 235 |
if line == len(m.lineStart) { |
| 236 |
eol = len(m.Content) |
| 237 |
} else { |
| 238 |
eol = m.lineStart[line] - 1 |
| 239 |
} |
| 240 |
cr := offset == eol && offset > 0 && m.Content[offset-1] == '\r' |
| 241 |
|
| 242 |
line-- |
| 243 |
|
| 244 |
return line, m.lineStart[line], cr |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
func (m *Mapper) RangeOffsets(r Range) (int, int, error) { |
| 251 |
start, err := m.PositionOffset(r.Start) |
| 252 |
if err != nil { |
| 253 |
return 0, 0, err |
| 254 |
} |
| 255 |
end, err := m.PositionOffset(r.End) |
| 256 |
if err != nil { |
| 257 |
return 0, 0, err |
| 258 |
} |
| 259 |
if start > end { |
| 260 |
return 0, 0, fmt.Errorf("start (offset %d) > end (offset %d)", start, end) |
| 261 |
} |
| 262 |
return start, end, nil |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
func (m *Mapper) PositionOffset(p Position) (int, error) { |
| 267 |
m.initLines() |
| 268 |
|
| 269 |
|
| 270 |
if p.Line > uint32(len(m.lineStart)) { |
| 271 |
return 0, fmt.Errorf("line number %d out of range 0-%d", p.Line, len(m.lineStart)) |
| 272 |
} else if p.Line == uint32(len(m.lineStart)) { |
| 273 |
if p.Character == 0 { |
| 274 |
return len(m.Content), nil |
| 275 |
} |
| 276 |
return 0, fmt.Errorf("column is beyond end of file") |
| 277 |
} |
| 278 |
|
| 279 |
offset := m.lineStart[p.Line] |
| 280 |
content := m.Content[offset:] |
| 281 |
|
| 282 |
|
| 283 |
col8 := 0 |
| 284 |
for col16 := 0; col16 < int(p.Character); col16++ { |
| 285 |
r, sz := utf8.DecodeRune(content) |
| 286 |
if sz == 0 { |
| 287 |
return 0, fmt.Errorf("column is beyond end of file") |
| 288 |
} |
| 289 |
if r == '\n' { |
| 290 |
return 0, fmt.Errorf("column is beyond end of line") |
| 291 |
} |
| 292 |
if sz == 1 && r == utf8.RuneError { |
| 293 |
return 0, fmt.Errorf("buffer contains invalid UTF-8 text") |
| 294 |
} |
| 295 |
content = content[sz:] |
| 296 |
|
| 297 |
if r >= 0x10000 { |
| 298 |
col16++ |
| 299 |
|
| 300 |
if col16 == int(p.Character) { |
| 301 |
break |
| 302 |
} |
| 303 |
} |
| 304 |
col8 += sz |
| 305 |
} |
| 306 |
return offset + col8, nil |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
func (m *Mapper) PosPosition(tf *token.File, pos token.Pos) (Position, error) { |
| 313 |
offset, err := safetoken.Offset(tf, pos) |
| 314 |
if err != nil { |
| 315 |
return Position{}, err |
| 316 |
} |
| 317 |
return m.OffsetPosition(offset) |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
func (m *Mapper) PosLocation(tf *token.File, start, end token.Pos) (Location, error) { |
| 322 |
startOffset, endOffset, err := safetoken.Offsets(tf, start, end) |
| 323 |
if err != nil { |
| 324 |
return Location{}, err |
| 325 |
} |
| 326 |
rng, err := m.OffsetRange(startOffset, endOffset) |
| 327 |
if err != nil { |
| 328 |
return Location{}, err |
| 329 |
} |
| 330 |
return m.URI.Location(rng), nil |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
func (m *Mapper) PosRange(tf *token.File, start, end token.Pos) (Range, error) { |
| 335 |
startOffset, endOffset, err := safetoken.Offsets(tf, start, end) |
| 336 |
if err != nil { |
| 337 |
return Range{}, err |
| 338 |
} |
| 339 |
return m.OffsetRange(startOffset, endOffset) |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
func (m *Mapper) PosText(tf *token.File, start, end token.Pos) ([]byte, error) { |
| 344 |
startOffset, endOffset, err := safetoken.Offsets(tf, start, end) |
| 345 |
if err != nil { |
| 346 |
return nil, err |
| 347 |
} |
| 348 |
return m.Content[startOffset:endOffset], nil |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
func (m *Mapper) NodeRange(tf *token.File, node ast.Node) (Range, error) { |
| 353 |
return m.PosRange(tf, node.Pos(), node.End()) |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
func (m *Mapper) NodeText(tf *token.File, node ast.Node) ([]byte, error) { |
| 358 |
return m.PosText(tf, node.Pos(), node.End()) |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
func LocationTextDocumentPositionParams(loc Location) TextDocumentPositionParams { |
| 363 |
return TextDocumentPositionParams{ |
| 364 |
TextDocument: TextDocumentIdentifier{URI: loc.URI}, |
| 365 |
Range: loc.Range, |
| 366 |
Position: loc.Range.Start, |
| 367 |
} |
| 368 |
} |