Skip to content

Commit 688a1a8

Browse files
committed
test reloading is compatible with changes
1 parent 958e6b7 commit 688a1a8

File tree

5 files changed

+105
-25
lines changed

5 files changed

+105
-25
lines changed

src/Agda/Interaction/Imports/Virtual.hs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module Agda.Interaction.Imports.Virtual
55
( VSourceFile (..),
66
vSrcFromUri,
7+
parseSourceFromContents,
78
parseVSource,
89
)
910
where
@@ -37,15 +38,20 @@ data VSourceFile = VSourceFile
3738
vSrcVFile :: VFS.VirtualFile
3839
}
3940

41+
#if MIN_VERSION_Agda(2,8,0)
42+
srcFilePath :: (TCM.MonadFileId m) => SourceFile -> m AbsolutePath
43+
srcFilePath = TCM.srcFilePath
44+
#else
45+
srcFilePath :: (Monad m) => SourceFile -> m AbsolutePath
46+
srcFilePath (SourceFile path) = return path
47+
#endif
48+
4049
#if MIN_VERSION_Agda(2,8,0)
4150
vSrcFilePath :: (TCM.MonadFileId m) => VSourceFile -> m AbsolutePath
42-
vSrcFilePath = TCM.srcFilePath . vSrcFileSrcFile
4351
#else
4452
vSrcFilePath :: (Monad m) => VSourceFile -> m AbsolutePath
45-
vSrcFilePath vSourceFile = do
46-
let (SourceFile path) = vSrcFileSrcFile vSourceFile
47-
return path
4853
#endif
54+
vSrcFilePath = srcFilePath . vSrcFileSrcFile
4955

5056
#if MIN_VERSION_Agda(2,8,0)
5157
vSrcFromUri ::
@@ -69,37 +75,44 @@ vSrcFromUri normUri file = do
6975
return $ VSourceFile src normUri file
7076
#endif
7177

72-
-- | Based on @parseSource@
73-
parseVSource :: (TCM.MonadTCM m, TCM.MonadTrace m, MonadAgdaLib m) => VSourceFile -> m Imp.Source
74-
parseVSource vSrcFile = do
75-
let sourceFile = vSrcFileSrcFile vSrcFile
76-
f <- TCM.liftTCM $ vSrcFilePath vSrcFile
78+
parseSourceFromContents ::
79+
(TCM.MonadTCM m, TCM.MonadTrace m, MonadAgdaLib m) =>
80+
LSP.NormalizedUri ->
81+
SourceFile ->
82+
Text.Text ->
83+
m Imp.Source
84+
parseSourceFromContents srcUri srcFile contentsStrict = do
85+
f <- TCM.liftTCM $ srcFilePath srcFile
7786

7887
let rf0 = mkRangeFile f Nothing
7988
TCM.setCurrentRange (Imp.beginningOfFile rf0) $ do
80-
let sourceStrict = VFS.virtualFileText $ vSrcVFile vSrcFile
81-
let source = Strict.toLazy sourceStrict
82-
let txt = Text.unpack sourceStrict
89+
let contents = Strict.toLazy contentsStrict
90+
let contentsString = Text.unpack contentsStrict
8391

8492
parsedModName0 <-
8593
TCM.liftTCM $
8694
Imp.moduleName f . fst . fst =<< do
87-
Imp.runPMDropWarnings $ parseFile moduleParser rf0 txt
95+
Imp.runPMDropWarnings $ parseFile moduleParser rf0 contentsString
8896

8997
let rf = mkRangeFile f $ Just parsedModName0
90-
((parsedMod, attrs), fileType) <- TCM.liftTCM $ Imp.runPM $ parseFile moduleParser rf txt
98+
((parsedMod, attrs), fileType) <- TCM.liftTCM $ Imp.runPM $ parseFile moduleParser rf contentsString
9199
parsedModName <- TCM.liftTCM $ Imp.moduleName f parsedMod
92100

93101
agdaLib <- askAgdaLib
94-
let libs = maybeToList $ agdaLibToFile (vSrcUri vSrcFile) agdaLib
102+
let libs = maybeToList $ agdaLibToFile srcUri agdaLib
95103

96104
return
97105
Imp.Source
98-
{ Imp.srcText = source,
106+
{ Imp.srcText = contents,
99107
Imp.srcFileType = fileType,
100-
Imp.srcOrigin = sourceFile,
108+
Imp.srcOrigin = srcFile,
101109
Imp.srcModule = parsedMod,
102110
Imp.srcModuleName = parsedModName,
103111
Imp.srcProjectLibs = libs,
104112
Imp.srcAttributes = attrs
105113
}
114+
115+
-- | Based on @parseSource@
116+
parseVSource :: (TCM.MonadTCM m, TCM.MonadTrace m, MonadAgdaLib m) => VSourceFile -> m Imp.Source
117+
parseVSource (VSourceFile srcFile uri vFile) =
118+
parseSourceFromContents uri srcFile (VFS.virtualFileText vFile)

src/Server/Model/AgdaFile.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ where
1414

1515
import Agda.Position (toLspRange)
1616
import qualified Agda.Syntax.Abstract as A
17-
import Agda.Syntax.Common.Pretty (Pretty, pretty, prettyAssign, prettyMap, text, vcat)
17+
import Agda.Syntax.Common.Pretty (Pretty, pretty, prettyAssign, prettyMap, prettyShow, text, vcat)
1818
import Agda.Syntax.Position (getRange)
1919
import Agda.Utils.Lens (Lens', over, (<&>), (^.))
2020
import Data.Foldable (find)
@@ -29,6 +29,7 @@ data AgdaFile = AgdaFile
2929
{ _agdaFileSymbols :: !(Map A.QName SymbolInfo),
3030
_agdaFileRefs :: !(Map A.QName [Ref])
3131
}
32+
deriving (Eq)
3233

3334
instance Pretty AgdaFile where
3435
pretty agdaFile =
@@ -37,6 +38,9 @@ instance Pretty AgdaFile where
3738
prettyAssign (text "refs", prettyMap $ Map.toList $ agdaFile ^. agdaFileRefs)
3839
]
3940

41+
instance Show AgdaFile where
42+
show = prettyShow
43+
4044
emptyAgdaFile :: AgdaFile
4145
emptyAgdaFile = AgdaFile Map.empty Map.empty
4246

src/Server/Model/Symbol.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ data SymbolInfo = SymbolInfo
6666
symbolType :: !(Maybe String),
6767
symbolParent :: !(Maybe A.QName)
6868
}
69+
deriving (Eq)
6970

7071
instance Pretty SymbolInfo where
7172
pretty symbolInfo =
@@ -115,6 +116,7 @@ data Ref = Ref
115116
refRange :: !LSP.Range,
116117
refIsAmbiguous :: !Bool
117118
}
119+
deriving (Eq)
118120

119121
prettyAmbiguity :: Ref -> Doc
120122
prettyAmbiguity ref =

test/Test/Indexer/Reload.hs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
module Test.Indexer.Reload (tests) where
22

3+
import Agda.Interaction.Imports.Virtual (parseSourceFromContents)
4+
import Agda.Syntax.Common.Pretty (prettyShow)
5+
import Control.Monad.IO.Class (liftIO)
6+
import qualified Data.Text as Text
37
import Indexer (indexFile)
48
import qualified Language.LSP.Protocol.Types as LSP
59
import qualified Language.LSP.Server as LSP
610
import Monad (runServerT)
711
import Server.Model.Monad (runWithAgdaLib)
812
import Test.Tasty (TestTree, testGroup)
9-
import Test.Tasty.HUnit (testCase)
13+
import Test.Tasty.HUnit (assertFailure, testCase, (@?=))
1014
import qualified TestData
1115

1216
tests :: TestTree
1317
tests =
1418
testGroup "Reload" $
15-
[ testCase "Reload single file" $ testReloadFile "test/data/A.agda"
19+
[ testCase "Reload single file" $ testReloadFile "test/data/A.agda",
20+
testCase "Reload after changes" testReloadChanges
1621
]
1722

1823
testReloadFile :: FilePath -> IO ()
@@ -28,3 +33,42 @@ testReloadFile path = do
2833
_ <- indexFile src
2934
_ <- indexFile src
3035
return ()
36+
37+
testReloadChanges :: IO ()
38+
testReloadChanges = do
39+
let path = "test/data/A.agda"
40+
let contentsA =
41+
Text.unlines
42+
[ "module A where",
43+
"",
44+
"data T : Set where",
45+
" tt : T"
46+
]
47+
let contentsB =
48+
Text.unlines
49+
[ "module A where",
50+
"",
51+
"data T : Set where",
52+
" tt : T",
53+
"",
54+
"f : T -> T",
55+
"f x = x"
56+
]
57+
58+
let uri = LSP.filePathToUri path
59+
model <- TestData.getModel
60+
61+
LSP.runLspT undefined $ do
62+
env <- TestData.getServerEnv model
63+
runServerT env $ do
64+
runWithAgdaLib uri $ do
65+
src0 <- TestData.parseSourceFromPathAndContents path contentsA
66+
agdaFile0 <- indexFile src0
67+
68+
src1 <- TestData.parseSourceFromPathAndContents path contentsB
69+
agdaFile1 <- indexFile src1
70+
71+
src2 <- TestData.parseSourceFromPathAndContents path contentsA
72+
agdaFile2 <- indexFile src2
73+
74+
liftIO $ agdaFile0 @?= agdaFile2

test/TestData.hs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module TestData
1212
AgdaFileDetails (..),
1313
agdaFileDetails,
1414
parseSourceFromPath,
15+
parseSourceFromPathAndContents,
1516
)
1617
where
1718

@@ -44,10 +45,12 @@ import qualified Server.CommandController as CommandController
4445
import Server.Model (Model (Model))
4546
import Server.Model.AgdaFile (AgdaFile, emptyAgdaFile)
4647
import Server.Model.AgdaLib (agdaLibIncludes, initAgdaLib)
47-
import Server.Model.Monad (runWithAgdaLib)
48+
import Server.Model.Monad (runWithAgdaLib, MonadAgdaLib)
4849
import qualified Server.ResponseController as ResponseController
4950
import System.FilePath (takeBaseName, (</>))
5051
import Agda.TypeChecking.Pretty (prettyTCM)
52+
import Data.Text (Text)
53+
import Agda.Interaction.Imports.Virtual (parseSourceFromContents)
5154

5255
data AgdaFileDetails = AgdaFileDetails
5356
{ fileName :: String,
@@ -84,16 +87,30 @@ agdaFileDetails inPath = do
8487

8588
return $ AgdaFileDetails testName file interface
8689

87-
parseSourceFromPath :: (TCM.MonadTCM m) => FilePath -> m Imp.Source
88-
parseSourceFromPath path = do
90+
sourceFileFromPath :: (TCM.MonadTCM m) => FilePath -> m SourceFile
91+
sourceFileFromPath path = do
8992
absPath <- liftIO $ absolute path
9093
#if MIN_VERSION_Agda(2,8,0)
91-
srcFile <- TCM.liftTCM $ TCM.srcFromPath absPath
94+
TCM.liftTCM $ TCM.srcFromPath absPath
9295
#else
93-
let srcFile = SourceFile absPath
96+
return $ SourceFile absPath
9497
#endif
98+
99+
parseSourceFromPath :: (TCM.MonadTCM m) => FilePath -> m Imp.Source
100+
parseSourceFromPath path = do
101+
srcFile <- sourceFileFromPath path
95102
TCM.liftTCM $ Imp.parseSource srcFile
96103

104+
parseSourceFromPathAndContents ::
105+
(TCM.MonadTCM m, TCM.MonadTrace m, MonadAgdaLib m) =>
106+
FilePath ->
107+
Text ->
108+
m Imp.Source
109+
parseSourceFromPathAndContents path contents = do
110+
srcFile <- sourceFileFromPath path
111+
let uri = LSP.toNormalizedUri $ LSP.filePathToUri path
112+
parseSourceFromContents uri srcFile contents
113+
97114
--------------------------------------------------------------------------------
98115

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

0 commit comments

Comments
 (0)