Skip to content

Commit 965d61b

Browse files
committed
complete outline prototype
1 parent 3574165 commit 965d61b

File tree

1 file changed

+81
-30
lines changed
  • plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal

1 file changed

+81
-30
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Outline.hs

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ import Control.Monad.IO.Class
1515
import Data.Maybe
1616
import Data.Text qualified as T
1717
import Data.Text.Encoding (decodeASCII, decodeLatin1)
18-
import Debug.Trace as Debug
1918
import Development.IDE.Core.Rules
2019
import Development.IDE.Core.Shake (IdeState (shakeExtras), runIdeAction, useWithStaleFast)
2120
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))
21+
import Distribution.Fields.Field
22+
( Field (Field, Section),
23+
FieldLine (FieldLine),
24+
Name (Name),
25+
SectionArg (SecArgName, SecArgOther, SecArgStr),
26+
)
27+
import Distribution.Parsec.Position (Position)
2428
import Ide.Plugin.Cabal.Completion.Types (ParseCabalFields (..), cabalPositionToLSPPosition)
2529
import Ide.Plugin.Cabal.Orphans ()
2630
import Ide.Types
@@ -32,8 +36,6 @@ moduleOutline ideState _ LSP.DocumentSymbolParams {_textDocument = LSP.TextDocum
3236
case LSP.uriToFilePath uri of
3337
Just (toNormalizedFilePath' -> fp) -> do
3438
mFields <- liftIO $ runIdeAction "cabal-plugin.fields" (shakeExtras ideState) (useWithStaleFast ParseCabalFields fp)
35-
let debug = fmap fst mFields
36-
-- Debug.traceShowM debug
3739
case fmap fst mFields of
3840
Just fieldPositions -> pure $ LSP.InR (LSP.InL allSymbols)
3941
where
@@ -42,39 +44,88 @@ moduleOutline ideState _ LSP.DocumentSymbolParams {_textDocument = LSP.TextDocum
4244
Nothing -> pure $ LSP.InL []
4345

4446
documentSymbolForField :: Field Position -> Maybe LSP.DocumentSymbol
45-
documentSymbolForField (Field (Name pos fieldName) fieldLines) = Just $ LSP.DocumentSymbol {..}
47+
documentSymbolForField (Field (Name pos fieldName) fieldLines) =
48+
Just
49+
(defDocumentSymbol range)
50+
{ LSP._name = decodeASCII fieldName,
51+
LSP._kind = LSP.SymbolKind_Object,
52+
LSP._children = Just $ mapMaybe documentSymbolForFieldLine fieldLines
53+
}
4654
where
47-
_detail = Nothing
48-
_deprecated = Nothing
49-
_name = decodeASCII fieldName
50-
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
55+
range = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII fieldName
56+
documentSymbolForField (Section (Name pos fieldName) sectionArgs fields) =
57+
Just
58+
(defDocumentSymbol range)
59+
{ LSP._name = decodeASCII fieldName,
60+
LSP._kind = LSP.SymbolKind_Object,
61+
LSP._children =
62+
Just
63+
( mapMaybe documentSymbolForField fields
64+
++ mapMaybe documentSymbolForSectionArgs sectionArgs
65+
)
66+
}
67+
where
68+
range = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII fieldName
5669

57-
documentSymbolForField _ = Nothing
70+
documentSymbolForSectionArgs :: SectionArg Position -> Maybe LSP.DocumentSymbol
71+
documentSymbolForSectionArgs (SecArgName pos identifier) =
72+
Just
73+
(defDocumentSymbol range)
74+
{ LSP._name = decodeLatin1 identifier,
75+
LSP._kind = LSP.SymbolKind_Variable,
76+
LSP._children = Nothing
77+
}
78+
where
79+
range = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII identifier
80+
documentSymbolForSectionArgs (SecArgStr pos quotedString) =
81+
Just
82+
(defDocumentSymbol range)
83+
{ LSP._name = decodeLatin1 quotedString,
84+
LSP._kind = LSP.SymbolKind_Constant,
85+
LSP._children = Nothing
86+
}
87+
where
88+
range = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII quotedString
89+
documentSymbolForSectionArgs (SecArgOther pos string) =
90+
Just
91+
(defDocumentSymbol range)
92+
{ LSP._name = decodeLatin1 string,
93+
LSP._kind = LSP.SymbolKind_String,
94+
LSP._children = Nothing
95+
}
96+
where
97+
range = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII string
5898

5999
documentSymbolForFieldLine :: FieldLine Position -> Maybe LSP.DocumentSymbol
60-
documentSymbolForFieldLine (FieldLine pos line) = Just $ LSP.DocumentSymbol {..}
100+
documentSymbolForFieldLine (FieldLine pos line) =
101+
Just
102+
(defDocumentSymbol range)
103+
{ LSP._name = decodeLatin1 line, -- since there is no ascii invariant (?)
104+
LSP._kind = LSP.SymbolKind_Field,
105+
LSP._children = Nothing -- can't delete even though the base case covers this (?)
106+
}
61107
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
108+
range = cabalPositionToLSPRange pos `addNameLengthToLSPRange` decodeASCII line
71109

72110
cabalPositionToLSPRange :: Position -> LSP.Range
73111
cabalPositionToLSPRange pos = LSP.Range lspPos lspPos
74-
where lspPos = cabalPositionToLSPPosition pos
112+
where
113+
lspPos = cabalPositionToLSPPosition pos
75114

76115
addNameLengthToLSPRange :: LSP.Range -> T.Text -> LSP.Range
77116
addNameLengthToLSPRange (LSP.Range pos1 (LSP.Position line char)) name =
78-
LSP.Range
79-
pos1
80-
(LSP.Position line (char + fromIntegral (T.length name)))
117+
LSP.Range
118+
pos1
119+
(LSP.Position line (char + fromIntegral (T.length name)))
120+
121+
defDocumentSymbol :: LSP.Range -> LSP.DocumentSymbol
122+
defDocumentSymbol range = LSP.DocumentSymbol {..}
123+
where
124+
_detail = Nothing
125+
_deprecated = Nothing
126+
_name = ""
127+
_kind = LSP.SymbolKind_File
128+
_range = range
129+
_selectionRange = range
130+
_children = Nothing
131+
_tags = Nothing

0 commit comments

Comments
 (0)