on master help sign in
Define ‘attachRelativePos’ in ‘Ormolu.Utils’, refactor
commit70978c6a9cfff967f733681f13ab6fce2a620152 authorMark Karpov <markkarpov92@gmail.com> dateWed 22 Apr 2020, 14:53 (7 years ago) parentec1a432
3 files changed+54−23
src/Ormolu/Printer/Meat/Declaration/Value.hs+10−7 src/Ormolu/Printer/Meat/ImportExport.hs+22−15 src/Ormolu/Utils.hs+22−1
src/Ormolu/Printer/Meat/Declaration/Value.hs+10−7
@@ -473,19 +473,22 @@ p_hsLocalBinds = \case
473473 (Left <$> bagToList bag) ++ (Right <$> lsigs)
474474 p_item (Left x) = located x p_valDecl
475475 p_item (Right x) = located x p_sigDecl
476− -- Assigns 'False' to the last element, 'True' to the rest.
477− markInit :: [a] -> [(Bool, a)]
478− markInit [] = []
479− markInit [x] = [(False, x)]
480− markInit (x : xs) = (True, x) : markInit xs
481476 -- When in a single-line layout, there is a chance that the inner
482477 -- elements will also contain semicolons and they will confuse the
483478 -- parser. so we request braces around every element except the last.
484479 br <- layoutToBraces <$> getLayout
485480 sitcc $
486481 sepSemi
487− (\(m, i) -> (if m then br else id) $ p_item i)
488− (markInit $ sortOn ssStart items)
482+ ( \(p, i) ->
483+ ( case p of
484+ SinglePos -> id
485+ FirstPos -> br
486+ MiddlePos -> br
487+ LastPos -> id
488+ )
489+ (p_item i)
490+ )
491+ (attachRelativePos $ sortOn ssStart items)
489492 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
490493 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
491494 -- Second argument of IPBind is always Left before type-checking.
src/Ormolu/Printer/Meat/ImportExport.hs+22−15
@@ -13,6 +13,7 @@ import Control.Monad
1313import GHC
1414import Ormolu.Printer.Combinators
1515import Ormolu.Printer.Meat.Common
16+import Ormolu.Utils (RelativePos (..), attachRelativePos)
1617
1718p_hsmodExports :: [LIE GhcPs] -> R ()
1819p_hsmodExports [] = do
@@ -22,7 +23,10 @@ p_hsmodExports [] = do
2223p_hsmodExports xs =
2324 parens N . sitcc $ do
2425 layout <- getLayout
25− sep breakpoint (sitcc . located' (uncurry (p_lie layout))) (attachPositions xs)
26+ sep
27+ breakpoint
28+ (\(p, l) -> sitcc (located l (p_lie layout p)))
29+ (attachRelativePos xs)
2630
2731p_hsmodImport :: Bool -> ImportDecl GhcPs -> R ()
2832p_hsmodImport useQualifiedPost ImportDecl {..} = do
@@ -63,12 +67,15 @@ p_hsmodImport useQualifiedPost ImportDecl {..} = do
6367 breakpoint
6468 parens N . sitcc $ do
6569 layout <- getLayout
66− sep breakpoint (sitcc . located' (uncurry (p_lie layout))) (attachPositions xs)
70+ sep
71+ breakpoint
72+ (\(p, l) -> sitcc (located l (p_lie layout p)))
73+ (attachRelativePos xs)
6774 newline
6875p_hsmodImport _ (XImportDecl x) = noExtCon x
6976
70−p_lie :: Layout -> (Int, Int) -> IE GhcPs -> R ()
71−p_lie encLayout (i, totalItems) = \case
77+p_lie :: Layout -> RelativePos -> IE GhcPs -> R ()
78+p_lie encLayout relativePos = \case
7279 IEVar NoExtField l1 -> do
7380 located l1 p_ieWrappedName
7481 p_comma
@@ -98,7 +105,11 @@ p_lie encLayout (i, totalItems) = \case
98105 located l1 p_hsmodName
99106 p_comma
100107 IEGroup NoExtField n str -> do
101− unless (i == 0) newline
108+ case relativePos of
109+ SinglePos -> return ()
110+ FirstPos -> return ()
111+ MiddlePos -> newline
112+ LastPos -> newline
102113 p_hsDocString (Asterisk n) False (noLoc str)
103114 IEDoc NoExtField str ->
104115 p_hsDocString Pipe False (noLoc str)
@@ -107,14 +118,10 @@ p_lie encLayout (i, totalItems) = \case
107118 where
108119 p_comma =
109120 case encLayout of
110− SingleLine -> unless (i + 1 == totalItems) comma
121+ SingleLine ->
122+ case relativePos of
123+ SinglePos -> return ()
124+ FirstPos -> comma
125+ MiddlePos -> comma
126+ LastPos -> return ()
111127 MultiLine -> comma
112−
113−-- | Attach positions to 'Located' things in a list.
114−attachPositions ::
115− [Located a] ->
116− [Located ((Int, Int), a)]
117−attachPositions xs =
118− let f i (L l x) = L l ((i, n), x)
119− n = length xs
120− in zipWith f [0 ..] xs
src/Ormolu/Utils.hs+22−1
@@ -3,7 +3,9 @@
33
44-- | Random utilities used by the code.
55module Ormolu.Utils
6− ( combineSrcSpans',
6+ ( RelativePos (..),
7+ attachRelativePos,
8+ combineSrcSpans',
79 isModule,
810 notImplemented,
911 showOutputable,
@@ -26,6 +28,25 @@ import GHC
2628import GHC.DynFlags (baseDynFlags)
2729import qualified Outputable as GHC
2830
31+-- | Relative positions in a list.
32+data RelativePos
33+ = SinglePos
34+ | FirstPos
35+ | MiddlePos
36+ | LastPos
37+ deriving (Eq, Show)
38+
39+-- | Attach 'RelativePos'es to elements of a given list.
40+attachRelativePos :: [a] -> [(RelativePos, a)]
41+attachRelativePos = \case
42+ [] -> []
43+ [x] -> [(SinglePos, x)]
44+ (x : xs) -> (FirstPos, x) : markLast xs
45+ where
46+ markLast [] = []
47+ markLast [x] = [(LastPos, x)]
48+ markLast (x : xs) = (MiddlePos, x) : markLast xs
49+
2950-- | Combine all source spans from the given list.
3051combineSrcSpans' :: NonEmpty SrcSpan -> SrcSpan
3152combineSrcSpans' (x :| xs) = foldr combineSrcSpans x xs