Documentation
¶
Overview ¶
Package lspos translates between three coordinate systems used in language-server work:
Byte offset — Go-native; what slices and most pkg/ipm/* APIs use. UTF-16 (line,col) — what the LSP protocol uses on the wire. UTF-8 (line,col) — convenient for some terminals / logs.
The conversion is non-trivial for documents that contain non-ASCII characters: a single rune may take multiple UTF-8 bytes and one or two UTF-16 code units. Getting this wrong shows up as off-by-some highlighting bugs when node names contain accented letters, emoji, or CJK characters.
Inspired by gopls' protocol/mapper.go. The shape is intentionally smaller; we only implement the conversions LSP method handlers actually call, not the full mapper surface gopls maintains.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Mapper ¶
type Mapper struct {
// contains filtered or unexported fields
}
Mapper precomputes the index needed to translate between byte offsets and (line, column) positions for a single document body. Build one per document version; throw it away on the next didChange and rebuild.
Mapper is read-only after construction; safe to share across goroutines.
func (*Mapper) LineColUTF16ToOffset ¶
LineColUTF16ToOffset is the inverse: given an LSP (line, col) position (both UTF-16, 0-based), return the byte offset into the document.
Useful when an LSP client tells us "the cursor is at line 12, character 5" and we need to look up which AST node sits there.
If the line is past the last line, returns len(text). If the column is past end-of-line, returns the offset of the newline (or EOF on the last line). Either way the returned offset is in [0, len(text)].
func (*Mapper) LineCount ¶
LineCount returns the number of lines in the document (1 for "no trailing newline" docs; 2 for "foo\n", etc.). Convenience for capability advertise.
func (*Mapper) OffsetRangeToUTF16 ¶
OffsetRangeToUTF16 is the natural extension to a [start, end) byte range.
func (*Mapper) OffsetToUTF16 ¶
OffsetToUTF16 returns the (0-based line, 0-based UTF-16 column) position of the given byte offset. If the offset is past EOF it's clamped.
"UTF-16 column" means: how many UTF-16 code units precede this offset on the same line. For ASCII text this equals the byte column; for a 4-byte emoji it's 2 (one surrogate pair). The LSP protocol expects this.