snippet-0.1.0.3: src/Snippet/Region.hs
-- |
--
-- Module : Snippet.Region
-- Copyright : (c) 2026 Patrick Brisbin
-- License : AGPL-3
-- Maintainer : pbrisbin@gmail.com
-- Stability : experimental
-- Portability : POSIX
module Snippet.Region
( Region (..)
, regionIsOneLine
-- * Single-line helpers
, lineRegion
, lineRegionFrom
, lineRegionFromTo
) where
import Prelude
import Data.Text (Text)
import GHC.Generics (Generic)
data Region = Region
{ startLine :: Int
-- ^ The line number of the line containing the first character in the region.
, endLine :: Maybe Int
-- ^ The line number of the line containing the last character in the region.
--
-- If missing, defaults to 'startLine'.
, startColumn :: Maybe Int
-- ^ The column number of the first character of the region.
--
-- ^ If missing, defaults to @1@.
, endColumn :: Maybe Int
-- ^ The column number /one past/ the last character in the region.
--
-- If missing defaults to the position after the last character of the line,
-- excluding newline.
, message :: Maybe Text
, subRegion :: Maybe Region
}
deriving stock (Eq, Generic, Show)
regionIsOneLine :: Region -> Bool
regionIsOneLine region = maybe True (== region.startLine) region.endLine
lineRegion :: Int -> Region
lineRegion startLine =
Region
{ startLine
, endLine = Nothing
, startColumn = Nothing
, endColumn = Nothing
, message = Nothing
, subRegion = Nothing
}
lineRegionFrom :: Int -> Int -> Region
lineRegionFrom startLine sc =
Region
{ startLine
, endLine = Nothing
, startColumn = Just sc
, endColumn = Nothing
, message = Nothing
, subRegion = Nothing
}
lineRegionFromTo :: Int -> Int -> Int -> Region
lineRegionFromTo startLine sc ec =
Region
{ startLine
, endLine = Nothing
, startColumn = Just sc
, endColumn = Just ec
, message = Nothing
, subRegion = Nothing
}