Skip to content

Commit 67ff25f

Browse files
committed
It works as a code lens!
1 parent 8230d0c commit 67ff25f

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

plugins/hls-tactics-plugin/src/Wingman/CodeGen.hs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
{-# LANGUAGE FlexibleContexts #-}
2-
{-# LANGUAGE OverloadedLabels #-}
3-
{-# LANGUAGE TupleSections #-}
4-
{-# LANGUAGE TypeApplications #-}
1+
{-# LANGUAGE FlexibleContexts #-}
2+
{-# LANGUAGE OverloadedLabels #-}
3+
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE TupleSections #-}
5+
{-# LANGUAGE TypeApplications #-}
56

67
module Wingman.CodeGen
78
( module Wingman.CodeGen
@@ -65,7 +66,7 @@ destructMatches f scrut t jdg = do
6566
args = conLikeInstOrigArgTys' con apps
6667
modify $ appEndo $ foldMap (Endo . evidenceToSubst) ev
6768
subst <- gets ts_unifier
68-
names <- mkManyGoodNames (hyNamesInScope hy) args
69+
let names = mkManyGoodNames (hyNamesInScope hy) args
6970
let hy' = patternHypothesis scrut con jdg
7071
$ zip names
7172
$ coerce args
@@ -81,6 +82,20 @@ destructMatches f scrut t jdg = do
8182
& #syn_val %~ match [mkDestructPat con names] . unLoc
8283

8384

85+
destructionFor :: Hypothesis a -> Type -> Maybe [RawMatch]
86+
destructionFor hy t = do
87+
case tacticsGetDataCons t of
88+
Nothing -> Nothing
89+
Just ([], _) -> Nothing
90+
Just (dcs, apps) -> do
91+
for dcs $ \dc -> do
92+
let con = RealDataCon dc
93+
args = conLikeInstOrigArgTys' con apps
94+
names = mkManyGoodNames (hyNamesInScope hy) args
95+
pure $ match [mkDestructPat con names] $ var "_"
96+
97+
98+
8499
------------------------------------------------------------------------------
85100
-- | Produces a pattern for a data con and the names of its fields.
86101
mkDestructPat :: ConLike -> [OccName] -> Pat GhcPs

plugins/hls-tactics-plugin/src/Wingman/Naming.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ mkGoodName in_scope t =
7777
------------------------------------------------------------------------------
7878
-- | Like 'mkGoodName' but creates several apart names.
7979
mkManyGoodNames
80-
:: (Traversable t, Monad m)
80+
:: (Traversable t)
8181
=> Set OccName
8282
-> t Type
83-
-> m (t OccName)
83+
-> t OccName
8484
mkManyGoodNames in_scope args =
85-
flip evalStateT in_scope $ for args $ \at -> do
85+
flip evalState in_scope $ for args $ \at -> do
8686
in_scope <- get
8787
let n = mkGoodName in_scope at
8888
modify $ S.insert n

plugins/hls-tactics-plugin/src/Wingman/Plugin.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import Data.Aeson
1515
import Data.Bifunctor (first)
1616
import Data.Data
1717
import Data.Foldable (for_)
18+
import Data.Functor ((<&>))
19+
import qualified Data.HashMap.Strict as HM
1820
import Data.Maybe
1921
import qualified Data.Text as T
22+
import Development.IDE (realSrcSpanToRange)
2023
import Development.IDE.Core.Shake (IdeState (..))
2124
import Development.IDE.Core.UseStale (Tracked, TrackedStale(..), unTrack, mapAgeFrom, unsafeMkCurrent)
2225
import Development.IDE.GHC.Compat
@@ -36,9 +39,8 @@ import Wingman.Machinery (scoreSolution)
3639
import Wingman.Range
3740
import Wingman.Tactics
3841
import Wingman.Types
39-
import Data.Functor ((<&>))
40-
import Development.IDE (srcSpanToRange, realSrcSpanToRange)
41-
import qualified Data.HashMap.Strict as HM
42+
import Wingman.CodeGen (destructionFor)
43+
import GHC.SourceGen (case', var)
4244

4345

4446
descriptor :: PluginId -> PluginDescriptor IdeState
@@ -76,15 +78,16 @@ completionProvider state plId (CodeLensParams _ _ (TextDocumentIdentifier uri))
7678
holes <- completionForHole state nfp $ cfg_feature_set cfg
7779
traceMX "found holes" holes
7880
pure $ Right $ List $ holes <&> \(ss, ty) ->
79-
let range = realSrcSpanToRange $ unTrack ss
81+
let range@(Range _ ep) = realSrcSpanToRange $ unTrack ss
82+
rep_range = Range ep ep
8083
in CodeLens
8184
range
8285
(Just $ mkLspCommand plId
8386
(CommandId "emptycase.complete")
84-
"Complete case constructors" $
87+
("Complete case constructors (" <> T.pack (unsafeRender ty) <> ")") $
8588
Just $ pure $ toJSON $
8689
WorkspaceEdit
87-
(Just $ HM.singleton uri $ List $ pure $ TextEdit range $ T.pack $ unsafeRender ty)
90+
(Just $ HM.singleton uri $ List $ pure $ TextEdit rep_range $ T.pack $ mappend "\n" $ unlines $ drop 1 $ lines $ unsafeRender $ case' (var "_") $ fromJust $ destructionFor mempty ty)
8891
Nothing
8992
Nothing
9093
)

plugins/hls-tactics-plugin/src/Wingman/Tactics.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ intros = rule $ \jdg -> do
9999
case tcSplitFunTys $ unCType g of
100100
([], _) -> throwError $ GoalMismatch "intros" g
101101
(as, b) -> do
102-
vs <- mkManyGoodNames (hyNamesInScope $ jEntireHypothesis jdg) as
102+
let vs = mkManyGoodNames (hyNamesInScope $ jEntireHypothesis jdg) as
103103
let top_hole = isTopHole ctx jdg
104104
hy' = lambdaHypothesis top_hole $ zip vs $ coerce as
105105
jdg' = introduce hy'

0 commit comments

Comments
 (0)