Pentiment.Diagnostic protocol (pentiment v0.1.5)

Protocol for types that can be rendered as diagnostics.

Implement this protocol to enable any struct to be formatted by Pentiment. This is useful when you want to define domain-specific error types that integrate with Pentiment's rendering.

Example Implementation

defmodule MyApp.TypeError do
  defstruct [:expected, :actual, :location, :file]

  defimpl Pentiment.Diagnostic do
    def message(%{expected: e, actual: a}) do
      "Expected `#{e}`, found `#{a}`"
    end

    def code(_), do: "T001"
    def severity(_), do: :error
    def source(%{file: f}), do: f

    def labels(%{location: {line, col}, actual: a}) do
      [Pentiment.Label.primary(Pentiment.Span.position(line, col), "has type `#{a}`")]
    end

    def help(_), do: ["Check your types"]
    def notes(_), do: []
  end
end

Required Callbacks

All callbacks have default implementations that return sensible defaults, but you should implement at least message/1 and labels/1 for useful output.

Summary

Types

t()

All the types that implement this protocol.

Functions

Returns an optional error code (e.g., "E0001", "PARSE001").

Returns a list of help/suggestion messages.

Returns a list of labeled spans to highlight in the source.

Returns the main diagnostic message.

Returns a list of additional notes.

Returns the severity level.

Returns the primary source identifier for this diagnostic.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

code(diagnostic)

@spec code(t()) :: String.t() | nil

Returns an optional error code (e.g., "E0001", "PARSE001").

Error codes help users look up documentation for specific errors. Return nil if no code is applicable.

help(diagnostic)

@spec help(t()) :: [String.t()]

Returns a list of help/suggestion messages.

These are actionable suggestions for fixing the issue, displayed with a "help:" prefix.

labels(diagnostic)

@spec labels(t()) :: [Pentiment.Label.t()]

Returns a list of labeled spans to highlight in the source.

Labels annotate specific regions of source code with messages. An empty list means no source highlighting.

message(diagnostic)

@spec message(t()) :: String.t()

Returns the main diagnostic message.

This is the primary description of the error/warning shown in the header.

notes(diagnostic)

@spec notes(t()) :: [String.t()]

Returns a list of additional notes.

Notes provide extra context about the error, displayed with a "note:" prefix.

severity(diagnostic)

@spec severity(t()) :: :error | :warning | :info | :hint

Returns the severity level.

  • :error - A problem that must be fixed
  • :warning - A potential issue that should be reviewed
  • :info - Informational message
  • :hint - Suggestion for improvement

source(diagnostic)

@spec source(t()) :: String.t() | nil

Returns the primary source identifier for this diagnostic.

This is typically a file path or source name. Return nil if no specific source is associated with the diagnostic.