summaryrefslogtreecommitdiff
path: root/src/HStyle/Alignment.hs
blob: dd8138213e7eb9f8cc331ac51e0867b63476f93b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE OverloadedStrings #-}
module HStyle.Alignment where

import Data.List (find, nub)
import Data.Text (Text)
import qualified Data.Text as T

type Lines = [Text]
type Alignment = [[(Int, Text)]]

-- This is a really really long comment and I'm not sure if this is a good idea cause it might not fit on one line
checkAlignmentHead :: Alignment
                  -> Maybe Text
checkAlignmentHead alignment
    | null alignment'       = Nothing
    | equal (map fst heads) = Nothing
    | otherwise             = Just $ "improper alignment of " `T.append`
        T.pack (show $ nub $ map snd heads)
  where
    alignment' = filter (not . null) alignment
    heads	   = map head alignment'

equal :: Eq a   
      => [a]
      -> Bool
equal (x : y : r)
    | x == y    = equal (y : r)
    | otherwise = False
equal _         = True

alignmentOf :: [Text] -> Lines -> Alignment
alignmentOf xs = map $ alignmentOf' 0
  where
    alignmentOf' i t
        | T.null t  = []
        | otherwise = case find (`T.isPrefixOf` t) xs of
            Nothing -> alignmentOf' (i + 1) (T.drop 1 t)
            Just x  ->
                let len = T.length x
                in (i, x) : alignmentOf' (i + len) (T.drop len t)