Pentiment.Label (pentiment v0.1.5)

A labeled span for annotating source code in diagnostics.

Labels combine a span with a message and visual priority. They are used to highlight specific regions of source code and explain what's happening there.

Priority

Labels have two priority levels:

  • :primary - The main error location, highlighted prominently (typically red)
  • :secondary - Supporting context, highlighted less prominently (typically yellow)

Multi-file Diagnostics

For diagnostics that span multiple files, set the :source field to identify which file this label refers to.

Examples

# Primary label at the error site
Label.primary(span, "expected `integer`, found `float`")

# Secondary label showing related context
Label.secondary(decl_span, "declared as `integer` here")

# Label in a different file
Label.primary(span, "error here", source: "lib/other.ex")

Summary

Functions

Creates a bracket label that highlights a multi-line block.

Returns true if this is a bracket label.

Creates a new label.

Creates a primary (error site) label.

Returns true if this is a primary label.

Returns the resolved span as a Pentiment.Span.t().

Creates a secondary (context) label.

Returns true if this is a secondary label.

Types

priority()

@type priority() :: :primary | :secondary

style()

@type style() :: :inline | :bracket

t()

@type t() :: %Pentiment.Label{
  message: String.t() | nil,
  priority: priority(),
  source: String.t() | nil,
  span: Pentiment.Span.t() | Pentiment.Spannable.t(),
  style: style()
}

Functions

bracket(span, message \\ nil, opts \\ [])

@spec bracket(
  Pentiment.Span.t() | Pentiment.Spannable.t(),
  String.t() | nil,
  keyword()
) :: t()

Creates a bracket label that highlights a multi-line block.

Bracket labels draw a vertical bar () in the left margin across a range of lines, with a ╰── message tail below. The span must cover the full line range (use end_line in the position span).

Examples

iex> Label.bracket(span, "property `no_stuck_states` failed")
%Label{span: span, message: "property `no_stuck_states` failed", style: :bracket}

bracket?(label)

@spec bracket?(t()) :: boolean()

Returns true if this is a bracket label.

new(span, opts \\ [])

@spec new(
  Pentiment.Span.t() | Pentiment.Spannable.t(),
  keyword()
) :: t()

Creates a new label.

Options

  • :message - The annotation message (optional)
  • :priority - :primary or :secondary (default: :primary)
  • :style - :inline or :bracket (default: :inline)
  • :source - Source identifier for multi-file diagnostics (optional)

Examples

iex> Label.new(span, message: "error here")
%Label{span: span, message: "error here", priority: :primary, source: nil}

primary(span, message \\ nil, opts \\ [])

@spec primary(
  Pentiment.Span.t() | Pentiment.Spannable.t(),
  String.t() | nil,
  keyword()
) :: t()

Creates a primary (error site) label.

Primary labels are highlighted prominently and represent the main location of the diagnostic.

Examples

iex> Label.primary(span, "expected `integer`")
%Label{span: span, message: "expected `integer`", priority: :primary}

iex> Label.primary(span)
%Label{span: span, message: nil, priority: :primary}

primary?(label)

@spec primary?(t()) :: boolean()

Returns true if this is a primary label.

resolved_span(label)

@spec resolved_span(t()) :: Pentiment.Span.t()

Returns the resolved span as a Pentiment.Span.t().

If the label's span implements Pentiment.Spannable, it is converted.

secondary(span, message \\ nil, opts \\ [])

@spec secondary(
  Pentiment.Span.t() | Pentiment.Spannable.t(),
  String.t() | nil,
  keyword()
) :: t()

Creates a secondary (context) label.

Secondary labels provide supporting context and are highlighted less prominently than primary labels.

Examples

iex> Label.secondary(span, "declared here")
%Label{span: span, message: "declared here", priority: :secondary}

secondary?(label)

@spec secondary?(t()) :: boolean()

Returns true if this is a secondary label.