|
1 | 1 | {-# LANGUAGE ImportQualifiedPost #-} |
| 2 | +{-# LANGUAGE LambdaCase #-} |
2 | 3 |
|
3 | 4 | {- |
4 | 5 | <TEST> |
@@ -33,17 +34,32 @@ directives = words $ |
33 | 34 | "LANGUAGE OPTIONS_GHC INCLUDE WARNING DEPRECATED MINIMAL INLINE NOINLINE INLINABLE " ++ |
34 | 35 | "CONLIKE LINE SPECIALIZE SPECIALISE UNPACK NOUNPACK SOURCE" |
35 | 36 |
|
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 = |
38 | 60 | traceShow (map (map commentText) xs) |
39 | 61 | xs |
40 | 62 | 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 | | - |
47 | 63 | xs = |
48 | 64 | foldl' |
49 | 65 | (\xs y@(L (anchor -> spanY) _) -> |
@@ -104,14 +120,32 @@ commentHint _ m = |
104 | 120 | -- b) runs of single-line comments |
105 | 121 | -- c) single-line comments |
106 | 122 | -- 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 |
109 | 132 | else concatMap (check singleLines someLines) comments |
110 | 133 | 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 | + |
112 | 140 | singleLines = sort $ commentLine <$> filter isSingle comments |
113 | 141 | 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) |
115 | 149 |
|
116 | 150 | -- | Does the commment start with "--"? Can be empty. Excludes haddock single |
117 | 151 | -- line comments, "-- |" and "-- ^". |
|
0 commit comments