@@ -139,11 +139,12 @@ spec = describe "code actions" $ do
139
139
, " main = when True $ putStrLn \" hello\" "
140
140
]
141
141
, -- Multiple import lists, should not introduce multiple newlines.
142
- [ " import Data.Maybe ( fromMaybe )"
143
- , " import Control.Monad ( when )"
144
- , " import System.IO ( hPutStrLn"
145
- , " , stdout"
142
+ [ " import System.IO ( stdout"
143
+ , " , hPutStrLn"
146
144
, " )"
145
+ , " import Control.Monad ( when )"
146
+ , " import Data.Maybe ( fromMaybe )"
147
+ , " main :: IO ()"
147
148
, " main ="
148
149
, " when True"
149
150
, " $ hPutStrLn stdout"
@@ -164,9 +165,10 @@ spec = describe "code actions" $ do
164
165
, " main = when True $ putStrLn \" hello\" "
165
166
]
166
167
, -- Multiple import lists, should not introduce multiple newlines.
167
- [ " import Data.Maybe (fromMaybe )"
168
+ [ " import System.IO (stdout, hPutStrLn )"
168
169
, " import Control.Monad (when)"
169
- , " import System.IO (hPutStrLn, stdout)"
170
+ , " import Data.Maybe (fromMaybe)"
171
+ , " main :: IO ()"
170
172
, " main ="
171
173
, " when True"
172
174
, " $ hPutStrLn stdout"
@@ -469,7 +471,7 @@ spec = describe "code actions" $ do
469
471
-- Parameterized HsImport Spec.
470
472
-- ---------------------------------------------------------------------
471
473
hsImportSpec :: T. Text -> [[T. Text ]]-> Spec
472
- hsImportSpec formatterName [e1, e2, _ ] =
474
+ hsImportSpec formatterName [e1, e2, e3 ] =
473
475
describe (" Execute HsImport with formatter " <> T. unpack formatterName) $ do
474
476
it " works with 3.8 code action kinds" $ runSession hieCommand fullCaps " test/testdata" $ do
475
477
doc <- openDoc " CodeActionImport.hs" " haskell"
@@ -529,50 +531,48 @@ hsImportSpec formatterName [e1, e2, _] =
529
531
contents <- getDocumentEdit doc
530
532
liftIO $ T. lines contents `shouldMatchList` e2
531
533
532
- -- it "multiple import-list formats" $ runSession hieCommand fullCaps "test/testdata" $ do
533
- -- doc <- openDoc "CodeActionImportList.hs" "haskell"
534
- -- _ <- waitForDiagnosticsSource "ghcmod"
535
-
536
- -- let config = def { formattingProvider = formatterName }
537
- -- sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
538
-
539
- -- let wantedCodeActionTitles = [ "Import module System.IO (hPutStrLn)"
540
- -- , "Import module System.IO (stdout)"
541
- -- , "Import module Control.Monad (when)"
542
- -- , "Import module Data.Maybe (fromMaybe)"
543
- -- ]
544
-
545
- -- mapM_ (const (executeCodeActionByName doc wantedCodeActionTitles)) wantedCodeActionTitles
546
-
547
- -- contents <- getDocumentEdit doc
548
- -- liftIO $ T.lines contents `shouldBe` e3
549
-
550
- -- it "respects format config, multiple import-list" $ runSession hieCommand fullCaps "test/testdata" $ do
551
- -- doc <- openDoc "CodeActionImportList.hs" "haskell"
552
- -- _ <- waitForDiagnosticsSource "ghcmod"
553
-
554
- -- let config = def { formatOnImportOn = False, formattingProvider = formatterName }
555
- -- sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
556
-
557
- -- let wantedCodeActionTitles = [ "Import module System.IO (hPutStrLn)"
558
- -- , "Import module System.IO (stdout)"
559
- -- , "Import module Control.Monad (when)"
560
- -- , "Import module Data.Maybe (fromMaybe)"
561
- -- ]
562
-
563
- -- mapM_ (const (executeCodeActionByName doc wantedCodeActionTitles)) wantedCodeActionTitles
564
-
565
- -- contents <- getDocumentEdit doc
566
- -- liftIO $ T.lines contents `shouldBe`
567
- -- [ "import Data.Maybe (fromMaybe)"
568
- -- , "import Control.Monad (when)"
569
- -- , "import System.IO (hPutStrLn, stdout)"
570
- -- , "main :: IO ()"
571
- -- , "main ="
572
- -- , "when True"
573
- -- , " $ hPutStrLn stdout"
574
- -- , " $ fromMaybe \"Good night, World!\" (Just \"Hello, World!\")]"
575
- -- ]
534
+ it " multiple import-list formats" $ runSession hieCommand fullCaps " test/testdata" $ do
535
+ doc <- openDoc " CodeActionImportList.hs" " haskell"
536
+
537
+ let config = def { formattingProvider = formatterName }
538
+ sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
539
+
540
+ let wantedCodeActionTitles = [ " Import module System.IO (hPutStrLn)"
541
+ , " Import module System.IO (stdout)"
542
+ , " Import module Control.Monad (when)"
543
+ , " Import module Data.Maybe (fromMaybe)"
544
+ ]
545
+
546
+ executeAllCodeActions doc wantedCodeActionTitles
547
+
548
+ contents <- documentContents doc
549
+ liftIO $ T. lines contents `shouldBe` e3
550
+
551
+ it " respects format config, multiple import-list" $ runSession hieCommand fullCaps " test/testdata" $ do
552
+ doc <- openDoc " CodeActionImportList.hs" " haskell"
553
+
554
+ let config = def { formatOnImportOn = False , formattingProvider = formatterName }
555
+ sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
556
+
557
+ let wantedCodeActionTitles = [ " Import module System.IO (hPutStrLn)"
558
+ , " Import module System.IO (stdout)"
559
+ , " Import module Control.Monad (when)"
560
+ , " Import module Data.Maybe (fromMaybe)"
561
+ ]
562
+
563
+ executeAllCodeActions doc wantedCodeActionTitles
564
+
565
+ contents <- documentContents doc
566
+ liftIO $ T. lines contents `shouldBe`
567
+ [ " import System.IO (stdout, hPutStrLn)"
568
+ , " import Control.Monad (when)"
569
+ , " import Data.Maybe (fromMaybe)"
570
+ , " main :: IO ()"
571
+ , " main ="
572
+ , " when True"
573
+ , " $ hPutStrLn stdout"
574
+ , " $ fromMaybe \" Good night, World!\" (Just \" Hello, World!\" )"
575
+ ]
576
576
it " respects format config" $ runSession hieCommand fullCaps " test/testdata" $ do
577
577
doc <- openDoc " CodeActionImportBrittany.hs" " haskell"
578
578
_ <- waitForDiagnosticsSource " ghcmod"
@@ -610,19 +610,27 @@ hsImportSpec formatterName [e1, e2, _] =
610
610
l2 `shouldBe` " import Control.Monad (when)"
611
611
l3 `shouldBe` " main :: IO ()"
612
612
l4 `shouldBe` " main = when True $ putStrLn \" hello\" "
613
- -- where
614
- -- executeCodeActionByName :: TextDocumentIdentifier -> [T.Text] -> Session ()
615
- -- executeCodeActionByName doc names = do
616
- -- actionsOrCommands <- getAllCodeActions doc
617
- -- let allActions = map fromAction actionsOrCommands
618
- -- let actions = filter (\actn -> actn ^. L.title `elem` names) allActions
619
- -- case actions of
620
- -- (action:_) -> executeCodeAction action
621
- -- xs ->
622
- -- error
623
- -- $ "Found an unexpected amount of action. Expected 1, but got: "
624
- -- ++ show (length xs)
625
- -- ++ "\n. Titles: " ++ show (map (^. L.title) allActions)
613
+ where
614
+ executeAllCodeActions :: TextDocumentIdentifier -> [T. Text ] -> Session ()
615
+ executeAllCodeActions doc names =
616
+ replicateM_ (length names) $ do
617
+ _ <- waitForDiagnosticsSource " ghcmod"
618
+ executeCodeActionByName doc names
619
+ _ <- skipManyTill publishDiagnosticsNotification $ getDocumentEdit doc
620
+ waitForDiagnosticsSource " ghcmod"
621
+
622
+ executeCodeActionByName :: TextDocumentIdentifier -> [T. Text ] -> Session ()
623
+ executeCodeActionByName doc names = do
624
+ actionsOrCommands <- getAllCodeActions doc
625
+ let allActions = map fromAction actionsOrCommands
626
+ let actions = filter (\ actn -> actn ^. L. title `elem` names) allActions
627
+ case actions of
628
+ (action: _) -> executeCodeAction action
629
+ xs ->
630
+ error
631
+ $ " Found an unexpected amount of action. Expected 1, but got: "
632
+ ++ show (length xs)
633
+ ++ " \n . Titles: " ++ show (map (^. L. title) allActions)
626
634
627
635
-- Silence warnings
628
636
hsImportSpec formatter args =
0 commit comments