package lsppos

import "golang.org/x/tools/internal/lsp/lsppos"

Package lsppos provides utilities for working with LSP positions. Much of this functionality is duplicated from the internal/span package, but this package is simpler and more accurate with respect to newline terminated content.

See https://microsoft.github.io/language-server-protocol/specification#textDocuments for a description of LSP positions. Notably:

Index

Functions

func UTF16len

func UTF16len(buf []byte) int

UTF16Len returns the UTF-16 length of the UTF-8 encoded content, were it to be re-encoded as UTF-16.

Types

type Mapper

type Mapper struct {
	// contains filtered or unexported fields
}

Mapper maps utf-8 byte offsets to LSP positions for a single file.

func NewMapper

func NewMapper(content []byte) *Mapper

NewMapper creates a new Mapper for the given content.

func (*Mapper) LineColUTF16

func (m *Mapper) LineColUTF16(offset int) (line, char int)

LineColUTF16 returns the 0-based UTF-16 line and character index for the given offset. It returns -1, -1 if offset is out of bounds for the file being mapped.

func (*Mapper) Position

func (m *Mapper) Position(offset int) (protocol.Position, bool)

Position returns the protocol position corresponding to the given offset. It returns false if offset is out of bounds for the file being mapped.

func (*Mapper) Range

func (m *Mapper) Range(start, end int) (protocol.Range, error)

Range returns the protocol range corresponding to the given start and end offsets.

type TokenMapper

type TokenMapper struct {
	// contains filtered or unexported fields
}

TokenMapper maps token.Pos to LSP positions for a single file.

func NewTokenMapper

func NewTokenMapper(content []byte, file *token.File) *TokenMapper

NewMapper creates a new TokenMapper for the given content, using the provided file to compute offsets.

func (*TokenMapper) Position

func (m *TokenMapper) Position(pos token.Pos) (protocol.Position, bool)

Position returns the protocol position corresponding to the given pos. It returns false if pos is out of bounds for the file being mapped.

func (*TokenMapper) Range

func (m *TokenMapper) Range(start, end token.Pos) (protocol.Range, error)

Range returns the protocol range corresponding to the given start and end positions. It returns an error if start or end is out of bounds for the file being mapped.

Source Files

lsppos.go token.go

Version
v0.1.12
Published
Jul 27, 2022
Platform
linux/amd64
Imports
7 packages
Last checked
6 minutes ago

Tools for package owners.