p4lang / p4analyzer

A Language Server Protocol (LSP) compliant analyzer for the P4 language
Apache License 2.0
19 stars 3 forks source link

Improve conversions between LSP positions and string offsets #1

Closed viluon closed 1 year ago

viluon commented 1 year ago

The code we currently use to convert between an LSP range or position and indices into Rust strings is hideously inefficient and doesn't handle UTF-8 properly. Long-term, we should probably look into maintaining a rope server-side to count offsets efficiently, but every little bit helps.

https://github.com/p4lang/p4analyzer/blob/39c9c0cbcc132d9fa689f77ea9240924cd6db54b/crates/analyzer-host/src/lsp_impl/active_initialized.rs#L276-L323

AndrewF001 commented 1 year ago

Most efficient solution would be to produce a sorted ranged list for each file that converts LSP positions -> byte. This allows, a binary search to get LSP position and use array index to get byte position.

Currently lsp_range_to_byte_range() is only used in on_text_document_did_change() which presumably would require reproducing that ranged list (could implement a lazy update), negating any benefits from the ranged list. I presume the conversion LSP <-> bytes will be used elsewhere, where it doesn't require file changes (hover, go to, highlight)?