on master help sign in
Group Imports in Outline into a single node (#443)
1e68cb0fendor
src/Development/IDE/LSP/Outline.hs+22−1
@@ -50,7 +50,9 @@ moduleOutline _lsp ideState DocumentSymbolParams { _textDocument = TextDocumentI
50 , _kind = SkFile
50 , _kind = SkFile
51 , _range = Range (Position 0 0) (Position maxBound 0) -- _ltop is 0 0 0 0
51 , _range = Range (Position 0 0) (Position maxBound 0) -- _ltop is 0 0 0 0
52 }
52 }
53+ importSymbols = maybe [] pure $
54+ documentSymbolForImportSummary
53− importSymbols = mapMaybe documentSymbolForImport hsmodImports
55+ (mapMaybe documentSymbolForImport hsmodImports)
54 allSymbols = case moduleSymbol of
56 allSymbols = case moduleSymbol of
55 Nothing -> importSymbols <> declSymbols
57 Nothing -> importSymbols <> declSymbols
56 Just x ->
58 Just x ->
@@ -167,6 +169,25 @@ documentSymbolForDecl (L l (ForD x)) = Just
167
169
168documentSymbolForDecl _ = Nothing
170documentSymbolForDecl _ = Nothing
169
171
172+-- | Wrap the Document imports into a hierarchical outline for
173+-- a better overview of symbols in scope.
174+-- If there are no imports, then no hierarchy will be created.
175+documentSymbolForImportSummary :: [DocumentSymbol] -> Maybe DocumentSymbol
176+documentSymbolForImportSummary [] = Nothing
177+documentSymbolForImportSummary importSymbols =
178+ let
179+ -- safe because if we have no ranges then we don't take this branch
180+ mergeRanges xs = Range (minimum $ map _start xs) (maximum $ map _end xs)
181+ importRange = mergeRanges $ map (_range :: DocumentSymbol -> Range) importSymbols
182+ in
183+ Just (defDocumentSymbol empty :: DocumentSymbol)
184+ { _name = "imports"
185+ , _kind = SkModule
186+ , _children = Just (List importSymbols)
187+ , _range = importRange
188+ , _selectionRange = importRange
189+ }
190+
170documentSymbolForImport :: Located (ImportDecl GhcPs) -> Maybe DocumentSymbol
191documentSymbolForImport :: Located (ImportDecl GhcPs) -> Maybe DocumentSymbol
171documentSymbolForImport (L l ImportDecl { ideclName, ideclQualified }) = Just
192documentSymbolForImport (L l ImportDecl { ideclName, ideclQualified }) = Just
172 (defDocumentSymbol l :: DocumentSymbol)
193 (defDocumentSymbol l :: DocumentSymbol)
test/exe/Main.hs+18−1
@@ -1717,7 +1717,24 @@ outlineTests = testGroup
1717 docId <- openDoc' "A.hs" "haskell" source
1717 docId <- openDoc' "A.hs" "haskell" source
1718 symbols <- getDocumentSymbols docId
1718 symbols <- getDocumentSymbols docId
1719 liftIO $ symbols @?= Left
1719 liftIO $ symbols @?= Left
1720+ [docSymbolWithChildren "imports"
1721+ SkModule
1722+ (R 0 0 0 17)
1720− [docSymbol "import Data.Maybe" SkModule (R 0 0 0 17)]
1723+ [ docSymbol "import Data.Maybe" SkModule (R 0 0 0 17)
1724+ ]
1725+ ]
1726+ , testSessionWait "multiple import" $ do
1727+ let source = T.unlines ["", "import Data.Maybe", "", "import Control.Exception", ""]
1728+ docId <- openDoc' "A.hs" "haskell" source
1729+ symbols <- getDocumentSymbols docId
1730+ liftIO $ symbols @?= Left
1731+ [docSymbolWithChildren "imports"
1732+ SkModule
1733+ (R 1 0 3 24)
1734+ [ docSymbol "import Data.Maybe" SkModule (R 1 0 1 17)
1735+ , docSymbol "import Control.Exception" SkModule (R 3 0 3 24)
1736+ ]
1737+ ]
1721 , testSessionWait "foreign import" $ do
1738 , testSessionWait "foreign import" $ do
1722 let source = T.unlines
1739 let source = T.unlines
1723 [ "{-# language ForeignFunctionInterface #-}"
1740 [ "{-# language ForeignFunctionInterface #-}"