diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-08-13 13:07:42 +0900 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-08-21 16:03:53 +0900 |
commit | 735c1a72ccdb4e10ad4f50138dc7c726a8494424 (patch) | |
tree | 6f78869a10134ea470b7a17ece16e952113cbbcd | |
parent | 1963d343e94c03ce9317cf0806265be876300cca (diff) | |
download | stylish-haskell-735c1a72ccdb4e10ad4f50138dc7c726a8494424.tar.gz |
Somewhat more general alignment
-rw-r--r-- | src/StylishHaskell/Step/Records.hs | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/src/StylishHaskell/Step/Records.hs b/src/StylishHaskell/Step/Records.hs index f9c7348..3b65e31 100644 --- a/src/StylishHaskell/Step/Records.hs +++ b/src/StylishHaskell/Step/Records.hs @@ -28,34 +28,30 @@ records modu = -------------------------------------------------------------------------------- -- | Align the type of a field -alignType :: Int -> H.FieldDecl H.SrcSpan -> Change String -alignType longest (H.FieldDecl srcSpan _ _) = - changeLine (H.srcSpanStartLine srcSpan) alignType' +align :: [(Int, Int)] -> [Change String] +align alignment = map align' alignment where - alignType' str = - let (pre, post) = break (== ':') str - in [padRight longest (trimRight pre) ++ post] + longest = maximum $ map snd alignment - trimRight = reverse . dropWhile isSpace . reverse + align' (line, column) = changeLine line $ \str -> + let (pre, post) = splitAt column str + in [padRight longest (trimRight pre) ++ trimLeft post] + + trimLeft = dropWhile isSpace + trimRight = reverse . trimLeft . reverse -------------------------------------------------------------------------------- --- | Find the length of the longest field name in a record -longestFieldName :: [H.FieldDecl H.SrcSpan] -> Int -longestFieldName fields = maximum - [ H.srcSpanEndColumn (H.ann name) +-- | Determine alignment of fields +fieldAlignment :: [H.FieldDecl H.SrcSpan] -> [(Int, Int)] +fieldAlignment fields = + [ (H.srcSpanStartLine ann, H.srcSpanEndColumn ann) | H.FieldDecl _ names _ <- fields - , name <- names + , let ann = H.ann (last names) ] -------------------------------------------------------------------------------- --- | Align all fields in a record -alignRecord :: [H.FieldDecl H.SrcSpan] -> [Change String] -alignRecord fields = map (alignType $ longestFieldName fields) fields - - --------------------------------------------------------------------------------- -- | Checks that all no field of the record appears on more than one line, -- amonst other things fixable :: [H.FieldDecl H.SrcSpan] -> Bool @@ -72,4 +68,4 @@ step :: Step step = makeStep "Records" $ \ls (module', _) -> let module'' = fmap H.srcInfoSpan module' fixableRecords = filter fixable $ records module'' - in applyChanges (fixableRecords >>= alignRecord) ls + in applyChanges (fixableRecords >>= align . fieldAlignment) ls |