Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions hindent.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ library
HIndent.Pretty.Combinators.String
HIndent.Pretty.Combinators.Switch
HIndent.Pretty.Combinators.Wrap
HIndent.Pretty.Import
HIndent.Pretty.Import.Sort
HIndent.Pretty.NodeComments
HIndent.Pretty.SigBindFamily
HIndent.Pretty.Types
Expand Down
46 changes: 45 additions & 1 deletion src/HIndent/Ast/Import/Collection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ module HIndent.Ast.Import.Collection
) where

import Control.Monad.RWS
import Data.Function
import Data.List
import qualified GHC.Hs as GHC
import GHC.Stack
import qualified GHC.Types.SrcLoc as GHC
import HIndent.Ast.Import
import HIndent.Ast.NodeComments
import HIndent.Ast.WithComments
import HIndent.Config
import qualified HIndent.GhcLibParserWrapper.GHC.Hs as GHC
import HIndent.Pretty
import HIndent.Pretty.Combinators
import HIndent.Pretty.Import
import HIndent.Pretty.NodeComments
import HIndent.Printer

Expand Down Expand Up @@ -45,3 +48,44 @@ mkImportCollection GHC.HsModule {..} =

hasImports :: ImportCollection -> Bool
hasImports (ImportCollection xs) = not $ null xs

-- | Extracts import declarations from the given module. Adjacent import
-- declarations are grouped as a single list.
extractImports' :: [GHC.LImportDecl GHC.GhcPs] -> [[GHC.LImportDecl GHC.GhcPs]]
extractImports' = groupImports . sortImportsByLocation

-- | Combines adjacent import declarations into a single list.
groupImports :: [GHC.LImportDecl GHC.GhcPs] -> [[GHC.LImportDecl GHC.GhcPs]]
groupImports = groupImports' []
where
groupImports' ::
[[GHC.LImportDecl GHC.GhcPs]]
-> [GHC.LImportDecl GHC.GhcPs]
-> [[GHC.LImportDecl GHC.GhcPs]]
groupImports' xs [] = xs
groupImports' [] (x:xs) = groupImports' [[x]] xs
groupImports' [[]] (x:xs) = groupImports' [[x]] xs
groupImports' ([]:x:xs) (y:ys) = groupImports' ([y] : x : xs) ys
groupImports' ((z:zs):xs) (y:ys)
| z `isAdjacentTo` y = groupImports' ((y : z : zs) : xs) ys
| otherwise = groupImports' ([y] : (z : zs) : xs) ys
a `isAdjacentTo` b =
GHC.srcSpanEndLine (sp a) + 1 == GHC.srcSpanStartLine (sp b)
|| GHC.srcSpanEndLine (sp b) + 1 == GHC.srcSpanStartLine (sp a)
sp x =
case GHC.locA $ GHC.getLoc x of
GHC.RealSrcSpan x' _ -> x'
_ -> error "Src span unavailable."

-- | This function sorts imports by their start line numbers.
sortImportsByLocation ::
[GHC.LImportDecl GHC.GhcPs] -> [GHC.LImportDecl GHC.GhcPs]
sortImportsByLocation = sortBy (flip compare `on` lineIdx)
where
lineIdx = startLine . GHC.locA . GHC.getLoc

-- | This function returns the start line of the given 'SrcSpan'. If it is
-- not available, it raises an error.
startLine :: HasCallStack => GHC.SrcSpan -> Int
startLine (GHC.RealSrcSpan x _) = GHC.srcSpanStartLine x
startLine (GHC.UnhelpfulSpan _) = error "The src span is unavailable."
60 changes: 0 additions & 60 deletions src/HIndent/Pretty/Import.hs

This file was deleted.

134 changes: 0 additions & 134 deletions src/HIndent/Pretty/Import/Sort.hs

This file was deleted.