View Source Lexical.Convertible protocol (lexical_shared v0.5.0)

A protocol that details conversions to and from Language Server idioms

The Language Server specification defines a couple of data structures that differ wildly from similar data structures and concepts in Elixir. Among these are positions and ranges. Elixir's parser thinks in terms of UTF-8 graphemes and uses one-based line and column numbers, while the language server speaks in UTF-16 code unit offsets and uses zero-based line and character (a misnomer, in reality this is a UTF-16 code unit) offsets. If not handled centrally, this leads to a profusion of conversion code throughout the language server codebase.

That's where this protocol comes in. Using this protocol allows us to define native Lexical.Document.Position and Lexical.Document.Range structs and have them automatically convert into their Language Server counterparts, centralizing the conversion logic in a single pace.

Note: You do not need to do conversions manually, If you define a new type, it is sufficient to implement this protocol for your new type

Link to this section Summary

Types

A Language server term

The result of converting a native term into a lsp term

A native term that contains ranges, positions or both

The result of converting a lsp term into a native term

t()

Any type that can be converted using this protocol

Functions

Converts the native representation to a LSP compatible struct

Converts the structure to a native implementation

Link to this section Types

@type lsp() :: term()

A Language server term

@type lsp_response() :: {:ok, lsp()} | {:error, term()}

The result of converting a native term into a lsp term

@type native() :: term()

A native term that contains ranges, positions or both

@type native_response() :: {:ok, native()} | {:error, term()}

The result of converting a lsp term into a native term

@type t() :: term()

Any type that can be converted using this protocol

Link to this section Functions

@spec to_lsp(t()) :: lsp_response()

Converts the native representation to a LSP compatible struct

Link to this function

to_native(t, context_document)

View Source

Converts the structure to a native implementation