Skip to content

Commit a8d1a4b

Browse files
committed
Refactor: Turn some getter functions into Lenses/Treversals
1 parent e26ef51 commit a8d1a4b

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

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

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,13 @@ diagnosticToChangeSig recorder decls diagnostic = runMaybeT $ do
138138
(expectedType, actualType, errInfo) <- hoistMaybe $ do
139139
msg <- diagnostic ^. fdStructuredMessageL ^? _SomeStructuredMessage
140140
tcRnMsg <- msg ^. msgEnvelopeErrorL ^? _TcRnMessage
141-
(solverReport, errInfo) <- findSolverReport tcRnMsg
142-
mismatch <- findMismatchMessage solverReport
143-
(expectedType', actualType') <- findTypeEqMismatch mismatch
144-
errInfo' <- errInfo
141+
TcRnMessageDetailed errInfo tcRnMsg' <- tcRnMsg ^? _TcRnMessageDetailed
142+
solverReport <- tcRnMsg' ^? _TcRnSolverReport . tcSolverReportMsgL
143+
mismatch <- solverReport ^? _MismatchMessage
144+
expectedType <- mismatch ^? _TypeEqMismatchExpected
145+
actualType <- mismatch ^? _TypeEqMismatchActual
145146

146-
pure (showType expectedType', showType actualType', errInfo')
147+
pure (showType expectedType, showType actualType, errInfo)
147148

148149
logWith recorder Debug (LogErrInfoCtxt errInfo)
149150

@@ -163,35 +164,48 @@ diagnosticToChangeSig recorder decls diagnostic = runMaybeT $ do
163164
showType :: Type -> Text
164165
showType = T.pack . showSDocUnsafe . pprTidiedType
165166

166-
-- TODO: Make this a prism?
167-
findSolverReport :: TcRnMessage -> Maybe (TcSolverReportMsg, Maybe ErrInfo)
168-
findSolverReport (TcRnMessageWithInfo _ (TcRnMessageDetailed errInfo msg)) =
169-
case findSolverReport msg of
170-
Just (mismatch, _) -> Just (mismatch, Just errInfo)
171-
_ -> Nothing
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
172173
#if MIN_VERSION_ghc(9,10,0)
173-
findSolverReport (TcRnSolverReport (SolverReportWithCtxt _ mismatch) _) =
174-
Just (mismatch, Nothing)
174+
_TcRnSolverReport focus (TcRnSolverReport report reason) =
175+
(\report' -> TcRnSolverReport report' reason) <$> focus report
175176
#else
176-
findSolverReport (TcRnSolverReport (SolverReportWithCtxt _ mismatch) _ _) =
177-
Just (mismatch, Nothing)
177+
_TcRnSolverReport focus (TcRnSolverReport report reason hints) =
178+
(\report' -> TcRnSolverReport report' reason hints) <$> focus report
178179
#endif
179-
findSolverReport _ = Nothing
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
180189

181-
-- TODO: Make this a prism?
182-
findMismatchMessage :: TcSolverReportMsg -> Maybe MismatchMsg
183-
findMismatchMessage (Mismatch m _ _ _) = Just m
184-
findMismatchMessage (CannotUnifyVariable m _) = Just m
185-
findMismatchMessage _ = Nothing
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
186199

187-
-- TODO: Make this a prism?
188-
findTypeEqMismatch :: MismatchMsg -> Maybe (Type, Type)
200+
_TypeEqMismatchActual :: Traversal' MismatchMsg Type
189201
#if MIN_VERSION_ghc(9,12,0)
190-
findTypeEqMismatch (TypeEqMismatch _ _ _ expected actual _ _) = Just (expected, actual)
202+
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ actual _ _) =
203+
(\actual' -> mismatch { teq_mismatch_actual = actual' }) <$> focus actual
191204
#else
192-
findTypeEqMismatch (TypeEqMismatch _ _ _ _ expected actual _ _) = Just (expected, actual)
205+
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ _ actual _ _) =
206+
(\actual' -> mismatch { teq_mismatch_expected = actual' }) <$> focus actual
193207
#endif
194-
findTypeEqMismatch _ = Nothing
208+
_TypeEqMismatchActual _ mismatch = pure mismatch
195209

196210
-- | If a diagnostic has the proper message create a ChangeSignature from it
197211
matchingDiagnostic :: ErrInfo -> Maybe DeclName
@@ -207,8 +221,7 @@ matchingDiagnostic ErrInfo{errInfoContext} =
207221
-- | List of regexes that match various Error Messages
208222
errorMessageRegexes :: [Text]
209223
errorMessageRegexes = [ -- be sure to add new Error Messages Regexes at the bottom to not fail any existing tests
210-
"In an equation for ‘(.+)’:" -- TODO: Check if this is useful only for tests
211-
, "In an equation for `(.+)':"
224+
"In an equation for ‘(.+)’:"
212225
]
213226

214227
-- | Given a String with the name of a declaration, GHC's "Expected Type", find the declaration that matches

0 commit comments

Comments
 (0)