Skip to content

Commit 57ff47c

Browse files
committed
Helpers for reference/ready message parsing
1 parent 8b041a9 commit 57ff47c

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

ghcide/test/exe/Main.hs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ import Development.IDE.Test (Cursor,
5858
standardizeQuotes,
5959
waitForAction,
6060
waitForGC,
61-
waitForTypecheck)
61+
waitForTypecheck,
62+
isReferenceReady,
63+
referenceReady)
6264
import Development.IDE.Test.Runfiles
6365
import qualified Development.IDE.Types.Diagnostics as Diagnostics
6466
import Development.IDE.Types.Location
@@ -5543,11 +5545,7 @@ simpleMultiDefTest = testCase "simple-multi-def-test" $ runWithExtraFiles "multi
55435545
adoc <- liftIO $ runInDir dir $ do
55445546
aSource <- liftIO $ readFileUtf8 aPath
55455547
adoc <- createDoc aPath "haskell" aSource
5546-
~() <- skipManyTill anyMessage $ satisfyMaybe $ \case
5547-
FromServerMess (SCustomMethod "ghcide/reference/ready") (NotMess NotificationMessage{_params = fp}) -> do
5548-
A.Success fp' <- pure $ fromJSON fp
5549-
if equalFilePath fp' aPath then pure () else Nothing
5550-
_ -> Nothing
5548+
skipManyTill anyMessage $ isReferenceReady aPath
55515549
closeDoc adoc
55525550
pure adoc
55535551
bSource <- liftIO $ readFileUtf8 bPath
@@ -5578,18 +5576,15 @@ bootTests = testGroup "boot"
55785576
-- `ghcide/reference/ready` notification.
55795577
-- Once we receive one of the above, we wait for the other that we
55805578
-- haven't received yet.
5581-
-- If we don't wait for the `ready` notification it is possible
5582-
-- that the `getDefinitions` request/response in the outer ghcide
5579+
-- If we don't wait for the `ready` notification it is possible
5580+
-- that the `getDefinitions` request/response in the outer ghcide
55835581
-- session will find no definitions.
55845582
let hoverParams = HoverParams cDoc (Position 4 3) Nothing
55855583
hoverRequestId <- sendRequest STextDocumentHover hoverParams
5586-
let parseReadyMessage = satisfy $ \case
5587-
FromServerMess (SCustomMethod "ghcide/reference/ready") (NotMess NotificationMessage{_params = params})
5588-
| A.Success fp <- fromJSON params -> equalFilePath fp cPath
5589-
_ -> False
5584+
let parseReadyMessage = isReferenceReady cPath
55905585
let parseHoverResponse = responseForId STextDocumentHover hoverRequestId
55915586
hoverResponseOrReadyMessage <- skipManyTill anyMessage ((Left <$> parseHoverResponse) <|> (Right <$> parseReadyMessage))
5592-
_ <- skipManyTill anyMessage $
5587+
_ <- skipManyTill anyMessage $
55935588
case hoverResponseOrReadyMessage of
55945589
Left _ -> void parseReadyMessage
55955590
Right _ -> void parseHoverResponse
@@ -6002,11 +5997,7 @@ referenceTestSession name thisDoc docs' f = testSessionWithExtraFiles "reference
60025997
loop :: [FilePath] -> Session ()
60035998
loop [] = pure ()
60045999
loop docs = do
6005-
doc <- skipManyTill anyMessage $ satisfyMaybe $ \case
6006-
FromServerMess (SCustomMethod "ghcide/reference/ready") (NotMess NotificationMessage{_params = fp}) -> do
6007-
A.Success fp' <- pure $ fromJSON fp
6008-
find (fp' ==) docs
6009-
_ -> Nothing
6000+
doc <- skipManyTill anyMessage $ referenceReady (`elem` docs)
60106001
loop (delete doc docs)
60116002
loop docs
60126003
f dir

ghcide/test/src/Development/IDE/Test.hs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ module Development.IDE.Test
2929
, getStoredKeys
3030
, waitForCustomMessage
3131
, waitForGC
32-
,getBuildKeysBuilt,getBuildKeysVisited,getBuildKeysChanged,getBuildEdgesCount,configureCheckProject) where
32+
, getBuildKeysBuilt
33+
, getBuildKeysVisited
34+
, getBuildKeysChanged
35+
, getBuildEdgesCount
36+
, configureCheckProject
37+
, isReferenceReady
38+
, referenceReady) where
3339

3440
import Control.Applicative.Combinators
3541
import Control.Lens hiding (List)
@@ -58,6 +64,7 @@ import Language.LSP.Types.Lens as Lsp
5864
import System.Directory (canonicalizePath)
5965
import System.Time.Extra
6066
import Test.Tasty.HUnit
67+
import System.FilePath (equalFilePath)
6168

6269
requireDiagnosticM
6370
:: (Foldable f, Show (f Diagnostic), HasCallStack)
@@ -254,3 +261,16 @@ configureCheckProject overrideCheckProject =
254261
sendNotification SWorkspaceDidChangeConfiguration
255262
(DidChangeConfigurationParams $ toJSON
256263
def{checkProject = overrideCheckProject})
264+
265+
-- | Pattern match a message from ghcide indicating that a file has been indexed
266+
isReferenceReady :: FilePath -> Session ()
267+
isReferenceReady p = void $ referenceReady (equalFilePath p)
268+
269+
referenceReady :: (FilePath -> Bool) -> Session FilePath
270+
referenceReady pred = satisfyMaybe $ \case
271+
FromServerMess (SCustomMethod "ghcide/reference/ready") (NotMess NotificationMessage{_params})
272+
| A.Success fp <- A.fromJSON _params
273+
, pred fp
274+
-> Just fp
275+
_ -> Nothing
276+

0 commit comments

Comments
 (0)