1
- {-# LANGUAGE CPP #-}
2
-
1
+ {-# LANGUAGE CPP #-}
2
+ {-# LANGUAGE DataKinds #-}
3
3
{-# LANGUAGE DuplicateRecordFields #-}
4
- {-# LANGUAGE GADTs #-}
5
-
6
- {-# LANGUAGE DataKinds #-}
7
- {-# LANGUAGE ViewPatterns #-}
8
- {-# LANGUAGE OverloadedStrings #-}
4
+ {-# LANGUAGE GADTs #-}
5
+ {-# LANGUAGE OverloadedStrings #-}
9
6
{-# LANGUAGE RecordWildCards #-}
10
-
7
+ {-# LANGUAGE ViewPatterns #-}
11
8
12
9
module Ide.Plugin.Cabal.Outline
13
- ( moduleOutline
14
- )
10
+ ( moduleOutline ,
11
+ )
15
12
where
16
13
17
- import Control.Monad.IO.Class
18
- import Data.Maybe
19
- import Development.IDE.Core.Rules
20
- import Development.IDE.Core.Shake ( IdeState (shakeExtras ), runIdeAction , useWithStaleFast )
21
- import Development.IDE.Types.Location ( toNormalizedFilePath' )
22
- import Ide.Types
23
- import qualified Language.LSP.Protocol.Types as LSP
24
- import qualified Language.LSP.Protocol.Message as LSP
25
-
26
- import Data.Text.Encoding (decodeASCII )
27
-
28
- import Ide.Plugin.Cabal.Completion.Types (ParseCabalFields (.. ))
29
- import Ide.Plugin.Cabal.Orphans ()
30
-
31
- import Distribution.Fields.Field (Field (Field ), Name (Name ))
32
- import Distribution.Parsec.Position (Position (Position ))
33
-
34
- import qualified Data.Text as T
35
- import Debug.Trace as Debug
14
+ import Control.Monad.IO.Class
15
+ import Data.Maybe
16
+ import Data.Text qualified as T
17
+ import Data.Text.Encoding (decodeASCII , decodeLatin1 )
18
+ import Debug.Trace as Debug
19
+ import Development.IDE.Core.Rules
20
+ import Development.IDE.Core.Shake (IdeState (shakeExtras ), runIdeAction , useWithStaleFast )
21
+ import Development.IDE.Types.Location (toNormalizedFilePath' )
22
+ import Distribution.Fields.Field (Field (Field ), Name (Name ), FieldName , FieldLine (FieldLine ))
23
+ import Distribution.Parsec.Position (Position (Position ))
24
+ import Ide.Plugin.Cabal.Completion.Types (ParseCabalFields (.. ), cabalPositionToLSPPosition )
25
+ import Ide.Plugin.Cabal.Orphans ()
26
+ import Ide.Types
27
+ import Language.LSP.Protocol.Message qualified as LSP
28
+ import Language.LSP.Protocol.Types qualified as LSP
36
29
37
30
moduleOutline :: PluginMethodHandler IdeState LSP. Method_TextDocumentDocumentSymbol
38
- moduleOutline ideState _ LSP. DocumentSymbolParams{ _textDocument = LSP. TextDocumentIdentifier uri }
39
- = case LSP. uriToFilePath uri of
31
+ moduleOutline ideState _ LSP. DocumentSymbolParams { _textDocument = LSP. TextDocumentIdentifier uri} =
32
+ case LSP. uriToFilePath uri of
40
33
Just (toNormalizedFilePath' -> fp) -> do
41
34
mFields <- liftIO $ runIdeAction " cabal-plugin.fields" (shakeExtras ideState) (useWithStaleFast ParseCabalFields fp)
42
35
let debug = fmap fst mFields
@@ -45,33 +38,43 @@ moduleOutline ideState _ LSP.DocumentSymbolParams{ _textDocument = LSP.TextDocum
45
38
Just fieldPositions -> pure $ LSP. InR (LSP. InL allSymbols)
46
39
where
47
40
allSymbols = mapMaybe documentSymbolForField fieldPositions
48
- -- pure $ InR (InL [DocumentSymbol {_name="hello!"
49
- -- ,_detail=Nothing
50
- -- ,_kind=SymbolKind_Module
51
- -- ,_tags=Nothing
52
- -- ,_range=mkRange 1 0 1 11
53
- -- ,_deprecated=Nothing
54
- -- ,_selectionRange=mkRange 1 0 1 11
55
- -- ,_children=Nothing}])
56
41
Nothing -> pure $ LSP. InL []
57
42
Nothing -> pure $ LSP. InL []
58
43
59
44
documentSymbolForField :: Field Position -> Maybe LSP. DocumentSymbol
60
- documentSymbolForField (Field (Name pos@ (Position line char) fieldName) _ )= Just $ LSP. DocumentSymbol { .. } where
61
- _detail = Nothing
62
- _deprecated = Nothing
63
- _name = decodeASCII fieldName
45
+ documentSymbolForField (Field (Name pos fieldName) fieldLines) = Just $ LSP. DocumentSymbol {.. }
46
+ where
47
+ _detail = Nothing
48
+ _deprecated = Nothing
49
+ _name = decodeASCII fieldName
64
50
65
- _kind = LSP. SymbolKind_Field
66
- _range = LSP. Range (parserToLSPPosition pos) (parserToLSPPosition ( Position line char))
67
- _selectionRange = LSP. Range (parserToLSPPosition pos) (parserToLSPPosition ( Position line char))
68
- _children = Nothing
69
- _tags = Nothing
51
+ _kind = LSP. SymbolKind_Field
52
+ _range = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII fieldName
53
+ _selectionRange = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII fieldName
54
+ _children = Just $ mapMaybe documentSymbolForFieldLine fieldLines
55
+ _tags = Nothing
70
56
71
- -- addNameLength :: UInt -> FieldName -> UInt
72
- -- addNameLength char name = toEnum (fromEnum char + length name)
73
57
documentSymbolForField _ = Nothing
74
58
59
+ documentSymbolForFieldLine :: FieldLine Position -> Maybe LSP. DocumentSymbol
60
+ documentSymbolForFieldLine (FieldLine pos line) = Just $ LSP. DocumentSymbol {.. }
61
+ where
62
+ _detail = Nothing
63
+ _deprecated = Nothing
64
+ _name = decodeLatin1 line -- since there is no ascii invariant (?)
65
+
66
+ _kind = LSP. SymbolKind_Field
67
+ _range = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII line
68
+ _selectionRange = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII line
69
+ _children = Nothing
70
+ _tags = Nothing
71
+
72
+ cabalPositionToLSPRange :: Position -> LSP. Range
73
+ cabalPositionToLSPRange pos = LSP. Range lspPos lspPos
74
+ where lspPos = cabalPositionToLSPPosition pos
75
75
76
- parserToLSPPosition :: Position -> LSP. Position
77
- parserToLSPPosition (Position start end) = LSP. Position (toEnum start) (toEnum end)
76
+ addNameLengthToLSPRange :: LSP. Range -> T. Text -> LSP. Range
77
+ addNameLengthToLSPRange (LSP. Range pos1 (LSP. Position line char)) name =
78
+ LSP. Range
79
+ pos1
80
+ (LSP. Position line (char + fromIntegral (T. length name)))
0 commit comments