Add longer doc/Refactor stripWhitespace'

pull/187/head
Lennart Spitzner 2018-10-11 20:14:29 +02:00
parent 66fd44058d
commit 38216cdc02
1 changed files with 48 additions and 18 deletions

View File

@ -668,23 +668,53 @@ layoutClsInst lcid@(L _ cid) = docLines
BDFExternal ann anns b $ stripWhitespace' t BDFExternal ann anns b $ stripWhitespace' t
stripWhitespace b = b stripWhitespace b = b
-- | We strip the first newline from each @data@/@type@ declaration. If the -- | This fixes two issues of output coming from Exactprinting
-- @data@/@type@ is the first declaration in the instance, then we also have -- associated (data) type decls. Firstly we place the output into docLines,
-- to strip whitespace from the start of the comments and the first line of -- so one newline coming from Exactprint is superfluous, so we drop the
-- the declaration. This is brittle and should be replaced by proper -- first (empty) line. The second issue is Exactprint indents the first
-- layouting -- member in a strange fashion:
-- as soon as possible. --
-- input:
--
-- > instance MyClass Int where
-- > -- | This data is very important
-- > data MyData = IntData
-- > { intData :: String
-- > , intData2 :: Int
-- > }
--
-- output of just exactprinting the associated data type syntax node
--
-- >
-- > -- | This data is very important
-- > data MyData = IntData
-- > { intData :: String
-- > , intData2 :: Int
-- > }
--
-- To fix this, we strip whitespace from the start of the comments and the
-- first line of the declaration, stopping when we see "data" or "type" at
-- the start of a line. I.e., this function yields
--
-- > -- | This data is very important
-- > data MyData = IntData
-- > { intData :: String
-- > , intData2 :: Int
-- > }
--
-- Downside apart from being a hacky and brittle fix is that this removes
-- possible additional indentation from comments before the first member.
--
-- But the whole thing is just a temporary measure until brittany learns
-- to layout data/type decls.
stripWhitespace' :: Text -> Text stripWhitespace' :: Text -> Text
stripWhitespace' t = stripWhitespace' t =
let Text.intercalate (Text.pack "\n") $ go $ List.drop 1 $ Text.lines t
where
go [] = []
go (line1 : lineR) = case Text.stripStart line1 of
st | isTypeOrData st -> st : lineR
| otherwise -> st : go lineR
isTypeOrData t' = isTypeOrData t' =
Text.pack "type" (Text.pack "type" `Text.isPrefixOf` t')
`Text.isPrefixOf` t' || (Text.pack "data" `Text.isPrefixOf` t')
|| Text.pack "data"
`Text.isPrefixOf` t'
(comments, dat : rest) =
break (isTypeOrData . Text.stripStart) (Text.lines (Text.tail t))
in Text.init
$ Text.unlines
$ fmap Text.stripStart comments
++ (Text.stripStart dat : rest)