Skip to content

Commit 3d79859

Browse files
committed
Only show unifiable ctors
1 parent 557432e commit 3d79859

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic/LanguageServer/TacticProviders.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Data.Maybe
2222
import Data.Monoid
2323
import qualified Data.Text as T
2424
import Data.Traversable
25-
import DataCon (dataConName)
25+
import DataCon (dataConName, dataConCannotMatch)
2626
import Development.IDE.GHC.Compat
2727
import GHC.Generics
2828
import GHC.LanguageExtensions.Type (Extension (LambdaCase))
@@ -80,9 +80,7 @@ commandProvider UseDataCon =
8080
requireFeature FeatureUseDataCon $
8181
filterTypeProjection
8282
( guardLength (<= cfg_max_use_ctor_actions cfg)
83-
. fromMaybe []
84-
. fmap fst
85-
. tacticsGetDataCons
83+
. useCtorFilter
8684
) $ \dcon ->
8785
provide UseDataCon
8886
. T.pack
@@ -231,3 +229,14 @@ destructFilter :: Type -> Type -> Bool
231229
destructFilter _ (algebraicTyCon -> Just _) = True
232230
destructFilter _ _ = False
233231

232+
233+
------------------------------------------------------------------------------
234+
-- | Only show data cons in "Use constructor" if they can unify with the goal
235+
useCtorFilter :: Type -> [DataCon]
236+
useCtorFilter ty
237+
| Just (dcs, apps) <- tacticsGetDataCons ty = do
238+
dc <- dcs
239+
guard $ not $ dataConCannotMatch apps dc
240+
pure dc
241+
useCtorFilter _ = []
242+

plugins/hls-tactics-plugin/test/GoldenSpec.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ spec = do
8080
describe "provider" $ do
8181
mkTest
8282
"Suggests all data cons for Either"
83-
"ConProviders.hs" 3 6
83+
"ConProviders.hs" 5 6
8484
[ (id, UseDataCon, "Left")
8585
, (id, UseDataCon, "Right")
8686
, (not, UseDataCon, ":")
@@ -89,9 +89,16 @@ spec = do
8989
]
9090
mkTest
9191
"Suggests no data cons for big types"
92-
"ConProviders.hs" 9 17 $ do
92+
"ConProviders.hs" 11 17 $ do
9393
c <- [1 :: Int .. 10]
9494
pure $ (not, UseDataCon, T.pack $ show c)
95+
mkTest
96+
"Suggests only matching data cons for GADT"
97+
"ConProviders.hs" 20 12
98+
[ (id, UseDataCon, "IntGADT")
99+
, (id, UseDataCon, "VarGADT")
100+
, (not, UseDataCon, "BoolGADT")
101+
]
95102

96103
describe "golden" $ do
97104
useTest "(,)" "UseConPair.hs" 2 8

plugins/hls-tactics-plugin/test/golden/ConProviders.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE GADTs #-}
2+
13
-- Should suggest Left and Right, but not []
24
t1 :: Either a b
35
t1 = _
@@ -8,3 +10,12 @@ data ManyConstructors = C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | C10
810
noCtorsIfMany :: ManyConstructors
911
noCtorsIfMany = _
1012

13+
14+
data GADT a where
15+
IntGADT :: GADT Int
16+
BoolGADT :: GADT Bool
17+
VarGADT :: GADT a
18+
19+
gadtCtor :: GADT Int
20+
gadtCtor = _
21+

0 commit comments

Comments
 (0)