Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take the fixities of lens' operators into account #742

Merged
merged 4 commits into from
May 10, 2023
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- HIndent now assumes no extensions are enabled by default ([#728]).
- All modules except for `HIndent` are now private, and the minimum necessary definitions are exposed from the module ([#729]).
- HIndent now prints all `do` expressions in a unified style ([#739]).
- HIndent now formats operators based on their fixities ([#741]).
- HIndent now formats operators based on their fixities ([#741], [#742]).

### Fixed

Expand Down Expand Up @@ -351,6 +351,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

[#742]: https://github.com/mihaimaruseac/hindent/pull/742
[#741]: https://github.com/mihaimaruseac/hindent/pull/741
[#739]: https://github.com/mihaimaruseac/hindent/pull/739
[#731]: https://github.com/mihaimaruseac/hindent/pull/731
Expand Down
11 changes: 11 additions & 0 deletions TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2659,6 +2659,17 @@ f =
- ffffffffffffffff / -ggggggggggggg
```

Lens operators

```haskell
updateUsr usr =
usr
& userFirstName .~ "newfirst"
& userLastName .~ "newlast"
& userEmail .~ "newemail"
& userPassword .~ "newpass"
```

### Primitive type values

`Char`
Expand Down
1 change: 1 addition & 0 deletions hindent.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ library
HIndent.CommandlineOptions
HIndent.Config
HIndent.Error
HIndent.Fixity
HIndent.GhcLibParserWrapper.GHC.Hs
HIndent.Language
HIndent.LanguageExtension
Expand Down
67 changes: 67 additions & 0 deletions src/HIndent/Fixity.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- | Operator fixities.
--
-- It is very difficult to take operators' fixities into account as fixity
-- information is not stored in an AST. While `ghc-lib-parser-ex` provides
-- `fixitiesFromModule`, it is almost useless as operators are usually imported
-- from other modules.
--
-- Ormolu is trying to resolve this issue by examing Hackage, but doing the same
-- way in HIndent is not so easy.
module HIndent.Fixity
( fixities
) where

import GHC.Types.Fixity
import Language.Haskell.GhclibParserEx.Fixity

-- | Operator fixities that HIndent supports.
fixities :: [(String, Fixity)]
fixities = baseFixities <> lensFixities

-- | Fixities of operators defined in lens package.
lensFixities :: [(String, Fixity)]
lensFixities =
concat
[ infixr_
4
[ ".~"
, "%~"
, "+~"
, "-~"
, "*~"
, "//~"
, "^~"
, "^^~"
, "**~"
, "||~"
, "<>~"
, "&&~"
, "<.~"
, "?~"
, "<?~"
, "%@~"
, ".@~"
]
, infix_
4
[ ".="
, "%="
, "+="
, "-="
, "*="
, "//="
, "^="
, "^^="
, "**="
, "||="
, "<>="
, "&&="
, "<.="
, "?="
, "<?="
, "%@="
, ".@="
]
, infixr_ 2 ["<^"]
, infixl_ 1 ["&"]
]
3 changes: 2 additions & 1 deletion src/HIndent/ModulePreprocessing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Data.Maybe
import GHC.Hs
import GHC.Types.SrcLoc
import Generics.SYB hiding (GT, typeOf, typeRep)
import HIndent.Fixity
import HIndent.GhcLibParserWrapper.GHC.Hs
import HIndent.ModulePreprocessing.CommentRelocation
import Language.Haskell.GhclibParserEx.Fixity
Expand Down Expand Up @@ -49,7 +50,7 @@ modifyASTForPrettyPrinting m = relocateComments (beforeRelocation m) allComments
-- | This function modifies the given module AST to apply fixities of infix
-- operators defined in the 'base' package.
fixFixities :: HsModule' -> HsModule'
fixFixities = applyFixities baseFixities
fixFixities = applyFixities fixities

-- | This function sets an 'LGRHS's end position to the end position of the
-- last RHS in the 'grhssGRHSs'.
Expand Down
4 changes: 2 additions & 2 deletions src/HIndent/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ import GHC.Types.SrcLoc
import GHC.Unit.Module.Warnings
import HIndent.Applicative
import HIndent.Config
import HIndent.Fixity
import HIndent.Pretty.Combinators
import HIndent.Pretty.Import
import HIndent.Pretty.NodeComments
import HIndent.Pretty.Pragma
import HIndent.Pretty.SigBindFamily
import HIndent.Pretty.Types
import HIndent.Printer
import Language.Haskell.GhclibParserEx.Fixity hiding (fixity)
import Language.Haskell.GhclibParserEx.GHC.Hs.Expr
import Text.Show.Unicode
#if MIN_VERSION_ghc_lib_parser(9,6,1)
Expand Down Expand Up @@ -1774,7 +1774,7 @@ instance Pretty InfixApp where
error
"The number of the sum of operants and operators should be odd."
prettyOps _ = error "Too short list."
findFixity o = fromMaybe defaultFixity $ lookup (varToStr o) baseFixities
findFixity o = fromMaybe defaultFixity $ lookup (varToStr o) fixities
allOperantsAndOperatorsLeftAssoc = reverse $ rhs : op : collect lhs
where
collect :: LHsExpr GhcPs -> [LHsExpr GhcPs]
Expand Down