View Source Lexical.Document (lexical_shared v0.5.0)

A representation of a LSP text document

A document is the fundamental data structure of the Lexical language server. All language server documents are represented and backed by documents, which provide functionality for fetching lines, applying changes, and tracking versions.

Link to this section Summary

Functions

Get the Lexical>Document.Line at the given index using fetch semantics.

Get the text at the given line using fetch semantics

Returns a fragment defined by the from and to arguments

Marks the document file as clean

Marks the document file as dirty

Creates a new document from a uri or path, the source code as a binary and the vewrsion.

Returns the number of lines in the document

Converts a document to a string

Link to this section Types

Link to this type

change_application_error()

View Source
@type change_application_error() :: {:error, {:invalid_range, map()}}
@type fragment_position() :: Lexical.Document.Position.t() | Lexical.Convertible.t()
@type t() :: %Lexical.Document{
  dirty?: boolean(),
  lines: Lexical.Document.Lines.t(),
  path: String.t(),
  uri: String.t(),
  version: version()
}
@type version() :: non_neg_integer()

Link to this section Functions

Link to this function

fetch_line_at(document, line_number)

View Source
@spec fetch_line_at(t(), version()) :: {:ok, Lexical.Document.Line.t()} | :error

Get the Lexical>Document.Line at the given index using fetch semantics.

This function is of limited utility, you probably want fetch_text_at/2 instead.

Link to this function

fetch_text_at(document, line_number)

View Source
@spec fetch_text_at(t(), version()) :: {:ok, String.t()} | :error

Get the text at the given line using fetch semantics

Returns {:ok, text} if the line exists, and :error if it doesn't. The line text is returned without the line end character(s).

This is a constant time operation.

Link to this function

fragment(document, from \\ :beginning, to)

View Source
@spec fragment(t(), fragment_position() | :beginning, fragment_position()) ::
  String.t()

Returns a fragment defined by the from and to arguments

Builds a string that represents the text of the document from the two positions given. The from position, defaults to :beginning meaning the start of the document. Positions can be a Document.Position.t or anything that will convert to a position using Lexical.Convertible.to_native/2.

@spec mark_clean(t()) :: t()

Marks the document file as clean

This function is mainly used internally by lexical

@spec mark_dirty(t()) :: t()

Marks the document file as dirty

This function is mainly used internally by lexical

Link to this function

new(maybe_uri, text, version)

View Source
@spec new(Lexical.path() | Lexical.uri(), String.t(), version()) :: t()

Creates a new document from a uri or path, the source code as a binary and the vewrsion.

@spec size(t()) :: non_neg_integer()

Returns the number of lines in the document

This is a constant time operation.

Converts a document to a string

This function converts the given document back into a string, with line endings preserved.