Skip to content

Commit a9f5b7c

Browse files
committed
Refactor: Extract additional Prisms/Lenses into a common module
1 parent 601c38c commit a9f5b7c

File tree

2 files changed

+52
-48
lines changed

2 files changed

+52
-48
lines changed

ghcide/src/Development/IDE/GHC/Compat/Error.hs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@ module Development.IDE.GHC.Compat.Error (
1717
DriverMessage (..),
1818
-- * General Diagnostics
1919
Diagnostic(..),
20-
-- * Prisms for error selection
20+
-- * Prisms and lenses for error selection
2121
_TcRnMessage,
2222
_TcRnMessageWithCtx,
2323
_GhcPsMessage,
2424
_GhcDsMessage,
2525
_GhcDriverMessage,
2626
_TcRnMissingSignature,
27+
_TcRnSolverReport,
28+
_TcRnMessageWithInfo,
29+
reportContextL,
30+
reportContentL,
31+
_MismatchMessage,
32+
_TypeEqMismatchActual,
33+
_TypeEqMismatchExpected,
2734
) where
2835

2936
import Control.Lens
37+
import Development.IDE.GHC.Compat (Type)
3038
import GHC.Driver.Errors.Types
3139
import GHC.HsToCore.Errors.Types
3240
import GHC.Tc.Errors.Types
@@ -82,3 +90,36 @@ msgEnvelopeErrorL :: Lens' (MsgEnvelope e) e
8290
msgEnvelopeErrorL = lens errMsgDiagnostic (\envelope e -> envelope { errMsgDiagnostic = e } )
8391

8492
makePrisms ''TcRnMessage
93+
94+
makeLensesWith
95+
(lensRules & lensField .~ mappingNamer (pure . (++ "L")))
96+
''SolverReportWithCtxt
97+
98+
-- | Focus @MismatchMsg@ from 'TcSolverReportMsg'. Currently, @MismatchMsg@ can be
99+
-- extracted from @CannotUnifyVariable@ and @Mismatch@ constructors.
100+
_MismatchMessage :: Traversal' TcSolverReportMsg MismatchMsg
101+
_MismatchMessage focus (Mismatch msg t a c) = (\msg' -> Mismatch msg' t a c) <$> focus msg
102+
_MismatchMessage focus (CannotUnifyVariable msg a) = flip CannotUnifyVariable a <$> focus msg
103+
_MismatchMessage _ report = pure report
104+
105+
-- | Focus 'teq_mismatch_expected' from @TypeEqMismatch@.
106+
_TypeEqMismatchExpected :: Traversal' MismatchMsg Type
107+
#if MIN_VERSION_ghc(9,12,0)
108+
_TypeEqMismatchExpected focus mismatch@(TypeEqMismatch _ _ _ expected _ _ _) =
109+
(\expected' -> mismatch { teq_mismatch_expected = expected' }) <$> focus expected
110+
#else
111+
_TypeEqMismatchExpected focus mismatch@(TypeEqMismatch _ _ _ _ expected _ _ _) =
112+
(\expected' -> mismatch { teq_mismatch_expected = expected' }) <$> focus expected
113+
#endif
114+
_TypeEqMismatchExpected _ mismatch = pure mismatch
115+
116+
-- | Focus 'teq_mismatch_actual from @TypeEqMismatch@.
117+
_TypeEqMismatchActual :: Traversal' MismatchMsg Type
118+
#if MIN_VERSION_ghc(9,12,0)
119+
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ actual _ _) =
120+
(\actual' -> mismatch { teq_mismatch_actual = actual' }) <$> focus actual
121+
#else
122+
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ _ actual _ _) =
123+
(\actual' -> mismatch { teq_mismatch_expected = actual' }) <$> focus actual
124+
#endif
125+
_TypeEqMismatchActual _ mismatch = pure mismatch

plugins/hls-change-type-signature-plugin/src/Ide/Plugin/ChangeTypeSignature.hs

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ import Development.IDE (FileDiagnostic,
2929
import Development.IDE.Core.PluginUtils
3030
import Development.IDE.Core.RuleTypes (GetParsedModule (GetParsedModule))
3131
import Development.IDE.GHC.Compat hiding (vcat)
32-
import Development.IDE.GHC.Compat.Error (_TcRnMessageWithCtx,
33-
msgEnvelopeErrorL)
32+
import Development.IDE.GHC.Compat.Error (_MismatchMessage,
33+
_TcRnMessageWithCtx,
34+
_TcRnMessageWithInfo,
35+
_TcRnSolverReport,
36+
_TypeEqMismatchActual,
37+
_TypeEqMismatchExpected,
38+
msgEnvelopeErrorL,
39+
reportContentL)
3440
import Development.IDE.GHC.Util (printOutputable)
3541
import Development.IDE.Types.Diagnostics (_SomeStructuredMessage)
3642
import Generics.SYB (extQ, something)
@@ -138,8 +144,8 @@ diagnosticToChangeSig recorder decls diagnostic = runMaybeT $ do
138144
(expectedType, actualType, errInfo) <- hoistMaybe $ do
139145
msg <- diagnostic ^. fdStructuredMessageL ^? _SomeStructuredMessage
140146
tcRnMsg <- msg ^. msgEnvelopeErrorL ^? _TcRnMessageWithCtx
141-
TcRnMessageDetailed errInfo tcRnMsg' <- tcRnMsg ^? _TcRnMessageDetailed
142-
solverReport <- tcRnMsg' ^? _TcRnSolverReport . tcSolverReportMsgL
147+
(_, TcRnMessageDetailed errInfo tcRnMsg') <- tcRnMsg ^? _TcRnMessageWithInfo
148+
solverReport <- tcRnMsg' ^? _TcRnSolverReport . _1 . reportContentL
143149
mismatch <- solverReport ^? _MismatchMessage
144150
expectedType <- mismatch ^? _TypeEqMismatchExpected
145151
actualType <- mismatch ^? _TypeEqMismatchActual
@@ -164,49 +170,6 @@ diagnosticToChangeSig recorder decls diagnostic = runMaybeT $ do
164170
showType :: Type -> Text
165171
showType = T.pack . showSDocUnsafe . pprTidiedType
166172

167-
_TcRnMessageDetailed :: Traversal' TcRnMessage TcRnMessageDetailed
168-
_TcRnMessageDetailed focus (TcRnMessageWithInfo errInfo detailed) =
169-
(\detailed' -> TcRnMessageWithInfo errInfo detailed') <$> focus detailed
170-
_TcRnMessageDetailed _ msg = pure msg
171-
172-
_TcRnSolverReport :: Traversal' TcRnMessage SolverReportWithCtxt
173-
#if MIN_VERSION_ghc(9,10,0)
174-
_TcRnSolverReport focus (TcRnSolverReport report reason) =
175-
(\report' -> TcRnSolverReport report' reason) <$> focus report
176-
#else
177-
_TcRnSolverReport focus (TcRnSolverReport report reason hints) =
178-
(\report' -> TcRnSolverReport report' reason hints) <$> focus report
179-
#endif
180-
_TcRnSolverReport _ msg = pure msg
181-
182-
tcSolverReportMsgL :: Lens' SolverReportWithCtxt TcSolverReportMsg
183-
tcSolverReportMsgL = lens reportContent (\report content' -> report { reportContent = content' })
184-
185-
_MismatchMessage :: Traversal' TcSolverReportMsg MismatchMsg
186-
_MismatchMessage focus (Mismatch msg t a c) = (\msg' -> Mismatch msg' t a c) <$> focus msg
187-
_MismatchMessage focus (CannotUnifyVariable msg a) = flip CannotUnifyVariable a <$> focus msg
188-
_MismatchMessage _ report = pure report
189-
190-
_TypeEqMismatchExpected :: Traversal' MismatchMsg Type
191-
#if MIN_VERSION_ghc(9,12,0)
192-
_TypeEqMismatchExpected focus mismatch@(TypeEqMismatch _ _ _ expected _ _ _) =
193-
(\expected' -> mismatch { teq_mismatch_expected = expected' }) <$> focus expected
194-
#else
195-
_TypeEqMismatchExpected focus mismatch@(TypeEqMismatch _ _ _ _ expected _ _ _) =
196-
(\expected' -> mismatch { teq_mismatch_expected = expected' }) <$> focus expected
197-
#endif
198-
_TypeEqMismatchExpected _ mismatch = pure mismatch
199-
200-
_TypeEqMismatchActual :: Traversal' MismatchMsg Type
201-
#if MIN_VERSION_ghc(9,12,0)
202-
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ actual _ _) =
203-
(\actual' -> mismatch { teq_mismatch_actual = actual' }) <$> focus actual
204-
#else
205-
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ _ actual _ _) =
206-
(\actual' -> mismatch { teq_mismatch_expected = actual' }) <$> focus actual
207-
#endif
208-
_TypeEqMismatchActual _ mismatch = pure mismatch
209-
210173
-- | If a diagnostic has the proper message create a ChangeSignature from it
211174
matchingDiagnostic :: ErrInfo -> Maybe DeclName
212175
matchingDiagnostic ErrInfo{errInfoContext} =

0 commit comments

Comments
 (0)