11{-# LANGUAGE OverloadedStrings #-}
22module Symbol (tests ) where
33
4+ import Control.Lens (to , ix , (^?) , _Just )
45import Control.Monad.IO.Class
56import Data.List
67import Language.Haskell.LSP.Test as Test
78import Language.Haskell.LSP.Types
9+ import qualified Language.Haskell.LSP.Types.Lens as L
810import Language.Haskell.LSP.Types.Capabilities
911import Test.Hls.Util
1012import Test.Tasty
@@ -19,69 +21,110 @@ tests = testGroup "document symbols" [
1921
2022v310Tests :: TestTree
2123v310Tests = testGroup " 3.10 hierarchical document symbols" [
22- ignoreTestBecause " Broken " $ testCase " provides nested data types and constructors" $ runSession hlsCommand fullCaps " test/testdata" $ do
24+ testCase " provides nested data types and constructors" $ runSession hlsCommand fullCaps " test/testdata" $ do
2325 doc <- openDoc " Symbols.hs" " haskell"
2426 Left symbs <- getDocumentSymbols doc
2527
26- let myData = DocumentSymbol " MyData" (Just " " ) SkClass Nothing myDataR myDataSR (Just (List [a, b]))
27- a = DocumentSymbol " A" (Just " " ) SkConstructor Nothing aR aSR (Just mempty )
28- b = DocumentSymbol " B" (Just " " ) SkConstructor Nothing bR bSR (Just mempty )
28+ let myData = DocumentSymbol " MyData" Nothing SkStruct Nothing myDataR myDataSR (Just (List [a, b]))
29+ a = DocumentSymbol " A" Nothing SkConstructor Nothing aR aSR Nothing
30+ b = DocumentSymbol " B" Nothing SkConstructor Nothing bR bSR Nothing
31+ let myData' = symbs ^? ix 0 . L. children . _Just . to fromList . ix 2
2932
30- liftIO $ myData `elem` symbs @? " Contains symbol"
33+ liftIO $ Just myData == myData' @? " Contains symbol"
3134
32- ,ignoreTestBecause " Broken " $ testCase " provides nested where functions" $ runSession hlsCommand fullCaps " test/testdata" $ do
35+ , ignoreTestBecause " extracting symbols from nested wheres not supported " $ testCase " provides nested where functions" $ runSession hlsCommand fullCaps " test/testdata" $ do
3336 doc <- openDoc " Symbols.hs" " haskell"
3437 Left symbs <- getDocumentSymbols doc
3538
36- let foo = DocumentSymbol " foo" (Just " " ) SkFunction Nothing fooR fooSR (Just (List [bar]))
37- bar = DocumentSymbol " bar" (Just " " ) SkFunction Nothing barR barSR (Just (List [dog, cat]))
38- dog = DocumentSymbol " dog" (Just " " ) SkVariable Nothing dogR dogSR (Just mempty )
39- cat = DocumentSymbol " cat" (Just " " ) SkVariable Nothing catR catSR (Just mempty )
39+ let foo = DocumentSymbol " foo" Nothing SkFunction Nothing fooR fooSR (Just (List [bar]))
40+ bar = DocumentSymbol " bar" Nothing SkFunction Nothing barR barSR (Just (List [dog, cat]))
41+ dog = DocumentSymbol " dog" Nothing SkVariable Nothing dogR dogSR (Just mempty )
42+ cat = DocumentSymbol " cat" Nothing SkVariable Nothing catR catSR (Just mempty )
43+ let foo' = symbs ^? ix 0 . L. children . _Just . to fromList . ix 1
4044
41- liftIO $ foo `elem` symbs @? " Contains symbol"
45+ liftIO $ Just foo == foo' @? " Contains symbol"
4246
43- , ignoreTestBecause " Broken " $ testCase " provides pattern synonyms" $ runSession hlsCommand fullCaps " test/testdata" $ do
47+ , ignoreTestBecause " extracting pattern synonym symbols not supported " $ testCase " provides pattern synonyms" $ runSession hlsCommand fullCaps " test/testdata" $ do
4448 doc <- openDoc " Symbols.hs" " haskell"
4549 Left symbs <- getDocumentSymbols doc
4650
4751 let testPattern = DocumentSymbol " TestPattern"
48- (Just " " ) SkFunction Nothing testPatternR testPatternSR (Just mempty )
52+ Nothing SkFunction Nothing testPatternR testPatternSR (Just mempty )
53+ let testPattern' = symbs ^? ix 0 . L. children . _Just . to fromList . ix 3
4954
50- liftIO $ testPattern `elem` symbs @? " Contains symbol"
51- ]
55+ liftIO $ Just testPattern == testPattern' @? " Contains symbol"
5256
53- -- TODO: Test module, imports
57+ , testCase " provides imports" $ runSession hlsCommand fullCaps " test/testdata" $ do
58+ doc <- openDoc " Symbols.hs" " haskell"
59+ Left symbs <- getDocumentSymbols doc
60+
61+ let imports = DocumentSymbol " imports" Nothing SkModule Nothing importsR importsSR (Just (List [importDataMaybe]))
62+ importDataMaybe = DocumentSymbol " import Data.Maybe" Nothing SkModule Nothing importDataMaybeR importDataMaybeSR Nothing
63+ let imports' = symbs ^? ix 0 . L. children . _Just . to fromList . ix 0
64+
65+ liftIO $ Just imports == imports' @? " Contains symbol"
66+ ]
5467
5568pre310Tests :: TestTree
5669pre310Tests = testGroup " pre 3.10 symbol information" [
57- ignoreTestBecause " Broken " $ testCase " provides nested data types and constructors" $ runSession hlsCommand oldCaps " test/testdata" $ do
70+ testCase " provides nested data types and constructors" $ runSession hlsCommand oldCaps " test/testdata" $ do
5871 doc@ (TextDocumentIdentifier testUri) <- openDoc " Symbols.hs" " haskell"
5972 Right symbs <- getDocumentSymbols doc
6073
61- let myData = SymbolInformation " MyData" SkClass Nothing (Location testUri myDataR) Nothing
74+ let myData = SymbolInformation " MyData" SkStruct Nothing (Location testUri myDataR) ( Just " Symbols " )
6275 a = SymbolInformation " A" SkConstructor Nothing (Location testUri aR) (Just " MyData" )
6376 b = SymbolInformation " B" SkConstructor Nothing (Location testUri bR) (Just " MyData" )
6477
6578 liftIO $ [myData, a, b] `isInfixOf` symbs @? " Contains symbols"
6679
67- ,ignoreTestBecause " Broken " $ testCase " provides nested where functions" $ runSession hlsCommand oldCaps " test/testdata" $ do
80+ , ignoreTestBecause " extracting symbols from nested wheres not supported " $ testCase " provides nested where functions" $ runSession hlsCommand oldCaps " test/testdata" $ do
6881 doc@ (TextDocumentIdentifier testUri) <- openDoc " Symbols.hs" " haskell"
6982 Right symbs <- getDocumentSymbols doc
7083
71- let foo = SymbolInformation " foo" SkFunction Nothing (Location testUri fooR) Nothing
84+ let foo = SymbolInformation " foo" SkFunction Nothing (Location testUri fooR) ( Just " Symbols " )
7285 bar = SymbolInformation " bar" SkFunction Nothing (Location testUri barR) (Just " foo" )
7386 dog = SymbolInformation " dog" SkVariable Nothing (Location testUri dogR) (Just " bar" )
7487 cat = SymbolInformation " cat" SkVariable Nothing (Location testUri catR) (Just " bar" )
7588
7689 -- Order is important!
7790 liftIO $ [foo, bar, dog, cat] `isInfixOf` symbs @? " Contains symbols"
91+
92+ , ignoreTestBecause " extracting pattern synonym symbols not supported" $ testCase " provides pattern synonyms" $ runSession hlsCommand oldCaps " test/testdata" $ do
93+ doc@ (TextDocumentIdentifier testUri) <- openDoc " Symbols.hs" " haskell"
94+ Right symbs <- getDocumentSymbols doc
95+
96+ let testPattern = SymbolInformation " TestPattern"
97+ SkFunction Nothing (Location testUri testPatternR) (Just " Symbols" )
98+
99+ liftIO $ testPattern `elem` symbs @? " Contains symbols"
100+
101+ , testCase " provides imports" $ runSession hlsCommand oldCaps " test/testdata" $ do
102+ doc@ (TextDocumentIdentifier testUri) <- openDoc " Symbols.hs" " haskell"
103+ Right symbs <- getDocumentSymbols doc
104+
105+ let imports = SymbolInformation " imports" SkModule Nothing (Location testUri importsR) (Just " Symbols" )
106+ importDataMaybe = SymbolInformation " import Data.Maybe" SkModule Nothing (Location testUri importDataMaybeR) (Just " imports" )
107+
108+ liftIO $ [imports, importDataMaybe] `isInfixOf` symbs @? " Contains symbol"
78109 ]
79110
80111oldCaps :: ClientCapabilities
81112oldCaps = capsForVersion (LSPVersion 3 9 )
113+
114+ fromList :: List a -> [a ]
115+ fromList (List a) = a
116+
82117-- Some common ranges and selection ranges in Symbols.hs
118+ importsR :: Range
119+ importsR = Range (Position 3 0 ) (Position 3 17 )
120+ importsSR :: Range
121+ importsSR = Range (Position 3 0 ) (Position 3 17 )
122+ importDataMaybeR :: Range
123+ importDataMaybeR = Range (Position 3 0 ) (Position 3 17 )
124+ importDataMaybeSR :: Range
125+ importDataMaybeSR = Range (Position 3 0 ) (Position 3 17 )
83126fooSR :: Range
84- fooSR = Range (Position 5 0 ) (Position 5 3 )
127+ fooSR = Range (Position 5 0 ) (Position 7 43 )
85128fooR :: Range
86129fooR = Range (Position 5 0 ) (Position 7 43 )
87130barSR :: Range
@@ -97,7 +140,7 @@ catSR = Range (Position 7 22) (Position 7 25)
97140catR :: Range
98141catR = Range (Position 7 16 ) (Position 7 43 )
99142myDataSR :: Range
100- myDataSR = Range (Position 9 5 ) (Position 9 11 )
143+ myDataSR = Range (Position 9 0 ) (Position 10 22 )
101144myDataR :: Range
102145myDataR = Range (Position 9 0 ) (Position 10 22 )
103146aSR :: Range
0 commit comments