agda-language-server 9 → 10
raw patch · 12 files changed
+1180/−34 lines, 12 filesdep ~Agda
Dependency ranges changed: Agda
Files
- CHANGELOG.md +16/−0
- README.md +11/−0
- agda-language-server.cabal +50/−1
- bench/Bench.hs +179/−0
- package.yaml +10/−1
- src/Agda.hs +18/−10
- src/Monad.hs +15/−3
- src/Options.hs +8/−1
- src/Server/Handler.hs +246/−16
- test/Test.hs +2/−0
- test/Test/Hover.hs +533/−0
- test/Test/LSP.hs +92/−2
CHANGELOG.md view
@@ -4,6 +4,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). +## v10 - 2026-11-15++### Added+- #68: Cache the hover's load and reuse it until its inputs change; add `--no-hover-cache` to load the file for every hover.+- A cache-only CI mode that skips ALS builds and tests, and a scheduled refresh of the shared caches every three days.++### Changed+- #68: Keep decoded modules on reload, except the changed ones.+- Run all native Linux jobs on `ubuntu-24.04`, and fail the Linux artifact check if it needs glibc above 2.38.+- Document the Ubuntu binaries' arch, glibc and system libraries, and point users of other distros to building from source.++### Fixed+- #66: Register the parsed backends so `Cmd_compile` finds the GHC backend, including when Agda options fail to parse.+- #68: Keep module parameters in hovers after an `.agdai` load, and print the hover type inside `atTopLevel`.+- Let tar restore the root-owned Linux toolchain cache.+ ## v9 - 2026-10-27 ### Changed
README.md view
@@ -22,6 +22,17 @@ Supported platforms: **Windows**, **Mac**, and **Ubuntu**. +#### Ubuntu++The `-ubuntu` binaries are for x86_64. They are built and tested on Ubuntu 24.04, and need glibc 2.38 or later.++The ICU libraries are bundled in the archive. The other libraries come from the system:+- glibc, including the dynamic loader `ld-linux-x86-64.so.2` and `libm`+- `libgmp`, `libz`, and `libtinfo`+- `libstdc++` from GCC 12 or later (`GLIBCXX_3.4.30`), and `libgcc_s`++The prebuilt binaries are not supported on other Linux distributions, such as NixOS. On those, [build from source](#build-from-source) instead.+ #### Development releases For testing the latest features and bug fixes, you can download development pre-releases:
agda-language-server.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: agda-language-server-version: 9+version: 10 synopsis: An implementation of language server protocal (LSP) for Agda 2. description: Please see the README on GitHub at <https://github.com/agda/agda-language-server#readme> category: Development@@ -185,6 +185,7 @@ type: exitcode-stdio-1.0 main-is: Test.hs other-modules:+ Test.Hover Test.LSP Test.Smoke Test.SrcLoc@@ -267,3 +268,51 @@ ghc-options: -with-rtsopts=-V1 if !arch(wasm32) ghc-options: -threaded -with-rtsopts=-N++benchmark als-bench+ type: exitcode-stdio-1.0+ main-is: Bench.hs+ other-modules:+ Paths_agda_language_server+ hs-source-dirs:+ bench+ default-extensions:+ LambdaCase+ OverloadedStrings+ PatternSynonyms+ TypeOperators+ ghc-options: -threaded+ build-depends:+ Agda+ , aeson+ , base >=4.7 && <5+ , bytestring+ , containers+ , directory+ , filepath+ , lsp >=2+ , lsp-test+ , lsp-types >=2+ , mtl+ , prettyprinter+ , process+ , stm+ , strict+ , text+ default-language: Haskell2010+ if flag(Agda-2-6-4)+ build-depends:+ Agda ==2.6.4.3+ if flag(Agda-2-7-0)+ build-depends:+ Agda ==2.7.0.1+ if flag(Agda-2-8-0)+ build-depends:+ Agda ==2.8.0+ if arch(wasm32)+ build-depends:+ unix >=2.8.0.0 && <2.9+ else+ build-depends:+ network+ , network-simple ==0.4.2
+ bench/Bench.hs view
@@ -0,0 +1,179 @@+-- | A by-hand benchmark of the hover. CI does not run it.+--+-- It times the same hovers on the als that stack builds from this tree,+-- with the hover cache off (--no-hover-cache) and on, and prints the+-- median of each and the ratio on / off:+--+-- > stack bench+--+-- Each case writes its own Agda files to a temporary directory, so the+-- result depends only on this tree.+module Main (main) where++import Control.Monad (forM, forM_, replicateM, when)+import Control.Monad.IO.Class (liftIO)+import Data.List (sort)+import Data.Text (Text)+import qualified Data.Text as Text+import qualified Data.Text.IO as TextIO+import GHC.Clock (getMonotonicTimeNSec)+import Language.LSP.Protocol.Message+import Language.LSP.Protocol.Types+import Language.LSP.Test+import System.Directory (createDirectoryIfMissing, doesDirectoryExist, getTemporaryDirectory, removeDirectoryRecursive)+import System.Environment (getArgs, getEnvironment)+import System.Exit (die)+import System.FilePath (takeDirectory, (</>))+import System.Process (CreateProcess (cwd, env))+import Text.Printf (printf)++-- | Generated Agda files, and where to hover+data Case = Case+ { caseName :: String,+ caseFiles :: [(FilePath, Text)],+ -- | The file that the hover is in+ caseMain :: FilePath,+ caseHover :: Position,+ -- | The file that the edit adds a definition to, and then removes it+ caseEdited :: FilePath+ }++-- | Main imports M(n-1), which imports M(n-2), and so on down to M0. The+-- edit is to M(n-2), near the top, so the modules below it do not change+-- and a reload can reuse them.+modules :: Int -> Case+modules n = (chain n 0 0) {caseName = "modules " ++ show n}++-- | 'modules 50', with this many more include dirs, all empty. Each module+-- lookup looks in every include dir.+includeDirs :: Int -> Case+includeDirs dirs = (chain 50 dirs 0) {caseName = "include dirs " ++ show dirs}++-- | 'modules 50', with this many more lines in Main. A reload checks Main+-- again.+largeFile :: Int -> Case+largeFile lines' = (chain 50 0 lines') {caseName = "Main " ++ show lines' ++ " lines"}++-- | A chain of n modules, with this many more include dirs and this many+-- more lines in Main+chain :: Int -> Int -> Int -> Case+chain n dirs lines' =+ Case+ { caseName = "",+ caseFiles = ("Main.agda", mainText) : [(file k, moduleText k) | k <- [0 .. n - 1]] ++ library,+ caseMain = "Main.agda",+ -- 'f' in 'g = f'+ caseHover = Position 5 4,+ caseEdited = file (n - 2)+ }+ where+ name k = "M" <> Text.pack (show k)+ file k = Text.unpack (name k) ++ ".agda"+ mainText =+ Text.unlines $+ ["module Main where", "", "open import " <> name (n - 1), "", "g : T → T", "g = f"]+ ++ concat [["", "h" <> Text.pack (show k) <> " : T", "h" <> Text.pack (show k) <> " = f t"] | k <- [1 .. lines' `div` 3]]+ dir k = "i" ++ show k+ -- an empty file in each include dir, so that the dir exists+ library+ | dirs == 0 = []+ | otherwise =+ ("bench.agda-lib", "name: bench\ninclude: . " <> Text.pack (unwords (map dir [1 .. dirs])) <> "\n")+ : [(dir k </> "empty", "") | k <- [1 .. dirs]]+ moduleText 0 = Text.unlines ["module M0 where", "", "data T : Set where", " t : T", "", "f : T → T", "f x = x"]+ moduleText k = Text.unlines ["module " <> name k <> " where", "", "open import " <> name (k - 1) <> " public", "", "d" <> name k <> " : T", "d" <> name k <> " = f t"]++-- | What every hover in every case shows+expected :: Text+expected = "\n```agda-language-server\nT → T\n```\n"++cases :: [Case]+cases =+ [modules 50, modules 100, modules 200]+ ++ [includeDirs 10, includeDirs 20, includeDirs 40]+ ++ [largeFile 1000, largeFile 2000, largeFile 4000]++-- | Hovers with no change, and hovers after an edit+noChangeSamples, editCycles :: Int+noChangeSamples = 10+editCycles = 3++main :: IO ()+main = do+ args <- getArgs+ -- 'stack bench' puts the als it builds on the PATH+ als <- case args of+ [] -> return "als"+ ["--als", path] -> return path+ _ -> die "usage: stack bench [--ba '--als <path to als>']"+ putStrLn "Median time of a hover, in ms, with the hover cache off and on. Ratio is on / off.\n"+ putStrLn " | no change | after an edit"+ putStrLn "case | off on ratio | off on ratio"+ forM_ cases $ \c -> do+ (offNoChange, offEdit) <- measure "off" (als ++ " --no-hover-cache") c+ (onNoChange, onEdit) <- measure "on" als c+ printf+ "%-16s | %8.1f %8.1f %9.2f | %8.1f %8.1f %9.2f\n"+ (caseName c)+ offNoChange+ onNoChange+ (onNoChange / offNoChange)+ offEdit+ onEdit+ (onEdit / offEdit)++-- | The medians of the hovers with no change and of the hovers after an+-- edit, on fresh copies of the files. The first hover loads the files and+-- is not timed. The command is the als executable and its arguments.+measure :: String -> FilePath -> Case -> IO (Double, Double)+measure label als c = do+ tmp <- getTemporaryDirectory+ let dir = tmp </> "als-bench" </> map (\x -> if x == ' ' then '-' else x) (caseName c) </> label+ exists <- doesDirectoryExist dir+ when exists $ removeDirectoryRecursive dir+ createDirectoryIfMissing True dir+ forM_ (caseFiles c) $ \(file, text) -> do+ createDirectoryIfMissing True (takeDirectory (dir </> file))+ TextIO.writeFile (dir </> file) text+ let edited = dir </> caseEdited c+ original <- TextIO.readFile edited+ inSession als dir $ do+ doc <- openDoc (caseMain c) "agda"+ _ <- timedHover doc+ noChange <- replicateM noChangeSamples (timedHover doc)+ afterEdit <- fmap concat $ forM [1 .. editCycles] $ \_ -> do+ liftIO $ TextIO.writeFile edited (original <> "\nextra : T\nextra = t\n")+ added <- timedHover doc+ liftIO $ TextIO.writeFile edited original+ removed <- timedHover doc+ return [added, removed]+ return (median noChange, median afterEdit)+ where+ -- the time of a hover in ms; stops the benchmark if the hover is wrong+ timedHover doc = do+ start <- liftIO getMonotonicTimeNSec+ TResponseMessage _ _ rsp <- request SMethod_TextDocumentHover (HoverParams doc (caseHover c) Nothing)+ end <- liftIO getMonotonicTimeNSec+ case rsp of+ Right (InL (Hover (InL (MarkupContent _ content)) _)) | content == expected -> return ()+ _ -> liftIO $ die (label ++ ", " ++ caseName c ++ ": unexpected hover: " ++ show rsp)+ return (fromIntegral (end - start) / 1e6)++-- | Runs a session with als in the directory, with its own AGDA_DIR, so+-- that the Agda libraries of the user do not change the result+inSession :: FilePath -> FilePath -> Session a -> IO a+inSession als dir session = do+ let agdaDir = dir </> ".agda"+ createDirectoryIfMissing True agdaDir+ environment <- filter ((/= "AGDA_DIR") . fst) <$> getEnvironment+ let inDir process = process {cwd = Just dir, env = Just (("AGDA_DIR", agdaDir) : environment)}+ runSessionWithConfigCustomProcess inDir defaultConfig als fullLatestClientCaps dir session++median :: [Double] -> Double+median xs+ | even n = (sorted !! (half - 1) + sorted !! half) / 2+ | otherwise = sorted !! half+ where+ sorted = sort xs+ n = length xs+ half = n `div` 2
package.yaml view
@@ -1,5 +1,5 @@ name: agda-language-server-version: 9+version: 10 github: "agda/agda-language-server" license: MIT author: "Ting-Gian LUA"@@ -172,6 +172,15 @@ ghc-options: - -threaded - -with-rtsopts=-N++benchmarks:+ als-bench:+ main: Bench.hs+ source-dirs: bench+ dependencies:+ - lsp-test+ ghc-options:+ - -threaded # tests: # als-test:
src/Agda.hs view
@@ -53,9 +53,12 @@ , prettyTCWarnings' ) import Agda.TypeChecking.Monad ( HasOptions+ , MonadTCState , TCErr , commandLineOptions , runTCMTop'+ , setTCLens+ , stBackends ) import Agda.TypeChecking.Monad.Base ( TCM ) import qualified Agda.TypeChecking.Monad.Benchmark@@ -194,7 +197,7 @@ -------------------------------------------------------------------------------- getCommandLineOptions- :: (HasOptions m, MonadIO m) => ServerM m CommandLineOptions+ :: (HasOptions m, MonadTCState m, MonadIO m) => ServerM m CommandLineOptions getCommandLineOptions = do -- command line options from ARGV argv <- asks (optRawAgdaOptions . envOptions)@@ -203,15 +206,20 @@ -- concatenate both let merged = argv <> config - result <- runExceptT $ do- let p = parseBackendOptions builtinBackends merged defaultOptions- let (r, _warns) = runOptM p- (bs, opts) <- ExceptT $ pure r- return opts- case result of- -- something bad happened, use the default options instead- Left _ -> commandLineOptions- Right opts -> return opts+ case parse merged of+ -- something bad happened, use the default options instead,+ -- and register the backends with their default options+ Left _ -> do+ forM_ (parse []) $ \(bs, _) -> register bs+ commandLineOptions+ Right (bs, opts) -> do+ register bs+ return opts+ where+ parse args =+ fst $ runOptM $ parseBackendOptions builtinBackends args defaultOptions+ -- every fresh TCM state starts with no backends+ register bs = lift $ setTCLens stBackends bs -- | Run a TCM action in IO and throw away all of the errors -- TODO: handle the caught errors
src/Monad.hs view
@@ -4,8 +4,8 @@ module Monad where import Agda.IR-import Agda.Interaction.Base (IOTCM)-import Agda.TypeChecking.Monad (TCMT)+import Agda.Interaction.Base (CommandState, IOTCM)+import Agda.TypeChecking.Monad (TCM, TCMT, TCState) import Control.Concurrent import Control.Monad.Reader import Data.IORef@@ -40,9 +40,20 @@ envLogChan :: Chan Text, envCommandController :: CommandController, envResponseChan :: Chan Response,- envResponseController :: ResponseController+ envResponseController :: ResponseController,+ -- | The state after the last load done for a hover+ envHoverCache :: IORef (Maybe HoverCache) } +-- | What a hover needs to skip loading a file again+data HoverCache = HoverCache+ { hcFilePath :: FilePath,+ -- | Asks Agda if it would still see the same inputs. Runs in a fresh state.+ hcStillValid :: TCM Bool,+ hcTCState :: TCState,+ hcCommandState :: CommandState+ }+ createInitEnv :: (MonadIO m, MonadLsp Config m) => Options -> m Env createInitEnv options = Env options (isJust (optViaTCP options))@@ -51,6 +62,7 @@ <*> liftIO CommandController.new <*> liftIO newChan <*> liftIO ResponseController.new+ <*> liftIO (newIORef Nothing) --------------------------------------------------------------------------------
src/Options.hs view
@@ -43,6 +43,8 @@ { optViaTCP :: Maybe Int, optRawAgdaOptions :: [String], optRawResponses :: Bool,+ -- | Load the file again for every hover, without the hover cache+ optNoHoverCache :: Bool, optSetup :: Bool, optHelp :: Bool, optVersion :: Bool@@ -50,7 +52,7 @@ defaultOptions :: Options defaultOptions =- Options {optViaTCP = Nothing, optRawAgdaOptions = [], optRawResponses = False, optSetup = False, optHelp = False, optVersion = False}+ Options {optViaTCP = Nothing, optRawAgdaOptions = [], optRawResponses = False, optNoHoverCache = False, optSetup = False, optHelp = False, optVersion = False} options :: [OptDescr (Options -> Options)] options =@@ -75,6 +77,11 @@ ["raw"] (NoArg (\opts -> opts {optRawResponses = True})) "return all responses in raw JSON format",+ Option+ []+ ["no-hover-cache"]+ (NoArg (\opts -> opts {optNoHoverCache = True}))+ "load the file again for every hover, for comparison with the hover cache", #if MIN_VERSION_Agda(2,8,0) Option []
src/Server/Handler.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE CPP #-}+{-# LANGUAGE ScopedTypeVariables #-} module Server.Handler where @@ -37,22 +38,36 @@ ) import Agda.Interaction.Options ( CommandLineOptions ( optAbsoluteIncludePaths+ , optTrustedExecutables ) ) import qualified Agda.Parser as Parser import Agda.Position ( makeToOffset , toAgdaPositionWithoutFile )+import Agda.Syntax.Abstract.Name ( isLeParentModuleOf ) import Agda.Syntax.Abstract.Pretty ( prettyATop ) import Agda.Syntax.Parser ( exprParser , parse ) import Agda.Syntax.Translation.ConcreteToAbstract ( concreteToAbstract_ )-import Agda.TypeChecking.Monad ( HasOptions(commandLineOptions)- , setInteractionOutputCallback- )+import Agda.Interaction.FindFile ( findFile' ) #if MIN_VERSION_Agda(2,8,0)+#else+import Agda.Interaction.FindFile ( SourceFile(srcFilePath) )+#endif+import Agda.Interaction.Library ( OptionsPragma(pragmaStrings) )+import Agda.Syntax.TopLevelModuleName ( TopLevelModuleName, moduleNameToFileName, projectRoot )+import Agda.TypeChecking.Monad+import Agda.Utils.FileName ( AbsolutePath, absolute, doesFileExistCaseSensitive, filePath )+import Agda.Utils.Hash ( Hash, hashTextFile )+#if MIN_VERSION_Agda(2,8,0)+import Agda.Syntax.Parser ( agdaFileExtensions )+#else+import Agda.Syntax.Parser ( acceptableFileExts )+#endif+#if MIN_VERSION_Agda(2,8,0) import Agda.Interaction.Command ( CommandM, localStateCommandM ) import Agda.TypeChecking.Monad.Trace ( runPM ) #endif@@ -62,8 +77,22 @@ #endif import Agda.Syntax.Common.Pretty ( render ) import Control.Concurrent.STM+import Control.Exception ( IOException+ , SomeAsyncException+ , SomeException+ , catch+ , fromException+ , throwIO+ )+import Control.Monad ( filterM, forM, forM_, unless ) import Control.Monad.Reader import Control.Monad.State+import Data.Foldable ( toList )+import Data.IORef ( readIORef+ , writeIORef+ )+import qualified Data.Map as Map+import qualified Data.Set as Set import Data.Text ( Text , pack , unpack@@ -75,8 +104,12 @@ import qualified Language.LSP.VFS as VFS import Monad import Options ( Config- , Options(optRawAgdaOptions)+ , Options(optNoHoverCache, optRawAgdaOptions) )+import Data.Char ( toLower )+import Data.List ( isSuffixOf )+import System.Directory ( listDirectory )+import System.FilePath ( (</>), takeDirectory ) initialiseCommandQueue :: IO CommandQueue initialiseCommandQueue = CommandQueue <$> newTChanIO <*> newTVarIO Nothing@@ -99,20 +132,217 @@ lift $ evalStateT program commandState +-- | The path Agda resolves a module to right now, if it resolves to one+resolveModule :: TopLevelModuleName -> TCM (Maybe AbsolutePath)+#if MIN_VERSION_Agda(2,8,0)+resolveModule m = findFile' m >>= either (const (return Nothing)) (fmap Just . srcFilePath)+#else+resolveModule m = either (const Nothing) (Just . srcFilePath) <$> findFile' m+#endif++hashOf :: AbsolutePath -> IO (Maybe Hash)+hashOf file = (Just <$> hashTextFile file) `catch` \(_ :: IOException) -> return Nothing++-- | The file extensions of Agda source files+sourceExtensions :: [String]+#if MIN_VERSION_Agda(2,8,0)+sourceExtensions = agdaFileExtensions+#else+sourceExtensions = acceptableFileExts+#endif++-- | The files that a module could be in, in these include dirs+candidateFiles :: [AbsolutePath] -> TopLevelModuleName -> [FilePath]+candidateFiles includes m = Set.toList $ Set.fromList+ [ filePath dir </> moduleNameToFileName m e | dir <- includes, e <- sourceExtensions ]++-- | A check of whether a module still has exactly one candidate file, and it+-- is this one, for these modules. It lists each directory that their+-- candidates are in once, and skips an include dir where that directory has+-- no file with an Agda extension, so that a module costs file checks only+-- in the include dirs that could have it. A directory that cannot be listed+-- is not skipped.+lookupCheck :: [AbsolutePath] -> [TopLevelModuleName] -> IO (TopLevelModuleName -> AbsolutePath -> IO Bool)+lookupCheck includes names = do+ let subdirs = Set.toList $ Set.fromList $ map subdirOf names+ hasSources <- fmap Map.fromList $ forM [ (dir, sub) | dir <- includes, sub <- subdirs ] $ \key@(dir, sub) -> do+ found <- (any isSourceFile <$> listDirectory (filePath dir </> sub)) `catch` \(_ :: IOException) -> return True+ return (key, found)+ return $ \m path -> do+ let dirs = [ dir | dir <- includes, Map.findWithDefault True (dir, subdirOf m) hasSources ]+ existing <- filterM doesFileExistCaseSensitive (candidateFiles dirs m)+ case existing of+ [file] | file == filePath path -> return True+ | otherwise -> (== path) <$> absolute file+ _ -> return False+ where+ subdirOf m = takeDirectory (moduleNameToFileName m "")+ -- in lower case, so that a file system that ignores case cannot hide a+ -- candidate+ isSourceFile name = any (`isSuffixOf` map toLower name) sourceExtensions++-- | Builds the check for later hovers from the load that just finished, or+-- gives Nothing if that load cannot be checked. The check runs Agda's own+-- library lookup and source hash, and replaces 'findFile'' with an existence+-- check over the same candidate paths.+loadCheck :: AbsolutePath -> CommandLineOptions -> TCM (Maybe (TCM Bool))+loadCheck mainFile opts = do+ visited <- Map.toList <$> getVisitedModules+ resolved <- forM visited $ \(m, mi) -> do+ path <- resolveModule m+ return $ (\p -> (m, p, iSourceHash (miInterface mi))) <$> path+ includes <- toList <$> getIncludeDirs+ case sequence resolved of+ Nothing -> return Nothing+ Just entries -> case [ m | (m, p, _) <- entries, p == mainFile ] of+ [] -> return Nothing+ mainModule : _ -> do+ let projects = Map.elems $ Map.fromList+ [ ((takeDirectory (filePath p), filePath (projectRoot p m)), (m, p)) | (m, p, _) <- entries ]+ libraryOptions = forM projects $ \(m, p) -> map pragmaStrings <$> getLibraryOptions p m+ sources = [ p | (_, p, _) <- entries ]+ hashes = [ Just h | (_, _, h) <- entries ]+ libOptions <- libraryOptions+ executables <- optTrustedExecutables <$> addTrustedExecutables opts+ return $ Just $ do+ -- cheapest check that finds an edit comes first+ hashes' <- liftIO $ mapM hashOf sources+ if hashes' /= hashes then return False else do+ setCommandLineOptions' (projectRoot mainFile mainModule) opts+ includes' <- toList <$> getIncludeDirs+ executables' <- optTrustedExecutables <$> addTrustedExecutables opts+ if includes' /= includes || executables' /= executables then return False else do+ -- the load proved that each module had exactly one candidate,+ -- the one Agda resolved, so only a later hover needs to look at+ -- the disk+ resolves <- liftIO $ do+ stillResolves <- lookupCheck includes [ m | (m, _, _) <- entries ]+ and <$> mapM (\(m, p, _) -> stillResolves m p) entries+ if not resolves then return False else (== libOptions) <$> libraryOptions++-- | The persistent state of the last load, for the next load to start from,+-- so that Agda reuses the modules it decoded. It drops each module whose+-- lookup changed, and each module that imports one of those, directly or+-- not. Agda checks the imports of a decoded module before it checks any+-- declaration, so a kept module reports a broken import with a shorter+-- range and less context than a fresh load does. Gives Nothing if the+-- lookups cannot be checked.+--+-- Agda's own 'modulesToKeep', in 'setIncludeDirs', does not cover this. It+-- drops the decoded modules whose lookup differs between the old and the+-- new include dirs, but 'cmd_load'' resets the state before it sets the+-- include dirs, which clears the old lookups, so Agda looks up the "old"+-- side on the disk as it is now. A change on the disk under the same+-- include dirs then goes unseen: when A/M.agda is deleted and B/M.agda is+-- added, Agda keeps the decoded M, and this function drops it.+persistentStateForReload :: TCState -> TCM (Maybe PersistentTCState)+persistentStateForReload old = do+ fresh <- getTC+ result <- (putTC old >> Just <$> keep) `orOnAnyException` return Nothing+ putTC fresh+ return result+ where+ persistent = stPersistentState old+ decoded = stDecodedModules persistent+ importsOf = map fst . iImportedModules . miInterface+ keep = do+ -- the include dirs of the last load, which its lookups used+ includes <- toList <$> getIncludeDirs+ let names = Set.toList $ Set.fromList $ Map.keys decoded ++ concatMap importsOf (Map.elems decoded)+ resolvesTo <- liftIO $ lookupCheck includes names+ let sameLookup m = resolveModule m >>= maybe (return False) (liftIO . resolvesTo m)+ changed <- Set.fromList <$> filterM (fmap not . sameLookup) names+ let addImporters bad = Set.union bad $ Map.keysSet $ Map.filter (any (`Set.member` bad) . importsOf) decoded+ closure bad = let bad' = addImporters bad in if bad' == bad then bad else closure bad'+ return persistent { stDecodedModules = Map.withoutKeys decoded (closure changed) }++-- | Runs a TCM action and gives the fallback on any exception, not only on+-- a 'TCErr': Agda reports a bug with '__IMPOSSIBLE__', which is a Haskell+-- exception, and the checks also do IO. Asynchronous exceptions, such as a+-- cancelled request, are thrown again.+orOnAnyException :: TCM a -> TCM a -> TCM a+orOnAnyException m fallback = TCM $ \r e ->+ unTCM m r e `catch` \(ex :: SomeException) -> case fromException ex of+ Just (async :: SomeAsyncException) -> throwIO async+ Nothing -> unTCM fallback r e++-- | Registers the current module and its submodules at the current+-- checkpoint, so that names defined in them are applied to the module+-- parameters. Runs inside 'atTopLevel'.+--+-- This works around Agda #8905. 'atTopLevel' moves the module checkpoints+-- that exist to the checkpoint of the context it builds, but after the+-- current module is loaded from its .agdai, none exist, so 'D' in+-- @module M (A : Set)@ comes out as @(A₁ : Set) → Set@ instead of @Set@.+-- The fix in Agda, agda/agda#8906, does the same as this function inside+-- 'atTopLevel', and is not merged.+--+-- When an Agda release has the fix, run this function only on the Agda+-- versions before that release, with a CPP version check, and remove it+-- once every supported Agda has the fix.+--+-- #8906 leaves open whether submodules with their own parameters should+-- get the checkpoint (they do here, as after a load that checks the+-- module), and whether loading the .agdai should register it instead. If+-- Agda decides otherwise, this function differs from Agda's fix on the+-- versions it still runs on. No test covers a submodule with parameters.+keepModuleParameters :: TCM ()+keepModuleParameters = do+ current <- currentModule+ sections <- Map.union <$> useTC (stSignature . sigSections)+ <*> useTC (stImports . sigSections)+ forM_ (filter (current `isLeParentModuleOf`) (Map.keys sections)) setModuleCheckpoint+ inferTypeOfText :: FilePath -> Text -> ServerM (LspM Config) (Either String String)-inferTypeOfText filepath text = runCommandM $ do- -- load first- cmd_load' filepath [] True Imp.TypeCheck $ \_ -> return ()- -- infer later- let norm = AsIs- -- localStateCommandM: restore TC state afterwards, do we need this here?- typ <- localStateCommandM $ do- (e, _attrs) <- lift $ runPM $ parse exprParser (unpack text)- lift $ atTopLevel $ do- concreteToAbstract_ e >>= typeInCurrent norm-- render <$> prettyATop typ+inferTypeOfText filepath text = do+ cacheRef <- asks envHoverCache+ -- with --no-hover-cache, every hover loads from a fresh state, and no+ -- cache is read, checked or stored+ noCache <- asks (optNoHoverCache . envOptions)+ runCommandM $ do+ cached <- if noCache then return Nothing else liftIO $ readIORef cacheRef+ -- ask Agda if it would still see the same inputs, and leave no trace of asking+ reusable <- case cached of+ Just c | hcFilePath c == filepath -> lift $ do+ fresh <- getTC+ ok <- hcStillValid c `orOnAnyException` return False+ putTC fresh+ return ok+ _ -> return False+ case cached of+ Just c | reusable -> do+ lift $ putTC (hcTCState c)+ put (hcCommandState c)+ _ -> do+ -- start from the modules the last load decoded, except the changed ones+ forM_ cached $ \c -> lift $ do+ kept <- persistentStateForReload (hcTCState c)+ forM_ kept $ \p -> modifyTC $ \st -> st { stPersistentState = p }+ -- load first+ cmd_load' filepath [] True Imp.TypeCheck $ \_ -> return ()+ unless noCache $ do+ tcState <- lift getTC+ commandState <- get+ mainFile <- liftIO $ absolute filepath+ opts <- gets optionsOnReload+ check <- lift $ loadCheck mainFile opts `orOnAnyException` return Nothing+ forM_ check $ \stillValid ->+ liftIO $ writeIORef cacheRef $ Just $+ HoverCache filepath stillValid tcState commandState+ -- infer later+ let norm = AsIs+ -- localStateCommandM: restore TC state afterwards, do we need this here?+ localStateCommandM $ do+ (e, _attrs) <- lift $ runPM $ parse exprParser (unpack text)+ -- print inside 'atTopLevel', which sets the scope of the module, like+ -- Agda's own Emacs mode does. The scope that 'cmd_load'' leaves is+ -- complete only when it checks the module, not when it loads the+ -- module from its .agdai, and printing in it qualifies names.+ lift $ atTopLevel $ do+ keepModuleParameters+ typ <- concreteToAbstract_ e >>= typeInCurrent norm+ render <$> prettyATop typ onHover :: LSP.Uri -> LSP.Position -> ServerM (LspM Config) (LSP.Hover LSP.|? LSP.Null) onHover uri pos = do
test/Test.hs view
@@ -2,6 +2,7 @@ import Data.Proxy (Proxy (..)) import Data.Typeable (Typeable)+import qualified Test.Hover as Hover import qualified Test.LSP as LSP import qualified Test.Smoke as Smoke import qualified Test.SrcLoc as SrcLoc@@ -35,6 +36,7 @@ [ SrcLoc.tests, Version.tests, LSP.tests alsPath,+ Hover.tests alsPath, Smoke.tests alsPath #if defined(wasm32_HOST_ARCH) , WASM.tests alsPath
+ test/Test/Hover.hs view
@@ -0,0 +1,533 @@+{-# LANGUAGE OverloadedStrings #-}++-- | Hover tests (#68)+module Test.Hover (tests) where++import Control.Monad (forM_)+import Control.Monad.IO.Class+import Data.List (sort)+import Data.Text (Text)+import qualified Data.Text as Text+import qualified Data.Text.IO as TextIO+import GHC.Stack (HasCallStack)+import Language.LSP.Protocol.Message+import Language.LSP.Protocol.Types+import Language.LSP.Test+import System.Directory (copyFile, createDirectoryIfMissing, doesDirectoryExist, getModificationTime, getPermissions, listDirectory, removeFile, setModificationTime, setOwnerExecutable, setPermissions)+import System.Environment (getEnvironment)+import System.FilePath (takeExtension, takeFileName, (</>))+import System.Info (os)+import System.Process (CreateProcess (cwd, env))+import Test.LSP (runIOTCM, withTempDir)+import Test.Tasty+import Test.Tasty.HUnit++tests :: FilePath -> TestTree+tests alsPath =+ testGroup+ "Hover"+ [ hoverAfterEdits alsPath,+ hoverAfterImportGraphChanges alsPath,+ hoverAfterProjectChanges alsPath,+ hoverInSession alsPath,+ testCase "hover the same with or without a valid .agdai" (hoverWithInterface alsPath),+ testCase "module parameters with or without a valid .agdai" (hoverParamsWithInterface alsPath),+ testCase "a hover with no changes reuses the last load" (hoverReusesLoad alsPath),+ testCase "with --no-hover-cache, every hover loads again" (hoverWithoutCache alsPath)+ ]++-- | Each test hovers, changes files on disk the way an editor or a git+-- checkout does, and hovers again in the same session. The hover must+-- show what a fresh load of the files on disk shows. Types are pinned as+-- text. Errors are compared with the hover of a fresh server, because+-- their text holds the temporary path.+hoverAfterEdits :: FilePath -> TestTree+hoverAfterEdits alsPath =+ testGroup+ "hover after edits"+ [ testCase "edit the hovered file" $ withFixtures alsPath "Hover" "als-hover-file" $ \_ -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ save doc "module Main where\n\nopen import Dep hiding (f)\n\nf : T -> T -> T\nf x _ = x\n\ng : _\ng = f\n"+ expectHover doc (Position 8 4) "T → T → T",+ testCase "edit an import, then undo the edit" $ withFixtures alsPath "Hover" "als-hover-import" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ original <- readText (dir </> "Dep.agda")+ writeText (dir </> "Dep.agda") (addArgumentToF original)+ expectHover doc onF "T → T → T"+ writeText (dir </> "Dep.agda") original+ expectHover doc onF "T → T",+ testCase "edit an import of an import" $ withFixtures alsPath "Hover" "als-hover-transitive" $ \dir -> do+ doc <- openDoc "ViaMid.agda" "agda"+ expectHover doc onF "T → T"+ original <- readText (dir </> "Dep.agda")+ writeText (dir </> "Dep.agda") (addArgumentToF original)+ expectHover doc onF "T → T → T",+ testCase "save with no change" $ withFixtures alsPath "Hover" "als-hover-touch" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ forM_ ["Main.agda", "Dep.agda"] $ \file -> readText (dir </> file) >>= writeText (dir </> file)+ expectHover doc onF "T → T",+ testCase "a new file shadows an import" $ withFixtures alsPath "HoverShadow" "als-hover-shadow" $ \dir -> do+ doc <- openDoc ("main" </> "Main.agda") "agda"+ expectHover doc onF "T → T"+ liftIO $ copyFile (dir </> "dep" </> "Dep.agda") (dir </> "main" </> "Dep.agda")+ expectFreshHover alsPath dir doc onF,+ testCase "delete an import, then restore it" $ withFixtures alsPath "Hover" "als-hover-delete" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ original <- readText (dir </> "Dep.agda")+ liftIO $ removeFile (dir </> "Dep.agda")+ expectFreshHover alsPath dir doc onF+ writeText (dir </> "Dep.agda") original+ expectHover doc onF "T → T",+ testCase "rename an import" $ withFixtures alsPath "Hover" "als-hover-rename" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ dep <- readText (dir </> "Dep.agda")+ liftIO $ removeFile (dir </> "Dep.agda")+ writeText (dir </> "Renamed.agda") (Text.replace "module Dep" "module Renamed" dep)+ expectFreshHover alsPath dir doc onF+ main <- readText (dir </> "Main.agda")+ save doc (Text.replace "import Dep" "import Renamed" main)+ expectHover doc onF "T → T",+ testCase "a literate file replaces an import" $ withFixtures alsPath "Hover" "als-hover-literate" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ dep <- readText (dir </> "Dep.agda")+ liftIO $ removeFile (dir </> "Dep.agda")+ writeText (dir </> "Dep.lagda.md") ("```agda\n" <> addArgumentToF dep <> "```\n")+ expectHover doc onF "T → T → T",+ testCase "a parse error in the hovered file, then fixed" $ withFixtures alsPath "Hover" "als-hover-parse" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ main <- readText (dir </> "Main.agda")+ save doc (main <> "\nh = (\n")+ expectFreshHover alsPath dir doc onF+ save doc main+ expectHover doc onF "T → T",+ testCase "a hole in the hovered file, then an import edit" $ withFixtures alsPath "Hover" "als-hover-hole" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ main <- readText (dir </> "Main.agda")+ save doc (main <> "\nh : T\nh = {! !}\n")+ expectHover doc onF "T → T"+ dep <- readText (dir </> "Dep.agda")+ writeText (dir </> "Dep.agda") (addArgumentToF dep)+ expectHover doc onF "T → T → T"+ save doc (main <> "\nh : T\nh = t\n")+ expectHover doc onF "T → T → T",+ testCase "an import cycle, then fixed" $ withFixtures alsPath "Hover" "als-hover-cycle" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ dep <- readText (dir </> "Dep.agda")+ writeText (dir </> "Dep.agda") ("open import Main\n" <> dep)+ expectFreshHover alsPath dir doc onF+ writeText (dir </> "Dep.agda") dep+ expectHover doc onF "T → T",+ testCase "switch branches and back, which restores old timestamps" $ withFixtures alsPath "Hover" "als-hover-branch" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ let file = dir </> "Dep.agda"+ dep <- readText file+ time <- liftIO $ getModificationTime file+ writeText file (addArgumentToF dep)+ expectHover doc onF "T → T → T"+ writeText file dep+ liftIO $ setModificationTime file time+ expectHover doc onF "T → T"+ ]++-- | Like 'hoverAfterEdits', but an import breaks deeper in the import+-- graph: ViaMid imports Mid, which imports Dep. Agda reports the error+-- where the broken import is, and checks the imports in the order of the+-- declarations. A reload must give the same error as a fresh server.+hoverAfterImportGraphChanges :: FilePath -> TestTree+hoverAfterImportGraphChanges alsPath =+ testGroup "hover after an import breaks deep in the import graph" $+ [ testCase (shape ++ ", " ++ change ++ " Dep") $ withFixtures alsPath "Hover" ("als-hover-graph-" ++ show i) $ \dir -> do+ writeText (dir </> "ViaMid.agda") ("module ViaMid where\n\n" <> imports <> "\ng : _\ng = f\n")+ -- a second include directory, empty until a file shadows Dep+ writeText (dir </> "graph.agda-lib") "name: graph\ninclude: . extra\n"+ liftIO $ createDirectoryIfMissing True (dir </> "extra")+ doc <- openDoc "ViaMid.agda" "agda"+ let onG = Position (fromIntegral (4 + length (Text.lines imports))) 4+ expectHover doc onG "T → T"+ dep <- readText (dir </> "Dep.agda")+ case change of+ "delete" -> liftIO $ removeFile (dir </> "Dep.agda")+ "rename" -> do+ liftIO $ removeFile (dir </> "Dep.agda")+ writeText (dir </> "Renamed.agda") (Text.replace "module Dep" "module Renamed" dep)+ _ -> writeText (dir </> "extra" </> "Dep.agda") dep+ expectFreshHover alsPath dir doc onG+ | (i, (shape, imports, change)) <-+ zip [1 :: Int ..]+ [ (shape, imports, change)+ | (shape, imports) <-+ [ ("ViaMid imports Mid", "open import Mid\n"),+ ("ViaMid imports Dep, then Mid", "open import Dep\nopen import Mid\n"),+ ("ViaMid imports Mid, then Dep", "open import Mid\nopen import Dep\n")+ ],+ change <- ["delete", "rename", "shadow"]+ ]+ ]+ ++ [ testCase ("Dep keeps its path but " ++ fault) $ withFixtures alsPath "Hover" ("als-hover-graph-fault-" ++ show i) $ \dir -> do+ doc <- openDoc "ViaMid.agda" "agda"+ expectHover doc onF "T → T"+ dep <- readText (dir </> "Dep.agda")+ writeText (dir </> "Dep.agda") (breakDep dep)+ expectFreshHover alsPath dir doc onF+ | (i, (fault, breakDep)) <-+ zip+ [1 :: Int ..]+ [ ("names another module", Text.replace "module Dep" "module Other"),+ ("has an unknown option", ("{-# OPTIONS --no-such-option #-}\n" <>)),+ ("has a type error", Text.replace "f x = x" "f x = Set"),+ ("has a postulate under --safe", \dep -> "{-# OPTIONS --safe #-}\n" <> dep <> "\npostulate bad : T\n"),+ ("imports a missing module", Text.replace "module Dep where\n" "module Dep where\n\nopen import Missing\n"),+ ("has a parse error", (<> "\nh = (\n"))+ ]+ ]+ ++ [ testCase ("two imports break, " ++ first ++ " imported first") $ withFixtures alsPath "Hover" ("als-hover-graph-two-" ++ first) $ \dir -> do+ writeText (dir </> "A.agda") "module A where\nopen import MissingA public\n"+ writeText (dir </> "Z.agda") "module Z where\nopen import MissingZ public\n"+ writeText (dir </> "MissingA.agda") "module MissingA where\npostulate TA : Set\n"+ writeText (dir </> "MissingZ.agda") "module MissingZ where\npostulate TZ : Set\n"+ let imports = if first == "Z" then "import Z\nimport A\n" else "import A\nimport Z\n"+ writeText (dir </> "Main.agda") ("module Main where\n\n" <> imports <> "open import Dep\ng : _\ng = f\n")+ doc <- openDoc "Main.agda" "agda"+ expectHover doc (Position 6 4) "T → T"+ liftIO $ removeFile (dir </> "MissingA.agda")+ liftIO $ removeFile (dir </> "MissingZ.agda")+ expectFreshHover alsPath dir doc (Position 6 4)+ | first <- ["A", "Z"]+ ]+ ++ [ testCase "a module in a subdirectory gets a shadow, then moves" $ withFixtures alsPath "Hover" "als-hover-graph-subdirectory" $ \dir -> do+ let dep = "module Sub.Dep where\n\ndata T : Set where\n t : T\n\nf : T -> T\nf x = x\n"+ writeText (dir </> "sub.agda-lib") "name: sub\ninclude: src extra\n"+ liftIO $ createDirectoryIfMissing True (dir </> "src" </> "Sub")+ liftIO $ createDirectoryIfMissing True (dir </> "extra")+ writeText (dir </> "src" </> "Sub" </> "Dep.agda") dep+ writeText (dir </> "src" </> "UsesSub.agda") "module UsesSub where\n\nopen import Sub.Dep\n\ng : _\ng = f\n"+ doc <- openDoc ("src" </> "UsesSub.agda") "agda"+ expectHover doc onF "T → T"+ -- extra/Sub did not exist before+ liftIO $ createDirectoryIfMissing True (dir </> "extra" </> "Sub")+ writeText (dir </> "extra" </> "Sub" </> "Dep.agda") dep+ expectFreshHover alsPath dir doc onF+ liftIO $ removeFile (dir </> "src" </> "Sub" </> "Dep.agda")+ expectHover doc onF "T → T",+ -- the edit after the move tells whether the hover reads Dep from+ -- its new place+ testCase "Dep moves to another listed include dir, then changes" $ withFixtures alsPath "Hover" "als-hover-graph-move" $ \dir -> do+ writeText (dir </> "graph.agda-lib") "name: graph\ninclude: . extra\n"+ liftIO $ createDirectoryIfMissing True (dir </> "extra")+ doc <- openDoc "ViaMid.agda" "agda"+ expectHover doc onF "T → T"+ dep <- readText (dir </> "Dep.agda")+ liftIO $ removeFile (dir </> "Dep.agda")+ writeText (dir </> "extra" </> "Dep.agda") dep+ expectHover doc onF "T → T"+ writeText (dir </> "extra" </> "Dep.agda") (addArgumentToF dep)+ expectHover doc onF "T → T → T"+ ]++-- | Like 'hoverAfterEdits', but the changes are to the project setup:+-- the .agda-lib files, the libraries file and the trusted executables+hoverAfterProjectChanges :: FilePath -> TestTree+hoverAfterProjectChanges alsPath =+ testGroup "hover after project changes" $+ [ testCase "add an include directory that has the import" $ withFixtures alsPath "HoverLibrary" "als-hover-include" $ \dir -> do+ doc <- openDoc ("src" </> "Main.agda") "agda"+ expectHover doc onF "T → T"+ liftIO $ removeFile (dir </> "src" </> "Dep.agda")+ writeText (dir </> "hover-library.agda-lib") "name: hover-library\ninclude: src other\n"+ expectHover doc onF "T → T → T",+ -- no .agda file changes here, so a change in the include dirs is the+ -- only thing that tells the hover to reload+ testCase "only the include dirs change, making an import ambiguous" $ withFixtures alsPath "HoverLibrary" "als-hover-only-includes" $ \dir -> do+ doc <- openDoc ("src" </> "Main.agda") "agda"+ expectHover doc onF "T → T"+ writeText (dir </> "hover-library.agda-lib") "name: hover-library\ninclude: src other\n"+ expectFreshHover alsPath dir doc onF,+ testCase "remove a flag the hovered file needs" $ withFixtures alsPath "HoverLibrary" "als-hover-flags" $ \dir -> do+ let library = dir </> "hover-library.agda-lib"+ original <- readText library+ writeText library (original <> "flags: --prop\n")+ doc <- openDoc ("src" </> "UsesProp.agda") "agda"+ expectHover doc onF "T → T"+ writeText library original+ expectFreshHover alsPath dir doc onF,+ testCase "an import moves into a dependency" $ withFixtures alsPath "HoverLibrary" "als-hover-depend" $ \dir -> do+ doc <- openDoc ("src" </> "Main.agda") "agda"+ expectHover doc onF "T → T"+ writeText (dir </> ".agda" </> "libraries") (Text.pack (dir </> "dep" </> "dep.agda-lib") <> "\n")+ liftIO $ removeFile (dir </> "src" </> "Dep.agda")+ writeText (dir </> "hover-library.agda-lib") "name: hover-library\ninclude: src\ndepend: dep\n"+ expectHover doc onF "T → T → T",+ testCase "the libraries file stops listing a dependency" $ withFixtures alsPath "HoverLibrary" "als-hover-libraries" $ \dir -> do+ let libraries = dir </> ".agda" </> "libraries"+ writeText libraries (Text.pack (dir </> "dep" </> "dep.agda-lib") <> "\n")+ liftIO $ removeFile (dir </> "src" </> "Dep.agda")+ writeText (dir </> "hover-library.agda-lib") "name: hover-library\ninclude: src\ndepend: dep\n"+ doc <- openDoc ("src" </> "Main.agda") "agda"+ expectHover doc onF "T → T → T"+ writeText libraries ""+ expectFreshHover alsPath dir doc onF,+ testCase "a new .agda-lib next to the hovered file" $ withFixtures alsPath "HoverLibrary" "als-hover-new-library" $ \dir -> do+ doc <- openDoc ("src" </> "Main.agda") "agda"+ expectHover doc onF "T → T"+ liftIO $ removeFile (dir </> "src" </> "Dep.agda")+ expectFreshHover alsPath dir doc onF+ writeText (dir </> "src" </> "near.agda-lib") "name: near\ninclude: . ../other\n"+ expectHover doc onF "T → T → T"+ ]+ -- the tool is a shell script, which Windows cannot run+ ++ [ testCase "remove a trusted executable, then add it back" $ withFixtures alsPath "HoverLibrary" "als-hover-executables" $ \dir -> do+ let tool = dir </> "tool"+ executables = dir </> ".agda" </> "executables"+ writeText tool "#!/bin/sh\necho done\n"+ liftIO $ getPermissions tool >>= setPermissions tool . setOwnerExecutable True+ writeText executables (Text.pack tool <> "\n")+ doc <- openDoc ("src" </> "UsesTool.agda") "agda"+ expectHover doc onToolF "T → T"+ writeText executables ""+ expectFreshHover alsPath dir doc onToolF+ writeText executables (Text.pack tool <> "\n")+ expectHover doc onToolF "T → T"+ | os /= "mingw32"+ ]+ where+ -- UsesTool.agda starts with an OPTIONS pragma, so 'g = f' is one line lower+ onToolF = Position 6 4++-- | Like 'hoverAfterEdits', but for what an editor does in one session:+-- unsaved edits, switching files, loading, and hovering on different+-- kinds of token. The hover loads the file on disk and reads the token+-- from the editor's text.+hoverInSession :: FilePath -> TestTree+hoverInSession alsPath =+ testGroup+ "hover across unsaved edits and file switches"+ [ testCase "an unsaved edit, then an import edit" $ withFixtures alsPath "Hover" "als-hover-unsaved" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ main <- readText (dir </> "Main.agda")+ edit doc (main <> "\nh : T\nh = f t\n")+ expectHover doc onF "T → T"+ expectHover doc onUnsavedF "T → T"+ dep <- readText (dir </> "Dep.agda")+ writeText (dir </> "Dep.agda") (addArgumentToF dep)+ expectHover doc onUnsavedF "T → T → T",+ testCase "an unsaved edit, then discarded" $ withFixtures alsPath "Hover" "als-hover-discard" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ main <- readText (dir </> "Main.agda")+ edit doc (Text.replace "\n\ng : _" "\ng : _" main)+ expectHover doc (Position 4 4) "T → T"+ edit doc main+ expectHover doc onF "T → T",+ testCase "switch between files" $ withFixtures alsPath "Hover" "als-hover-switch" $ \dir -> do+ main <- openDoc "Main.agda" "agda"+ viaMid <- openDoc "ViaMid.agda" "agda"+ expectHover main onF "T → T"+ expectHover viaMid onF "T → T"+ expectHover main onF "T → T"+ dep <- readText (dir </> "Dep.agda")+ writeText (dir </> "Dep.agda") (addArgumentToF dep)+ expectHover viaMid onF "T → T → T"+ expectHover main onF "T → T → T",+ testCase "load between hovers" $ withFixtures alsPath "Hover" "als-hover-load" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ load doc+ expectHover doc onF "T → T"+ dep <- readText (dir </> "Dep.agda")+ writeText (dir </> "Dep.agda") (addArgumentToF dep)+ load doc+ expectHover doc onF "T → T → T",+ testCase "kinds of token" $ withFixtures alsPath "Hover" "als-hover-tokens" $ \dir -> do+ doc <- openDoc "Tokens.agda" "agda"+ -- a name from an import+ expectHover doc (Position 4 6) "Set"+ -- a name with a Unicode character+ expectHover doc (Position 8 1) "T → T"+ -- a qualified name+ expectHover doc (Position 14 12) "T → T"+ -- an operator, a local variable and a name from a where block,+ -- which the hover does not find in the scope of the module, and+ -- a keyword, which does not parse as an expression+ expectFreshHover alsPath dir doc (Position 11 8)+ expectFreshHover alsPath dir doc (Position 11 6)+ expectFreshHover alsPath dir doc (Position 11 11)+ expectFreshHover alsPath dir doc (Position 12 3)+ ]+ where+ -- 'f' in the line 'h = f t' that the unsaved edit adds to Main.agda+ onUnsavedF = Position 8 4++-- | The first hover checks Main.agda and Dep.agda and writes their .agdai+-- files. A new server then hovers at the same place, with those .agdai+-- files valid. The hover text must not depend on whether a .agdai was+-- there.+hoverWithInterface :: FilePath -> IO ()+hoverWithInterface alsPath = withFixtures alsPath "Hover" "als-hover-interface" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ liftIO $ runSessionIn alsPath dir $ do+ fresh <- openDoc (docPath doc) "agda"+ expectHover fresh onF "T → T"++-- | The first hover checks Main.agda and Dep.agda and writes their .agdai+-- files. With no change to the files, the second hover reuses that load,+-- so it writes no .agdai file again after they are deleted.+hoverReusesLoad :: FilePath -> IO ()+hoverReusesLoad alsPath = withFixtures alsPath "Hover" "als-hover-reuse" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ written <- liftIO $ interfaceFiles dir+ liftIO $ map takeFileName written @?= ["Dep.agdai", "Main.agdai"]+ liftIO $ mapM_ removeFile written+ expectHover doc onF "T → T"+ writtenAgain <- liftIO $ interfaceFiles dir+ liftIO $ writtenAgain @?= []++-- | Like 'hoverReusesLoad', but with --no-hover-cache, which turns the+-- cache off: the second hover loads the files again and writes the .agdai+-- files again+hoverWithoutCache :: FilePath -> IO ()+hoverWithoutCache alsPath = withFixtures (alsPath ++ " --no-hover-cache") "Hover" "als-hover-no-cache" $ \dir -> do+ doc <- openDoc "Main.agda" "agda"+ expectHover doc onF "T → T"+ written <- liftIO $ interfaceFiles dir+ liftIO $ map takeFileName written @?= ["Dep.agdai", "Main.agdai"]+ liftIO $ mapM_ removeFile written+ expectHover doc onF "T → T"+ writtenAgain <- liftIO $ interfaceFiles dir+ liftIO $ map takeFileName writtenAgain @?= ["Dep.agdai", "Main.agdai"]++-- | Like 'hoverWithInterface', but in a module with parameters. The first+-- hover checks Param.agda. A new server then loads Param from its valid+-- .agdai, and hovers twice, the second time on the load it reuses. Agda+-- (#8905) forgets the module parameters at the top level after an .agdai+-- load, and gives '(A₁ : Set) → Set' for 'D' and '_A_2 → D' for 'd'.+hoverParamsWithInterface :: FilePath -> IO ()+hoverParamsWithInterface alsPath = withFixtures alsPath "Hover" "als-hover-param" $ \dir -> do+ doc <- openDoc "Param.agda" "agda"+ expectHover doc onD "Set"+ expectHover doc onLowerD "A → D"+ liftIO $ runSessionIn alsPath dir $ do+ fresh <- openDoc (docPath doc) "agda"+ expectHover fresh onD "Set"+ expectHover fresh onLowerD "A → D"+ expectHover fresh onD "Set"+ where+ -- 'D' in 'g = D' and 'd' in 'e = d'+ onD = Position 8 4+ onLowerD = Position 11 4++--------------------------------------------------------------------------------+-- Helpers++-- | The .agdai files in a directory and its subdirectories, sorted+interfaceFiles :: FilePath -> IO [FilePath]+interfaceFiles dir = do+ entries <- sort <$> listDirectory dir+ concat <$> mapM inEntry entries+ where+ inEntry entry = do+ let path = dir </> entry+ isDirectory <- doesDirectoryExist path+ if isDirectory+ then interfaceFiles path+ else return [path | takeExtension path == ".agdai"]++-- | Copies the fixtures in test/data/<fixtures> to a temporary directory+-- and runs a session there+withFixtures :: FilePath -> FilePath -> String -> (FilePath -> Session a) -> IO a+withFixtures alsPath fixtures name session =+ withTempDir name $ \dir -> do+ copyTree ("test" </> "data" </> fixtures) dir+ runSessionIn alsPath dir (session dir)++-- | Runs a session in the given directory, with an empty AGDA_DIR there,+-- so the libraries and defaults of the user running the tests stay out+runSessionIn :: FilePath -> FilePath -> Session a -> IO a+runSessionIn alsPath dir session = do+ let agdaDir = dir </> ".agda"+ createDirectoryIfMissing True agdaDir+ environment <- filter ((/= "AGDA_DIR") . fst) <$> getEnvironment+ let inDir process = process {cwd = Just dir, env = Just (("AGDA_DIR", agdaDir) : environment)}+ runSessionWithConfigCustomProcess inDir defaultConfig alsPath fullLatestClientCaps dir session++-- | Copies a directory and everything in it+copyTree :: FilePath -> FilePath -> IO ()+copyTree from to = do+ createDirectoryIfMissing True to+ entries <- listDirectory from+ forM_ entries $ \entry -> do+ isDirectory <- doesDirectoryExist (from </> entry)+ if isDirectory+ then copyTree (from </> entry) (to </> entry)+ else copyFile (from </> entry) (to </> entry)++-- | Where 'f' is in 'g = f' of Main.agda and ViaMid.agda+onF :: Position+onF = Position 5 4++-- | Changes 'f : T -> T' in Dep.agda to take two arguments+addArgumentToF :: Text -> Text+addArgumentToF = Text.replace "f : T -> T\nf x = x" "f : T -> T -> T\nf x _ = x"++readText :: FilePath -> Session Text+readText = liftIO . TextIO.readFile++writeText :: FilePath -> Text -> Session ()+writeText file = liftIO . TextIO.writeFile file++-- | Saves an open file the way an editor does: the new text goes to the+-- server and to the disk+save :: TextDocumentIdentifier -> Text -> Session ()+save doc text = do+ edit doc text+ writeText (docPath doc) text++-- | Changes an open file in the editor only, without saving it+edit :: TextDocumentIdentifier -> Text -> Session ()+edit doc text = changeDoc doc [TextDocumentContentChangeEvent (InR (TextDocumentContentChangeWholeDocument text))]++-- | Loads an open file with 'Cmd_load', the way agda-mode does+load :: TextDocumentIdentifier -> Session ()+load doc = do+ let file = show (docPath doc)+ _ <- runIOTCM ("IOTCM " ++ file ++ " NonInteractive Direct( Cmd_load " ++ file ++ " [] )")+ return ()++docPath :: TextDocumentIdentifier -> FilePath+docPath (TextDocumentIdentifier docUri) = case uriToFilePath docUri of+ Just path -> path+ Nothing -> error ("not a file: " ++ show docUri)++-- | Hovers at a position and returns the text of the hover+hoverText :: TextDocumentIdentifier -> Position -> Session Text+hoverText doc pos = do+ TResponseMessage _ _ rsp <- request SMethod_TextDocumentHover (HoverParams doc pos Nothing)+ case rsp of+ Right (InL (Hover (InL (MarkupContent _ content)) _)) -> return content+ _ -> liftIO $ assertFailure ("Unexpected hover response: " ++ show rsp)++-- | Expects the hover to show the given type+expectHover :: HasCallStack => TextDocumentIdentifier -> Position -> Text -> Session ()+expectHover doc pos typ = do+ actual <- hoverText doc pos+ liftIO $ actual @?= "\n```agda-language-server\n" <> typ <> "\n```\n"++-- | Expects the hover to be the same as that of a fresh server+expectFreshHover :: HasCallStack => FilePath -> FilePath -> TextDocumentIdentifier -> Position -> Session ()+expectFreshHover alsPath dir doc pos = do+ actual <- hoverText doc pos+ expected <- liftIO $ runSessionIn alsPath dir $ do+ fresh <- openDoc (docPath doc) "agda"+ hoverText fresh pos+ liftIO $ actual @?= expected
test/Test/LSP.hs view
@@ -1,14 +1,22 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} -module Test.LSP (tests) where+module Test.LSP (tests, withTempDir, runIOTCM) where import Agda+import qualified Agda.IR as IR+import Control.Exception (bracket) import Control.Monad.IO.Class import qualified Data.Aeson as JSON+import qualified Data.Aeson.KeyMap as KeyMap+import GHC.TypeLits (symbolVal) import Language.LSP.Protocol.Message import Language.LSP.Protocol.Types import Language.LSP.Test import Switchboard (agdaCustomMethod)+import System.Directory (createDirectoryIfMissing, getTemporaryDirectory, makeAbsolute, removeDirectoryRecursive)+import System.FilePath ((</>))+import System.Process (CreateProcess (cwd)) import Test.Tasty import Test.Tasty.HUnit @@ -16,7 +24,9 @@ tests alsPath = testGroup "LSP"- [ testCase "load" (demo alsPath)+ [ testCase "load" (demo alsPath),+ testCase "compile with the GHC and GHCNoMain backends" (testCompile alsPath),+ testCase "compile with the HTML backend after an invalid Agda option" (testCompileAfterInvalidOption alsPath) ] demo :: FilePath -> IO ()@@ -38,6 +48,86 @@ -- agda-mode:load testCustomMethod "IOTCM \"test/data/A.agdaa\" NonInteractive Direct( Cmd_load \"test/data/A.agda\" [] )"++-- | Compiles a fixture with both GHC backend settings and expects+-- 'DisplayInfoCompilationOk' for each. Before #66 was fixed, als never+-- registered Agda's builtin backends, so both failed with "No backend+-- called 'GHC'". '--ghc-dont-call-ghc' stops after generating the+-- Haskell code, so this doesn't need a working GHC, and '--compile-dir'+-- keeps the generated MAlonzo tree out of test/data.+testCompile :: FilePath -> IO ()+testCompile alsPath = withTempDir "als-test-compile" $ \compileDir -> do+ let server = alsPath ++ " +AGDA --ghc-dont-call-ghc --compile-dir=" ++ compileDir ++ " -AGDA"+ runSession server fullLatestClientCaps "test/data/" $ do+ ghc <- displayInfos <$> runIOTCM "IOTCM \"test/data/A.agda\" NonInteractive Direct( Cmd_compile GHC \"test/data/A.agda\" [] )"+ liftIO $ ghc @?= [JSON.toJSON (IR.ResponseDisplayInfo (IR.DisplayInfoCompilationOk [noMainWarning] []))]++ ghcNoMain <- displayInfos <$> runIOTCM "IOTCM \"test/data/A.agda\" NonInteractive Direct( Cmd_compile GHCNoMain \"test/data/A.agda\" [] )"+ liftIO $ ghcNoMain @?= [JSON.toJSON (IR.ResponseDisplayInfo (IR.DisplayInfoCompilationOk [] []))]+ where+ -- A.agda has no 'main', so the GHC backend warns about it+ noMainWarning :: String+#if MIN_VERSION_Agda(2,7,0)+ noMainWarning = "warning: -W[no]NoMain\nNo main function defined in A.\nUse option --no-main to suppress this warning."+#else+ noMainWarning = "warning: -W[no]GenericWarning\nNo main function defined in A. Use --no-main to suppress this\nwarning."+#endif++-- | Like 'testCompile', but with an Agda option that fails to parse. als+-- then falls back to the default options, and it must still register+-- Agda's builtin backends. The fallback also drops '--ghc-dont-call-ghc'+-- and '--compile-dir', so this compiles with the HTML backend instead,+-- which goes through the same backend lookup but needs no GHC. The+-- server runs in a temporary directory, so the generated html/ tree+-- stays out of the repo.+testCompileAfterInvalidOption :: FilePath -> IO ()+testCompileAfterInvalidOption alsPath = withTempDir "als-test-html" $ \dir -> do+ file <- makeAbsolute "test/data/A.agda"+ let server = alsPath ++ " +AGDA --no-such-option -AGDA"+ inDir process = process {cwd = Just dir}+ runSessionWithConfigCustomProcess inDir defaultConfig server fullLatestClientCaps "test/data/" $ do+ html <- displayInfos <$> runIOTCM ("IOTCM " ++ show file ++ " NonInteractive Direct( Cmd_compile HTML " ++ show file ++ " [] )")+ liftIO $ html @?= [JSON.toJSON (IR.ResponseDisplayInfo (IR.DisplayInfoCompilationOk [] []))]++-- | Keeps only the 'ResponseDisplayInfo' responses+displayInfos :: [JSON.Value] -> [JSON.Value]+displayInfos = filter isDisplayInfo+ where+ isDisplayInfo (JSON.Object o) = KeyMap.lookup "tag" o == Just "ResponseDisplayInfo"+ isDisplayInfo _ = False++-- | Runs an action with a fresh directory of the given name under the+-- system's temporary directory, and removes it afterwards+withTempDir :: String -> (FilePath -> IO a) -> IO a+withTempDir name = bracket create removeDirectoryRecursive+ where+ create = do+ tmp <- getTemporaryDirectory+ let dir = tmp </> name+ createDirectoryIfMissing True dir+ return dir++-- | Sends an IOTCM command over the custom method, then answers every+-- request the server sends back on that method (the server waits for+-- each one to be answered before ending the command), until+-- 'ResponseEnd'. Returns the params of those requests, 'ResponseEnd'+-- excluded.+runIOTCM :: String -> Session [JSON.Value]+runIOTCM cmd = do+ _ <- sendRequest agdaCustomMethod (JSON.toJSON (CmdReq cmd))+ collect []+ where+ collect :: [JSON.Value] -> Session [JSON.Value]+ collect acc = do+ msg <- anyMessage+ case msg of+ FromServerMess (SMethod_CustomMethod p) (ReqMess (TRequestMessage _ reqId _ params))+ | symbolVal p == "agda" -> do+ sendResponse (TResponseMessage "2.0" (Just reqId) (Right JSON.Null))+ if params == JSON.toJSON IR.ResponseEnd+ then return (reverse acc)+ else collect (params : acc)+ _ -> collect acc -- | Sends a custom method request to the server and expects a response of `CmdRes Nothing` testCustomMethod :: String -> Session ()