Skip to content

Commit 4ef3904

Browse files
committed
REWORDME: Make maybes into traversals
1 parent ed4d061 commit 4ef3904

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

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

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import Ide.Types (Config, HandlerM,
5252
import Language.LSP.Protocol.Message
5353
import Language.LSP.Protocol.Types
5454
import Text.Regex.TDFA ((=~))
55+
import Control.Applicative (liftA)
5556

5657
data Log
5758
= LogErrInfoCtxt ErrInfo
@@ -137,12 +138,13 @@ diagnosticToChangeSig recorder decls diagnostic = runMaybeT $ do
137138
(expectedType, actualType, errInfo) <- hoistMaybe $ do
138139
msg <- diagnostic ^. fdStructuredMessageL ^? _SomeStructuredMessage
139140
tcRnMsg <- msg ^. msgEnvelopeErrorL ^? _TcRnMessage
140-
(solverReport, errInfo) <- findSolverReport tcRnMsg
141-
mismatch <- findMismatchMessage solverReport
142-
(expectedType', actualType') <- findTypeEqMismatch mismatch
143-
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
144146

145-
pure (showType expectedType', showType actualType', errInfo')
147+
pure (showType expectedType, showType actualType, errInfo)
146148

147149
logWith recorder Development.IDE.Warning (LogErrInfoCtxt errInfo)
148150

@@ -160,35 +162,48 @@ diagnosticToChangeSig recorder decls diagnostic = runMaybeT $ do
160162
showType :: Type -> Text
161163
showType = T.pack . showSDocUnsafe . pprTidiedType
162164

163-
-- TODO: Make this a prism?
164-
findSolverReport :: TcRnMessage -> Maybe (TcSolverReportMsg, Maybe ErrInfo)
165-
findSolverReport (TcRnMessageWithInfo _ (TcRnMessageDetailed errInfo msg)) =
166-
case findSolverReport msg of
167-
Just (mismatch, _) -> Just (mismatch, Just errInfo)
168-
_ -> Nothing
165+
_TcRnMessageDetailed :: Traversal' TcRnMessage TcRnMessageDetailed
166+
_TcRnMessageDetailed focus (TcRnMessageWithInfo errInfo detailed) =
167+
(\detailed' -> TcRnMessageWithInfo errInfo detailed') <$> focus detailed
168+
_TcRnMessageDetailed _ msg = pure msg
169+
170+
_TcRnSolverReport :: Traversal' TcRnMessage SolverReportWithCtxt
169171
#if MIN_VERSION_ghc(9,10,0)
170-
findSolverReport (TcRnSolverReport (SolverReportWithCtxt _ mismatch) _) =
171-
Just (mismatch, Nothing)
172+
_TcRnSolverReport focus (TcRnSolverReport report reason) =
173+
(\report' -> TcRnSolverReport report' reason) <$> focus report
172174
#else
173-
findSolverReport (TcRnSolverReport (SolverReportWithCtxt _ mismatch) _ _) =
174-
Just (mismatch, Nothing)
175+
_TcRnSolverReport focus (TcRnSolverReport report reason hints) =
176+
(\report' -> TcRnSolverReport report' reason hints) <$> focus report
175177
#endif
176-
findSolverReport _ = Nothing
178+
_TcRnSolverReport _ msg = pure msg
179+
180+
tcSolverReportMsgL :: Lens' SolverReportWithCtxt TcSolverReportMsg
181+
tcSolverReportMsgL = lens reportContent (\report content' -> report { reportContent = content' })
182+
183+
_MismatchMessage :: Traversal' TcSolverReportMsg MismatchMsg
184+
_MismatchMessage focus (Mismatch msg t a c) = (\msg' -> Mismatch msg' t a c) <$> focus msg
185+
_MismatchMessage focus (CannotUnifyVariable msg a) = flip CannotUnifyVariable a <$> focus msg
186+
_MismatchMessage _ report = pure report
177187

178-
-- TODO: Make this a prism?
179-
findMismatchMessage :: TcSolverReportMsg -> Maybe MismatchMsg
180-
findMismatchMessage (Mismatch m _ _ _) = Just m
181-
findMismatchMessage (CannotUnifyVariable m _) = Just m
182-
findMismatchMessage _ = Nothing
188+
_TypeEqMismatchExpected :: Traversal' MismatchMsg Type
189+
#if MIN_VERSION_ghc(9,12,0)
190+
_TypeEqMismatchExpected focus mismatch@(TypeEqMismatch _ _ _ expected _ _ _) =
191+
(\expected' -> mismatch { teq_mismatch_expected = expected' }) <$> focus expected
192+
#else
193+
_TypeEqMismatchExpected focus mismatch@(TypeEqMismatch _ _ _ expected _ _ _ _) =
194+
(\expected' -> mismatch { teq_mismatch_expected = expected' }) <$> focus expected
195+
#endif
196+
_TypeEqMismatchExpected _ mismatch = pure mismatch
183197

184-
-- TODO: Make this a prism?
185-
findTypeEqMismatch :: MismatchMsg -> Maybe (Type, Type)
198+
_TypeEqMismatchActual :: Traversal' MismatchMsg Type
186199
#if MIN_VERSION_ghc(9,12,0)
187-
findTypeEqMismatch (TypeEqMismatch _ _ _ expected actual _ _) = Just (expected, actual)
200+
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ actual _ _) =
201+
(\actual' -> mismatch { teq_mismatch_actual = actual' }) <$> focus actual
188202
#else
189-
findTypeEqMismatch (TypeEqMismatch _ _ _ _ expected actual _ _) = Just (expected, actual)
203+
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ actual _ _ _) =
204+
(\actual' -> mismatch { teq_mismatch_expected = actual' }) <$> focus actual
190205
#endif
191-
findTypeEqMismatch _ = Nothing
206+
_TypeEqMismatchActual _ mismatch = pure mismatch
192207

193208
-- | If a diagnostic has the proper message create a ChangeSignature from it
194209
matchingDiagnostic :: ErrInfo -> Maybe DeclName
@@ -204,8 +219,7 @@ matchingDiagnostic ErrInfo{errInfoContext} =
204219
-- | List of regexes that match various Error Messages
205220
errorMessageRegexes :: [Text]
206221
errorMessageRegexes = [ -- be sure to add new Error Messages Regexes at the bottom to not fail any existing tests
207-
"In an equation for ‘(.+)’:" -- TODO: Check if this is useful only for tests
208-
, "In an equation for `(.+)':"
222+
"In an equation for ‘(.+)’:"
209223
]
210224

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

0 commit comments

Comments
 (0)