diff options
author | Artyom Kazak <yom@artyom.me> | 2017-04-24 13:51:40 +0300 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2017-04-24 12:51:40 +0200 |
commit | 6888814f309f206d6b9860f3ae9b5e6c8525f17b (patch) | |
tree | 9eef3ea0ab4daaed24a0e9d410c04e2bd7791c3a /lib | |
parent | d10ebd066bfb93ae8e0a155c45da3c574a431911 (diff) | |
download | stylish-haskell-6888814f309f206d6b9860f3ae9b5e6c8525f17b.tar.gz |
Take package imports into account when prettifying imports
Fixes #106
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Language/Haskell/Stylish/Step/Imports.hs | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/lib/Language/Haskell/Stylish/Step/Imports.hs b/lib/Language/Haskell/Stylish/Step/Imports.hs index be60cba..78912d6 100644 --- a/lib/Language/Haskell/Stylish/Step/Imports.hs +++ b/lib/Language/Haskell/Stylish/Step/Imports.hs @@ -104,16 +104,33 @@ imports _ = [] importName :: H.ImportDecl l -> String importName i = let (H.ModuleName _ n) = H.importModule i in n +importPackage :: H.ImportDecl l -> Maybe String +importPackage i = H.importPkg i + + +-------------------------------------------------------------------------------- +-- | A "compound import name" is import's name and package (if present). For +-- instance, if you have an import @Foo.Bar@ from package @foobar@, the full +-- name will be @"foobar" Foo.Bar@. +compoundImportName :: H.ImportDecl l -> String +compoundImportName i = + case importPackage i of + Nothing -> importName i + Just pkg -> show pkg ++ " " ++ importName i + -------------------------------------------------------------------------------- longestImport :: [H.ImportDecl l] -> Int -longestImport = maximum . map (length . importName) +longestImport = maximum . map (length . compoundImportName) -------------------------------------------------------------------------------- -- | Compare imports for ordering compareImports :: H.ImportDecl l -> H.ImportDecl l -> Ordering -compareImports = comparing (map toLower . importName &&& H.importQualified) +compareImports = + comparing (map toLower . importName &&& + fmap (map toLower) . importPackage &&& + H.importQualified) -------------------------------------------------------------------------------- @@ -292,9 +309,9 @@ prettyImport columns Options{..} padQualified padName longest imp . withTail (", " ++)) ++ [")"]) - paddedBase = base $ padImport $ importName imp + paddedBase = base $ padImport $ compoundImportName imp - paddedNoSpecBase = base $ padImportNoSpec $ importName imp + paddedNoSpecBase = base $ padImportNoSpec $ compoundImportName imp padImport = if hasExtras && padName then padRight longest @@ -304,12 +321,11 @@ prettyImport columns Options{..} padQualified padName longest imp then padRight longest else id - base' baseName importAs hasHiding' = unwords $ concat $ filter (not . null) + base' baseName importAs hasHiding' = unwords $ concat $ [ ["import"] , source , safe , qualified - , show <$> maybeToList (H.importPkg imp) , [baseName] , importAs , hasHiding' @@ -319,9 +335,10 @@ prettyImport columns Options{..} padQualified padName longest imp ["as " ++ as | H.ModuleName _ as <- maybeToList $ H.importAs imp] ["hiding" | hasHiding] - inlineBaseLength = length $ base' (padImport $ importName imp) [] [] + inlineBaseLength = length $ + base' (padImport $ compoundImportName imp) [] [] - afterAliasBaseLength = length $ base' (padImport $ importName imp) + afterAliasBaseLength = length $ base' (padImport $ compoundImportName imp) ["as " ++ as | H.ModuleName _ as <- maybeToList $ H.importAs imp] [] (hasHiding, importSpecs) = case H.importSpecs imp of |