Pentiment.Span.Search (pentiment v0.1.5)

A deferred span that searches for a pattern at format time.

Search spans are resolved when the diagnostic is formatted, using the source content to find the pattern and determine exact positions. This is useful when span positions aren't known at diagnostic creation time, such as in macros where keyword argument positions aren't in the AST.

Fields

  • :line - The starting line number to search from (1-indexed, required)
  • :pattern - The string pattern to find (required)
  • :after_column - Only match patterns starting at or after this column on the first line (optional, defaults to 1)
  • :max_lines - Maximum number of lines to search (optional, defaults to 1)

Resolution

At format time, the search span is resolved to a Position span:

  • If the pattern is found, returns a span covering the match
  • If not found, falls back to a point span at {line, after_column}

Examples

# Search for "hoost:" on line 3
Pentiment.Span.search(line: 3, pattern: "hoost:")

# Search after column 10 (skip earlier matches)
Pentiment.Span.search(line: 3, pattern: "host", after_column: 10)

# Search across multiple lines (for multi-line constructs)
Pentiment.Span.search(line: 3, pattern: "my_key:", max_lines: 5)

Summary

Functions

Creates a new search span.

Resolves a search span against source content, returning a Position span.

Types

t()

@type t() :: %Pentiment.Span.Search{
  after_column: pos_integer(),
  line: pos_integer(),
  max_lines: pos_integer(),
  pattern: String.t()
}

Functions

new(opts)

@spec new(keyword()) :: t()

Creates a new search span.

Options

  • :line - The line number to start searching from (required)
  • :pattern - The pattern to find (required)
  • :after_column - Start searching at this column on the first line (default: 1)
  • :max_lines - Maximum number of lines to search (default: 1)

Examples

iex> Pentiment.Span.Search.new(line: 3, pattern: "hoost:")
%Pentiment.Span.Search{line: 3, pattern: "hoost:", after_column: 1, max_lines: 1}

iex> Pentiment.Span.Search.new(line: 3, pattern: "host", after_column: 10)
%Pentiment.Span.Search{line: 3, pattern: "host", after_column: 10, max_lines: 1}

iex> Pentiment.Span.Search.new(line: 3, pattern: "key:", max_lines: 5)
%Pentiment.Span.Search{line: 3, pattern: "key:", after_column: 1, max_lines: 5}

resolve(search, source)

@spec resolve(t(), Pentiment.Source.t() | nil) :: Pentiment.Span.Position.t()

Resolves a search span against source content, returning a Position span.

Searches for the pattern starting from the specified line, optionally spanning multiple lines. Returns a Position span covering the match, or a point span at {line, after_column} if not found.