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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module Check.AST.Indent.Data where

import Types

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

import List  (last)

-- applies actual check on Data constructs
checkData :: Decl a -> Int -> CSM ()
checkData e _ =
  case e of
    (DataDecl sI _ _ constr@(l:ls) _) -> checkData' sI constr
    _                                 -> return ()

-- apply check according to declaration type
checkData' :: SpanInfo -> [ConstrDecl] -> CSM ()
checkData' sI (l:ls) = case l of
  (ConstrDecl _ _ _)  -> checkCData sI (l:ls)
  (RecordDecl _ _ _)  -> checkRecord sI (l:ls)
  _                   -> return ()

-- check different formatting ascpects
checkCData :: SpanInfo -> [ConstrDecl] -> CSM ()
checkCData (SpanInfo (Span (Position ls cs) (Position le ce)) sp@(k:(Span (Position l c) _):ks)) (con:cons) =
  unlessM (ls == (getLi (getSpanInfo (last (con:cons))))) --one line
    (if (ls == l) -- first line till equal sign
       then
         (if (not (null (cons))) -- one constructor
            then
              (if (spanAlign (take (length cons) ks)) -- alignment of bars
                 then
                   (if ((null ks) || ((getFirstBar ks) == (cs+2)) || (getFirstBar ks == c)) -- indentation
                      then
                        (unlessM (checkAlign getCol (getCol (getSpanInfo con)) cons) -- alignment of constructors
                          (report (Message (Span (Position ls cs) (Position le ce))
                                           (colorizeKey "data declaration constructors"<+> text "not aligned")
                                           ( text "align"
                                           <+> colorizeKey "constructors"
                                           )
                                  )
                          )
                        )
                      else
                        (report (Message (Span (Position ls cs) (Position le ce))
                                        (colorizeKey "data declaration" <+> text "body wrong indention")
                                        ( text "indent by 2"
                                        <+> colorizeKey "from declaration"
                                        <+> text "or write"
                                        <+> colorizeKey "="
                                        <+> text "in line with data"
                                        )
                                )
                        )
                   )
                 else
                   (report (Message (Span (Position ls cs) (Position le ce))
                                    (colorizeKey "|" <+> text "of data declaration not aligned")
                                    ( text "align"
                                    <+> colorizeKey "|"
                                    )
                           )
                   )
              )
            else
              (report (Message (Span (Position ls cs) (Position le ce))
                               (colorizeKey "data declaration" <+> text "wrong formatting")
                               ( text "if only one constructor, write in one line"
                               )
                      )
              )
         )
       else
         (report (Message (Span (Position ls cs) (Position l c))
                                (colorizeKey "data declaration" <+> text "wrong formatting")
                                ( text "write"
                                <+> colorizeKey "data"
                                <+> text "till"
                                <+> colorizeKey "="
                                <+> text "in one line"
                                )
                        )
                )
    )

-- returns column Position of first |
getFirstBar :: [Span] -> Int
getFirstBar ((Span (Position _ c) _):ks) = c
getFirstBar [] = -1

-- checks various formatting aspects of records
checkRecord :: SpanInfo -> [ConstrDecl] -> CSM ()
checkRecord (SpanInfo (Span (Position ls cs) (Position le ce)) _)
            ((RecordDecl (SpanInfo _ symbs@((Span (Position _ c) _):_)) ident fs):_)
            = if (ls == (getLi (getSpanInfo ident))) -- one line
                then
                  (if (spanAlign symbs) -- alignment symbols { } ,
                     then
                       (if ((c == (cs + 2))||(c == (getCol (getSpanInfo ident)) + 2)) -- indentation of record body
                          then
                            (if (spanAlign (gatherSpanFromFieldDecls getNameSpan fs)) -- alignment names
                               then
                                 (if (spanAlign (gatherSpanFromFieldDecls getSigSpan fs)) -- alignment ::
                                    then
                                      (unlessM (spanAlign (gatherSpanFromFieldDecls getTypeSpan fs)) -- alignment types
                                        (report (Message (Span (Position ls cs) (Position le ce))
                                                         (colorizeKey "Type" <+> text "of record fields not aligned")
                                                         ( text "align"
                                                         <+> colorizeKey "types"
                                                         )
                                                )
                                        )
                                      )
                                    else
                                      (report (Message (Span (Position ls cs) (Position le ce))
                                                       (colorizeKey "::" <+> text "of records fields not aligned")
                                                       ( text "align"
                                                       <+> colorizeKey "::"
                                                       )
                                              )
                                      )
                                 )
                               else
                                (report (Message (Span (Position ls cs) (Position le ce))
                                                 (colorizeKey "names :" <+> text "of record fields not aligned")
                                                 ( text "align"
                                                 <+> colorizeKey "names"
                                                 )
                                        )
                                )
                            )
                          else
                            (report (Message (Span (Position ls cs) (Position le ce))
                                             (colorizeKey "record body" <+> text "wrong indentation")
                                             ( text "indent by"
                                             <+> colorizeKey "2"
                                             <+> text "from record declaration or name of record"
                                             )
                                    )
                            )
                       )
                     else
                       (report (Message (Span (Position ls cs) (Position le ce))
                                        (colorizeKey "record body" <+> text "not aligned")
                                        ( text "align"
                                        <+> colorizeKey "{"
                                        <+> text ","
                                        <+> colorizeKey "}"
                                        <+> text "and"
                                        <+> colorizeKey ","
                                        )
                                )
                        )
                  )
                else
                  (report (Message (Span (Position ls cs) (Position le ce))
                                   (colorizeKey "record declaration" <+> text "wrong formatting")
                                   ( text "write"
                                   <+> colorizeKey "data"
                                   <+> text "till name of record in one line"
                                   )
                          )
                  )

-- pick out span of ::
getSigSpan :: FieldDecl -> Span
getSigSpan (FieldDecl (SpanInfo _ (s:_)) _ _) = s

-- pick out span of name
getNameSpan :: FieldDecl -> Span
getNameSpan (FieldDecl _ ((Ident (SpanInfo s _ ) _ _):_) _) = s

-- pick out span type
getTypeSpan :: FieldDecl -> Span
getTypeSpan (FieldDecl _ _ (ConstructorType (SpanInfo s _) _)) = s

-- using a span picker, construct a list of spans
gatherSpanFromFieldDecls :: (FieldDecl -> Span) -> [FieldDecl] -> [Span]
gatherSpanFromFieldDecls _   []     = []
gatherSpanFromFieldDecls fun (f:fs) = [(fun f)]++(gatherSpanFromFieldDecls fun fs)