Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add module for pretty-printing blocks of code

  • Loading branch information...
commit 15e196c3f7e94ba05f14697b3f2ceebbad1caaa3 1 parent 66136af
@jaspervdj jaspervdj authored
Showing with 49 additions and 0 deletions.
  1. +49 −0 src/HStyle/Block.hs
View
49 src/HStyle/Block.hs
@@ -0,0 +1,49 @@
+-- | A block of code
+{-# LANGUAGE OverloadedStrings #-}
+module HStyle.Block
+ ( Block
+ , fromText
+ , prettyBlock
+ , toLines
+ , subBlock
+ ) where
+
+import Data.Text (Text)
+import Data.Vector (Vector)
+import qualified Data.Vector as V
+import qualified Data.Text as T
+
+data Block = Block
+ { blockOffset :: Int
+ , blockLines :: Vector Text
+ } deriving (Show)
+
+fromText :: Text -> Block
+fromText text = Block
+ { blockOffset = 0
+ , blockLines = V.fromList $ T.lines text
+ }
+
+prettyBlock :: Block -> Text
+prettyBlock block = T.unlines $ map pretty $
+ zip [offset + 1 ..] $ V.toList lines'
+ where
+ offset = blockOffset block
+ lines' = blockLines block
+ width = length $ show (offset + V.length lines')
+
+ pretty (ln, t) =
+ let ln' = T.pack (show ln)
+ lnl = T.length ln'
+ in T.replicate (width - lnl) " " `T.append`
+ ln' `T.append` " " `T.append` t
+
+toLines :: Block -> [Text]
+toLines = V.toList . blockLines
+
+-- | Subblock from start to end -- including both.
+subBlock :: Int -> Int -> Block -> Block
+subBlock start end block = Block
+ { blockOffset = blockOffset block + start - 1
+ , blockLines = V.slice (start - 1) (end - start + 1) $ blockLines block
+ }
Please sign in to comment.
Something went wrong with that request. Please try again.