Skip to content

Commit 5b774f6

Browse files
Move some utility functions to utility module.
1 parent 7f6080e commit 5b774f6

File tree

3 files changed

+69
-54
lines changed

3 files changed

+69
-54
lines changed

haskell-language-server.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ common hls-test-utils
197197
, hslogger
198198
, hspec
199199
, hspec-core
200+
, lens
200201
, lsp-test >=0.11.0.6
201202
, stm
202203
, tasty-hunit

test/functional/FunctionalCodeAction.hs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -444,61 +444,8 @@ unusedTermTests = testGroup "unused term code actions" [
444444
all (Just CodeActionRefactorInline ==) kinds @? "All CodeActionRefactorInline"
445445
]
446446

447-
fromAction :: CAResult -> CodeAction
448-
fromAction (CACodeAction action) = action
449-
fromAction _ = error "Not a code action"
450-
451-
fromCommand :: CAResult -> Command
452-
fromCommand (CACommand command) = command
453-
fromCommand _ = error "Not a command"
454-
455447
noLiteralCaps :: C.ClientCapabilities
456448
noLiteralCaps = def { C._textDocument = Just textDocumentCaps }
457449
where
458450
textDocumentCaps = def { C._codeAction = Just codeActionCaps }
459451
codeActionCaps = C.CodeActionClientCapabilities (Just True) Nothing
460-
461-
onMatch :: [a] -> (a -> Bool) -> String -> IO a
462-
onMatch as pred err = maybe (fail err) return (find pred as)
463-
464-
inspectDiagnostic :: [Diagnostic] -> [T.Text] -> IO Diagnostic
465-
inspectDiagnostic diags s = onMatch diags (\ca -> all (`T.isInfixOf` (ca ^. L.message)) s) err
466-
where err = "expected diagnostic matching '" ++ show s ++ "' but did not find one"
467-
468-
expectDiagnostic :: [Diagnostic] -> [T.Text] -> IO ()
469-
expectDiagnostic diags s = void $ inspectDiagnostic diags s
470-
471-
inspectCodeAction :: [CAResult] -> [T.Text] -> IO CodeAction
472-
inspectCodeAction cars s = fromAction <$> onMatch cars pred err
473-
where pred (CACodeAction ca) = all (`T.isInfixOf` (ca ^. L.title)) s
474-
pred _ = False
475-
err = "expected code action matching '" ++ show s ++ "' but did not find one"
476-
477-
expectCodeAction :: [CAResult] -> [T.Text] -> IO ()
478-
expectCodeAction cars s = void $ inspectCodeAction cars s
479-
480-
inspectCommand :: [CAResult] -> [T.Text] -> IO Command
481-
inspectCommand cars s = fromCommand <$> onMatch cars pred err
482-
where pred (CACommand command) = all (`T.isInfixOf` (command ^. L.title)) s
483-
pred _ = False
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)

test/utils/Test/Hls/Util.hs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,42 @@ module Test.Hls.Util
33
(
44
codeActionSupportCaps
55
, dummyLspFuncs
6+
, expectCodeAction
7+
, expectDiagnostic
68
, flushStackEnvironment
9+
, fromAction
10+
, fromCommand
711
, getHspecFormattedConfig
812
, ghcVersion, GhcVersion(..)
913
, hlsCommand
1014
, hlsCommandExamplePlugin
1115
, hlsCommandVomit
16+
, inspectCodeAction
17+
, inspectCommand
18+
, inspectDiagnostic
1219
, logConfig
1320
, logFilePath
1421
, noLogConfig
1522
, setupBuildToolFiles
23+
, waitForDiagnosticsFrom
24+
, waitForDiagnosticsFromSource
1625
, withFileLogging
1726
, withCurrentDirectoryInTmp
1827
)
1928
where
2029

2130
import Control.Monad
31+
import Control.Applicative.Combinators (skipManyTill)
32+
import Control.Lens ((^.))
2233
import Data.Default
2334
import Data.List (intercalate)
35+
import Data.List.Extra (find)
2436
import Data.Maybe
37+
import qualified Data.Text as T
2538
import Language.Haskell.LSP.Core
2639
import Language.Haskell.LSP.Types
2740
import qualified Language.Haskell.LSP.Test as T
41+
import qualified Language.Haskell.LSP.Types.Lens as L
2842
import qualified Language.Haskell.LSP.Types.Capabilities as C
2943
import System.Directory
3044
import System.Environment
@@ -35,7 +49,7 @@ import System.IO.Unsafe
3549
import Test.Hspec.Runner
3650
import Test.Hspec.Core.Formatters
3751
import Text.Blaze.Renderer.String (renderMarkup)
38-
import Text.Blaze.Internal
52+
import Text.Blaze.Internal hiding (null)
3953

4054

4155
noLogConfig :: T.SessionConfig
@@ -282,3 +296,56 @@ copyDir src dst = do
282296
then createDirectory dstFp >> copyDir srcFp dstFp
283297
else copyFile srcFp dstFp
284298
where ignored = ["dist", "dist-newstyle", ".stack-work"]
299+
300+
fromAction :: CAResult -> CodeAction
301+
fromAction (CACodeAction action) = action
302+
fromAction _ = error "Not a code action"
303+
304+
fromCommand :: CAResult -> Command
305+
fromCommand (CACommand command) = command
306+
fromCommand _ = error "Not a command"
307+
308+
onMatch :: [a] -> (a -> Bool) -> String -> IO a
309+
onMatch as pred err = maybe (fail err) return (find pred as)
310+
311+
inspectDiagnostic :: [Diagnostic] -> [T.Text] -> IO Diagnostic
312+
inspectDiagnostic diags s = onMatch diags (\ca -> all (`T.isInfixOf` (ca ^. L.message)) s) err
313+
where err = "expected diagnostic matching '" ++ show s ++ "' but did not find one"
314+
315+
expectDiagnostic :: [Diagnostic] -> [T.Text] -> IO ()
316+
expectDiagnostic diags s = void $ inspectDiagnostic diags s
317+
318+
inspectCodeAction :: [CAResult] -> [T.Text] -> IO CodeAction
319+
inspectCodeAction cars s = fromAction <$> onMatch cars pred err
320+
where pred (CACodeAction ca) = all (`T.isInfixOf` (ca ^. L.title)) s
321+
pred _ = False
322+
err = "expected code action matching '" ++ show s ++ "' but did not find one"
323+
324+
expectCodeAction :: [CAResult] -> [T.Text] -> IO ()
325+
expectCodeAction cars s = void $ inspectCodeAction cars s
326+
327+
inspectCommand :: [CAResult] -> [T.Text] -> IO Command
328+
inspectCommand cars s = fromCommand <$> onMatch cars pred err
329+
where pred (CACommand command) = all (`T.isInfixOf` (command ^. L.title)) s
330+
pred _ = False
331+
err = "expected code action matching '" ++ show s ++ "' but did not find one"
332+
333+
waitForDiagnosticsFrom :: TextDocumentIdentifier -> T.Session [Diagnostic]
334+
waitForDiagnosticsFrom doc = do
335+
diagsNot <- skipManyTill T.anyMessage T.message :: T.Session PublishDiagnosticsNotification
336+
let (List diags) = diagsNot ^. L.params . L.diagnostics
337+
if doc ^. L.uri /= diagsNot ^. L.params . L.uri
338+
then waitForDiagnosticsFrom doc
339+
else return diags
340+
341+
waitForDiagnosticsFromSource :: TextDocumentIdentifier -> String -> T.Session [Diagnostic]
342+
waitForDiagnosticsFromSource doc src = do
343+
diagsNot <- skipManyTill T.anyMessage T.message :: T.Session PublishDiagnosticsNotification
344+
let (List diags) = diagsNot ^. L.params . L.diagnostics
345+
let res = filter matches diags
346+
if doc ^. L.uri /= diagsNot ^. L.params . L.uri || null res
347+
then waitForDiagnosticsFromSource doc src
348+
else return res
349+
where
350+
matches :: Diagnostic -> Bool
351+
matches d = d ^. L.source == Just (T.pack src)

0 commit comments

Comments
 (0)