7 releases

new 0.3.4 Dec 13, 2024
0.3.3 Mar 6, 2024
0.3.2 Jun 8, 2023
0.3.1 Aug 23, 2022
0.1.0 Dec 17, 2021

#233 in Text editors

Download history 131/week @ 2024-08-23 119/week @ 2024-08-30 102/week @ 2024-09-06 830/week @ 2024-09-13 642/week @ 2024-09-20 133/week @ 2024-09-27 150/week @ 2024-10-04 195/week @ 2024-10-11 184/week @ 2024-10-18 208/week @ 2024-10-25 138/week @ 2024-11-01 82/week @ 2024-11-08 54/week @ 2024-11-15 75/week @ 2024-11-22 109/week @ 2024-11-29 146/week @ 2024-12-06

401 downloads per month
Used in 9 crates (2 directly)

MIT/Apache

24KB
295 lines

lsp-positions

The lsp-positions crate defines LSP-compatible positioning information for source code.

When writing a tool that analyzes or operates on source code, there's a good chance you need to interoperate with the Language Server Protocol. This seemingly simple requirement makes it surprisingly difficult to deal with character locations. This is because Rust stores Unicode string content (i.e., the source code you're analyzing) in UTF-8, while LSP specifies character locations using UTF-16 code units.

For some background, Unicode characters, or code points, are encoded as one or more code units. In UTF-8 a code unit is 1 byte, and a character is encoded in 1–4 code units (1–4 bytes). In UTF-16 a code unit is 2 bytes, and characters are encoded in 1–2 code units (2 or 4 bytes). Rust strings are encoded as UTF-8, and indexed by byte (which is the same as by code unit). Indices are only valid if they point to the first code unit of a code point.

We keep track of each source code position using two units: the UTF-8 byte position within the file or containing line, which can be used to index into UTF-8 encoded str and [u8] data, and the UTF-16 code unit position within the line, which can be used to generate Position values for LSP.

To use this library, add the following to your Cargo.toml:

[dependencies]
lsp-positions = "0.3"

Check out our documentation for more details on how to use this library.

Notable changes for each version are documented in the release notes.

License

Licensed under either of

at your option.

Dependencies

~0.4–1.5MB
~26K SLoC