Skip to content

Commit ef25323

Browse files
committed
Fix operator and multiline export exactprint on GHC <9.9
Freshly built names carry EpAnnNotUsed before GHC 9.9, so addParens could not parenthesize operators.
1 parent aa41832 commit ef25323

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

hls-exactprint-utils/src/Development/IDE/GHC/ExactPrint/Annotation.hs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ module Development.IDE.GHC.ExactPrint.Annotation
1313
, withTrailingComma
1414
, modifyAnns
1515
, addParens
16+
, parenthesizeName
1617
) where
1718

1819
import Data.Bifunctor (first)
1920
import Development.IDE.GHC.Compat
2021
import Development.IDE.GHC.Orphans ()
22+
import GHC (LocatedN)
2123
#if MIN_VERSION_ghc(9,11,0)
2224
import GHC (DeltaPos (..), EpAnn (..),
23-
EpToken (..),
24-
EpaLocation,
25+
EpToken (..), EpaLocation,
2526
EpaLocation' (..),
2627
NameAdornment (..),
2728
SrcSpanAnnA, TrailingAnn (..))
@@ -33,11 +34,15 @@ import GHC (DeltaPos (..), EpAnn (..),
3334
NameAdornment (..),
3435
SrcSpanAnnA, TrailingAnn (..))
3536
#else
36-
import GHC (DeltaPos (..), EpAnn (..),
37+
import GHC (Anchor (..),
38+
AnchorOperation (..),
39+
DeltaPos (..), EpAnn (..),
3740
EpaLocation (..),
3841
NameAdornment (NameParens),
3942
SrcSpanAnn' (..), SrcSpanAnnA,
40-
TrailingAnn (..))
43+
TrailingAnn (..),
44+
emptyComments, realSrcSpan)
45+
import GHC.Types.SrcLoc (generatedSrcSpan)
4146
#endif
4247
import Language.Haskell.GHC.ExactPrint (addComma)
4348

@@ -112,3 +117,21 @@ addParens True NameAnnTrailing{..} =
112117
NameAnn{nann_adornment = NameParens, nann_open=epl 0, nann_close=epl 0, nann_name = epl 0, ..}
113118
#endif
114119
addParens _ it = it
120+
121+
-- | Parenthesize an operator name for an export/import item, e.g. @(<|)@.
122+
parenthesizeName :: LocatedN RdrName -> LocatedN RdrName
123+
#if MIN_VERSION_ghc(9,9,0)
124+
parenthesizeName ln = modifyAnns ln (addParens True)
125+
#else
126+
-- A freshly built name carries EpAnnNotUsed pre-9.9, giving 'addParens' no
127+
-- NameAnn to act on, so install a concrete annotation first.
128+
parenthesizeName (L (SrcSpanAnn ann l) rdr) =
129+
L (SrcSpanAnn (EpAnn anc (addParens True nameAnn) cs) l) rdr
130+
where
131+
(anc, nameAnn, cs) = case ann of
132+
EpAnn a n c -> (a, n, c)
133+
EpAnnNotUsed -> (genAnchor0, NameAnnTrailing [], emptyComments)
134+
135+
genAnchor0 :: Anchor
136+
genAnchor0 = Anchor (realSrcSpan generatedSrcSpan) (MovedAnchor (SameLine 0))
137+
#endif

plugins/hls-export-plugin/src/Ide/Plugin/Export/ExactPrint.hs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,13 @@ import GHC (EpToken (..),
4949
#else
5050
import GHC (AddEpAnn (..))
5151
#endif
52-
import Development.IDE.GHC.ExactPrint.Annotation (addParens,
53-
ensureTrailingComma,
54-
epl, modifyAnns,
52+
import Data.Maybe (listToMaybe)
53+
import Development.IDE.GHC.ExactPrint.Annotation (ensureTrailingComma,
54+
epl, isCommaAnn,
55+
parenthesizeName,
5556
removeTrailingCommaAnn,
57+
trailingAnns,
5658
withTrailingComma)
57-
-- Only the leading-comma separator logic (9.9+) inspects trailing annotations.
58-
#if MIN_VERSION_ghc(9,9,0)
59-
import Data.Maybe (listToMaybe)
60-
import Development.IDE.GHC.ExactPrint.Annotation (isCommaAnn,
61-
trailingAnns)
62-
#endif
6359
import GHC (LocatedN)
6460
import Ide.Plugin.Export.Cursor (ExportFlavor (..))
6561
import Ide.Plugin.Export.Utils
@@ -209,7 +205,7 @@ mkWrappedName kind rdr =
209205

210206
parenthesizeOperator :: LocatedN RdrName -> LocatedN RdrName
211207
parenthesizeOperator ln
212-
| isSymOcc (rdrNameOcc (unLoc ln)) = modifyAnns ln (addParens True)
208+
| isSymOcc (rdrNameOcc (unLoc ln)) = parenthesizeName ln
213209
| otherwise = ln
214210

215211
appendIE :: LIE GhcPs -> LExportList -> LExportList
@@ -225,12 +221,8 @@ appendIE item (L l items) = L l (fixLast items ++ [newItem (not (null items))])
225221

226222
-- | The trailing comma that separates existing items, if the list has any.
227223
separatorComma :: [LIE GhcPs] -> Maybe TrailingAnn
228-
#if MIN_VERSION_ghc(9,9,0)
229224
separatorComma items =
230225
listToMaybe [c | L ann _ <- items, c <- trailingAnns ann, isCommaAnn c]
231-
#else
232-
separatorComma _ = Nothing
233-
#endif
234226

235227
-- | 'Nothing' iff @ctor@ is already exported (via @T(..)@ or @T(...,ctor,...)@).
236228
addCtorUnderParent ::

0 commit comments

Comments
 (0)