Skip to content

Commit 2fd2d8d

Browse files
authored
Cleanup the TacticProviders interface (#1572)
1 parent 1ca533f commit 2fd2d8d

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

Diff for: plugins/hls-tactics-plugin/src/Wingman/LanguageServer/TacticProviders.hs

+34-28
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE RecordWildCards #-}
23

34
module Wingman.LanguageServer.TacticProviders
45
( commandProvider
56
, commandTactic
67
, tcCommandId
78
, TacticParams (..)
9+
, TacticProviderData (..)
810
) where
911

1012
import Control.Monad
@@ -140,14 +142,18 @@ guardLength f as = bool [] as $ f $ length as
140142
-- | A 'TacticProvider' is a way of giving context-sensitive actions to the LS
141143
-- UI.
142144
type TacticProvider
143-
= DynFlags
144-
-> Config
145-
-> PluginId
146-
-> Uri
147-
-> Range
148-
-> Judgement
145+
= TacticProviderData
149146
-> IO [Command |? CodeAction]
150147

148+
data TacticProviderData = TacticProviderData
149+
{ tpd_dflags :: DynFlags
150+
, tpd_config :: Config
151+
, tpd_plid :: PluginId
152+
, tpd_uri :: Uri
153+
, tpd_range :: Range
154+
, tpd_jdg :: Judgement
155+
}
156+
151157

152158
data TacticParams = TacticParams
153159
{ tp_file :: Uri -- ^ Uri of the file to fill the hole in
@@ -162,38 +168,37 @@ data TacticParams = TacticParams
162168
-- | Restrict a 'TacticProvider', making sure it appears only when the given
163169
-- 'Feature' is in the feature set.
164170
requireFeature :: Feature -> TacticProvider -> TacticProvider
165-
requireFeature f tp dflags cfg plId uri range jdg = do
166-
case hasFeature f $ cfg_feature_set cfg of
167-
True -> tp dflags cfg plId uri range jdg
171+
requireFeature f tp tpd =
172+
case hasFeature f $ cfg_feature_set $ tpd_config tpd of
173+
True -> tp tpd
168174
False -> pure []
169175

170176

171177
------------------------------------------------------------------------------
172178
-- | Restrict a 'TacticProvider', making sure it appears only when the given
173179
-- predicate holds for the goal.
174180
requireExtension :: Extension -> TacticProvider -> TacticProvider
175-
requireExtension ext tp dflags cfg plId uri range jdg =
176-
case xopt ext dflags of
177-
True -> tp dflags cfg plId uri range jdg
181+
requireExtension ext tp tpd =
182+
case xopt ext $ tpd_dflags tpd of
183+
True -> tp tpd
178184
False -> pure []
179185

180186

181187
------------------------------------------------------------------------------
182188
-- | Restrict a 'TacticProvider', making sure it appears only when the given
183189
-- predicate holds for the goal.
184190
filterGoalType :: (Type -> Bool) -> TacticProvider -> TacticProvider
185-
filterGoalType p tp dflags cfg plId uri range jdg =
186-
case p $ unCType $ jGoal jdg of
187-
True -> tp dflags cfg plId uri range jdg
191+
filterGoalType p tp tpd =
192+
case p $ unCType $ jGoal $ tpd_jdg tpd of
193+
True -> tp tpd
188194
False -> pure []
189195

190196

191197
------------------------------------------------------------------------------
192198
-- | Restrict a 'TacticProvider', making sure it appears only when the given
193199
-- predicate holds for the goal.
194200
withJudgement :: (Judgement -> TacticProvider) -> TacticProvider
195-
withJudgement tp dflags fs plId uri range jdg =
196-
tp jdg dflags fs plId uri range jdg
201+
withJudgement tp tpd = tp (tpd_jdg tpd) tpd
197202

198203

199204
------------------------------------------------------------------------------
@@ -203,13 +208,14 @@ filterBindingType
203208
:: (Type -> Type -> Bool) -- ^ Goal and then binding types.
204209
-> (OccName -> Type -> TacticProvider)
205210
-> TacticProvider
206-
filterBindingType p tp dflags cfg plId uri range jdg =
207-
let hy = jHypothesis jdg
208-
g = jGoal jdg
211+
filterBindingType p tp tpd =
212+
let jdg = tpd_jdg tpd
213+
hy = jHypothesis jdg
214+
g = jGoal jdg
209215
in fmap join $ for (unHypothesis hy) $ \hi ->
210216
let ty = unCType $ hi_type hi
211217
in case p (unCType g) ty of
212-
True -> tp (hi_name hi) ty dflags cfg plId uri range jdg
218+
True -> tp (hi_name hi) ty tpd
213219
False -> pure []
214220

215221

@@ -220,15 +226,15 @@ filterTypeProjection
220226
:: (Type -> [a]) -- ^ Features of the goal to look into further
221227
-> (a -> TacticProvider)
222228
-> TacticProvider
223-
filterTypeProjection p tp dflags cfg plId uri range jdg =
224-
fmap join $ for (p $ unCType $ jGoal jdg) $ \a ->
225-
tp a dflags cfg plId uri range jdg
229+
filterTypeProjection p tp tpd =
230+
fmap join $ for (p $ unCType $ jGoal $ tpd_jdg tpd) $ \a ->
231+
tp a tpd
226232

227233

228234
------------------------------------------------------------------------------
229235
-- | Get access to the 'Config' when building a 'TacticProvider'.
230236
withConfig :: (Config -> TacticProvider) -> TacticProvider
231-
withConfig tp dflags cfg plId uri range jdg = tp cfg dflags cfg plId uri range jdg
237+
withConfig tp tpd = tp (tpd_config tpd) tpd
232238

233239

234240

@@ -247,10 +253,10 @@ useNameFromHypothesis f name = do
247253
-- | Terminal constructor for providing context-sensitive tactics. Tactics
248254
-- given by 'provide' are always available.
249255
provide :: TacticCommand -> T.Text -> TacticProvider
250-
provide tc name _ _ plId uri range _ = do
256+
provide tc name TacticProviderData{..} = do
251257
let title = tacticTitle tc name
252-
params = TacticParams { tp_file = uri , tp_range = range , tp_var_name = name }
253-
cmd = mkLspCommand plId (tcCommandId tc) title (Just [toJSON params])
258+
params = TacticParams { tp_file = tpd_uri , tp_range = tpd_range , tp_var_name = name }
259+
cmd = mkLspCommand tpd_plid (tcCommandId tc) title (Just [toJSON params])
254260
pure
255261
$ pure
256262
$ InR

Diff for: plugins/hls-tactics-plugin/src/Wingman/Plugin.hs

+8-7
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ codeActionProvider state plId (CodeActionParams _ _ (TextDocumentIdentifier uri)
6060
(_, jdg, _, dflags) <- judgementForHole state nfp range $ cfg_feature_set cfg
6161
actions <- lift $
6262
-- This foldMap is over the function monoid.
63-
foldMap commandProvider [minBound .. maxBound]
64-
dflags
65-
cfg
66-
plId
67-
uri
68-
range
69-
jdg
63+
foldMap commandProvider [minBound .. maxBound] $ TacticProviderData
64+
{ tpd_dflags = dflags
65+
, tpd_config = cfg
66+
, tpd_plid = plId
67+
, tpd_uri = uri
68+
, tpd_range = range
69+
, tpd_jdg = jdg
70+
}
7071
pure $ Right $ List actions
7172
codeActionProvider _ _ _ = pure $ Right $ List []
7273

0 commit comments

Comments
 (0)