snippet: Pretty-print regions within source code files

[ agpl, data, library ] [ Propose Tags ] [ Report a vulnerability ]

Pretty-print Regions of source-code, i.e. snippets.

This is meant to mimic tooling available in rust and used by tools such as sarif-fmt.

For example, focusing on a region within our README:

   ┌─ README.md:32:8
   │
32 │   main = do
   │ ╭────────^
33 │ │   let
34 │ │     region = Region
35 │ │       { startLine = 32
36 │ │       , endLine = Just 44
37 │ │       , startColumn = Just 8
38 │ │       , endColumn = Nothing
39 │ │       }
40 │ │
41 │ │   -- Returns Nothing if file is missing/empty or region is invalid
42 │ │   mSnippet <- snippetFromFile region "README.md"
43 │ │
44 │ │   traverse_ (putDoc . reAnnotate annToAnsi . prettySnippet) mSnippet
   │ ╰────────────────────────────────────────────────────────────────────^

To see the code that produces this, and an image of the output (e.g. with color), view the README on Codeberg.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.0.0, 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.2.0.0, 0.2.0.1
Change log CHANGELOG.md
Dependencies base (>=4.16.4.0 && <5), directory (>=1.3.6.2), prettyprinter (>=1.7.1), prettyprinter-ansi-terminal (>=1.1.3), text (>=1.2.5.0) [details]
License AGPL-3.0-only
Author
Maintainer Patrick Brisbin
Uploaded by PatrickBrisbin at 2026-11-03T14:51:46Z
Category Data
Home page https://codeberg.org/pbrisbin/snippet#readme
Bug tracker https://codeberg.org/pbrisbin/snippet/issues
Source repo head: git clone https://codeberg.org/pbrisbin/snippet
Distributions
Downloads 21 total (21 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2026-11-03 [all 1 reports]

Readme for snippet-0.2.0.1

[back to package description]

Snippet

Release Hackage Stackage LTS Stackage Nightly CI

Pretty-print regions within source code files.

Usage

import Prelude

import Data.Foldable (traverse_)
import Prettyprinter (reAnnotate)
import Prettyprinter.Render.Terminal (putDoc)
import Snippet
import Snippet.Pretty

main :: IO ()
main = do
  let
    region = Region
      { startLine = 32
      , startColumn = Just 8
      , endLine = Just 64
      , endColumn = Nothing
      , message = Nothing
      , subRegion =
          Just
            Region
              { startLine = 34
              , startColumn = Just 5
              , endLine = Just 59
              , endColumn = Nothing
              , message = Just "Denotes a region in the snippet"
              , subRegion =
                  Just
                    Region
                      { startLine = 45
                      , startColumn = Just 27
                      , endLine = Nothing
                      , endColumn = Just 31
                      , message = Just "Would default to startLine"
                      , subRegion = Nothing
                      }
              }
      }

  -- Returns Nothing if file is missing/empty or region is invalid
  mSnippet <- snippetFromFile region "README.md"

  traverse_ (putDoc . reAnnotate annToAnsi . prettySnippet) mSnippet

The above Region focuses one of the Just constructors, within the region binding, within the body of the do block. Rendering it with the default annToAnsi looks like:

Supplying an alternative annToAnsi function is how you can adjust colors.

Limitations

Drawing these regions is not an easy problem. Therefore, there are some limitations to the kind of Region values we support:

  1. One top-level region per snippet,
  2. A region may only have zero or one sub-region,
  3. A sub-region must be contained within its parent, and
  4. A sub-region cannot share start or end lines or columns with its parent

One notable consequence is that a one-line region cannot contain sub-regions.

License

This package is licensed AGPLv3. See COPYING.