Skip to content

Commit 79effae

Browse files
committed
ignore but retain trailing or leading blank lines
1 parent 4f58af3 commit 79effae

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

TODO

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ string-literal
44
* ensure single line works
55
* real tests, empty line handling and round trip
66
- documentation
7-
- ignore but retain trailing or leading blank lines
8-
. only when not --wrapped though
7+
* ignore but retain trailing or leading blank lines
98
- I might want to be able to ignore but preserve leading gunk on the first
109
line, e.g. for
1110
( "blah\

src/StringLiteral.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ main :: IO ()
2727
main = do
2828
let showUsage = putStr usage >> Exit.exitFailure
2929
args <- maybe showUsage return . parseArgs =<< Environment.getArgs
30-
Text.IO.interact $ Text.unlines . process args . Text.lines
30+
Text.IO.interact $ Text.unlines . focusNonEmpty (process args) . Text.lines
3131

3232
data Operation = Add | Remove deriving (Eq, Show)
3333
data Kind = Backslash | Lines deriving (Eq, Show)
@@ -64,6 +64,17 @@ process (wrapped, op, kind) = case (wrapped, op, kind) of
6464
indentation :: Text
6565
indentation = " "
6666

67+
-- | Pass through leading and trailing blank lines.
68+
focusNonEmpty :: ([Text] -> [Text]) -> [Text] -> [Text]
69+
focusNonEmpty f lines = pre ++ f within ++ post
70+
where (pre, within, post) = span2 (Text.null . Text.strip) lines
71+
72+
span2 :: (a -> Bool) -> [a] -> ([a], [a], [a])
73+
span2 f xs = (pre, in2, post)
74+
where
75+
(pre, in1) = span f xs
76+
(in2, post) = List.Extra.spanEnd f in1
77+
6778
-- * backslashes
6879

6980
inferBackslashed :: [Text] -> Bool

0 commit comments

Comments
 (0)