Skip to content

Commit 65a87b1

Browse files
committed
Add classify comments
- Partition pragmas - Partition haddocks
1 parent fdd100e commit 65a87b1

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

src/Hint/Comment.hs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE ImportQualifiedPost #-}
2+
{-# LANGUAGE LambdaCase #-}
23

34
{-
45
<TEST>
@@ -33,17 +34,32 @@ directives = words $
3334
"LANGUAGE OPTIONS_GHC INCLUDE WARNING DEPRECATED MINIMAL INLINE NOINLINE INLINABLE " ++
3435
"CONLIKE LINE SPECIALIZE SPECIALISE UNPACK NOUNPACK SOURCE"
3536

36-
commentRuns :: ModuleEx -> [[LEpaComment]]
37-
commentRuns m =
37+
data Comments = Comments
38+
{ commPragma :: ![LEpaComment]
39+
, commBlockHaddocks :: ![LEpaComment]
40+
, commBlocks :: ![LEpaComment]
41+
-- TODO: Process the different types of block comments; [" |",""].
42+
-- * Haddock comments
43+
-- * Simple comments
44+
, commRunHaddocks :: ![[LEpaComment]]
45+
, commRuns :: ![[LEpaComment]]
46+
, commLineHaddocks :: ![LEpaComment]
47+
, commLines :: ![LEpaComment]
48+
}
49+
50+
classifyComments :: [LEpaComment] -> Comments
51+
classifyComments xs = Comments pragmas blockHaddocks blocks runHaddocks runs lineHaddocks lines where
52+
(partition isCommentPragma -> (pragmas, allBlocks), singles) = partition isCommentMultiline xs
53+
(blockHaddocks, blocks) = partition isCommentHaddock allBlocks
54+
(concat -> singles', rawRuns) = partition ((== 1) . length) $ commentRuns singles
55+
(runHaddocks, runs) = partition (\case x : _ -> isCommentHaddock x; _ -> False) rawRuns
56+
(lineHaddocks, lines) = partition isCommentHaddock singles'
57+
58+
commentRuns :: [LEpaComment] -> [[LEpaComment]]
59+
commentRuns comments =
3860
traceShow (map (map commentText) xs)
3961
xs
4062
where
41-
-- Comments need to be sorted by line number for detecting runs of single
42-
-- line comments but @ghcComments@ doesn't always do that even though most
43-
-- of the time it seems to.
44-
comments :: [LEpaComment]
45-
comments = sortOn (\(L (anchor -> span) _) -> srcSpanStartLine span) $ ghcComments m
46-
4763
xs =
4864
foldl'
4965
(\xs y@(L (anchor -> spanY) _) ->
@@ -104,14 +120,32 @@ commentHint _ m =
104120
-- b) runs of single-line comments
105121
-- c) single-line comments
106122
-- TODO: Remove (True, _) runs and then run the other checks on the rest.
107-
if any fst runs
108-
then concatMap snd runs
123+
traceShow ("pragmas", commentText <$> pragmas) $
124+
traceShow ("blockHaddocks", commentText <$> blockHaddocks) $
125+
traceShow ("blocks", commentText <$> blocks) $
126+
traceShow ("runHaddocks", fmap commentText <$> runHaddocks) $
127+
traceShow ("runs", fmap commentText <$> runs) $
128+
traceShow ("lineHaddocks", commentText <$> lineHaddocks) $
129+
traceShow ("lines", commentText <$> lines) $
130+
if any fst runReplacements
131+
then concatMap snd runReplacements
109132
else concatMap (check singleLines someLines) comments
110133
where
111-
comments = ghcComments m
134+
-- Comments need to be sorted by line number for detecting runs of single
135+
-- line comments but @ghcComments@ doesn't always do that even though most
136+
-- of the time it seems to.
137+
comments :: [LEpaComment]
138+
comments = sortOn (\(L (anchor -> span) _) -> srcSpanStartLine span) $ ghcComments m
139+
112140
singleLines = sort $ commentLine <$> filter isSingle comments
113141
someLines = sort $ commentLine <$> filter isSingleSome comments
114-
runs = dropBlankLinesHint <$> commentRuns m
142+
143+
Comments pragmas blockHaddocks blocks runHaddocks runs lineHaddocks lines = classifyComments comments
144+
145+
runReplacements =
146+
(dropBlankLinesHint <$> runHaddocks)
147+
++
148+
(dropBlankLinesHint <$> runs)
115149

116150
-- | Does the commment start with "--"? Can be empty. Excludes haddock single
117151
-- line comments, "-- |" and "-- ^".

0 commit comments

Comments
 (0)