Skip to content

Commit

Permalink
Only show unifiable ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
isovector committed Feb 28, 2021
1 parent 557432e commit 3d79859
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Data.Maybe
import Data.Monoid
import qualified Data.Text as T
import Data.Traversable
import DataCon (dataConName)
import DataCon (dataConName, dataConCannotMatch)
import Development.IDE.GHC.Compat
import GHC.Generics
import GHC.LanguageExtensions.Type (Extension (LambdaCase))
Expand Down Expand Up @@ -80,9 +80,7 @@ commandProvider UseDataCon =
requireFeature FeatureUseDataCon $
filterTypeProjection
( guardLength (<= cfg_max_use_ctor_actions cfg)
. fromMaybe []
. fmap fst
. tacticsGetDataCons
. useCtorFilter
) $ \dcon ->
provide UseDataCon
. T.pack
Expand Down Expand Up @@ -231,3 +229,14 @@ destructFilter :: Type -> Type -> Bool
destructFilter _ (algebraicTyCon -> Just _) = True
destructFilter _ _ = False


------------------------------------------------------------------------------
-- | Only show data cons in "Use constructor" if they can unify with the goal
useCtorFilter :: Type -> [DataCon]
useCtorFilter ty
| Just (dcs, apps) <- tacticsGetDataCons ty = do
dc <- dcs
guard $ not $ dataConCannotMatch apps dc
pure dc
useCtorFilter _ = []

11 changes: 9 additions & 2 deletions plugins/hls-tactics-plugin/test/GoldenSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ spec = do
describe "provider" $ do
mkTest
"Suggests all data cons for Either"
"ConProviders.hs" 3 6
"ConProviders.hs" 5 6
[ (id, UseDataCon, "Left")
, (id, UseDataCon, "Right")
, (not, UseDataCon, ":")
Expand All @@ -89,9 +89,16 @@ spec = do
]
mkTest
"Suggests no data cons for big types"
"ConProviders.hs" 9 17 $ do
"ConProviders.hs" 11 17 $ do
c <- [1 :: Int .. 10]
pure $ (not, UseDataCon, T.pack $ show c)
mkTest
"Suggests only matching data cons for GADT"
"ConProviders.hs" 20 12
[ (id, UseDataCon, "IntGADT")
, (id, UseDataCon, "VarGADT")
, (not, UseDataCon, "BoolGADT")
]

describe "golden" $ do
useTest "(,)" "UseConPair.hs" 2 8
Expand Down
11 changes: 11 additions & 0 deletions plugins/hls-tactics-plugin/test/golden/ConProviders.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE GADTs #-}

-- Should suggest Left and Right, but not []
t1 :: Either a b
t1 = _
Expand All @@ -8,3 +10,12 @@ data ManyConstructors = C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | C10
noCtorsIfMany :: ManyConstructors
noCtorsIfMany = _


data GADT a where
IntGADT :: GADT Int
BoolGADT :: GADT Bool
VarGADT :: GADT a

gadtCtor :: GADT Int
gadtCtor = _

0 comments on commit 3d79859

Please sign in to comment.