Skip to content

Commit 5556c3e

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

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
@@ -138,12 +139,13 @@ diagnosticToChangeSig recorder decls diagnostic = runMaybeT $ do
138139
(expectedType, actualType, errInfo) <- hoistMaybe $ do
139140
msg <- diagnostic ^. fdStructuredMessageL ^? _SomeStructuredMessage
140141
tcRnMsg <- msg ^. msgEnvelopeErrorL ^? _TcRnMessage
141-
(solverReport, errInfo) <- findSolverReport tcRnMsg
142-
mismatch <- findMismatchMessage solverReport
143-
(expectedType', actualType') <- findTypeEqMismatch mismatch
144-
errInfo' <- errInfo
142+
TcRnMessageDetailed errInfo tcRnMsg' <- tcRnMsg ^? _TcRnMessageDetailed
143+
solverReport <- tcRnMsg' ^? _TcRnSolverReport . tcSolverReportMsgL
144+
mismatch <- solverReport ^? _MismatchMessage
145+
expectedType <- mismatch ^? _TypeEqMismatchExpected
146+
actualType <- mismatch ^? _TypeEqMismatchActual
145147

146-
pure (showType expectedType', showType actualType', errInfo')
148+
pure (showType expectedType, showType actualType, errInfo)
147149

148150
logWith recorder Debug (LogErrInfoCtxt errInfo)
149151

@@ -163,35 +165,48 @@ diagnosticToChangeSig recorder decls diagnostic = runMaybeT $ do
163165
showType :: Type -> Text
164166
showType = T.pack . showSDocUnsafe . pprTidiedType
165167

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
168+
_TcRnMessageDetailed :: Traversal' TcRnMessage TcRnMessageDetailed
169+
_TcRnMessageDetailed focus (TcRnMessageWithInfo errInfo detailed) =
170+
(\detailed' -> TcRnMessageWithInfo errInfo detailed') <$> focus detailed
171+
_TcRnMessageDetailed _ msg = pure msg
172+
173+
_TcRnSolverReport :: Traversal' TcRnMessage SolverReportWithCtxt
172174
#if MIN_VERSION_ghc(9,10,0)
173-
findSolverReport (TcRnSolverReport (SolverReportWithCtxt _ mismatch) _) =
174-
Just (mismatch, Nothing)
175+
_TcRnSolverReport focus (TcRnSolverReport report reason) =
176+
(\report' -> TcRnSolverReport report' reason) <$> focus report
175177
#else
176-
findSolverReport (TcRnSolverReport (SolverReportWithCtxt _ mismatch) _ _) =
177-
Just (mismatch, Nothing)
178+
_TcRnSolverReport focus (TcRnSolverReport report reason hints) =
179+
(\report' -> TcRnSolverReport report' reason hints) <$> focus report
178180
#endif
179-
findSolverReport _ = Nothing
181+
_TcRnSolverReport _ msg = pure msg
182+
183+
tcSolverReportMsgL :: Lens' SolverReportWithCtxt TcSolverReportMsg
184+
tcSolverReportMsgL = lens reportContent (\report content' -> report { reportContent = content' })
185+
186+
_MismatchMessage :: Traversal' TcSolverReportMsg MismatchMsg
187+
_MismatchMessage focus (Mismatch msg t a c) = (\msg' -> Mismatch msg' t a c) <$> focus msg
188+
_MismatchMessage focus (CannotUnifyVariable msg a) = flip CannotUnifyVariable a <$> focus msg
189+
_MismatchMessage _ report = pure report
180190

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
191+
_TypeEqMismatchExpected :: Traversal' MismatchMsg Type
192+
#if MIN_VERSION_ghc(9,12,0)
193+
_TypeEqMismatchExpected focus mismatch@(TypeEqMismatch _ _ _ expected _ _ _) =
194+
(\expected' -> mismatch { teq_mismatch_expected = expected' }) <$> focus expected
195+
#else
196+
_TypeEqMismatchExpected focus mismatch@(TypeEqMismatch _ _ _ expected _ _ _ _) =
197+
(\expected' -> mismatch { teq_mismatch_expected = expected' }) <$> focus expected
198+
#endif
199+
_TypeEqMismatchExpected _ mismatch = pure mismatch
186200

187-
-- TODO: Make this a prism?
188-
findTypeEqMismatch :: MismatchMsg -> Maybe (Type, Type)
201+
_TypeEqMismatchActual :: Traversal' MismatchMsg Type
189202
#if MIN_VERSION_ghc(9,12,0)
190-
findTypeEqMismatch (TypeEqMismatch _ _ _ expected actual _ _) = Just (expected, actual)
203+
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ actual _ _) =
204+
(\actual' -> mismatch { teq_mismatch_actual = actual' }) <$> focus actual
191205
#else
192-
findTypeEqMismatch (TypeEqMismatch _ _ _ _ expected actual _ _) = Just (expected, actual)
206+
_TypeEqMismatchActual focus mismatch@(TypeEqMismatch _ _ _ _ actual _ _ _) =
207+
(\actual' -> mismatch { teq_mismatch_expected = actual' }) <$> focus actual
193208
#endif
194-
findTypeEqMismatch _ = Nothing
209+
_TypeEqMismatchActual _ mismatch = pure mismatch
195210

196211
-- | If a diagnostic has the proper message create a ChangeSignature from it
197212
matchingDiagnostic :: ErrInfo -> Maybe DeclName
@@ -207,8 +222,7 @@ matchingDiagnostic ErrInfo{errInfoContext} =
207222
-- | List of regexes that match various Error Messages
208223
errorMessageRegexes :: [Text]
209224
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 `(.+)':"
225+
"In an equation for ‘(.+)’:"
212226
]
213227

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

0 commit comments

Comments
 (0)