@@ -43,7 +43,7 @@ hlintTests :: TestTree
43
43
hlintTests = testGroup " hlint suggestions" [
44
44
testCase " provides 3.8 code actions including apply all" $ runSession hlsCommand fullCaps " test/testdata/hlint" $ do
45
45
doc <- openDoc " ApplyRefact2.hs" " haskell"
46
- diags@ (reduceDiag: _) <- waitForDiagnosticsSource " hlint"
46
+ diags@ (reduceDiag: _) <- waitForDiagnosticsFromSource doc " hlint"
47
47
48
48
liftIO $ do
49
49
length diags @?= 2 -- "Eta Reduce" and "Redundant Id"
@@ -70,7 +70,7 @@ hlintTests = testGroup "hlint suggestions" [
70
70
, testCase " falls back to pre 3.8 code actions" $ runSession hlsCommand noLiteralCaps " test/testdata/hlint" $ do
71
71
doc <- openDoc " ApplyRefact2.hs" " haskell"
72
72
73
- _ <- waitForDiagnosticsSource " hlint"
73
+ _ <- waitForDiagnosticsFromSource doc " hlint"
74
74
75
75
cars <- getAllCodeActions doc
76
76
etaReduce <- liftIO $ inspectCommand cars [" Apply hint: Eta reduce" ]
@@ -84,15 +84,15 @@ hlintTests = testGroup "hlint suggestions" [
84
84
let config = def { hlintOn = True }
85
85
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
86
86
87
- _ <- openDoc " ApplyRefact2.hs" " haskell"
88
- diags <- waitForDiagnosticsSource " hlint"
87
+ doc <- openDoc " ApplyRefact2.hs" " haskell"
88
+ diags <- waitForDiagnosticsFromSource doc " hlint"
89
89
90
90
liftIO $ length diags > 0 @? " There are hlint diagnostics"
91
91
92
92
let config' = def { hlintOn = False }
93
93
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config'))
94
94
95
- diags' <- waitForDiagnostics
95
+ diags' <- waitForDiagnosticsFrom doc
96
96
97
97
liftIO $ Just " hlint" `notElem` map (^. L. source) diags' @? " There are no hlint diagnostics"
98
98
@@ -118,7 +118,7 @@ hlintTests = testGroup "hlint suggestions" [
118
118
119
119
changeDoc doc [change']
120
120
121
- diags'' <- waitForDiagnosticsSource " hlint"
121
+ diags'' <- waitForDiagnosticsFromSource doc " hlint"
122
122
123
123
liftIO $ length diags'' @?= 2
124
124
]
@@ -128,7 +128,7 @@ renameTests = testGroup "rename suggestions" [
128
128
testCase " works" $ runSession hlsCommand noLiteralCaps " test/testdata" $ do
129
129
doc <- openDoc " CodeActionRename.hs" " haskell"
130
130
131
- _ <- waitForDiagnosticsSource " typecheck"
131
+ _ <- waitForDiagnosticsFromSource doc " typecheck"
132
132
133
133
cars <- getAllCodeActions doc
134
134
replaceButStrLn <- liftIO $ inspectCommand cars [" Replace with" , " putStrLn" ]
@@ -141,7 +141,7 @@ renameTests = testGroup "rename suggestions" [
141
141
$ runSession hlsCommand noLiteralCaps " test/testdata" $ do
142
142
doc <- openDoc " CodeActionRename.hs" " haskell"
143
143
144
- _ <- waitForDiagnosticsSource " typecheck"
144
+ _ <- waitForDiagnosticsFromSource doc " typecheck"
145
145
146
146
cars <- getAllCodeActions doc
147
147
cmd <- liftIO $ inspectCommand cars [" Replace with" , " putStrLn" ]
@@ -168,7 +168,7 @@ importTests = testGroup "import suggestions" [
168
168
let config = def { formattingProvider = " none" }
169
169
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
170
170
171
- diag: _ <- waitForDiagnostics
171
+ ( diag: _) <- waitForDiagnosticsFrom doc
172
172
liftIO $ diag ^. L. message @?= " Variable not in scope: when :: Bool -> IO () -> IO ()"
173
173
174
174
actionsOrCommands <- getAllCodeActions doc
@@ -195,7 +195,7 @@ packageTests = testGroup "add package suggestions" [
195
195
doc <- openDoc " AddPackage.hs" " haskell"
196
196
197
197
-- ignore the first empty hlint diagnostic publish
198
- [_,diag: _] <- count 2 waitForDiagnostics
198
+ [_,diag: _] <- count 2 $ waitForDiagnosticsFrom doc
199
199
200
200
let prefixes = [ " Could not load module `Data.Text'" -- Windows && GHC >= 8.6
201
201
, " Could not find module `Data.Text'" -- Windows
@@ -223,7 +223,7 @@ packageTests = testGroup "add package suggestions" [
223
223
doc <- openDoc " app/Asdf.hs" " haskell"
224
224
225
225
-- ignore the first empty hlint diagnostic publish
226
- [_,_: diag: _] <- count 2 waitForDiagnostics
226
+ [_,_: diag: _] <- count 2 $ waitForDiagnosticsFrom doc
227
227
228
228
let prefixes = [ " Could not load module `Codec.Compression.GZip'" -- Windows && GHC >= 8.6
229
229
, " Could not find module `Codec.Compression.GZip'" -- Windows
@@ -255,7 +255,7 @@ redundantImportTests = testGroup "redundant import code actions" [
255
255
runSession hlsCommand fullCaps " test/testdata/redundantImportTest/" $ do
256
256
doc <- openDoc " src/CodeActionRedundant.hs" " haskell"
257
257
258
- diags <- waitForDiagnostics
258
+ diags <- waitForDiagnosticsFrom doc
259
259
liftIO $ expectDiagnostic diags [" The import of" , " Data.List" , " is redundant" ]
260
260
261
261
mActions <- getAllCodeActions doc
@@ -280,7 +280,7 @@ redundantImportTests = testGroup "redundant import code actions" [
280
280
281
281
, testCase " doesn't touch other imports" $ runSession hlsCommand noLiteralCaps " test/testdata/redundantImportTest/" $ do
282
282
doc <- openDoc " src/MultipleImports.hs" " haskell"
283
- _ <- waitForDiagnostics
283
+ _ <- waitForDiagnosticsFrom doc
284
284
CACommand cmd : _ <- getAllCodeActions doc
285
285
executeCommand cmd
286
286
contents <- documentContents doc
@@ -297,7 +297,7 @@ typedHoleTests = testGroup "typed hole code actions" [
297
297
testCase " works" $
298
298
runSession hlsCommand fullCaps " test/testdata" $ do
299
299
doc <- openDoc " TypedHoles.hs" " haskell"
300
- _ <- waitForDiagnosticsSource " typecheck"
300
+ _ <- waitForDiagnosticsFromSource doc " typecheck"
301
301
cas <- getAllCodeActions doc
302
302
liftIO $ do
303
303
expectCodeAction cas [" replace _ with minBound" ]
@@ -317,7 +317,7 @@ typedHoleTests = testGroup "typed hole code actions" [
317
317
, testCase " shows more suggestions" $
318
318
runSession hlsCommand fullCaps " test/testdata" $ do
319
319
doc <- openDoc " TypedHoles2.hs" " haskell"
320
- _ <- waitForDiagnosticsSource " typecheck"
320
+ _ <- waitForDiagnosticsFromSource doc " typecheck"
321
321
cas <- getAllCodeActions doc
322
322
323
323
liftIO $ do
@@ -345,7 +345,7 @@ signatureTests = testGroup "missing top level signature code actions" [
345
345
runSession hlsCommand fullCaps " test/testdata/" $ do
346
346
doc <- openDoc " TopLevelSignature.hs" " haskell"
347
347
348
- _ <- waitForDiagnosticsSource " typecheck"
348
+ _ <- waitForDiagnosticsFromSource doc " typecheck"
349
349
cas <- map fromAction <$> getAllCodeActions doc
350
350
351
351
liftIO $ " add signature: main :: IO ()" `elem` (map (^. L. title) cas) @? " Contains code action"
@@ -371,7 +371,7 @@ missingPragmaTests = testGroup "missing pragma warning code actions" [
371
371
runSession hlsCommand fullCaps " test/testdata/addPragmas" $ do
372
372
doc <- openDoc " NeedsPragmas.hs" " haskell"
373
373
374
- _ <- waitForDiagnosticsSource " typecheck"
374
+ _ <- waitForDiagnosticsFromSource doc " typecheck"
375
375
cas <- map fromAction <$> getAllCodeActions doc
376
376
377
377
liftIO $ " Add \" TypeSynonymInstances\" " `elem` map (^. L. title) cas @? " Contains TypeSynonymInstances code action"
@@ -408,7 +408,7 @@ unusedTermTests = testGroup "unused term code actions" [
408
408
runSession hlsCommand fullCaps " test/testdata/" $ do
409
409
doc <- openDoc " UnusedTerm.hs" " haskell"
410
410
411
- _ <- waitForDiagnosticsSource " typecheck"
411
+ _ <- waitForDiagnosticsFromSource doc " typecheck"
412
412
cars <- getAllCodeActions doc
413
413
prefixImUnused <- liftIO $ inspectCodeAction cars [" Prefix imUnused with _" ]
414
414
@@ -430,7 +430,7 @@ unusedTermTests = testGroup "unused term code actions" [
430
430
-- `CodeActionContext`
431
431
, testCase " respect 'only' parameter" $ runSession hlsCommand fullCaps " test/testdata" $ do
432
432
doc <- openDoc " CodeActionOnly.hs" " haskell"
433
- _ <- waitForDiagnostics
433
+ _ <- waitForDiagnosticsFrom doc
434
434
diags <- getCurrentDiagnostics doc
435
435
let params = CodeActionParams doc (Range (Position 2 10 ) (Position 4 0 )) caContext Nothing
436
436
caContext = CodeActionContext (List diags) (Just (List [CodeActionRefactorInline ]))
@@ -482,3 +482,23 @@ inspectCommand cars s = fromCommand <$> onMatch cars pred err
482
482
where pred (CACommand command) = all (`T.isInfixOf` (command ^. L. title)) s
483
483
pred _ = False
484
484
err = " expected code action matching '" ++ show s ++ " ' but did not find one"
485
+
486
+ waitForDiagnosticsFrom :: TextDocumentIdentifier -> Session [Diagnostic ]
487
+ waitForDiagnosticsFrom doc = do
488
+ diagsNot <- skipManyTill anyMessage message :: Session PublishDiagnosticsNotification
489
+ let (List diags) = diagsNot ^. L. params . L. diagnostics
490
+ if doc ^. L. uri /= diagsNot ^. L. params . L. uri
491
+ then waitForDiagnosticsFrom doc
492
+ else return diags
493
+
494
+ waitForDiagnosticsFromSource :: TextDocumentIdentifier -> String -> Session [Diagnostic ]
495
+ waitForDiagnosticsFromSource doc src = do
496
+ diagsNot <- skipManyTill anyMessage message :: Session PublishDiagnosticsNotification
497
+ let (List diags) = diagsNot ^. L. params . L. diagnostics
498
+ let res = filter matches diags
499
+ if doc ^. L. uri /= diagsNot ^. L. params . L. uri || null res
500
+ then waitForDiagnosticsFromSource doc src
501
+ else return res
502
+ where
503
+ matches :: Diagnostic -> Bool
504
+ matches d = d ^. L. source == Just (T. pack src)
0 commit comments