pretty-loc-0.1.0.1: Tracking and highlighting of locations in source files

Safe HaskellSafe
LanguageHaskell2010

Data.Text.Prettyprint.Location

Contents

Description

This module implements types and functions to track and highlight locations in source files.

Synopsis

Source position

data Pos Source #

A point in a source file as a line number, a column number and character offset from the beginning of the file. The line and column number start at 1, the offset starts at 0.

Constructors

Pos 

Fields

Instances
Eq Pos Source #

Two positions are equal if they point in the same file with the same offset.

Instance details

Defined in Data.Text.Prettyprint.Location

Methods

(==) :: Pos -> Pos -> Bool #

(/=) :: Pos -> Pos -> Bool #

Show Pos Source # 
Instance details

Defined in Data.Text.Prettyprint.Location

Methods

showsPrec :: Int -> Pos -> ShowS #

show :: Pos -> String #

showList :: [Pos] -> ShowS #

fromOffset :: Monad m => (FilePath -> m Text) -> FilePath -> Int -> m Pos Source #

Constructs a complete Pos from the source file and a character offset. Will throw an error if the file is too short.

The function passed as first argument is used to retrieve the text of the source file. It can be made, for example, to work with a cache to avoid reading the same source file multiple times.

Source span

data Span Source #

A piece of source file, starting at spanStart and spanning spanLen characters.

Constructors

Span 

Fields

Instances
Eq Span Source # 
Instance details

Defined in Data.Text.Prettyprint.Location

Methods

(==) :: Span -> Span -> Bool #

(/=) :: Span -> Span -> Bool #

Show Span Source # 
Instance details

Defined in Data.Text.Prettyprint.Location

Methods

showsPrec :: Int -> Span -> ShowS #

show :: Span -> String #

showList :: [Span] -> ShowS #

Semigroup Span Source # 
Instance details

Defined in Data.Text.Prettyprint.Location

Methods

(<>) :: Span -> Span -> Span #

sconcat :: NonEmpty Span -> Span #

stimes :: Integral b => b -> Span -> Span #

mergeSpan :: Span -> Span -> Span Source #

Merge two Spans into one covering both.

For example, merging:

int main () { mldkd; }
    ^^^^

and:

int main () { mkdkd; }
              ^^^^^

results in:

int main () { mkdkd; }
    ^^^^^^^^^^^^^^^

Rendering

displayPos :: Pos -> Text Source #

Shows a Pos in a standard format.

>>> displayPos Pos { posFile = "Foo.hs", posLine = 8, posCol = 3, posOffset = 54 }
"Foo.hs:8:3"

renderPos :: Monad m => (FilePath -> m Text) -> Pos -> m Text Source #

Highlights a point in the source code with a caret, like so:

int main () { mldkd; }
              ^

The function passed as first argument is used to retrieve the text of the source file. It can be used, for example, to work with a cache to avoid reading the same source file multiple times.

renderPosIO :: Pos -> IO Text Source #

Specialised version of renderPos that uses readFile to retrieve the source text.

renderSpan :: Monad m => (FilePath -> m Text) -> Span -> m Text Source #

Highlights the piece of source code designated by the Span.

If it spans a single line, it is rendered like so:

int main () { mldkd; }
              ^^^^^

If it spans multiple lines, it is rendered like so:

> int main() {
>  int x = 0;
>  int y = 1;
>  foo(x,y);
> }

It may throw an error if the source file is too short.

The function passed as first argument is used to retrieve the text of the source file. It can be used, for example, to work with a cache to avoid reading the same source file multiple times.

renderSpanIO :: Span -> IO Text Source #

Specialised version of renderSpan using readFile to retrieve the source text.