Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
This module implements types and functions to track and highlight locations in source files.
Synopsis
- data Pos = Pos {}
- fromOffset :: Monad m => (FilePath -> m Text) -> FilePath -> Int -> m Pos
- data Span = Span {}
- mergeSpan :: Span -> Span -> Span
- displayPos :: Pos -> Text
- renderPos :: Monad m => (FilePath -> m Text) -> Pos -> m Text
- renderPosIO :: Pos -> IO Text
- renderSpan :: Monad m => (FilePath -> m Text) -> Span -> m Text
- renderSpanIO :: Span -> IO Text
Source position
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.
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
mergeSpan :: Span -> Span -> Span Source #
Merge two Span
s 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.
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.