-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid using
showOutputable
if possible (#900)
- Loading branch information
Showing
9 changed files
with
107 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ import HIndent.Pretty.NodeComments | |
import qualified Data.List.NonEmpty as NE | ||
#endif | ||
data GADTConstructor = GADTConstructor | ||
{ names :: [WithComments String] | ||
{ names :: [WithComments (GHC.IdP GHC.GhcPs)] | ||
, forallNeeded :: Bool | ||
, bindings :: WithComments (GHC.HsOuterSigTyVarBndrs GHC.GhcPs) | ||
, context :: Maybe (WithComments Context) | ||
|
@@ -32,7 +32,7 @@ instance CommentExtraction GADTConstructor where | |
|
||
instance Pretty GADTConstructor where | ||
pretty' (GADTConstructor {..}) = do | ||
hCommaSep $ fmap (`prettyWith` string) names | ||
hCommaSep $ fmap (`prettyWith` pretty) names | ||
hor <-|> ver | ||
where | ||
hor = string " :: " |=> body | ||
|
@@ -71,12 +71,10 @@ mkGADTConstructor [email protected] {..} = Just $ GADTConstructor {..} | |
context = fmap (fmap mkContext . fromGenLocated) con_mb_cxt | ||
mkGADTConstructor _ = Nothing | ||
|
||
getNames :: GHC.ConDecl GHC.GhcPs -> Maybe [WithComments String] | ||
getNames :: GHC.ConDecl GHC.GhcPs -> Maybe [WithComments (GHC.IdP GHC.GhcPs)] | ||
#if MIN_VERSION_ghc_lib_parser(9, 6, 0) | ||
getNames GHC.ConDeclGADT {..} = | ||
Just $ NE.toList $ fmap (fmap showOutputable . fromGenLocated) con_names | ||
getNames GHC.ConDeclGADT {..} = Just $ NE.toList $ fmap fromGenLocated con_names | ||
#else | ||
getNames GHC.ConDeclGADT {..} = | ||
Just $ fmap (fmap showOutputable . fromGenLocated) con_names | ||
getNames GHC.ConDeclGADT {..} = Just $ fmap fromGenLocated con_names | ||
#endif | ||
getNames _ = Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ module HIndent.Ast.Import | |
import Control.Monad | ||
import Data.Function | ||
import Data.List | ||
import qualified GHC.Types.SourceText as GHC | ||
import qualified GHC.Unit as GHC | ||
import HIndent.Applicative | ||
import HIndent.Ast.Import.Entry.Collection | ||
|
@@ -27,16 +28,16 @@ data QualificationPosition | |
deriving (Eq) | ||
|
||
data Qualification = Qualification | ||
{ qualifiedAs :: Maybe String | ||
{ qualifiedAs :: Maybe (GHC.XRec GHC.GhcPs GHC.ModuleName) | ||
, position :: QualificationPosition | ||
} deriving (Eq) | ||
|
||
data Import = Import | ||
{ moduleName :: String | ||
{ moduleName :: GHC.XRec GHC.GhcPs GHC.ModuleName | ||
, isSafe :: Bool | ||
, isBoot :: Bool | ||
, qualification :: Maybe Qualification | ||
, packageName :: Maybe String | ||
, packageName :: Maybe GHC.StringLiteral | ||
, importEntries :: Maybe (WithComments ImportEntryCollection) | ||
} | ||
|
||
|
@@ -49,19 +50,19 @@ instance Pretty Import where | |
when isBoot $ string "{-# SOURCE #-} " | ||
when isSafe $ string "safe " | ||
when (fmap position qualification == Just Pre) $ string "qualified " | ||
whenJust packageName $ \name -> string name >> space | ||
string moduleName | ||
whenJust packageName $ \name -> pretty name >> space | ||
pretty moduleName | ||
when (fmap position qualification == Just Post) $ string " qualified" | ||
case qualification of | ||
Just Qualification {qualifiedAs = Just name} -> | ||
string " as " >> string name | ||
string " as " >> pretty name | ||
_ -> pure () | ||
whenJust importEntries pretty | ||
|
||
mkImport :: GHC.ImportDecl GHC.GhcPs -> Import | ||
mkImport decl@GHC.ImportDecl {..} = Import {..} | ||
where | ||
moduleName = showOutputable ideclName | ||
moduleName = ideclName | ||
isSafe = ideclSafe | ||
isBoot = ideclSource == GHC.IsBoot | ||
qualification = | ||
|
@@ -72,22 +73,18 @@ mkImport [email protected] {..} = Import {..} | |
(_, Nothing, GHC.QualifiedPost) -> | ||
Just Qualification {qualifiedAs = Nothing, position = Post} | ||
(_, Just name, GHC.QualifiedPre) -> | ||
Just | ||
Qualification | ||
{qualifiedAs = Just $ showOutputable name, position = Pre} | ||
Just Qualification {qualifiedAs = Just name, position = Pre} | ||
(_, Just name, GHC.QualifiedPost) -> | ||
Just | ||
Qualification | ||
{qualifiedAs = Just $ showOutputable name, position = Post} | ||
packageName = showOutputable <$> GHC.getPackageName decl | ||
Just Qualification {qualifiedAs = Just name, position = Post} | ||
packageName = GHC.getPackageName decl | ||
importEntries = mkImportEntryCollection decl | ||
|
||
sortByName :: [WithComments Import] -> [WithComments Import] | ||
sortByName = fmap sortExplicitImportsInDecl . sortByModuleName | ||
|
||
-- | This function sorts import declarations by their module names. | ||
sortByModuleName :: [WithComments Import] -> [WithComments Import] | ||
sortByModuleName = sortBy (compare `on` moduleName . getNode) | ||
sortByModuleName = sortBy (compare `on` showOutputable . moduleName . getNode) | ||
|
||
-- | This function sorts explicit imports in the given import declaration | ||
-- by their names. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.