haskell-tools-prettyprint 0.3.0.1 → 0.4.0.0
raw patch · 8 files changed
+190/−85 lines, 8 filesdep ~haskell-tools-astPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: haskell-tools-ast
API changes (from Hackage documentation)
- Language.Haskell.Tools.Transform: indentedList :: ListInfo SrcTemplateStage
- Language.Haskell.Tools.Transform: indentedListAfter :: String -> ListInfo SrcTemplateStage
- Language.Haskell.Tools.Transform: indentedListBefore :: String -> ListInfo SrcTemplateStage
- Language.Haskell.Tools.Transform: listSep :: String -> ListInfo SrcTemplateStage
- Language.Haskell.Tools.Transform: listSepAfter :: String -> String -> ListInfo SrcTemplateStage
- Language.Haskell.Tools.Transform: listSepBefore :: String -> String -> ListInfo SrcTemplateStage
- Language.Haskell.Tools.Transform: listSepBeforeAfter :: String -> String -> String -> ListInfo SrcTemplateStage
- Language.Haskell.Tools.Transform: optAfter :: String -> OptionalInfo SrcTemplateStage
- Language.Haskell.Tools.Transform: optBefore :: String -> OptionalInfo SrcTemplateStage
+ Language.Haskell.Tools.IndentationUtils: setMinimalIndent :: SourceInfoTraversal elem => Int -> elem dom SrcTemplateStage -> elem dom SrcTemplateStage
+ Language.Haskell.Tools.Transform: after :: AfterBefore i => String -> i -> i
+ Language.Haskell.Tools.Transform: followedBy :: AfterBefore i => String -> i -> i
+ Language.Haskell.Tools.Transform: indented :: ListInfo SrcTemplateStage -> ListInfo SrcTemplateStage
+ Language.Haskell.Tools.Transform: minimumIndented :: MinimumIndent i => Int -> i -> i
+ Language.Haskell.Tools.Transform: relativeIndented :: RelativeIndent i => Int -> i -> i
+ Language.Haskell.Tools.Transform: separatedBy :: String -> ListInfo SrcTemplateStage -> ListInfo SrcTemplateStage
Files
- Language/Haskell/Tools/IndentationUtils.hs +16/−0
- Language/Haskell/Tools/PrettyPrint.hs +66/−35
- Language/Haskell/Tools/Transform.hs +1/−2
- Language/Haskell/Tools/Transform/PlaceComments.hs +1/−1
- Language/Haskell/Tools/Transform/RangeTemplateToSourceTemplate.hs +3/−3
- Language/Haskell/Tools/Transform/SourceTemplate.hs +49/−17
- Language/Haskell/Tools/Transform/SourceTemplateHelpers.hs +50/−25
- haskell-tools-prettyprint.cabal +4/−2
+ Language/Haskell/Tools/IndentationUtils.hs view
@@ -0,0 +1,16 @@+-- | Utilities to modify the indentation of AST fragments +module Language.Haskell.Tools.IndentationUtils where + +import Control.Reference +import Control.Monad.Identity + +import Language.Haskell.Tools.AST +import Language.Haskell.Tools.Transform.SourceTemplate + +-- | Set the minimal indentation recursively for a part of the AST +setMinimalIndent :: SourceInfoTraversal elem + => Int -> elem dom SrcTemplateStage -> elem dom SrcTemplateStage +setMinimalIndent ind = runIdentity . + sourceInfoTraverse (SourceInfoTrf (Identity . (sourceTemplateMinimalIndent .= ind)) + (Identity . (srcTmpListMinimalIndent .= ind)) + (Identity . (srcTmpOptMinimalIndent .= ind)))
Language/Haskell/Tools/PrettyPrint.hs view
@@ -17,48 +17,78 @@ import Control.Monad.State import Control.Reference import Data.Maybe +import Data.List as List import Data.List.Split import Data.Foldable import Data.Sequence hiding (null, replicate) import Language.Haskell.Tools.AST +import Debug.Trace + -- | Pretty prints an AST by using source templates stored as node info prettyPrint :: (SourceInfoTraversal node) => node dom SrcTemplateStage -> String prettyPrint = toList . printRose . toRoseTree printRose :: RoseTree SrcTemplateStage -> Seq Char -printRose rt = evalState (printRose' rt) (mkRealSrcLoc (fsLit "") 1 1) - +printRose rt = evalState (printRose' startLoc rt) startLoc + where startLoc = mkRealSrcLoc (fsLit "") 1 1 + type PPState = State RealSrcLoc -- | Pretty prints a rose tree according to the source templates remaining from the original AST -printRose' :: RoseTree SrcTemplateStage -> PPState (Seq Char) +printRose' :: RealSrcLoc -> RoseTree SrcTemplateStage -> PPState (Seq Char) -- simple implementation could be optimized a bit -- warning: the length of the file should not exceed maxbound::Int -printRose' (RoseTree (RoseSpan (SourceTemplateNode _ elems)) children) - = printTemplateElems elems children - where printTemplateElems :: [SourceTemplateElem] -> [RoseTree SrcTemplateStage] -> PPState (Seq Char) - printTemplateElems (TextElem txt : rest) children = putString txt >+< printTemplateElems rest children - printTemplateElems (ChildElem : rest) (child : children) = printRose' child >+< printTemplateElems rest children - printTemplateElems [] [] = return empty - printTemplateElems _ [] = error $ "More child elem in template than actual children (elems: " ++ show elems ++ ", children: " ++ show children ++ ")" - printTemplateElems [] _ = error $ "Not all children are used to pretty printing. (elems: " ++ show elems ++ ", children: " ++ show children ++ ")" +printRose' parent (RoseTree (RoseSpan (SourceTemplateNode rng elems minInd relInd)) children) + = do slide <- calculateSlide rng + actRng <- get + let printTemplateElems :: [SourceTemplateElem] -> [RoseTree SrcTemplateStage] -> PPState (Seq Char) + printTemplateElems (TextElem txt : rest) children = putString slide min txt >+< printTemplateElems rest children + printTemplateElems (ChildElem : rest) (child : children) = printRose' parent child >+< printTemplateElems rest children + printTemplateElems [] [] = return empty + printTemplateElems _ [] = error $ "More child elem in template than actual children (elems: " ++ show elems ++ ", children: " ++ show children ++ ")" + printTemplateElems [] _ = error $ "Not all children are used to pretty printing. (elems: " ++ show elems ++ ", children: " ++ show children ++ ")" + + min = minInd `max` getPosByRelative parent relInd + + printTemplateElems elems children -printRose' (RoseTree (RoseList (SourceTemplateList {})) []) = return empty -printRose' (RoseTree (RoseList (SourceTemplateList _ bef aft defSep indented [])) children) - = putString bef >+< (if indented then printListWithSepsIndented else printListWithSeps) (repeat defSep) children >+< putString aft -printRose' (RoseTree (RoseList (SourceTemplateList _ bef aft _ indented seps)) children) - = putString bef >+< (if indented then printListWithSepsIndented else printListWithSeps) (seps ++ repeat (last seps)) children >+< putString aft - -printRose' (RoseTree (RoseOptional (SourceTemplateOpt {})) []) = return empty -printRose' (RoseTree (RoseOptional (SourceTemplateOpt _ bef aft)) [child]) = putString bef >+< printRose' child >+< putString aft -printRose' (RoseTree (RoseOptional _) _) = error "More than one child element in an optional node." +printRose' _ (RoseTree (RoseList (SourceTemplateList {})) []) = return empty +printRose' parent (RoseTree (RoseList (SourceTemplateList rng bef aft defSep indented seps minInd relInd)) children) + = do slide <- calculateSlide rng + actRng <- get + let min = minInd `max` getPosByRelative parent relInd + putString slide min bef + >+< (if indented then printListWithSepsIndented else printListWithSeps) actRng slide min actualSeps children + >+< putString slide min aft + where actualSeps = case seps of [] -> repeat defSep + _ -> seps ++ repeat (last seps) + +printRose' _ (RoseTree (RoseOptional (SourceTemplateOpt {})) []) = return empty +printRose' parent (RoseTree (RoseOptional (SourceTemplateOpt rng bef aft minInd relInd)) [child]) + = do slide <- calculateSlide rng + actRng <- get + let min = minInd `max` getPosByRelative parent relInd + putString slide min bef >+< printRose' actRng child >+< putString slide min aft +printRose' _ (RoseTree (RoseOptional _) _) = error "More than one child element in an optional node." +getPosByRelative :: RealSrcLoc -> Maybe Int -> Int +getPosByRelative sp (Just i) = srcLocCol sp + i - 1 +getPosByRelative _ _ = 0 +calculateSlide :: SrcSpan -> PPState Int +calculateSlide (RealSrcSpan originalSpan) = do + actualSpan <- get + return $ srcLocCol actualSpan - srcLocCol (realSrcSpanStart originalSpan) +calculateSlide _ = return 0 -putString :: String -> PPState (Seq Char) -putString s = do modify $ advanceStr s - return (fromList s) +putString :: Int -> Int -> String -> PPState (Seq Char) +putString slide minInd s + = do modify $ advanceStr newStr + return (fromList newStr) + where start:rest = splitOn "\n" s + newStr = concat $ intersperse ("\n" ++ replicate slide ' ') (start : map (extendToNSpaces minInd) rest) + extendToNSpaces n str = replicate n ' ' ++ (List.dropWhile (== ' ') $ List.take n str) ++ List.drop n str advanceStr :: String -> RealSrcLoc -> RealSrcLoc advanceStr s loc = foldl advanceSrcLoc loc s @@ -82,20 +112,21 @@ (>+<) :: PPState (Seq Char) -> PPState (Seq Char) -> PPState (Seq Char) (>+<) = liftM2 (><) -printListWithSeps :: [String] -> [RoseTree SrcTemplateStage] -> PPState (Seq Char) +printListWithSeps :: RealSrcLoc -> Int -> Int -> [String] -> [RoseTree SrcTemplateStage] -> PPState (Seq Char) printListWithSeps = printListWithSeps' putString -printListWithSepsIndented :: [String] -> [RoseTree SrcTemplateStage] -> PPState (Seq Char) -printListWithSepsIndented seps children +-- | Prints the elements of a list where the elements must be printed in the same line (do stmts, case alts, let binds, ...) +printListWithSepsIndented :: RealSrcLoc -> Int -> Int -> [String] -> [RoseTree SrcTemplateStage] -> PPState (Seq Char) +printListWithSepsIndented parent slide minInd seps children = do base <- get - let putCorrectSep s = do curr <- get - let (shortened, currCol) = untilReaches s curr base - putString $ shortened ++ replicate (srcLocCol base - currCol) ' ' - printListWithSeps' putCorrectSep seps children + let putCorrectSep _ min s = do curr <- get + let (shortened, currCol) = untilReaches s curr base + putString 0 min $ shortened ++ replicate (srcLocCol base - currCol) ' ' + printListWithSeps' putCorrectSep parent slide minInd seps children -printListWithSeps' :: (String -> PPState (Seq Char)) -> [String] -> [RoseTree SrcTemplateStage] -> PPState (Seq Char) -printListWithSeps' _ _ [] = return empty -printListWithSeps' putCorrectSep _ [child] = printRose' child -printListWithSeps' putCorrectSep (sep:seps) (child:children) - = printRose' child >+< putCorrectSep sep >+< printListWithSeps' putCorrectSep seps children +printListWithSeps' :: (Int -> Int -> String -> PPState (Seq Char)) -> RealSrcLoc -> Int -> Int -> [String] -> [RoseTree SrcTemplateStage] -> PPState (Seq Char) +printListWithSeps' _ _ _ _ _ [] = return empty +printListWithSeps' _ parent _ _ _ [child] = printRose' parent child +printListWithSeps' putCorrectSep parent slide minInd (sep:seps) (child:children) + = printRose' parent child >+< putCorrectSep slide minInd sep >+< printListWithSeps' putCorrectSep parent slide minInd seps children
Language/Haskell/Tools/Transform.hs view
@@ -4,8 +4,7 @@ -- comment handling , placeComments, getNormalComments, getPragmaComments -- generating source templates - , child, opt, optBefore, optAfter, list, indentedList, indentedListBefore, indentedListAfter, listSep - , listSepBefore, listSepAfter, listSepBeforeAfter, (<>) + , child, opt, list, after, followedBy, relativeIndented, minimumIndented, separatedBy, indented, (<>) -- references on source templates , sourceTemplateNodeRange, sourceTemplateNodeElems , sourceTemplateListRange, srcTmpListBefore, srcTmpListAfter, srcTmpDefaultSeparator, srcTmpIndented, srcTmpSeparators
Language/Haskell/Tools/Transform/PlaceComments.hs view
@@ -33,7 +33,7 @@ where getPragmaCommand = takeWhile (\c -> isAlphaNum c || c == '_') . dropWhile isSpace . drop 3 isPragma :: AnnotationComment -> Bool -isPragma (AnnBlockComment str) = take 3 str == "{-#" && take 3 (reverse str) == "#-}" +isPragma (AnnBlockComment str) = take 3 str == "{-#" && take 3 (reverse str) == "}-#" isPragma _ = False -- | Puts comments in the nodes they should be attached to. Leaves the AST in a state where parent nodes
Language/Haskell/Tools/Transform/RangeTemplateToSourceTemplate.hs view
@@ -55,12 +55,12 @@ applyFragments srcs = flip evalState srcs . sourceInfoTraverseDown (SourceInfoTrf (\ni -> do template <- mapM getTextFor (ni ^. rngTemplateNodeElems) - return $ SourceTemplateNode (RealSrcSpan $ ni ^. rngTemplateNodeRange) template) + return $ SourceTemplateNode (RealSrcSpan $ ni ^. rngTemplateNodeRange) template 0 Nothing) (\(RangeTemplateList rng bef aft sep indented seps) -> do (own, rest) <- splitAt (length seps) <$> get put rest - return (SourceTemplateList (RealSrcSpan rng) bef aft sep indented own)) - (\(RangeTemplateOpt rng bef aft) -> return (SourceTemplateOpt (RealSrcSpan rng) bef aft))) + return (SourceTemplateList (RealSrcSpan rng) bef aft sep indented own 0 Nothing)) + (\(RangeTemplateOpt rng bef aft) -> return (SourceTemplateOpt (RealSrcSpan rng) bef aft 0 Nothing))) (return ()) (return ()) where getTextFor RangeChildElem = return ChildElem getTextFor (RangeElem sp) = do (src:rest) <- get
Language/Haskell/Tools/Transform/SourceTemplate.hs view
@@ -16,25 +16,34 @@ import Language.Haskell.Tools.AST instance SourceInfo SrcTemplateStage where - data SpanInfo SrcTemplateStage = SourceTemplateNode { _sourceTemplateNodeRange :: SrcSpan - , _sourceTemplateNodeElems :: [SourceTemplateElem] - } + data SpanInfo SrcTemplateStage + = SourceTemplateNode { _sourceTemplateNodeRange :: SrcSpan -- ^ The (original) range of the given element + , _sourceTemplateNodeElems :: [SourceTemplateElem] -- ^ The children of the given node, could be text or child nodes + , _srcTmpMinInd :: Int -- ^ Minimum indentation for the element + , _srcTmpRelPos :: Maybe Int -- ^ Relative indentation for newly created elements + } deriving (Eq, Ord, Data) - data ListInfo SrcTemplateStage = SourceTemplateList { _sourceTemplateListRange :: SrcSpan - , _srcTmpListBefore :: String -- ^ Text that should be put before the first element if the list becomes populated - , _srcTmpListAfter :: String -- ^ Text that should be put after the last element if the list becomes populated - , _srcTmpDefaultSeparator :: String -- ^ The default separator if the list were empty - , _srcTmpIndented :: Bool -- ^ True, if the elements need to be aligned in the same column - , _srcTmpSeparators :: [String] -- ^ The actual separators that were found in the source code - } + data ListInfo SrcTemplateStage + = SourceTemplateList { _sourceTemplateListRange :: SrcSpan -- ^ The (original) range of the given element + , _srcTmpListBefore :: String -- ^ Text that should be put before the first element if the list becomes populated + , _srcTmpListAfter :: String -- ^ Text that should be put after the last element if the list becomes populated + , _srcTmpDefaultSeparator :: String -- ^ The default separator if the list were empty + , _srcTmpIndented :: Bool -- ^ True, if the elements need to be aligned in the same column + , _srcTmpSeparators :: [String] -- ^ The actual separators that were found in the source code + , _srcTmpListMinInd :: Int -- ^ Minimum indentation for the element + , _srcTmpListRelPos :: Maybe Int -- ^ Relative indentation for newly created elements + } deriving (Eq, Ord, Data) - data OptionalInfo SrcTemplateStage = SourceTemplateOpt { _sourceTemplateOptRange :: SrcSpan - , _srcTmpOptBefore :: String -- ^ Text that should be put before the element if it appears - , _srcTmpOptAfter :: String -- ^ Text that should be put after the element if it appears - } + data OptionalInfo SrcTemplateStage + = SourceTemplateOpt { _sourceTemplateOptRange :: SrcSpan -- ^ The (original) range of the given element + , _srcTmpOptBefore :: String -- ^ Text that should be put before the element if it appears + , _srcTmpOptAfter :: String -- ^ Text that should be put after the element if it appears + , _srcTmpOptMinInd :: Int -- ^ Minimum indentation for the element + , _srcTmpOptRelPos :: Maybe Int -- ^ Relative indentation for newly created elements + } deriving (Eq, Ord, Data) --- QUESTION: why cant these references be generated? +-- * References for span infos sourceTemplateNodeRange :: Simple Lens (SpanInfo SrcTemplateStage) SrcSpan sourceTemplateNodeRange = lens _sourceTemplateNodeRange (\v s -> s { _sourceTemplateNodeRange = v }) @@ -42,6 +51,14 @@ sourceTemplateNodeElems :: Simple Lens (SpanInfo SrcTemplateStage) [SourceTemplateElem] sourceTemplateNodeElems = lens _sourceTemplateNodeElems (\v s -> s { _sourceTemplateNodeElems = v }) +sourceTemplateMinimalIndent :: Simple Lens (SpanInfo SrcTemplateStage) Int +sourceTemplateMinimalIndent = lens _srcTmpMinInd (\v s -> s { _srcTmpMinInd = v }) + +srcTmpRelPos :: Simple Lens (SpanInfo SrcTemplateStage) (Maybe Int) +srcTmpRelPos = lens _srcTmpRelPos (\v s -> s { _srcTmpRelPos = v }) + +-- * References for list infos + sourceTemplateListRange :: Simple Lens (ListInfo SrcTemplateStage) SrcSpan sourceTemplateListRange = lens _sourceTemplateListRange (\v s -> s { _sourceTemplateListRange = v }) @@ -60,6 +77,14 @@ srcTmpSeparators :: Simple Lens (ListInfo SrcTemplateStage) [String] srcTmpSeparators = lens _srcTmpSeparators (\v s -> s { _srcTmpSeparators = v }) +srcTmpListMinimalIndent :: Simple Lens (ListInfo SrcTemplateStage) Int +srcTmpListMinimalIndent = lens _srcTmpListMinInd (\v s -> s { _srcTmpListMinInd = v }) + +srcTmpListRelPos :: Simple Lens (ListInfo SrcTemplateStage) (Maybe Int) +srcTmpListRelPos = lens _srcTmpListRelPos (\v s -> s { _srcTmpListRelPos = v }) + +-- * References for optional infos + sourceTemplateOptRange :: Simple Lens (OptionalInfo SrcTemplateStage) SrcSpan sourceTemplateOptRange = lens _sourceTemplateOptRange (\v s -> s { _sourceTemplateOptRange = v }) @@ -68,10 +93,17 @@ srcTmpOptAfter :: Simple Lens (OptionalInfo SrcTemplateStage) String srcTmpOptAfter = lens _srcTmpOptAfter (\v s -> s { _srcTmpOptAfter = v }) + +srcTmpOptMinimalIndent :: Simple Lens (OptionalInfo SrcTemplateStage) Int +srcTmpOptMinimalIndent = lens _srcTmpOptMinInd (\v s -> s { _srcTmpOptMinInd = v }) +srcTmpOptRelPos :: Simple Lens (OptionalInfo SrcTemplateStage) (Maybe Int) +srcTmpOptRelPos = lens _srcTmpOptRelPos (\v s -> s { _srcTmpOptRelPos = v }) + + -- | An element of a source template for a singleton AST node. data SourceTemplateElem - = TextElem String -- ^ Source text belonging to the current node + = TextElem { _sourceTemplateText :: String } -- ^ Source text belonging to the current node | ChildElem -- ^ Placeholder for the next children of the node deriving (Eq, Ord, Data) @@ -90,7 +122,7 @@ setRange = (sourceTemplateOptRange .=) instance Show (SpanInfo SrcTemplateStage) where - show (SourceTemplateNode rng sp) = concatMap show sp + show (SourceTemplateNode _ sp _ _) = concatMap show sp instance Show (ListInfo SrcTemplateStage) where show SourceTemplateList{..} = "«*" ++ show _srcTmpListBefore ++ " " ++ show _srcTmpDefaultSeparator ++ " " ++ show _srcTmpListAfter ++ "*»" instance Show (OptionalInfo SrcTemplateStage) where
Language/Haskell/Tools/Transform/SourceTemplateHelpers.hs view
@@ -18,44 +18,69 @@ type ASTMulti node dom = AnnListG node dom SrcTemplateStage instance IsString (SpanInfo SrcTemplateStage) where - fromString s = SourceTemplateNode noSrcSpan [TextElem s] - + fromString s = SourceTemplateNode noSrcSpan [TextElem s] 0 Nothing + +-- * Basic elements child :: SpanInfo SrcTemplateStage -child = SourceTemplateNode noSrcSpan [ChildElem] +child = SourceTemplateNode noSrcSpan [ChildElem] 0 Nothing opt :: OptionalInfo SrcTemplateStage -opt = SourceTemplateOpt noSrcSpan "" "" +opt = SourceTemplateOpt noSrcSpan "" "" 0 Nothing -optBefore :: String -> OptionalInfo SrcTemplateStage -optBefore s = SourceTemplateOpt noSrcSpan s "" +list :: ListInfo SrcTemplateStage +list = SourceTemplateList noSrcSpan "" "" "" False [] 0 Nothing -optAfter :: String -> OptionalInfo SrcTemplateStage -optAfter s = SourceTemplateOpt noSrcSpan "" s +-- * Modifiers -list :: ListInfo SrcTemplateStage -list = SourceTemplateList noSrcSpan "" "" "" False [] +class AfterBefore i where + -- | Put the given string before the element if it is not empty + after :: String -> i -> i + -- | The given string should follow the element if it is not empty + followedBy :: String -> i -> i -indentedList :: ListInfo SrcTemplateStage -indentedList = SourceTemplateList noSrcSpan "" "" "\n" True [] +instance AfterBefore (ListInfo SrcTemplateStage) where + after str = srcTmpListBefore .= str + followedBy str = srcTmpListAfter .= str -indentedListBefore :: String -> ListInfo SrcTemplateStage -indentedListBefore bef = SourceTemplateList noSrcSpan bef "" "\n" True [] +instance AfterBefore (OptionalInfo SrcTemplateStage) where + after str = srcTmpOptBefore .= str + followedBy str = srcTmpOptAfter .= str -indentedListAfter :: String -> ListInfo SrcTemplateStage -indentedListAfter aft = SourceTemplateList noSrcSpan "" aft "\n" True [] +class RelativeIndent i where + -- | The element should be indented relatively to its parent + relativeIndented :: Int -> i -> i -listSep :: String -> ListInfo SrcTemplateStage -listSep s = SourceTemplateList noSrcSpan "" "" s False [] +instance RelativeIndent (SpanInfo SrcTemplateStage) where + relativeIndented i = srcTmpRelPos .= Just i -listSepBefore :: String -> String -> ListInfo SrcTemplateStage -listSepBefore s bef = SourceTemplateList noSrcSpan bef "" s False [] +instance RelativeIndent (ListInfo SrcTemplateStage) where + relativeIndented i = srcTmpListRelPos .= Just i -listSepAfter :: String -> String -> ListInfo SrcTemplateStage -listSepAfter s aft = SourceTemplateList noSrcSpan "" aft s False [] +instance RelativeIndent (OptionalInfo SrcTemplateStage) where + relativeIndented i = srcTmpOptRelPos .= Just i -listSepBeforeAfter :: String -> String -> String -> ListInfo SrcTemplateStage -listSepBeforeAfter s bef aft = SourceTemplateList noSrcSpan bef aft s False [] +class MinimumIndent i where + -- | The elements should be indented at least to the given number of spaces + minimumIndented :: Int -> i -> i + +instance MinimumIndent (SpanInfo SrcTemplateStage) where + minimumIndented i = sourceTemplateMinimalIndent .= i + +instance MinimumIndent (ListInfo SrcTemplateStage) where + minimumIndented i = srcTmpListMinimalIndent .= i + +instance MinimumIndent (OptionalInfo SrcTemplateStage) where + minimumIndented i = srcTmpOptMinimalIndent .= i + +-- | The elements of the list should be separated by the given string by default (might be overridden) +separatedBy :: String -> ListInfo SrcTemplateStage -> ListInfo SrcTemplateStage +separatedBy sep = srcTmpDefaultSeparator .= sep + +-- | The elements of the list should be indented on the same column +indented :: ListInfo SrcTemplateStage -> ListInfo SrcTemplateStage +indented = (srcTmpIndented .= True) . (srcTmpDefaultSeparator .= "\n") + -- | Concatenates two source templates to produce a new template with all child elements. (<>) :: SpanInfo SrcTemplateStage -> SpanInfo SrcTemplateStage -> SpanInfo SrcTemplateStage -SourceTemplateNode sp1 el1 <> SourceTemplateNode sp2 el2 = SourceTemplateNode (combineSrcSpans sp1 sp2) (el1 ++ el2) +SourceTemplateNode sp1 el1 _ _ <> SourceTemplateNode sp2 el2 _ _ = SourceTemplateNode (combineSrcSpans sp1 sp2) (el1 ++ el2) 0 Nothing
haskell-tools-prettyprint.cabal view
@@ -1,5 +1,5 @@ name: haskell-tools-prettyprint -version: 0.3.0.1 +version: 0.4.0.0 synopsis: Pretty printing of Haskell-Tools AST description: Converts the Haskell-Tools AST to text. Prepares the AST for this conversion. If the AST was created from the GHC AST this pretty printing will result in the original source code. Generated AST parts will get the default formatting. Works using the source annotations that are present in the AST. Creates a rose tree first to simplify the conversion. homepage: https://github.com/haskell-tools/haskell-tools @@ -12,8 +12,10 @@ cabal-version: >=1.10 library + ghc-options: -O2 exposed-modules: Language.Haskell.Tools.PrettyPrint , Language.Haskell.Tools.Transform + , Language.Haskell.Tools.IndentationUtils other-modules: Language.Haskell.Tools.PrettyPrint.RoseTree , Language.Haskell.Tools.Transform.RangeToRangeTemplate , Language.Haskell.Tools.Transform.RangeTemplateToSourceTemplate @@ -28,5 +30,5 @@ , uniplate >= 1.6 && < 1.7 , split >= 0.2 && < 0.3 , ghc >= 8.0 && < 8.1 - , haskell-tools-ast >= 0.3 && < 0.4 + , haskell-tools-ast >= 0.4 && < 0.5 default-language: Haskell2010