Skip to content
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

Fix duplication of code actions for adding NamedFieldPuns #1334

Merged
merged 4 commits into from
Feb 9, 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
12 changes: 2 additions & 10 deletions plugins/default/src/Ide/Plugin/Pragmas.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ module Ide.Plugin.Pragmas
) where

import Control.Lens hiding (List)
import Data.Aeson
import qualified Data.HashMap.Strict as H
import qualified Data.Text as T
import Development.IDE as D
import qualified GHC.Generics as Generics
import Ide.Types
import Language.Haskell.LSP.Types
import qualified Language.Haskell.LSP.Types as J
Expand All @@ -26,6 +24,7 @@ import Development.IDE.GHC.Compat
import qualified Language.Haskell.LSP.Core as LSP
import qualified Language.Haskell.LSP.VFS as VFS
import qualified Text.Fuzzy as Fuzzy
import Data.List.Extra (nubOrd)

-- ---------------------------------------------------------------------

Expand All @@ -37,13 +36,6 @@ descriptor plId = (defaultPluginDescriptor plId)

-- ---------------------------------------------------------------------

-- | Parameters for the addPragma PluginCommand.
data AddPragmaParams = AddPragmaParams
{ file :: J.Uri -- ^ Uri of the file to add the pragma to
, pragma :: T.Text -- ^ Name of the Pragma to add
}
deriving (Show, Eq, Generics.Generic, ToJSON, FromJSON)

-- | Add a Pragma to the given URI at the top of the file.
-- Pragma is added to the first line of the Uri.
-- It is assumed that the pragma name is a valid pragma,
Expand All @@ -68,7 +60,7 @@ codeActionProvider _ state _plId docId _ (J.CodeActionContext (J.List diags) _mo
pm <- fmap join $ runAction "addPragma" state $ getParsedModule `traverse` mFile
let dflags = ms_hspp_opts . pm_mod_summary <$> pm
-- Get all potential Pragmas for all diagnostics.
pragmas = concatMap (\d -> genPragma dflags (d ^. J.message)) diags
pragmas = nubOrd $ concatMap (\d -> genPragma dflags (d ^. J.message)) diags
cmds <- mapM mkCodeAction pragmas
return $ Right $ List cmds
where
Expand Down
30 changes: 30 additions & 0 deletions test/functional/FunctionalCodeAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,36 @@ missingPragmaTests = testGroup "missing pragma warning code actions" [
, "foo = id @a"
]

liftIO $ T.lines contents @?= expected
, testCase "No duplication" $ do
runSession hlsCommand fullCaps "test/testdata/addPragmas" $ do
doc <- openDoc "NamedFieldPuns.hs" "haskell"

_ <- waitForDiagnosticsFrom doc
cas <- map fromAction <$> getCodeActions doc (Range (Position 8 9) (Position 8 9))

liftIO $ length cas == 1 @? "Expected one code action, but got: " <> show cas
let ca = head cas

liftIO $ (ca ^. L.title == "Add \"NamedFieldPuns\"") @? "NamedFieldPuns code action"

executeCodeAction ca

contents <- documentContents doc

let expected =
[ "{-# LANGUAGE NamedFieldPuns #-}"
, "module NamedFieldPuns where"
, ""
, "data Record = Record"
, " { a :: Int,"
, " b :: Double,"
, " c :: String"
, " }"
, ""
, "f Record{a, b} = a"
]

liftIO $ T.lines contents @?= expected
]

Expand Down
9 changes: 9 additions & 0 deletions test/testdata/addPragmas/NamedFieldPuns.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module NamedFieldPuns where

data Record = Record
{ a :: Int,
b :: Double,
c :: String
}

f Record{a, b} = a
1 change: 1 addition & 0 deletions test/testdata/addPragmas/hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ cradle:
arguments:
- "NeedsPragmas"
- "TypeApplications"
- "NamedFieldPuns"
19 changes: 0 additions & 19 deletions test/testdata/addPragmas/test.cabal

This file was deleted.