Skip to content

Commit 691d821

Browse files
authored
Fix duplication of code actions for adding NamedFieldPuns (#1334)
* Fix duplication of code actions for adding NamedFieldPuns * Avoid pattern matching in do notation * Add range
1 parent 0a4514a commit 691d821

File tree

5 files changed

+42
-29
lines changed

5 files changed

+42
-29
lines changed

plugins/default/src/Ide/Plugin/Pragmas.hs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ module Ide.Plugin.Pragmas
1111
) where
1212

1313
import Control.Lens hiding (List)
14-
import Data.Aeson
1514
import qualified Data.HashMap.Strict as H
1615
import qualified Data.Text as T
1716
import Development.IDE as D
18-
import qualified GHC.Generics as Generics
1917
import Ide.Types
2018
import Language.Haskell.LSP.Types
2119
import qualified Language.Haskell.LSP.Types as J
@@ -26,6 +24,7 @@ import Development.IDE.GHC.Compat
2624
import qualified Language.Haskell.LSP.Core as LSP
2725
import qualified Language.Haskell.LSP.VFS as VFS
2826
import qualified Text.Fuzzy as Fuzzy
27+
import Data.List.Extra (nubOrd)
2928

3029
-- ---------------------------------------------------------------------
3130

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

3837
-- ---------------------------------------------------------------------
3938

40-
-- | Parameters for the addPragma PluginCommand.
41-
data AddPragmaParams = AddPragmaParams
42-
{ file :: J.Uri -- ^ Uri of the file to add the pragma to
43-
, pragma :: T.Text -- ^ Name of the Pragma to add
44-
}
45-
deriving (Show, Eq, Generics.Generic, ToJSON, FromJSON)
46-
4739
-- | Add a Pragma to the given URI at the top of the file.
4840
-- Pragma is added to the first line of the Uri.
4941
-- It is assumed that the pragma name is a valid pragma,
@@ -68,7 +60,7 @@ codeActionProvider _ state _plId docId _ (J.CodeActionContext (J.List diags) _mo
6860
pm <- fmap join $ runAction "addPragma" state $ getParsedModule `traverse` mFile
6961
let dflags = ms_hspp_opts . pm_mod_summary <$> pm
7062
-- Get all potential Pragmas for all diagnostics.
71-
pragmas = concatMap (\d -> genPragma dflags (d ^. J.message)) diags
63+
pragmas = nubOrd $ concatMap (\d -> genPragma dflags (d ^. J.message)) diags
7264
cmds <- mapM mkCodeAction pragmas
7365
return $ Right $ List cmds
7466
where

test/functional/FunctionalCodeAction.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,36 @@ missingPragmaTests = testGroup "missing pragma warning code actions" [
517517
, "foo = id @a"
518518
]
519519

520+
liftIO $ T.lines contents @?= expected
521+
, testCase "No duplication" $ do
522+
runSession hlsCommand fullCaps "test/testdata/addPragmas" $ do
523+
doc <- openDoc "NamedFieldPuns.hs" "haskell"
524+
525+
_ <- waitForDiagnosticsFrom doc
526+
cas <- map fromAction <$> getCodeActions doc (Range (Position 8 9) (Position 8 9))
527+
528+
liftIO $ length cas == 1 @? "Expected one code action, but got: " <> show cas
529+
let ca = head cas
530+
531+
liftIO $ (ca ^. L.title == "Add \"NamedFieldPuns\"") @? "NamedFieldPuns code action"
532+
533+
executeCodeAction ca
534+
535+
contents <- documentContents doc
536+
537+
let expected =
538+
[ "{-# LANGUAGE NamedFieldPuns #-}"
539+
, "module NamedFieldPuns where"
540+
, ""
541+
, "data Record = Record"
542+
, " { a :: Int,"
543+
, " b :: Double,"
544+
, " c :: String"
545+
, " }"
546+
, ""
547+
, "f Record{a, b} = a"
548+
]
549+
520550
liftIO $ T.lines contents @?= expected
521551
]
522552

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module NamedFieldPuns where
2+
3+
data Record = Record
4+
{ a :: Int,
5+
b :: Double,
6+
c :: String
7+
}
8+
9+
f Record{a, b} = a

test/testdata/addPragmas/hie.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ cradle:
33
arguments:
44
- "NeedsPragmas"
55
- "TypeApplications"
6+
- "NamedFieldPuns"

test/testdata/addPragmas/test.cabal

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)