Skip to content

Commit ab3f84a

Browse files
committed
refactor
1 parent 7bc5843 commit ab3f84a

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

plugins/hls-explicit-record-fields-plugin/src/Ide/Plugin/ExplicitFields.hs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ import Language.LSP.Protocol.Types (CodeAction (..),
107107
WorkspaceEdit (WorkspaceEdit),
108108
type (|?) (InL, InR))
109109

110-
#if __GLASGOW_HASKELL__ < 910
110+
111111
import Development.IDE.GHC.Compat (HsExpansion (HsExpanded))
112-
#endif
112+
113113

114114
data Log
115115
= LogShake Shake.Log
@@ -271,11 +271,11 @@ collectNamesRule = defineNoDiagnostics mempty $ \CollectNames nfp -> runMaybeT $
271271
-- | Collects all 'Name's of a given source file, to be used
272272
-- in the variable usage analysis.
273273
getNames :: TcModuleResult -> UniqFM Name [Name]
274-
#if __GLASGOW_HASKELL__ < 910
274+
275275
getNames (tmrRenamed -> (group,_,_,_)) = collectNames group
276-
#else
277-
getNames (tmrRenamed -> (group,_,_,_,_)) = collectNames group
278-
#endif
276+
277+
278+
279279

280280
data CollectRecords = CollectRecords
281281
deriving (Eq, Show, Generic)
@@ -441,10 +441,16 @@ showRecordPat names = fmap printOutputable . mapConPatDetail (\case
441441
_ -> Nothing)
442442

443443
showRecordPatFlds :: Pat GhcTc -> Maybe [Name]
444-
showRecordPatFlds (ConPat _ _ args) = fmap (fmap ((\case FieldOcc x _ -> getName x) . unLoc . hfbLHS . unLoc) . rec_flds) (m args)
444+
showRecordPatFlds (ConPat _ _ args) = do
445+
fields <- processRecCon args
446+
names <- mapM getFieldName (rec_flds fields)
447+
pure names
445448
where
446-
m (RecCon flds) = Just $ processRecordFlds flds
447-
m _ = Nothing
449+
processRecCon (RecCon flds) = Just $ processRecordFlds flds
450+
processRecCon _ = Nothing
451+
getOccName (FieldOcc x _) = Just $ getName x
452+
getOccName _ = Nothing
453+
getFieldName = getOccName . unLoc . hfbLHS . unLoc
448454
showRecordPatFlds _ = Nothing
449455

450456
showRecordCon :: Outputable (HsExpr (GhcPass c)) => HsExpr (GhcPass c) -> Maybe Text
@@ -454,10 +460,12 @@ showRecordCon expr@(RecordCon _ _ flds) =
454460
showRecordCon _ = Nothing
455461

456462
showRecordConFlds :: p ~ GhcTc => HsExpr p -> Maybe [Name]
457-
showRecordConFlds (RecordCon _ _ flds) = mapM (m . unLoc . hfbRHS . unLoc) (rec_flds $ processRecordFlds flds)
463+
showRecordConFlds (RecordCon _ _ flds) =
464+
mapM getFieldName (rec_flds $ processRecordFlds flds)
458465
where
459-
m (HsVar _ lidp) = Just $ getName lidp
460-
m _ = Nothing
466+
getVarName (HsVar _ lidp) = Just $ getName lidp
467+
getVarName _ = Nothing
468+
getFieldName = getVarName . unLoc . hfbRHS . unLoc
461469
showRecordConFlds _ = Nothing
462470

463471

@@ -485,11 +493,11 @@ getRecCons :: LHsExpr GhcTc -> ([RecordInfo], Bool)
485493
-- because there is a possibility that there were be more than one result per
486494
-- branch
487495

488-
#if __GLASGOW_HASKELL__ >= 910
489-
getRecCons (unLoc -> XExpr (ExpandedThingTc a _)) = (collectRecords a, False)
490-
#else
496+
497+
498+
491499
getRecCons (unLoc -> XExpr (ExpansionExpr (HsExpanded _ a))) = (collectRecords a, True)
492-
#endif
500+
493501
getRecCons e@(unLoc -> RecordCon _ _ flds)
494502
| isJust (rec_dotdot flds) = (mkRecInfo e, False)
495503
where

0 commit comments

Comments
 (0)