|
| 1 | +module InlayHintTests (tests) where |
| 2 | + |
| 3 | +import Config (mkIdeTestFs, testWithDummyPlugin, |
| 4 | + testWithDummyPluginEmpty) |
| 5 | +import Control.Monad (void) |
| 6 | +import Control.Monad.IO.Class (MonadIO (liftIO)) |
| 7 | +import qualified Data.Aeson as A |
| 8 | +import Data.Maybe (mapMaybe) |
| 9 | +import qualified Data.Text as T |
| 10 | +import Language.LSP.Protocol.Types (InlayHint (_textEdits), |
| 11 | + Position (Position), |
| 12 | + Range (Range, _end, _start), |
| 13 | + TextDocumentIdentifier (TextDocumentIdentifier), |
| 14 | + VersionedTextDocumentIdentifier (_uri)) |
| 15 | +import Language.LSP.Test (applyEdit, createDoc, |
| 16 | + documentContents, getInlayHints, |
| 17 | + openDoc, setConfigSection) |
| 18 | +import Test.Hls (Session, expectFail, |
| 19 | + waitForTypecheck) |
| 20 | +import Test.Hls.FileSystem (copyDir) |
| 21 | +import Test.Tasty (TestTree, testGroup) |
| 22 | +import Test.Tasty.HUnit ((@?=)) |
| 23 | + |
| 24 | +tests :: TestTree |
| 25 | +tests = testGroup "inlay hints" |
| 26 | + [ whereInlayHintsTests |
| 27 | + ] |
| 28 | + |
| 29 | +whereInlayHintsTests :: TestTree |
| 30 | +whereInlayHintsTests = testGroup "add signature for where clauses" |
| 31 | + [ testWithDummyPluginEmpty "No where inlay hints if disabled" $ do |
| 32 | + let content = T.unlines |
| 33 | + [ "module Sigs where" |
| 34 | + , "f :: b" |
| 35 | + , "f = undefined" |
| 36 | + , " where" |
| 37 | + , " g = True" |
| 38 | + ] |
| 39 | + range = Range { _start = Position 4 0 |
| 40 | + , _end = Position 4 1000 |
| 41 | + } |
| 42 | + doc <- createDoc "Sigs.hs" "haskell" content |
| 43 | + setConfigSection "haskell" (createConfig False) |
| 44 | + inlayHints <- getInlayHints doc range |
| 45 | + liftIO $ length inlayHints @?= 0 |
| 46 | + , editTest "Simple" "Simple" |
| 47 | + , editTest "Tuple" "Tuple" |
| 48 | + , editTest "Inline" "Inline" |
| 49 | + , editTest "Infix" "Infix" |
| 50 | + , editTest "Operator" "Operator" |
| 51 | + , expectFail $ editTest "ScopedTypeVariables" "ScopedTypeVariables" |
| 52 | + , editTest "Nest" "Nest" |
| 53 | + , editTest "No lens" "NoLens" |
| 54 | + , expectFail $ editTest "Typeclass" "Typeclass" |
| 55 | + , editTest "Quqlified" "Qualified" |
| 56 | + ] |
| 57 | + where |
| 58 | + createConfig on = |
| 59 | + A.object [ "plugin" |
| 60 | + A..= A.object [ "ghcide-type-lenses" |
| 61 | + A..= A.object [ "config" |
| 62 | + A..= A.object [ "whereInlayHintOn" A..= A.Bool on ]]]] |
| 63 | + |
| 64 | + editTest title file = |
| 65 | + testWithDummyPlugin title (mkIdeTestFs [copyDir "local-sig-lens"]) $ do |
| 66 | + doc <- openDoc (file ++ ".hs") "haskell" |
| 67 | + executeAllHints doc globalRange |
| 68 | + real <- documentContents doc |
| 69 | + expectedDoc <- openDoc (file ++ ".expected.hs") "haskell" |
| 70 | + expected <- documentContents expectedDoc |
| 71 | + liftIO $ real @?= expected |
| 72 | + |
| 73 | + executeAllHints :: TextDocumentIdentifier -> Range -> Session () |
| 74 | + executeAllHints doc range = do |
| 75 | + void $ waitForTypecheck doc |
| 76 | + hints <- getInlayHints doc range |
| 77 | + let edits = concat $ mapMaybe _textEdits hints |
| 78 | + case edits of |
| 79 | + [] -> pure () |
| 80 | + edit : _ -> do |
| 81 | + newDoc <- applyEdit doc edit |
| 82 | + -- pure () |
| 83 | + executeAllHints (TextDocumentIdentifier $ _uri newDoc) range |
| 84 | + |
| 85 | +globalRange :: Range |
| 86 | +globalRange = Range { _start = Position 0 0 |
| 87 | + , _end = Position 1000 0 |
| 88 | + } |
0 commit comments