Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 50c82af

Browse files
alanzfendor
authored andcommitted
Add passPublishDiagnostics to publish diagnostics via the server
It injects a message that gets processed in the right context.
1 parent 3b5f408 commit 50c82af

File tree

4 files changed

+59
-39
lines changed

4 files changed

+59
-39
lines changed

hie-plugin-api/Haskell/Ide/Engine/ModuleCache.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ modifyCache f = modifyModuleCache f
7171

7272
-- ---------------------------------------------------------------------
7373

74-
type PublishDiagnostics = Int -> J.NormalizedUri -> J.TextDocumentVersion -> J.DiagnosticsBySource -> IO ()
74+
type PublishDiagnostics = J.NormalizedUri -> J.TextDocumentVersion -> J.DiagnosticsBySource -> IO ()
7575

7676
-- | Run the given action in context and initialise a session with hie-bios.
7777
-- If a context is given, the context is used to initialise a session for GHC.
@@ -186,7 +186,7 @@ loadCradle publishDiagnostics iniDynFlags (NewCradle fp) def action = do
186186
source = Just "bios"
187187
diag = Diagnostic range sev Nothing source (Text.unlines msgTxt) Nothing
188188

189-
liftIO $ publishDiagnostics maxBound normalizedUri Nothing
189+
liftIO $ publishDiagnostics normalizedUri Nothing
190190
(Map.singleton source (SL.singleton diag))
191191

192192
return $ IdeResultFail $ IdeError

src/Haskell/Ide/Engine/Server.hs

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ run scheduler _origDir plugins captureFp = flip E.catches handlers $ do
198198
-- recognized properly by ghc-mod
199199
flip labelThread "scheduler" =<<
200200
forkIO
201-
( Scheduler.runScheduler scheduler errorHandler callbackHandler lf (publishDiagnostics' lf) mcradle
201+
( Scheduler.runScheduler scheduler errorHandler callbackHandler lf (passPublishDiagnostics rin) mcradle
202202
`E.catch`
203203
\(e :: E.SomeException) ->
204204
errorm $ "Scheduler thread exited unexpectedly: " ++ show e
@@ -256,9 +256,13 @@ run scheduler _origDir plugins captureFp = flip E.catches handlers $ do
256256

257257
-- ---------------------------------------------------------------------
258258

259-
type ReactorInput
260-
= FromClientMessage
261-
-- ^ injected into the reactor input by each of the individual callback handlers
259+
data ReactorInput
260+
= CM FromClientMessage
261+
-- ^ injected into the reactor input by each of the individual
262+
-- callback handlers
263+
| PD J.NormalizedUri J.TextDocumentVersion DiagnosticsBySource
264+
-- ^ injected into the reactor input by any scheduler needing to
265+
-- publish additional diagnostics
262266

263267
-- ---------------------------------------------------------------------
264268

@@ -359,8 +363,10 @@ updatePositionMap uri changes = pluginGetFile "updatePositionMap: " uri $ \file
359363
-- ---------------------------------------------------------------------
360364

361365
publishDiagnostics :: (MonadIO m, MonadReader REnv m)
362-
=> Int -> J.NormalizedUri -> J.TextDocumentVersion -> DiagnosticsBySource -> m ()
363-
publishDiagnostics maxToSend uri' mv diags = do
366+
=> J.NormalizedUri -> J.TextDocumentVersion -> DiagnosticsBySource -> m ()
367+
publishDiagnostics uri' mv diags = do
368+
clientConfig <- getClientConfig
369+
let maxToSend = maxNumberOfProblems clientConfig
364370
lf <- asks lspFuncs
365371
publishDiagnostics' lf maxToSend uri' mv diags
366372

@@ -415,15 +421,15 @@ reactor inp diagIn = do
415421
liftIO $ U.logs $ "****** reactor: got message number:" ++ show tn
416422

417423
case inval of
418-
RspFromClient resp@(J.ResponseMessage _ _ _ merr) -> do
424+
CM (RspFromClient resp@(J.ResponseMessage _ _ _ merr)) -> do
419425
liftIO $ U.logs $ "reactor:got RspFromClient:" ++ show resp
420426
case merr of
421427
Nothing -> return ()
422428
Just _ -> sendErrorLog $ "Got error response:" <> decodeUtf8 (BL.toStrict $ A.encode resp)
423429

424430
-- -------------------------------
425431

426-
NotInitialized _notification -> do
432+
CM (NotInitialized _notification) -> do
427433
liftIO $ U.logm "****** reactor: processing Initialized Notification"
428434
-- Server is ready, register any specific capabilities we need
429435

@@ -477,7 +483,7 @@ reactor inp diagIn = do
477483

478484
-- -------------------------------
479485

480-
NotDidOpenTextDocument notification -> do
486+
CM (NotDidOpenTextDocument notification) -> do
481487
liftIO $ U.logm "****** reactor: processing NotDidOpenTextDocument"
482488
let
483489
td = notification ^. J.params . J.textDocument
@@ -489,17 +495,17 @@ reactor inp diagIn = do
489495

490496
-- -------------------------------
491497

492-
NotDidChangeWatchedFiles _notification -> do
498+
CM (NotDidChangeWatchedFiles _notification) -> do
493499
liftIO $ U.logm "****** reactor: not processing NotDidChangeWatchedFiles"
494500

495501
-- -------------------------------
496502

497-
NotWillSaveTextDocument _notification -> do
503+
CM (NotWillSaveTextDocument _notification) -> do
498504
liftIO $ U.logm "****** reactor: not processing NotWillSaveTextDocument"
499505

500506
-- -------------------------------
501507

502-
NotDidSaveTextDocument notification -> do
508+
CM (NotDidSaveTextDocument notification) -> do
503509
-- This notification is redundant, as we get the NotDidChangeTextDocument
504510
liftIO $ U.logm "****** reactor: processing NotDidSaveTextDocument"
505511
let
@@ -511,7 +517,7 @@ reactor inp diagIn = do
511517

512518
-- -------------------------------
513519

514-
NotDidChangeTextDocument notification -> do
520+
CM (NotDidChangeTextDocument notification) -> do
515521
liftIO $ U.logm "****** reactor: processing NotDidChangeTextDocument"
516522
let
517523
params = notification ^. J.params
@@ -531,7 +537,7 @@ reactor inp diagIn = do
531537

532538
-- -------------------------------
533539

534-
NotDidCloseTextDocument notification -> do
540+
CM (NotDidCloseTextDocument notification) -> do
535541
liftIO $ U.logm "****** reactor: processing NotDidCloseTextDocument"
536542
let
537543
uri = notification ^. J.params . J.textDocument . J.uri
@@ -543,7 +549,7 @@ reactor inp diagIn = do
543549

544550
-- -------------------------------
545551

546-
ReqRename req -> do
552+
CM (ReqRename req) -> do
547553
liftIO $ U.logs $ "reactor:got RenameRequest:" ++ show req
548554
-- TODO: re-enable HaRe
549555
-- let (params, doc, pos) = reqParams req
@@ -556,7 +562,7 @@ reactor inp diagIn = do
556562

557563
-- -------------------------------
558564

559-
ReqHover req -> do
565+
CM (ReqHover req) -> do
560566
liftIO $ U.logs $ "reactor:got HoverRequest:" ++ show req
561567
let params = req ^. J.params
562568
pos = params ^. J.position
@@ -586,13 +592,13 @@ reactor inp diagIn = do
586592

587593
-- -------------------------------
588594

589-
ReqCodeAction req -> do
595+
CM (ReqCodeAction req) -> do
590596
liftIO $ U.logs $ "reactor:got CodeActionRequest:" ++ show req
591597
handleCodeActionReq tn req
592598

593599
-- -------------------------------
594600

595-
ReqExecuteCommand req -> do
601+
CM (ReqExecuteCommand req) -> do
596602
liftIO $ U.logs $ "reactor:got ExecuteCommandRequest:" ++ show req
597603
lf <- asks lspFuncs
598604

@@ -665,7 +671,7 @@ reactor inp diagIn = do
665671

666672
-- -------------------------------
667673

668-
ReqCompletion req -> do
674+
CM (ReqCompletion req) -> do
669675
liftIO $ U.logs $ "reactor:got CompletionRequest:" ++ show req
670676
let (_, doc, pos) = reqParams req
671677

@@ -683,7 +689,7 @@ reactor inp diagIn = do
683689
$ lift $ Completions.getCompletions doc prefix snippets
684690
makeRequest hreq
685691

686-
ReqCompletionItemResolve req -> do
692+
CM (ReqCompletionItemResolve req) -> do
687693
liftIO $ U.logs $ "reactor:got CompletionItemResolveRequest:" ++ show req
688694
snippets <- Completions.WithSnippets <$> configVal completionSnippetsOn
689695
let origCompl = req ^. J.params
@@ -696,7 +702,7 @@ reactor inp diagIn = do
696702

697703
-- -------------------------------
698704

699-
ReqDocumentHighlights req -> do
705+
CM (ReqDocumentHighlights req) -> do
700706
liftIO $ U.logs $ "reactor:got DocumentHighlightsRequest:" ++ show req
701707
let (_, doc, pos) = reqParams req
702708
callback = reactorSend . RspDocumentHighlights . Core.makeResponseMessage req . J.List
@@ -706,7 +712,7 @@ reactor inp diagIn = do
706712

707713
-- -------------------------------
708714

709-
ReqDefinition req -> do
715+
CM (ReqDefinition req) -> do
710716
liftIO $ U.logs $ "reactor:got DefinitionRequest:" ++ show req
711717
let params = req ^. J.params
712718
doc = params ^. J.textDocument . J.uri
@@ -716,7 +722,7 @@ reactor inp diagIn = do
716722
$ fmap J.MultiLoc <$> Hie.findDef doc pos
717723
makeRequest hreq
718724

719-
ReqTypeDefinition req -> do
725+
CM (ReqTypeDefinition req) -> do
720726
liftIO $ U.logs $ "reactor:got DefinitionTypeRequest:" ++ show req
721727
let params = req ^. J.params
722728
doc = params ^. J.textDocument . J.uri
@@ -726,7 +732,7 @@ reactor inp diagIn = do
726732
$ fmap J.MultiLoc <$> Hie.findTypeDef doc pos
727733
makeRequest hreq
728734

729-
ReqFindReferences req -> do
735+
CM (ReqFindReferences req) -> do
730736
liftIO $ U.logs $ "reactor:got FindReferences:" ++ show req
731737
-- TODO: implement project-wide references
732738
let (_, doc, pos) = reqParams req
@@ -738,7 +744,7 @@ reactor inp diagIn = do
738744

739745
-- -------------------------------
740746

741-
ReqDocumentFormatting req -> do
747+
CM (ReqDocumentFormatting req) -> do
742748
liftIO $ U.logs $ "reactor:got FormatRequest:" ++ show req
743749
provider <- getFormattingProvider
744750
let params = req ^. J.params
@@ -750,7 +756,7 @@ reactor inp diagIn = do
750756

751757
-- -------------------------------
752758

753-
ReqDocumentRangeFormatting req -> do
759+
CM (ReqDocumentRangeFormatting req) -> do
754760
liftIO $ U.logs $ "reactor:got FormatRequest:" ++ show req
755761
provider <- getFormattingProvider
756762
let params = req ^. J.params
@@ -763,7 +769,7 @@ reactor inp diagIn = do
763769

764770
-- -------------------------------
765771

766-
ReqDocumentSymbols req -> do
772+
CM (ReqDocumentSymbols req) -> do
767773
liftIO $ U.logs $ "reactor:got Document symbol request:" ++ show req
768774
sps <- asks symbolProviders
769775
C.ClientCapabilities _ tdc _ _ <- asksLspFuncs Core.clientCapabilities
@@ -788,14 +794,14 @@ reactor inp diagIn = do
788794

789795
-- -------------------------------
790796

791-
NotCancelRequestFromClient notif -> do
797+
CM (NotCancelRequestFromClient notif) -> do
792798
liftIO $ U.logs $ "reactor:got CancelRequest:" ++ show notif
793799
let lid = notif ^. J.params . J.id
794800
cancelRequest lid
795801

796802
-- -------------------------------
797803

798-
NotDidChangeConfiguration notif -> do
804+
CM (NotDidChangeConfiguration notif) -> do
799805
liftIO $ U.logs $ "reactor:didChangeConfiguration notification:" ++ show notif
800806
-- if hlint has been turned off, flush the diagnostics
801807
diagsOn <- configVal hlintOn
@@ -808,8 +814,15 @@ reactor inp diagIn = do
808814
else flushDiagnosticsBySource maxDiagnosticsToSend (Just "hlint")
809815

810816
-- -------------------------------
811-
om -> do
817+
818+
CM om -> do
812819
liftIO $ U.logs $ "reactor:got HandlerRequest:" ++ show om
820+
821+
-- -------------------------------
822+
823+
PD uri version diagnostics -> do
824+
publishDiagnostics uri version diagnostics
825+
813826
loop (tn + 1)
814827

815828
-- Actually run the thing
@@ -943,18 +956,17 @@ requestDiagnosticsNormal tn file mVer = do
943956
sendOneGhc :: J.DiagnosticSource -> (J.NormalizedUri, [Diagnostic]) -> R ()
944957
sendOneGhc pid (fileUri,ds) = do
945958
if any (hasSeverity J.DsError) ds
946-
then publishDiagnostics maxToSend fileUri Nothing
959+
then publishDiagnostics fileUri Nothing
947960
(Map.fromList [(Just "hlint",SL.toSortedList []),(Just pid,SL.toSortedList ds)])
948961
else sendOne pid (fileUri,ds)
949962

950963
sendOne pid (fileUri,ds) = do
951-
publishDiagnostics maxToSend fileUri Nothing (Map.fromList [(Just pid,SL.toSortedList ds)])
964+
publishDiagnostics fileUri Nothing (Map.fromList [(Just pid,SL.toSortedList ds)])
952965

953966
hasSeverity :: J.DiagnosticSeverity -> J.Diagnostic -> Bool
954967
hasSeverity sev (J.Diagnostic _ (Just s) _ _ _ _) = s == sev
955968
hasSeverity _ _ = False
956-
sendEmpty = publishDiagnostics maxToSend (J.toNormalizedUri file) Nothing (Map.fromList [(Just "bios",SL.toSortedList [])])
957-
maxToSend = maxNumberOfProblems clientConfig
969+
sendEmpty = publishDiagnostics (J.toNormalizedUri file) Nothing (Map.fromList [(Just "bios",SL.toSortedList [])])
958970

959971
let sendHlint = hlintOn clientConfig
960972
when sendHlint $ do
@@ -1053,6 +1065,14 @@ hieHandlers rin
10531065

10541066
passHandler :: TChan ReactorInput -> (a -> FromClientMessage) -> Core.Handler a
10551067
passHandler rin c notification = do
1056-
atomically $ writeTChan rin (c notification)
1068+
atomically $ writeTChan rin (CM (c notification))
1069+
1070+
-- ---------------------------------------------------------------------
1071+
1072+
-- | Generate a 'PublishDiagnostics' function that will simply insert
1073+
-- the request into the main server loop
1074+
passPublishDiagnostics :: TChan ReactorInput -> PublishDiagnostics
1075+
passPublishDiagnostics rin uri version diagnostics = do
1076+
atomically $ writeTChan rin (PD uri version diagnostics)
10571077

10581078
-- ---------------------------------------------------------------------

test/dispatcher/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ startServer = do
8181
(\lid errCode e -> logToChan logChan ("received an error", Left (lid, errCode, e)))
8282
(\g x -> g x)
8383
dummyLspFuncs
84+
(\_ _ _ -> return ())
8485
(Just crdl)
8586

8687
return (scheduler, logChan, dispatcher)

test/plugin-dispatcher/Main.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ newPluginSpec = do
5151
(\_ _ _ -> return ())
5252
(\f x -> f x)
5353
dummyLspFuncs
54+
(\_ _ _ -> return ())
5455
(Just crdl)
5556

5657
updateDocument scheduler (filePathToUri "test") 3
@@ -65,5 +66,3 @@ newPluginSpec = do
6566
killThread pid
6667
resp1 `shouldBe` "text1"
6768
resp2 `shouldBe` "text4"
68-
69-

0 commit comments

Comments
 (0)