Skip to content

Commit

Permalink
Support ImportPostQualified (#875)
Browse files Browse the repository at this point in the history
* Add a test

* Support `ImportQualifiedPost`

* Add a changelog
  • Loading branch information
toku-sa-n authored Apr 16, 2024
1 parent f305081 commit faf08a6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Support for GHC 9.8 ([#775]).
- Support for `ImportPostQualified` ([#875]).

### Changed

Expand Down Expand Up @@ -376,6 +377,7 @@ This version is accidentally pushlished, and is the same as 5.3.3.
[@uhbif19]: https://github.com/uhbif19
[@toku-sa-n]: https://github.com/toku-sa-n

[#875]: https://github.com/mihaimaruseac/hindent/pull/875
[#873]: https://github.com/mihaimaruseac/hindent/pull/873
[#868]: https://github.com/mihaimaruseac/hindent/pull/868
[#849]: https://github.com/mihaimaruseac/hindent/pull/849
Expand Down
8 changes: 8 additions & 0 deletions TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ import CommentAfter
import qualified "base" Prelude as P
```

`ImportQualifiedPost`

```haskell
{-# LANGUAGE ImportQualifiedPost #-}

import Data.Text qualified as T
```

Importing a `#`

```haskell
Expand Down
1 change: 0 additions & 1 deletion hindent.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ library
HIndent.Ast.Import.Entry
HIndent.Ast.Import.Entry.Collection
HIndent.Ast.Import.ImportingOrHiding
HIndent.Ast.Import.Qualification
HIndent.Ast.Module
HIndent.Ast.Module.Declaration
HIndent.Ast.Module.Export.Collection
Expand Down
37 changes: 29 additions & 8 deletions src/HIndent/Ast/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Data.List
import qualified GHC.Unit as GHC
import HIndent.Applicative
import HIndent.Ast.Import.Entry.Collection
import HIndent.Ast.Import.Qualification
import HIndent.Ast.NodeComments
import HIndent.Ast.WithComments
import qualified HIndent.GhcLibParserWrapper.GHC.Hs as GHC
Expand All @@ -22,11 +21,21 @@ import HIndent.Pretty
import HIndent.Pretty.Combinators
import HIndent.Pretty.NodeComments

data QualificationPosition
= Pre
| Post
deriving (Eq)

data Qualification = Qualification
{ qualifiedAs :: Maybe String
, position :: QualificationPosition
} deriving (Eq)

data Import = Import
{ moduleName :: String
, isSafe :: Bool
, isBoot :: Bool
, qualification :: Qualification
, qualification :: Maybe Qualification
, packageName :: Maybe String
, importEntries :: Maybe (WithComments ImportEntryCollection)
}
Expand All @@ -39,11 +48,13 @@ instance Pretty Import where
string "import "
when isBoot $ string "{-# SOURCE #-} "
when isSafe $ string "safe "
unless (qualification == NotQualified) $ string "qualified "
when (fmap position qualification == Just Pre) $ string "qualified "
whenJust packageName $ \name -> string name >> space
string moduleName
when (fmap position qualification == Just Post) $ string " qualified"
case qualification of
QualifiedAs name -> string " as " >> string name
Just Qualification {qualifiedAs = Just name} ->
string " as " >> string name
_ -> pure ()
whenJust importEntries pretty

Expand All @@ -54,10 +65,20 @@ mkImport decl@GHC.ImportDecl {..} = Import {..}
isSafe = ideclSafe
isBoot = ideclSource == GHC.IsBoot
qualification =
case (ideclQualified, ideclAs) of
(GHC.NotQualified, _) -> NotQualified
(_, Nothing) -> FullyQualified
(_, Just name) -> QualifiedAs $ showOutputable name
case (ideclQualified, ideclAs, ideclQualified) of
(GHC.NotQualified, _, _) -> Nothing
(_, Nothing, GHC.QualifiedPre) ->
Just Qualification {qualifiedAs = Nothing, position = Pre}
(_, Nothing, GHC.QualifiedPost) ->
Just Qualification {qualifiedAs = Nothing, position = Post}
(_, Just name, GHC.QualifiedPre) ->
Just
Qualification
{qualifiedAs = Just $ showOutputable name, position = Pre}
(_, Just name, GHC.QualifiedPost) ->
Just
Qualification
{qualifiedAs = Just $ showOutputable name, position = Post}
packageName = showOutputable <$> GHC.getPackageName decl
importEntries = mkImportEntryCollection decl

Expand Down
9 changes: 0 additions & 9 deletions src/HIndent/Ast/Import/Qualification.hs

This file was deleted.

0 comments on commit faf08a6

Please sign in to comment.