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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module Check.AST.Indent.Guard where

import Curry.SpanInfo
import Curry.Span
import Curry.Position
import Curry.Types
import Text.Pretty

import Types

-- aplies actual check on Guards, the function lefthand side is needed
-- for positions
checkGuard :: Equation a -> Int -> CSM ()
checkGuard e i =
  case e of
    (Equation _ (FunLhs fSI _ _) (GuardedRhs gSI cExprs _))
                     -> checkGuard' fSI gSI cExprs i
    _                -> return ()

-- if bars are aligned check their indent, else report
-- same for equal signs, if one starts in same line as its guard,
-- disregard indentation
checkGuard' ::SpanInfo -> SpanInfo -> [CondExpr a] -> Int -> CSM ()
checkGuard' (SpanInfo (Span (Position lf _) _) _)
            sI
            (cExpr:cExprs) i = do
  if checkAlign getCol (getCol (getSpanInfo cExpr)) cExprs
    then checkGuardIndent sI lf cExpr i
    else report (Message (getSpan sI)
                  (colorizeKey "guards" <+> text "not aligned")
                  (text "align" <+> colorizeKey "guards"))
  if checkAlign getEqualCol (getEqualCol (getSpanInfo cExpr)) cExprs
    then (unlessM (guardsEqualSSameLine (cExpr:cExprs)) (checkEqualIndent sI cExpr))
    else report (Message (getSpan sI)
                  (colorizeKey "guard" <+> text "equal signs not aligned")
                  (text "align" <+> colorizeKey "guard" <+> text "equal signs"))

-- check if they are indented by 2 from edge or start in same line as functionlhs
checkGuardIndent :: SpanInfo -> Int -> CondExpr a -> Int -> CSM ()
checkGuardIndent sI f cExpr i =
  let posCExprEq = (getSpanInfo cExpr)
      l = getLi sI
      c = getCol sI
  in
    unlessM ((getCol posCExprEq) == (i+2))
      (unlessM ((getLi posCExprEq) == f)
        $ report (Message (getSpan sI)
                     (colorizeKey "guards" <+> text "wrong indentation")
                     (text "indent by 2 from" <+> colorizeKey "outer block")))

-- check if one equal sign continues in the same line as its guard
guardsEqualSSameLine :: [CondExpr a] -> Bool
guardsEqualSSameLine (cExpr:cExprs) =
  (getLi (getSpanInfo cExpr) == (getEqualLi (getSpanInfo cExpr))) || guardsEqualSSameLine cExprs
guardsEqualSSameLine []             = True

-- check if they are aligned with guards
checkEqualIndent :: SpanInfo -> CondExpr a -> CSM ()
checkEqualIndent sI cExpr =
  let posCExprEq = (getSpanInfo cExpr)
      l = getLi sI
  in unlessM ((getLi posCExprEq) == l)
             $ report (Message (getSpan sI) (colorizeKey "equal signs" <+> text "wrong indentation")
                                            (text "indent by 2 from" <+> colorizeKey "outer block"))

-- get column position of equal sign from spaninfo
getEqualCol :: SpanInfo -> Int
getEqualCol (SpanInfo _ [ _ , Span (Position _ c) _]) = c

-- get line position of equal sign from spaninfo
getEqualLi :: SpanInfo -> Int
getEqualLi (SpanInfo _ [ _ , Span (Position l _) _]) = l