forked from haskell/haskell-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionalCodeAction.hs
705 lines (570 loc) · 29.5 KB
/
FunctionalCodeAction.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
{-# LANGUAGE CPP #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module FunctionalCodeAction (tests) where
import Control.Applicative.Combinators
import Control.Lens hiding (List)
import Control.Monad
import Control.Monad.IO.Class
import Data.Aeson
import Data.Default
import qualified Data.HashMap.Strict as HM
import Data.List
import Data.Maybe
import qualified Data.Text as T
import Ide.Plugin.Config
import Language.LSP.Test as Test
import Language.LSP.Types
import qualified Language.LSP.Types.Capabilities as C
import qualified Language.LSP.Types.Lens as L
import Test.Hls.Util
import Test.Hspec.Expectations
import System.FilePath ((</>))
import System.IO.Extra (withTempDir)
import Test.Tasty
import Test.Tasty.ExpectedFailure (expectFailBecause,
ignoreTestBecause)
import Test.Tasty.HUnit
{-# ANN module ("HLint: ignore Reduce duplication"::String) #-}
tests :: TestTree
tests = testGroup "code actions" [
hlintTests
, importTests
, missingPragmaTests
, disableWarningTests
, packageTests
, redundantImportTests
, renameTests
, signatureTests
, typedHoleTests
, unusedTermTests
]
hlintTests :: TestTree
hlintTests = testGroup "hlint suggestions" [
testCase "provides 3.8 code actions including apply all" $ runHlintSession "" $ do
doc <- openDoc "ApplyRefact2.hs" "haskell"
diags@(reduceDiag:_) <- waitForDiagnosticsFromSource doc "hlint"
liftIO $ do
length diags @?= 2 -- "Eta Reduce" and "Redundant Id"
reduceDiag ^. L.range @?= Range (Position 1 0) (Position 1 12)
reduceDiag ^. L.severity @?= Just DsInfo
reduceDiag ^. L.code @?= Just (InR "refact:Eta reduce")
reduceDiag ^. L.source @?= Just "hlint"
cas <- map fromAction <$> getAllCodeActions doc
let applyAll = find (\ca -> "Apply all hints" `T.isSuffixOf` (ca ^. L.title)) cas
let redId = find (\ca -> "Redundant id" `T.isSuffixOf` (ca ^. L.title)) cas
let redEta = find (\ca -> "Eta reduce" `T.isSuffixOf` (ca ^. L.title)) cas
liftIO $ isJust applyAll @? "There is 'Apply all hints' code action"
liftIO $ isJust redId @? "There is 'Redundant id' code action"
liftIO $ isJust redEta @? "There is 'Eta reduce' code action"
executeCodeAction (fromJust redId)
contents <- skipManyTill anyMessage $ getDocumentEdit doc
liftIO $ contents @?= "main = undefined\nfoo x = x\n"
, testCase "falls back to pre 3.8 code actions" $ runSession hlsCommand noLiteralCaps "test/testdata/hlint" $ do
doc <- openDoc "ApplyRefact2.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "hlint"
cars <- getAllCodeActions doc
etaReduce <- liftIO $ inspectCommand cars ["Eta reduce"]
executeCommand etaReduce
contents <- skipManyTill anyMessage $ getDocumentEdit doc
liftIO $ contents @?= "main = undefined\nfoo = id\n"
, testCase "changing configuration enables or disables hlint diagnostics" $ runHlintSession "" $ do
let config = def { hlintOn = True }
sendNotification SWorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
doc <- openDoc "ApplyRefact2.hs" "haskell"
testHlintDiagnostics doc
let config' = def { hlintOn = False }
sendNotification SWorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config'))
diags' <- waitForDiagnosticsFrom doc
liftIO $ noHlintDiagnostics diags'
, testCase "changing document contents updates hlint diagnostics" $ runHlintSession "" $ do
doc <- openDoc "ApplyRefact2.hs" "haskell"
testHlintDiagnostics doc
let change = TextDocumentContentChangeEvent
(Just (Range (Position 1 8) (Position 1 12)))
Nothing "x"
changeDoc doc [change]
expectNoMoreDiagnostics 3 doc "hlint"
let change' = TextDocumentContentChangeEvent
(Just (Range (Position 1 8) (Position 1 12)))
Nothing "id x"
changeDoc doc [change']
testHlintDiagnostics doc
, knownBrokenForGhcVersions [GHC88, GHC86] "hlint doesn't take in account cpp flag as ghc -D argument" $
testCase "hlint diagnostics works with CPP via ghc -XCPP argument (#554)" $ runHlintSession "cpp" $ do
doc <- openDoc "ApplyRefact3.hs" "haskell"
testHlintDiagnostics doc
, knownBrokenForGhcVersions [GHC88, GHC86] "hlint doesn't take in account cpp flag as ghc -D argument" $
testCase "hlint diagnostics works with CPP via language pragma (#554)" $ runHlintSession "" $ do
doc <- openDoc "ApplyRefact3.hs" "haskell"
testHlintDiagnostics doc
, testCase "hlint diagnostics works with CPP via -XCPP argument and flag via #include header (#554)" $ runHlintSession "cpp" $ do
doc <- openDoc "ApplyRefact2.hs" "haskell"
testHlintDiagnostics doc
, testCase "apply-refact works with -XLambdaCase argument (#590)" $ runHlintSession "lambdacase" $ do
testRefactor "ApplyRefact1.hs" "Redundant bracket"
expectedLambdaCase
, testCase "apply-refact works with -XTypeApplications argument (#1242)" $ runHlintSession "typeapps" $ do
testRefactor "ApplyRefact1.hs" "Redundant bracket"
expectedTypeApp
, testCase "apply hints works with LambdaCase via language pragma" $ runHlintSession "" $ do
testRefactor "ApplyRefact1.hs" "Redundant bracket"
("{-# LANGUAGE LambdaCase #-}" : expectedLambdaCase)
, expectFailBecause "apply-refact doesn't work with cpp" $
testCase "apply hints works with CPP via -XCPP argument" $ runHlintSession "cpp" $ do
testRefactor "ApplyRefact3.hs" "Redundant bracket"
expectedCPP
, expectFailBecause "apply-refact doesn't work with cpp" $
testCase "apply hints works with CPP via language pragma" $ runHlintSession "" $ do
testRefactor "ApplyRefact3.hs" "Redundant bracket"
("{-# LANGUAGE CPP #-}" : expectedCPP)
, testCase "hlint diagnostics ignore hints honouring .hlint.yaml" $ runHlintSession "ignore" $ do
doc <- openDoc "ApplyRefact.hs" "haskell"
expectNoMoreDiagnostics 3 doc "hlint"
, testCase "hlint diagnostics ignore hints honouring ANN annotations" $ runHlintSession "" $ do
doc <- openDoc "ApplyRefact4.hs" "haskell"
expectNoMoreDiagnostics 3 doc "hlint"
, knownBrokenForGhcVersions [GHC810] "hlint plugin doesn't honour HLINT annotations (#838)" $
testCase "hlint diagnostics ignore hints honouring HLINT annotations" $ runHlintSession "" $ do
doc <- openDoc "ApplyRefact5.hs" "haskell"
expectNoMoreDiagnostics 3 doc "hlint"
, testCase "apply-refact preserve regular comments" $ runHlintSession "" $ do
testRefactor "ApplyRefact6.hs" "Redundant bracket" expectedComments
]
where
runHlintSession :: FilePath -> Session a -> IO a
runHlintSession subdir =
failIfSessionTimeout . runSession hlsCommand fullCaps ("test/testdata/hlint" </> subdir)
noHlintDiagnostics :: [Diagnostic] -> Assertion
noHlintDiagnostics diags =
Just "hlint" `notElem` map (^. L.source) diags @? "There are no hlint diagnostics"
testHlintDiagnostics doc = do
diags <- waitForDiagnosticsFromSource doc "hlint"
liftIO $ length diags > 0 @? "There are hlint diagnostics"
testRefactor file caTitle expected = do
doc <- openDoc file "haskell"
testHlintDiagnostics doc
cas <- map fromAction <$> getAllCodeActions doc
let ca = find (\ca -> caTitle `T.isSuffixOf` (ca ^. L.title)) cas
liftIO $ isJust ca @? ("There is '" ++ T.unpack caTitle ++"' code action")
executeCodeAction (fromJust ca)
contents <- skipManyTill anyMessage $ getDocumentEdit doc
liftIO $ contents @?= T.unlines expected
expectedLambdaCase = [ "module ApplyRefact1 where", ""
, "f = \\case \"true\" -> True"
, " _ -> False"
]
expectedCPP = [ "module ApplyRefact3 where", ""
, "#ifdef FLAG"
, "f = 1"
, "#else"
, "g = 2"
, "#endif", ""
]
expectedComments = [ "-- comment before header"
, "module ApplyRefact6 where", ""
, "{-# standalone annotation #-}", ""
, "-- standalone comment", ""
, "-- | haddock comment"
, "f = {- inline comment -}{- inline comment inside refactored code -} 1 -- ending comment", ""
, "-- final comment"
]
expectedTypeApp = [ "module ApplyRefact1 where", ""
, "a = id @Int 1"
]
renameTests :: TestTree
renameTests = testGroup "rename suggestions" [
testCase "works" $ runSession hlsCommand noLiteralCaps "test/testdata" $ do
doc <- openDoc "CodeActionRename.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "typecheck"
cars <- getAllCodeActions doc
replaceButStrLn <- liftIO $ inspectCommand cars ["Replace with", "putStrLn"]
executeCommand replaceButStrLn
_ <- anyRequest
x:_ <- T.lines <$> documentContents doc
liftIO $ x @?= "main = putStrLn \"hello\""
, testCase "doesn't give both documentChanges and changes"
$ runSession hlsCommand noLiteralCaps "test/testdata" $ do
doc <- openDoc "CodeActionRename.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "typecheck"
cars <- getAllCodeActions doc
cmd <- liftIO $ inspectCommand cars ["Replace with", "putStrLn"]
let Just (List [Object args]) = cmd ^. L.arguments
Object editParams = args HM.! "fallbackWorkspaceEdit"
liftIO $ do
"changes" `HM.member` editParams @? "Contains changes"
not ("documentChanges" `HM.member` editParams) @? "Doesn't contain documentChanges"
executeCommand cmd
_ <- anyRequest
x1:x2:_ <- T.lines <$> documentContents doc
liftIO $
x1 == "main = putStrLn \"hello\""
|| x2 == "foo = putStrLn \"world\""
@? "One of the typos got fixed"
]
importTests :: TestTree
importTests = testGroup "import suggestions" [
testCase "works with 3.8 code action kinds" $ runSession hlsCommand fullCaps "test/testdata" $ do
doc <- openDoc "CodeActionImport.hs" "haskell"
-- No Formatting:
let config = def { formattingProvider = "none" }
sendNotification SWorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
(diag:_) <- waitForDiagnosticsFrom doc
liftIO $ diag ^. L.message @?= "Variable not in scope: when :: Bool -> IO () -> IO ()"
actionsOrCommands <- getAllCodeActions doc
let actns = map fromAction actionsOrCommands
importControlMonad <- liftIO $ inspectCodeAction actionsOrCommands ["import Control.Monad"]
liftIO $ do
expectCodeAction actionsOrCommands ["import Control.Monad (when)"]
length actns >= 10 @? "There are some actions"
executeCodeAction importControlMonad
contents <- documentContents doc
liftIO $ contents @?= "import Control.Monad\nmain :: IO ()\nmain = when True $ putStrLn \"hello\""
]
packageTests :: TestTree
packageTests = testGroup "add package suggestions" [
ignoreTestBecause "no support for adding dependent packages via code action" $ testCase "adds to .cabal files" $ do
flushStackEnvironment
runSession hlsCommand fullCaps "test/testdata/addPackageTest/cabal-exe" $ do
doc <- openDoc "AddPackage.hs" "haskell"
-- ignore the first empty hlint diagnostic publish
[_,diag:_] <- count 2 $ waitForDiagnosticsFrom doc
let prefixes = [ "Could not load module `Data.Text'" -- Windows && GHC >= 8.6
, "Could not find module `Data.Text'" -- Windows
, "Could not load module ‘Data.Text’" -- GHC >= 8.6
, "Could not find module ‘Data.Text’"
]
in liftIO $ any (`T.isPrefixOf` (diag ^. L.message)) prefixes @? "Contains prefix"
acts <- getAllCodeActions doc
let (InR action:_) = acts
liftIO $ do
action ^. L.title @?= "Add text as a dependency"
action ^. L.kind @?= Just CodeActionQuickFix
"package:add" `T.isSuffixOf` (action ^. L.command . _Just . L.command) @? "Command contains package:add"
executeCodeAction action
contents <- skipManyTill anyMessage $ getDocumentEdit . TextDocumentIdentifier =<< getDocUri "add-package-test.cabal"
liftIO $
any (\l -> "text -any" `T.isSuffixOf` l || "text : {} -any" `T.isSuffixOf` l) (T.lines contents) @? "Contains text package"
, ignoreTestBecause "no support for adding dependent packages via code action" $ testCase "adds to hpack package.yaml files" $
runSession hlsCommand fullCaps "test/testdata/addPackageTest/hpack-exe" $ do
doc <- openDoc "app/Asdf.hs" "haskell"
-- ignore the first empty hlint diagnostic publish
[_,_:diag:_] <- count 2 $ waitForDiagnosticsFrom doc
let prefixes = [ "Could not load module `Codec.Compression.GZip'" -- Windows && GHC >= 8.6
, "Could not find module `Codec.Compression.GZip'" -- Windows
, "Could not load module ‘Codec.Compression.GZip’" -- GHC >= 8.6
, "Could not find module ‘Codec.Compression.GZip’"
]
in liftIO $ any (`T.isPrefixOf` (diag ^. L.message)) prefixes @? "Diagnostic contains message"
mActions <- getAllCodeActions doc
let allActions = map fromAction mActions
action = head allActions
liftIO $ do
action ^. L.title @?= "Add zlib as a dependency"
forM_ allActions $ \a -> a ^. L.kind @?= Just CodeActionQuickFix
forM_ allActions $ \a -> "package:add" `T.isSuffixOf` (a ^. L.command . _Just . L.command) @? "Command contains package:add"
executeCodeAction action
contents <- skipManyTill anyMessage $ getDocumentEdit . TextDocumentIdentifier =<< getDocUri "package.yaml"
liftIO $ do
"zlib" `T.isSuffixOf` (T.lines contents !! 3) @? "Contains zlib"
"zlib" `T.isSuffixOf` (T.lines contents !! 21) @? "Does not contain zlib in unrelated component"
]
redundantImportTests :: TestTree
redundantImportTests = testGroup "redundant import code actions" [
testCase "remove solitary redundant imports" $
runSession hlsCommand fullCaps "test/testdata/redundantImportTest/" $ do
doc <- openDoc "src/CodeActionRedundant.hs" "haskell"
diags <- waitForDiagnosticsFromSource doc "typecheck"
liftIO $ expectDiagnostic diags ["The import of", "Data.List", "is redundant"]
mActions <- getAllCodeActions doc
let allActions = map fromAction mActions
actionTitles = map (view L.title) allActions
liftIO $ actionTitles `shouldContain` ["Remove import", "Remove all redundant imports"]
let Just removeAction = find (\x -> x ^. L.title == "Remove import") allActions
liftIO $ do
forM_ allActions $ \a -> a ^. L.kind @?= Just CodeActionQuickFix
forM_ allActions $ \a -> a ^. L.command @?= Nothing
forM_ allActions $ \a -> isJust (a ^. L.edit) @? "Has edit"
executeCodeAction removeAction
-- No command/applyworkspaceedit should be here, since action
-- provides workspace edit property which skips round trip to
-- the server
contents <- documentContents doc
liftIO $ contents @?= "{-# OPTIONS_GHC -Wunused-imports #-}\nmodule CodeActionRedundant where\nmain :: IO ()\nmain = putStrLn \"hello\"\n"
, testCase "doesn't touch other imports" $ runSession hlsCommand noLiteralCaps "test/testdata/redundantImportTest/" $ do
doc <- openDoc "src/MultipleImports.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "typecheck"
InL cmd : _ <- getAllCodeActions doc
executeCommand cmd
_ <- anyRequest
contents <- documentContents doc
liftIO $ T.lines contents @?=
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module MultipleImports where"
, "import Data.Maybe"
, "foo :: Int"
, "foo = fromJust (Just 3)"
]
]
typedHoleTests :: TestTree
typedHoleTests = testGroup "typed hole code actions" [
testCase "works" $
runSession hlsCommand fullCaps "test/testdata" $ do
doc <- openDoc "TypedHoles.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "typecheck"
cas <- getAllCodeActions doc
liftIO $ do
expectCodeAction cas ["replace _ with minBound"]
expectCodeAction cas ["replace _ with foo _"]
replaceWithMaxBound <- liftIO $ inspectCodeAction cas ["replace _ with maxBound"]
executeCodeAction replaceWithMaxBound
contents <- documentContents doc
liftIO $ contents @?= T.concat
[ "module TypedHoles where\n"
, "foo :: [Int] -> Int\n"
, "foo x = maxBound"
]
, testCase "shows more suggestions" $
runSession hlsCommand fullCaps "test/testdata" $ do
doc <- openDoc "TypedHoles2.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "typecheck"
cas <- getAllCodeActions doc
liftIO $ do
expectCodeAction cas ["replace _ with foo2 _"]
expectCodeAction cas ["replace _ with A _"]
replaceWithStuff <- liftIO $ inspectCodeAction cas ["replace _ with stuff _"]
executeCodeAction replaceWithStuff
contents <- documentContents doc
liftIO $ T.lines contents @?=
[ "module TypedHoles2 (foo2) where"
, "newtype A = A Int"
, "foo2 :: [A] -> A"
, "foo2 x = (stuff _)"
, " where"
, " stuff (A a) = A (a + 1)"
]
]
signatureTests :: TestTree
signatureTests = testGroup "missing top level signature code actions" [
testCase "Adds top level signature" $
runSession hlsCommand fullCaps "test/testdata/" $ do
doc <- openDoc "TopLevelSignature.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "typecheck"
cas <- map fromAction <$> getAllCodeActions doc
liftIO $ "add signature: main :: IO ()" `elem` map (^. L.title) cas @? "Contains code action"
executeCodeAction $ head cas
contents <- documentContents doc
let expected = [ "{-# OPTIONS_GHC -Wall #-}"
, "module TopLevelSignature where"
, "main :: IO ()"
, "main = do"
, " putStrLn \"Hello\""
, " return ()"
]
liftIO $ T.lines contents @?= expected
]
missingPragmaTests :: TestTree
missingPragmaTests = testGroup "missing pragma warning code actions" [
testCase "Adds TypeSynonymInstances pragma" $ do
runSession hlsCommand fullCaps "test/testdata/addPragmas" $ do
doc <- openDoc "NeedsPragmas.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "typecheck"
cas <- map fromAction <$> getAllCodeActions doc
liftIO $ "Add \"TypeSynonymInstances\"" `elem` map (^. L.title) cas @? "Contains TypeSynonymInstances code action"
liftIO $ "Add \"FlexibleInstances\"" `elem` map (^. L.title) cas @? "Contains FlexibleInstances code action"
executeCodeAction $ head cas
contents <- documentContents doc
let expected = [ "{-# LANGUAGE TypeSynonymInstances #-}"
, "module NeedsPragmas where"
, ""
, "import GHC.Generics"
, ""
, "main = putStrLn \"hello\""
, ""
, "type Foo = Int"
, ""
, "instance Show Foo where"
, " show x = undefined"
, ""
, "instance Show (Int,String) where"
, " show = undefined"
, ""
, "data FFF a = FFF Int String a"
, " deriving (Generic,Functor,Traversable)"
]
liftIO $ T.lines contents @?= expected
, testCase "Adds TypeApplications pragma" $ do
runSession hlsCommand fullCaps "test/testdata/addPragmas" $ do
doc <- openDoc "TypeApplications.hs" "haskell"
_ <- waitForDiagnosticsFrom doc
cas <- map fromAction <$> getAllCodeActions doc
liftIO $ "Add \"TypeApplications\"" `elem` map (^. L.title) cas @? "Contains TypeApplications code action"
executeCodeAction $ head cas
contents <- documentContents doc
let expected =
-- TODO: Why CPP???
#if __GLASGOW_HASKELL__ < 810
[ "{-# LANGUAGE ScopedTypeVariables #-}"
, "{-# LANGUAGE TypeApplications #-}"
#else
[ "{-# LANGUAGE TypeApplications #-}"
, "{-# LANGUAGE ScopedTypeVariables #-}"
#endif
, "module TypeApplications where"
, ""
, "foo :: forall a. a -> a"
, "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
, testCase "After Shebang" $ do
runSession hlsCommand fullCaps "test/testdata/addPragmas" $ do
doc <- openDoc "AfterShebang.hs" "haskell"
_ <- waitForDiagnosticsFrom doc
cas <- map fromAction <$> getAllCodeActions doc
liftIO $ "Add \"NamedFieldPuns\"" `elem` map (^. L.title) cas @? "Contains NamedFieldPuns code action"
executeCodeAction $ head cas
contents <- documentContents doc
let expected =
[ "#! /usr/bin/env nix-shell"
, "#! nix-shell --pure -i runghc -p \"haskellPackages.ghcWithPackages (hp: with hp; [ turtle ])\""
, ""
, "{-# LANGUAGE NamedFieldPuns #-}"
, "module AfterShebang where"
, ""
, "data Record = Record"
, " { a :: Int,"
, " b :: Double,"
, " c :: String"
, " }"
, ""
, "f Record{a, b} = a"
]
liftIO $ T.lines contents @?= expected
]
disableWarningTests :: TestTree
disableWarningTests =
testGroup "disable warnings" $
[
( "missing-signatures"
, T.unlines
[ "{-# OPTIONS_GHC -Wall #-}"
, "main = putStrLn \"hello\""
]
, T.unlines
[ "{-# OPTIONS_GHC -Wall #-}"
, "{-# OPTIONS_GHC -Wno-missing-signatures #-}"
, "main = putStrLn \"hello\""
]
)
,
( "unused-imports"
, T.unlines
[ "{-# OPTIONS_GHC -Wall #-}"
, ""
, ""
, "module M where"
, ""
, "import Data.Functor"
]
, T.unlines
[ "{-# OPTIONS_GHC -Wall #-}"
, ""
, ""
, "{-# OPTIONS_GHC -Wno-unused-imports #-}"
, "module M where"
, ""
, "import Data.Functor"
]
)
]
<&> \(warning, initialContent, expectedContent) -> testSession (T.unpack warning) $ do
doc <- createDoc "Module.hs" "haskell" initialContent
_ <- waitForDiagnostics
codeActs <- mapMaybe caResultToCodeAct <$> getCodeActions doc (Range (Position 0 0) (Position 0 0))
case find (\CodeAction{_title} -> _title == "Disable \"" <> warning <> "\" warnings") codeActs of
Nothing -> liftIO $ assertFailure "No code action with expected title"
Just action -> do
executeCodeAction action
contentAfterAction <- documentContents doc
liftIO $ expectedContent @=? contentAfterAction
where
caResultToCodeAct = \case
InL _ -> Nothing
InR c -> Just c
unusedTermTests :: TestTree
unusedTermTests = testGroup "unused term code actions" [
ignoreTestBecause "no support for prefixing unused names with _" $ testCase "Prefixes with '_'" $
runSession hlsCommand fullCaps "test/testdata/" $ do
doc <- openDoc "UnusedTerm.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "typecheck"
cars <- getAllCodeActions doc
prefixImUnused <- liftIO $ inspectCodeAction cars ["Prefix imUnused with _"]
executeCodeAction prefixImUnused
edit <- skipManyTill anyMessage $ getDocumentEdit doc
let expected = [ "{-# OPTIONS_GHC -Wall #-}"
, "module UnusedTerm () where"
, "_imUnused :: Int -> Int"
, "_imUnused 1 = 1"
, "_imUnused 2 = 2"
, "_imUnused _ = 3"
]
liftIO $ edit @?= T.unlines expected
-- See https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_codeAction
-- `CodeActionContext`
, testCase "respect 'only' parameter" $ runSession hlsCommand fullCaps "test/testdata" $ do
doc <- openDoc "CodeActionOnly.hs" "haskell"
_ <- waitForDiagnosticsFrom doc
diags <- getCurrentDiagnostics doc
let params = CodeActionParams Nothing Nothing doc (Range (Position 1 0) (Position 4 0)) caContext
caContext = CodeActionContext (List diags) (Just (List [CodeActionRefactorInline]))
caContextAllActions = CodeActionContext (List diags) Nothing
-- Verify that we get code actions of at least two different kinds.
ResponseMessage _ _ (Right (List allCodeActions))
<- request STextDocumentCodeAction (params & L.context .~ caContextAllActions)
liftIO $ do
redundantId <- inspectCodeAction allCodeActions ["Redundant id"]
redundantId ^. L.kind @?= Just CodeActionQuickFix
unfoldFoo <- inspectCodeAction allCodeActions ["Unfold foo"]
unfoldFoo ^. L.kind @?= Just CodeActionRefactorInline
-- Verify that that when we set the only parameter, we only get actions
-- of the right kind.
ResponseMessage _ _ (Right (List res)) <- request STextDocumentCodeAction params
let cas = map fromAction res
kinds = map (^. L.kind) cas
liftIO $ do
not (null kinds) @? "We found an action of kind RefactorInline"
all (Just CodeActionRefactorInline ==) kinds @? "All CodeActionRefactorInline"
]
noLiteralCaps :: C.ClientCapabilities
noLiteralCaps = def { C._textDocument = Just textDocumentCaps }
where
textDocumentCaps = def { C._codeAction = Just codeActionCaps }
codeActionCaps = CodeActionClientCapabilities (Just True) Nothing Nothing
testSession :: String -> Session () -> TestTree
testSession name s = testCase name $ withTempDir $ \dir ->
runSession hlsCommand fullCaps dir s