@@ -6,13 +6,15 @@ module Main
66 ) where
77
88import Control.Lens (_Just , folded , preview , view , (^.) ,
9- (^..) )
9+ (^..) , (^?) )
10+ import Control.Monad (join )
1011import Data.Aeson (Value (Object ), fromJSON , object ,
1112 (.=) )
1213import Data.Aeson.Types (Pair , Result (Success ))
1314import Data.List (isInfixOf )
1415import Data.List.Extra (nubOrdOn )
1516import qualified Data.Map as Map
17+ import qualified Data.Maybe as Maybe
1618import qualified Data.Text as T
1719import Ide.Plugin.Config (Config )
1820import qualified Ide.Plugin.Config as Plugin
@@ -59,6 +61,9 @@ tests =
5961 lenses <- getCodeLenses doc
6062 liftIO $ map (view range) lenses @?= [Range (Position 4 0 ) (Position 5 0 )]
6163
64+ , goldenWithEvalForCodeAction " Evaluation of expressions via code action" " T1" " hs"
65+ , goldenWithEvalForCodeAction " Reevaluation of expressions via code action" " T2" " hs"
66+
6267 , goldenWithEval " Evaluation of expressions" " T1" " hs"
6368 , goldenWithEval " Reevaluation of expressions" " T2" " hs"
6469 , goldenWithEval " Evaluation of expressions w/ imports" " T3" " hs"
@@ -221,6 +226,10 @@ goldenWithEval :: TestName -> FilePath -> FilePath -> TestTree
221226goldenWithEval title path ext =
222227 goldenWithHaskellDocInTmpDir def evalPlugin title (mkFs $ FS. directProject (path <.> ext)) path " expected" ext executeLensesBackwards
223228
229+ goldenWithEvalForCodeAction :: TestName -> FilePath -> FilePath -> TestTree
230+ goldenWithEvalForCodeAction title path ext =
231+ goldenWithHaskellDocInTmpDir def evalPlugin title (mkFs $ FS. directProject (path <.> ext)) path " expected" ext executeCodeActionsBackwards
232+
224233goldenWithEvalAndFs :: TestName -> [FS. FileTree ] -> FilePath -> FilePath -> TestTree
225234goldenWithEvalAndFs title tree path ext =
226235 goldenWithHaskellDocInTmpDir def evalPlugin title (mkFs tree) path " expected" ext executeLensesBackwards
@@ -239,14 +248,24 @@ goldenWithEvalAndFs' title tree path ext expected =
239248-- | Execute lenses backwards, to avoid affecting their position in the source file
240249executeLensesBackwards :: TextDocumentIdentifier -> Session ()
241250executeLensesBackwards doc = do
242- codeLenses <- reverse <$> getCodeLenses doc
251+ codeLenses <- getCodeLenses doc
243252 -- liftIO $ print codeLenses
253+ executeCmdsBackwards [c | CodeLens {_command = Just c} <- codeLenses]
254+
255+ executeCodeActionsBackwards :: TextDocumentIdentifier -> Session ()
256+ executeCodeActionsBackwards doc = do
257+ codeLenses <- getCodeLenses doc
258+ let ranges = [_range | CodeLens {_range} <- codeLenses]
259+ -- getAllCodeActions cannot get our code actions because they have no diagnostics
260+ codeActions <- join <$> traverse (getCodeActions doc) ranges
261+ let cmds = Maybe. mapMaybe (^? _L) codeActions
262+ executeCmdsBackwards cmds
244263
245- -- Execute sequentially , nubbing elements to avoid
246- -- evaluating the same section with multiple tests
247- -- more than twice
248- mapM_ executeCmd $
249- nubOrdOn actSectionId [c | CodeLens {_command = Just c} <- codeLenses]
264+ -- Execute commands backwards , nubbing elements to avoid
265+ -- evaluating the same section with multiple tests
266+ -- more than twice
267+ executeCmdsBackwards :: [ Command ] -> Session ()
268+ executeCmdsBackwards = mapM_ executeCmd . nubOrdOn actSectionId . reverse
250269
251270actSectionId :: Command -> Int
252271actSectionId Command {_arguments = Just [fromJSON -> Success EvalParams {.. }]} = evalId
0 commit comments