diff --git a/CHANGELOG.md b/CHANGELOG.md index f95417771..e01212491 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/TESTS.md b/TESTS.md index f0acd3206..1f4006654 100644 --- a/TESTS.md +++ b/TESTS.md @@ -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` diff --git a/hindent.cabal b/hindent.cabal index 3b9062723..3d385d1d2 100644 --- a/hindent.cabal +++ b/hindent.cabal @@ -41,6 +41,7 @@ library HIndent.CommandlineOptions HIndent.Config HIndent.Error + HIndent.Fixity HIndent.GhcLibParserWrapper.GHC.Hs HIndent.Language HIndent.LanguageExtension diff --git a/src/HIndent/Fixity.hs b/src/HIndent/Fixity.hs new file mode 100644 index 000000000..e1c901ee8 --- /dev/null +++ b/src/HIndent/Fixity.hs @@ -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 + [ ".~" + , "%~" + , "+~" + , "-~" + , "*~" + , "//~" + , "^~" + , "^^~" + , "**~" + , "||~" + , "<>~" + , "&&~" + , "<.~" + , "?~" + , "=" + , "&&=" + , "<.=" + , "?=" + , " 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'. diff --git a/src/HIndent/Pretty.hs b/src/HIndent/Pretty.hs index ba4569fc2..f9717309b 100644 --- a/src/HIndent/Pretty.hs +++ b/src/HIndent/Pretty.hs @@ -40,6 +40,7 @@ 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 @@ -47,7 +48,6 @@ 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) @@ -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]