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
module Check.AST.TopLevel.BlankLines where

import Text.Pretty

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

import Types

-- applies actual check on Modules
checkBlankLines :: Module a -> Int-> CSM ()
checkBlankLines e _ =
  case e of
    (Module sI _ _ _ _ decls) -> checkBlankLines' sI decls
    _                         -> return ()

--checks if there is at least one blank line between TopLevel Declarations,
--case its a typesignature, there is no need to check, else check
checkBlankLines' :: SpanInfo -> [Decl a] -> CSM ()
checkBlankLines' _ []                   = return ()
checkBlankLines' sI (decl:[])           = return () --noBlank sI decl
checkBlankLines' sI (decl1:decl2:decls) =
  case decl1 of
    (TypeSig _ _ _)     -> checkBlankLines' sI (decl2:decls)
    (InfixDecl _ _ _ _) -> case decl2 of
                           (InfixDecl _ _ _ _) ->  checkBlankLines' sI (decl2:decls)
                           _                   ->  do blankLine decl1 decl2
                                                      checkBlankLines' sI (decl2:decls)
    _                   -> do blankLine decl1 decl2
                              checkBlankLines' sI (decl2:decls)

-- see if the difference between start and end of the two declarations is at
-- least one (This check doesn't work if there are comments in between)
blankLine :: Decl a -> Decl a -> CSM ()
blankLine decl1 decl2 = do
  let sI1@(SpanInfo (Span _ (Position l c)) _) = getSpanInfo decl1
      sI2 = getSpanInfo decl2
  unlessM ((getLi sI2) - l > 1)
          (report (Message
                    (getSpan sI2)
                    ( colorizeKey "Blank Line" <+> text "missing")
                    ( text "leave a"
                      <+> colorizeKey "blank line"
                      <+> text "between"
                      <+> colorizeKey "top level declarations")
                  )
          )

-- not used yet, checking for "trailing blanklines"
noBlank :: SpanInfo -> Decl a -> CSM ()
noBlank sI decl = do
  let sID = getSpanInfo decl
  unlessM ((getEndLi sI) == (getEndLi sID))
          (report (Message
                    (getSpan sI)
                    ( colorizeKey "blank line(s)"
                      <+> text "at the end of"
                      <+> colorizeKey "module"
                    )
                    (text "delete superfluous blank line(s) at end of module" )
                  )
          )

-- returns line of end position from Spaninfo
getEndLi :: SpanInfo -> Int
getEndLi (SpanInfo (Span _ (Position l _)) _) = l