DevactionUI.Highlight (devaction_ui v0.2.3)

Copy Markdown View Source

Splits HEEx or Elixir source into tokens a code block can colour: {kind, text} pairs whose texts, joined, are the source again. It is cheap and never fails: what it does not know, or what is cut short, is plain :text.

heex/1 reads the shapes of a template (tags, components, slots, attributes, strings in single or double quotes, {} and <% %> expressions, <!-- --> and <%!-- --%> comments). An expression is not one piece: its delimiters ({, }, <%=, %>) are :expr and what is inside goes through elixir/1. The special attributes of HEEx (:if, :for, :let, :key and every phx-*) are :directive, apart from the plain :attr. It is what CodeBlock runs for lang: :heex.

elixir/1 is a small lexer, not a parser: comments, strings, atoms and keyword keys, numbers, @assigns, module names and reserved words; the rest is :text. Sigils, heredocs and charlists are not told apart: their quotes are strings and the rest of a sigil is :text. It is what CodeBlock runs for lang: :elixir.

lines/1 cuts a token list at its line breaks, for the blocks that number or mark lines.

Kinds: :punct, :html, :component, :slot, :attr, :directive, :string, :expr, :comment, :keyword, :atom, :number, :module, :assign and :text.

Summary

Functions

The tokens of an Elixir source.

The tokens of a HEEx source.

The tokens cut at their line breaks: a list of lines, each a list of tokens, without the breaks.

Types

kind()

@type kind() ::
  :punct
  | :html
  | :component
  | :slot
  | :attr
  | :directive
  | :string
  | :expr
  | :comment
  | :keyword
  | :atom
  | :number
  | :module
  | :assign
  | :text

token()

@type token() :: {kind(), String.t()}

Functions

elixir(source)

@spec elixir(String.t()) :: [token()]

The tokens of an Elixir source.

heex(source)

@spec heex(String.t()) :: [token()]

The tokens of a HEEx source.

lines(tokens)

@spec lines([token()]) :: [[token()]]

The tokens cut at their line breaks: a list of lines, each a list of tokens, without the breaks.