Skip to content

Commit 958e6b7

Browse files
committed
clear state before loading files
1 parent b4b4172 commit 958e6b7

File tree

10 files changed

+81
-114
lines changed

10 files changed

+81
-114
lines changed

agda-language-server.cabal

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,13 @@ test-suite als-test
186186
type: exitcode-stdio-1.0
187187
main-is: Test.hs
188188
other-modules:
189+
Test.Indexer
189190
Test.Indexer.Invariants
191+
Test.Indexer.Invariants.NoDuplicateDecl
192+
Test.Indexer.Invariants.NoMissing
193+
Test.Indexer.Invariants.NoOverlap
190194
Test.Indexer.NoAnonFunSymbol
191-
Test.Indexer.NoDuplicateDecl
192-
Test.Indexer.NoMissing
193-
Test.Indexer.NoOverlap
195+
Test.Indexer.Reload
194196
Test.LSP
195197
Test.Model
196198
Test.ModelMonad

src/Indexer.hs

Lines changed: 9 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import Server.Model.Monad (WithAgdaLibM)
2727

2828
usingSrcAsCurrent :: Imp.Source -> WithAgdaLibM a -> WithAgdaLibM a
2929
usingSrcAsCurrent src x = do
30+
TCM.liftTCM TCM.resetState
31+
3032
TCM.liftTCM $ Imp.setOptionsFromSourcePragmas True src
3133

3234
TCM.setCurrentRange (C.modPragmas . Imp.srcModule $ src) $
@@ -35,118 +37,30 @@ usingSrcAsCurrent src x = do
3537

3638
#if MIN_VERSION_Agda(2,8,0)
3739
TCM.modifyTCLens TCM.stModuleToSourceId $ Map.insert (Imp.srcModuleName src) (Imp.srcOrigin src)
38-
TCM.localTC (\e -> e {TCM.envCurrentPath = Just (TCM.srcFileId $ Imp.srcOrigin src)}) $ do
40+
TCM.localTC (\e -> e {TCM.envCurrentPath = Just (TCM.srcFileId $ Imp.srcOrigin src)}) x
3941
#else
4042
TCM.modifyTCLens TCM.stModuleToSource $ Map.insert (Imp.srcModuleName src) (srcFilePath $ Imp.srcOrigin src)
41-
TCM.localTC (\e -> e {TCM.envCurrentPath = Just (srcFilePath $ Imp.srcOrigin src)}) $ do
43+
TCM.localTC (\e -> e {TCM.envCurrentPath = Just (srcFilePath $ Imp.srcOrigin src)}) x
4244
#endif
43-
x
4445

4546
withAstFor :: Imp.Source -> (TopLevelInfo -> WithAgdaLibM a) -> WithAgdaLibM a
46-
#if MIN_VERSION_Agda(2,8,0)
4747
withAstFor src f = usingSrcAsCurrent src $ do
48-
let topLevel =
49-
TopLevel
50-
(Imp.srcOrigin src)
51-
(Imp.srcModuleName src)
52-
(C.modDecls $ Imp.srcModule src)
53-
ast <- TCM.liftTCM $ toAbstract topLevel
54-
f ast
48+
#if MIN_VERSION_Agda(2,8,0)
49+
let srcFile = Imp.srcOrigin src
5550
#else
56-
withAstFor src f = usingSrcAsCurrent src $ do
51+
let srcFile = srcFilePath $ Imp.srcOrigin src
52+
#endif
5753
let topLevel =
5854
TopLevel
59-
(srcFilePath $ Imp.srcOrigin src)
55+
srcFile
6056
(Imp.srcModuleName src)
6157
(C.modDecls $ Imp.srcModule src)
6258
ast <- TCM.liftTCM $ toAbstract topLevel
6359
f ast
64-
#endif
6560

6661
indexFile ::
6762
Imp.Source ->
6863
WithAgdaLibM AgdaFile
6964
indexFile src = withAstFor src $ \ast -> execIndexerM $ do
7065
indexAst ast
7166
postprocess
72-
73-
-- let options = defaultOptions
74-
75-
-- TCM.liftTCM TCM.resetState
76-
77-
-- TCM.liftTCM $ TCM.setCommandLineOptions' rootPath options
78-
-- TCM.liftTCM $ Imp.setOptionsFromSourcePragmas True src
79-
-- loadPrims <- optLoadPrimitives <$> TCM.pragmaOptions
80-
81-
-- when loadPrims $ do
82-
-- libdirPrim <- liftIO getPrimitiveLibDir
83-
84-
-- -- Turn off import-chasing messages.
85-
-- -- We have to modify the persistent verbosity setting, since
86-
-- -- getInterface resets the current verbosity settings to the persistent ones.
87-
88-
-- bracket_ (TCM.getsTC TCM.getPersistentVerbosity) TCM.putPersistentVerbosity $ do
89-
-- TCM.modifyPersistentVerbosity
90-
-- (Strict.Just . Trie.insert [] 0 . Strict.fromMaybe Trie.empty)
91-
-- -- set root verbosity to 0
92-
93-
-- -- We don't want to generate highlighting information for Agda.Primitive.
94-
-- TCM.liftTCM $
95-
-- TCM.withHighlightingLevel TCM.None $
96-
-- forM_ (Set.map (libdirPrim </>) TCM.primitiveModules) $ \f -> do
97-
-- primSource <- Imp.parseSource (SourceFile $ mkAbsolute f)
98-
-- Imp.checkModuleName' (Imp.srcModuleName primSource) (Imp.srcOrigin primSource)
99-
-- void $ Imp.getNonMainInterface (Imp.srcModuleName primSource) (Just primSource)
100-
101-
-- TCM.liftTCM $ Imp.checkModuleName' (Imp.srcModuleName src) (Imp.srcOrigin src)
102-
103-
-- addImportCycleCheck (Imp.srcModuleName src) $
104-
-- TCM.localTC (\e -> e {TCM.envCurrentPath = Just $ TCM.srcFileId srcFile}) $ do
105-
-- let topLevel =
106-
-- C.TopLevel
107-
-- srcFile
108-
-- (Imp.srcModuleName src)
109-
-- (C.modDecls $ Imp.srcModule src)
110-
-- ast <- TCM.liftTCM $ C.toAbstract topLevel
111-
112-
-- deps <- TCM.useTC TCM.stImportedModulesTransitive
113-
-- moduleToSourceId <- TCM.useTC TCM.stModuleToSourceId
114-
-- forM_ deps $ maybeUpdateCacheForDepFile moduleToSourceId
115-
116-
-- cache <- getCache
117-
-- let entries = LSP.fromNormalizedUri . fst <$> Cache.getAllEntries cache
118-
-- alwaysReportSDoc "lsp.cache" 20 $ return (text "cache entries:" <+> pretty entries)
119-
120-
-- let ds = C.topLevelDecls ast
121-
-- let scope = C.topLevelScope ast
122-
123-
-- TCM.liftTCM TCM.activateLoadedFileCache
124-
125-
-- TCM.liftTCM TCM.cachingStarts
126-
-- opts <- TCM.liftTCM $ TCM.useTC TCM.stPragmaOptions
127-
-- me <- TCM.liftTCM TCM.readFromCachedLog
128-
-- case me of
129-
-- Just (TCM.Pragmas opts', _)
130-
-- | opts == opts' ->
131-
-- return ()
132-
-- _ -> TCM.liftTCM TCM.cleanCachedLog
133-
-- TCM.liftTCM $ TCM.writeToCurrentLog $ TCM.Pragmas opts
134-
135-
-- TCM.liftTCM $
136-
-- mapM_ TC.checkDeclCached ds `TCM.finally_` TCM.cacheCurrentLog
137-
138-
-- TCM.liftTCM TCM.unfreezeMetas
139-
140-
-- TCM.liftTCM $ TCM.setScope scope
141-
142-
-- TCM.liftTCM $
143-
-- TCM.stCurrentModule
144-
-- `TCM.setTCLens'` Just
145-
-- ( C.topLevelModuleName ast,
146-
-- Imp.srcModuleName src
147-
-- )
148-
149-
-- file <- Index.abstractToIndex $ C.topLevelDecls ast
150-
151-
-- -- return (file, moduleToSourceId, deps)
152-
-- return file

test/Test.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import qualified Test.ModelMonad as ModelMonad
1414
import qualified Test.Indexer.Invariants as IndexerInvariants
1515
import qualified Test.Indexer.NoAnonFunSymbol as NoAnonFunSymbol
1616
import qualified Test.Uri as URI
17+
import qualified Test.Indexer as Indexer
1718

1819
-- Define the custom option
1920
newtype AlsPathOption = AlsPathOption FilePath
@@ -33,7 +34,7 @@ main = do
3334

3435
tests :: IO TestTree
3536
tests = do
36-
indexerInvariantsTest <- IndexerInvariants.tests
37+
indexerTests <- Indexer.tests
3738
return $ askOption $ \(AlsPathOption alsPath) ->
3839
testGroup
3940
"Tests"
@@ -42,8 +43,7 @@ tests = do
4243
URI.tests,
4344
Model.tests,
4445
ModelMonad.tests,
45-
indexerInvariantsTest,
46-
NoAnonFunSymbol.tests
46+
indexerTests
4747
#if defined(wasm32_HOST_ARCH)
4848
, WASM.tests alsPath
4949
#endif

test/Test/Indexer.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Test.Indexer (tests) where
2+
3+
import qualified Test.Indexer.Invariants as Invariants
4+
import qualified Test.Indexer.NoAnonFunSymbol as NoAnonFunSymbol
5+
import qualified Test.Indexer.Reload as Reload
6+
import Test.Tasty (TestTree, testGroup)
7+
8+
tests :: IO TestTree
9+
tests = do
10+
invariantsTests <- Invariants.tests
11+
return $
12+
testGroup "Indexer" $
13+
[ invariantsTests,
14+
NoAnonFunSymbol.tests,
15+
Reload.tests
16+
]

test/Test/Indexer/Invariants.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module Test.Indexer.Invariants (tests) where
22

33
import Control.Monad (forM)
4-
import Test.Indexer.NoDuplicateDecl (testNoDuplicateDecl)
5-
import Test.Indexer.NoMissing (testNoMissing)
6-
import Test.Indexer.NoOverlap (testNoOverlap)
4+
import Test.Indexer.Invariants.NoDuplicateDecl (testNoDuplicateDecl)
5+
import Test.Indexer.Invariants.NoMissing (testNoMissing)
6+
import Test.Indexer.Invariants.NoOverlap (testNoOverlap)
77
import Test.Tasty (TestTree, testGroup)
88
import Test.Tasty.Golden (findByExtension)
99
import qualified TestData

test/Test/Indexer/NoDuplicateDecl.hs renamed to test/Test/Indexer/Invariants/NoDuplicateDecl.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Test.Indexer.NoDuplicateDecl (testNoDuplicateDecl) where
1+
module Test.Indexer.Invariants.NoDuplicateDecl (testNoDuplicateDecl) where
22

33
import qualified Agda.Syntax.Abstract as A
44
import Agda.Syntax.Common.Pretty (Pretty, align, pretty, prettyList, prettyShow, text, vcat, (<+>))

test/Test/Indexer/NoMissing.hs renamed to test/Test/Indexer/Invariants/NoMissing.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- | Check that highlighting data doesn't show more references than we have
22
-- `Ref`s. We expect to sometimes pick up references that highlighting does not,
33
-- so it's okay if we have more.
4-
module Test.Indexer.NoMissing (testNoMissing) where
4+
module Test.Indexer.Invariants.NoMissing (testNoMissing) where
55

66
import Agda.Interaction.Highlighting.Precise (Aspects, HighlightingInfo)
77
import qualified Agda.Interaction.Highlighting.Precise as HL

test/Test/Indexer/NoOverlap.hs renamed to test/Test/Indexer/Invariants/NoOverlap.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- | Check that `Ref`s don't overlap, so that there is only one `Ref` instance
22
-- per actual reference.
3-
module Test.Indexer.NoOverlap (testNoOverlap) where
3+
module Test.Indexer.Invariants.NoOverlap (testNoOverlap) where
44

55
import qualified Agda.Syntax.Abstract as A
66
import Agda.Syntax.Common.Pretty (Doc, Pretty (prettyList), align, parens, pretty, prettyShow, text, vcat, (<+>))

test/Test/Indexer/Reload.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Test.Indexer.Reload (tests) where
2+
3+
import Indexer (indexFile)
4+
import qualified Language.LSP.Protocol.Types as LSP
5+
import qualified Language.LSP.Server as LSP
6+
import Monad (runServerT)
7+
import Server.Model.Monad (runWithAgdaLib)
8+
import Test.Tasty (TestTree, testGroup)
9+
import Test.Tasty.HUnit (testCase)
10+
import qualified TestData
11+
12+
tests :: TestTree
13+
tests =
14+
testGroup "Reload" $
15+
[ testCase "Reload single file" $ testReloadFile "test/data/A.agda"
16+
]
17+
18+
testReloadFile :: FilePath -> IO ()
19+
testReloadFile path = do
20+
let uri = LSP.filePathToUri path
21+
model <- TestData.getModel
22+
23+
LSP.runLspT undefined $ do
24+
env <- TestData.getServerEnv model
25+
runServerT env $ do
26+
runWithAgdaLib uri $ do
27+
src <- TestData.parseSourceFromPath path
28+
_ <- indexFile src
29+
_ <- indexFile src
30+
return ()

test/TestData.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module TestData
1111
getServerEnv,
1212
AgdaFileDetails (..),
1313
agdaFileDetails,
14+
parseSourceFromPath,
1415
)
1516
where
1617

@@ -65,13 +66,7 @@ agdaFileDetails inPath = do
6566
runServerT env $ do
6667
let withSrc f = runWithAgdaLib uri $ do
6768
TCM.liftTCM $ TCM.setCommandLineOptions Agda.Interaction.Options.defaultOptions
68-
absInPath <- liftIO $ absolute inPath
69-
#if MIN_VERSION_Agda(2,8,0)
70-
srcFile <- TCM.srcFromPath absInPath
71-
#else
72-
let srcFile = SourceFile absInPath
73-
#endif
74-
src <- TCM.liftTCM $ Imp.parseSource srcFile
69+
src <- parseSourceFromPath inPath
7570

7671
f src
7772

@@ -89,6 +84,16 @@ agdaFileDetails inPath = do
8984

9085
return $ AgdaFileDetails testName file interface
9186

87+
parseSourceFromPath :: (TCM.MonadTCM m) => FilePath -> m Imp.Source
88+
parseSourceFromPath path = do
89+
absPath <- liftIO $ absolute path
90+
#if MIN_VERSION_Agda(2,8,0)
91+
srcFile <- TCM.liftTCM $ TCM.srcFromPath absPath
92+
#else
93+
let srcFile = SourceFile absPath
94+
#endif
95+
TCM.liftTCM $ Imp.parseSource srcFile
96+
9297
--------------------------------------------------------------------------------
9398

9499
documentSymbolMessage :: LSP.NormalizedUri -> LSP.TRequestMessage LSP.Method_TextDocumentDocumentSymbol

0 commit comments

Comments
 (0)