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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
module Check.AST.Indent.IfThenElse where

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

import Types

-- per patternmatching apply actual check only on ifthenelse constructs
checkIfThenElse :: Expression a -> Int -> CSM ()
checkIfThenElse e _ =
  case e of
    (IfThenElse sI expr1 expr2 expr3) -> checkIfThenElse' sI
                                                          (getSpanInfo expr1)
                                                          (getSpanInfo expr2)
                                                          (getSpanInfo expr3)
    _                                 -> return ()

-- check if various situations:
-- all in one line
-- if - else in one line, else expression not
-- if - then in one line, else and else expression in another -> twolines
-- if - then in one line, then expression and else in one
-- if and then not in one line, if expression and then in one
-- if then else in different lines, if and then expressions not right in front
-- if then and else
checkIfThenElse' :: SpanInfo -> SpanInfo -> SpanInfo -> SpanInfo -> CSM ()
checkIfThenElse'
  (SpanInfo
    sp
    [ Span (Position lpi pi) _
    , Span (Position lpt pt) _
    , Span (Position lpe pe) _
    ]
  ) sI1@(SpanInfo (Span _ (Position li ci)) _)
    sI2@(SpanInfo (Span _ (Position lt ct)) _)
    sI3@(SpanInfo (Span _ (Position le ce)) _)
  |lpi == le               = return ()
  |lpi == lpe              = do report (Message
                                         sp
                                         ( text "wrong formatting"
                                           <+> colorizeKey "else expression"
                                         )
                                         ( text "do not break"
                                           <+> colorizeKey " else expression"
                                           <+> text "if writing"
                                           <+> colorizeKey "if-then-else"
                                           <+> text "in one line"
                                         ))
  |lpi == lpt && lt /= lpe = do checkIfThenElseInTwoLines sp pt pe
                                checkBreakIndent "then" lpt pt sI2
                                checkBreakIndent "else" lpe pe sI3
  |lt == lpe               = do report (Message
                                         sp
                                         ( text "wrong formatting"
                                           <+> colorizeKey "else"
                                         )
                                         ( colorizeKey "else"
                                           <+> text " should start in seperate line"
                                         ))
  |li == lpt               = do report (Message
                                         sp
                                         ( text "wrong formatting"
                                           <+> colorizeKey "then"
                                         )
                                         ( colorizeKey "then"
                                           <+> text " should start in seperate line"
                                         ))
  |li /= lpt && lt /= lpe  = do checkIfThenElseInThreeLines sp pi pt pe
                                checkBreakIndent "if" lpi pi sI1
                                checkBreakIndent "then" lpt pt sI2
                                checkBreakIndent "else" lpe pe sI3
  -- |otherwise

-- then and else should alway be aligned
checkIfThenElseInTwoLines :: Span -> Int -> Int -> CSM ()
checkIfThenElseInTwoLines sp pt pe =
  unlessM (pt == pe) (report (Message
                                sp
                                ( colorizeKey "then"
                                  <+> text "and"
                                  <+> colorizeKey "else"
                                  <+> text "wrong alignement"
                                  )
                                ( text "align"
                                  <+> colorizeKey "then"
                                  <+> text "and"
                                  <+> colorizeKey "else"
                                  )
                              )
                      )

-- see above, but also, if then starts in another line, indent by 2
checkIfThenElseInThreeLines :: Span -> Int -> Int -> Int -> CSM ()
checkIfThenElseInThreeLines sp pi pt pe =
  if pt == pe
    then unlessM (pe-pi == 2) (report (Message
                                        sp
                                        ( colorizeKey "then"
                                          <+> text "and"
                                          <+> colorizeKey "else"
                                          <+> text "not properly indented"
                                        )
                                        ( text "indent by 2 from"
                                          <+> colorizeKey "if"
                                        )
                                      )
                              )
    else report (Message
                    sp
                    ( colorizeKey "then"
                      <+> text "and"
                      <+> colorizeKey "else"
                      <+> text "not aligned"
                      )
                      ( text "align"
                        <+> colorizeKey "then"
                        <+> text "and"
                        <+> colorizeKey "else"
                        )
                  )

-- gets keyname as string, keypositions and the expression to check
-- if the expression is in next line, check indent
checkBreakIndent :: String -> Int -> Int -> SpanInfo -> CSM ()
checkBreakIndent s l c sI = do
  unlessM (l == (getLi sI))
          (unlessM ((getCol sI)==(c+2))
                   (report (Message
                             (getSpan sI)
                             ( colorizeKey "expression"
                               <+> text "wrongly indented"
                             )
                             ( colorizeKey(s ++ " expression")
                               <+> text "should be indented by 2 from"
                               <+> colorizeKey s
                             )
                           )
                  )
          )