Skip to content

Wingman: Don't suggest empty case lenses for case exprs with no data cons #1962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions plugins/hls-tactics-plugin/src/Wingman/EmptyCase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,19 @@ emptyCaseScrutinees state nfp = do
hscenv <- stale GhcSessionDeps

let scrutinees = traverse (emptyCaseQ . tcg_binds) tcg
for scrutinees $ \aged@(unTrack -> (ss, scrutinee)) -> do
fmap catMaybes $ for scrutinees $ \aged@(unTrack -> (ss, scrutinee)) -> do
ty <- MaybeT
. fmap (scrutinzedType <=< sequence)
. traverse (typeCheck (hscEnv $ untrackedStaleValue hscenv) tcg')
$ scrutinee
case ss of
RealSrcSpan r -> do
rss' <- liftMaybe $ mapAgeTo tcg_map $ unsafeCopyAge aged r
pure (rss', ty)
UnhelpfulSpan _ -> empty
case null $ tacticsGetDataCons ty of
True -> pure empty
False ->
case ss of
RealSrcSpan r -> do
rss' <- liftMaybe $ mapAgeTo tcg_map $ unsafeCopyAge aged r
pure $ Just (rss', ty)
UnhelpfulSpan _ -> empty

data EmptyCaseSort a
= EmptyCase a
Expand Down
4 changes: 4 additions & 0 deletions plugins/hls-tactics-plugin/test/CodeLens/EmptyCaseSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Utils
spec :: Spec
spec = do
let test = mkCodeLensTest
noTest = mkNoCodeLensTest

describe "golden" $ do
test "EmptyCaseADT"
Expand All @@ -19,3 +20,6 @@ spec = do
test "EmptyCaseGADT"
test "EmptyCaseLamCase"

describe "no code lenses" $ do
noTest "EmptyCaseSpuriousGADT"

16 changes: 14 additions & 2 deletions plugins/hls-tactics-plugin/test/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import Control.Monad.IO.Class
import Data.Aeson
import Data.Foldable
import Data.Function (on)
import qualified Data.Map as M
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Ide.Plugin.Config as Plugin
import Ide.Plugin.Tactic as Tactic
import Language.LSP.Types
import Language.LSP.Types.Lens hiding (actions, applyEdit, capabilities, executeCommand, id, line, message, name, rename, title)
Expand Down Expand Up @@ -129,6 +127,20 @@ mkCodeLensTest input =
liftIO $ edited `shouldBe` expected


------------------------------------------------------------------------------
-- | A test that no code lenses can be run in the file
mkNoCodeLensTest
:: FilePath
-> SpecWith ()
mkNoCodeLensTest input =
it (input <> " (no code lenses)") $ do
runSessionWithServer plugin tacticPath $ do
doc <- openDoc (input <.> "hs") "haskell"
_ <- waitForDiagnostics
lenses <- fmap (reverse . filter isWingmanLens) $ getCodeLenses doc
liftIO $ lenses `shouldBe` []



isWingmanLens :: CodeLens -> Bool
isWingmanLens (CodeLens _ (Just (Command _ cmd _)) _)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE GADTs #-}

data Foo a where
Foo :: Foo Int

foo :: Foo Bool -> ()
foo x = case x of