/
SyntaxHighlight.idr
247 lines (212 loc) · 7.98 KB
/
SyntaxHighlight.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
module Idris.IDEMode.SyntaxHighlight
import Core.Context
import Core.Context.Log
import Core.Directory
import Core.InitPrimitives
import Core.Metadata
import Core.TT
import Idris.REPL
import Idris.Syntax
import Idris.Pretty
import Idris.Doc.String
import Idris.IDEMode.Commands
import Data.List
import Data.Maybe
import Libraries.Data.PosMap
%default total
------------------------------------------------------------------------
-- Text properties supported by the IDE mode
------------------------------------------------------------------------
data Formatting : Type where
Bold : Formatting
Italic : Formatting
Underline : Formatting
-- CAREFUL: this instance is used in SExpable Formatting. If you change
-- it then you need to fix the SExpable implementation in order not to
-- break the IDE mode.
Show Formatting where
show Bold = "bold"
show Italic = "italic"
show Underline = "underline"
-- At most one decoration & one formatting
-- (We could use `These` to guarantee non-emptiness but I am not
-- convinced this will stay being just 2 fields e.g. the emacs mode
-- has support for tagging things as errors, adding links, etc.)
public export
record Properties where
constructor MkProperties
decor : Maybe Decoration
format : Maybe Formatting
export
mkDecor : Decoration -> Properties
mkDecor dec = MkProperties (Just dec) Nothing
export
mkFormat : Formatting -> Properties
mkFormat = MkProperties Nothing . Just
export
syntaxToProperties : IdrisSyntax -> Maybe Properties
syntaxToProperties syn = mkDecor <$> syntaxToDecoration syn
export
annToProperties : IdrisAnn -> Maybe Properties
annToProperties Warning = Nothing
annToProperties Error = Nothing
annToProperties ErrorDesc = Nothing
annToProperties FileCtxt = Nothing
annToProperties Code = Nothing
annToProperties Meta = Nothing
annToProperties (Syntax syn) = syntaxToProperties syn
export
docToProperties : IdrisDocAnn -> Maybe Properties
docToProperties Header = pure $ mkFormat Underline
docToProperties Declarations = Nothing
docToProperties (Decl _) = Nothing
docToProperties DocStringBody = Nothing
docToProperties (Syntax syn) = syntaxToProperties syn
SExpable Decoration where
toSExp decor = SExpList [ SymbolAtom "decor"
, SymbolAtom (show decor)
]
SExpable Formatting where
toSExp format = SExpList [ SymbolAtom "text-formatting"
, SymbolAtom (show format)
]
export
SExpable Properties where
toSExp (MkProperties dec form) = SExpList $ catMaybes
[ toSExp <$> form
, toSExp <$> dec
]
record Highlight where
constructor MkHighlight
location : NonEmptyFC
filename : String
name : String
isImplicit : Bool
key : String
decor : Decoration
docOverview : String
typ : String
ns : String
SExpable (FileName, FC) where
toSExp (fname, fc) = case isNonEmptyFC fc of
Just (origin, (startLine, startCol), (endLine, endCol)) =>
SExpList [ SExpList [ SymbolAtom "filename", StringAtom fname ]
, SExpList [ SymbolAtom "start"
, IntegerAtom (cast startLine + 1)
, IntegerAtom (cast startCol + 1)
]
, SExpList [ SymbolAtom "end"
, IntegerAtom (cast endLine + 1)
, IntegerAtom (cast endCol)
]
]
Nothing => SExpList []
SExpable Highlight where
toSExp (MkHighlight loc fname nam impl k dec doc t ns)
= SExpList [ toSExp $ (fname, justFC loc)
, SExpList [ SExpList [ SymbolAtom "name", StringAtom nam ]
, SExpList [ SymbolAtom "namespace", StringAtom ns ]
, toSExp dec
, SExpList [ SymbolAtom "implicit", toSExp impl ]
, SExpList [ SymbolAtom "key", StringAtom k ]
, SExpList [ SymbolAtom "doc-overview", StringAtom doc ]
, SExpList [ SymbolAtom "type", StringAtom t ]
]
]
||| Output some data using current dialog index
export
printOutput : {auto c : Ref Ctxt Defs} ->
{auto o : Ref ROpts REPLOpts} ->
SExp -> Core ()
printOutput msg
= do opts <- get ROpts
case idemode opts of
REPL _ => pure ()
IDEMode i _ f =>
send f (SExpList [SymbolAtom "output",
msg, toSExp i])
outputHighlight : {auto c : Ref Ctxt Defs} ->
{auto opts : Ref ROpts REPLOpts} ->
Highlight -> Core ()
outputHighlight h =
printOutput $ SExpList [ SymbolAtom "ok"
, SExpList [ SymbolAtom "highlight-source"
, toSExp hlt
]
]
where
hlt : List Highlight
hlt = [h]
lwOutputHighlight :
{auto c : Ref Ctxt Defs} ->
{auto opts : Ref ROpts REPLOpts} ->
(FileName, NonEmptyFC, Decoration) -> Core ()
lwOutputHighlight (fname, nfc, decor) =
printOutput $ SExpList [ SymbolAtom "ok"
, SExpList [ SymbolAtom "highlight-source"
, toSExp $ the (List _) [
SExpList [ toSExp $ (fname, justFC nfc)
, SExpList [ toSExp decor]
]]]]
outputNameSyntax : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto opts : Ref ROpts REPLOpts} ->
(FileName, NonEmptyFC, Decoration, Name) -> Core ()
outputNameSyntax (fname, nfc, decor, nm) = do
defs <- get Ctxt
log "ide-mode.highlight" 20 $ "highlighting at " ++ show nfc
++ ": " ++ show nm
++ "\nAs: " ++ show decor
let fc = justFC nfc
let (mns, name) = displayName nm
outputHighlight $ MkHighlight
{ location = nfc
, name
, filename = fname
, isImplicit = False
, key = ""
, decor
, docOverview = "" --!(getDocsForName fc nm)
, typ = "" -- TODO: extract type maybe "" show !(lookupTyExact nm (gamma defs))
, ns = maybe "" show mns
}
export
outputSyntaxHighlighting : {auto c : Ref Ctxt Defs} ->
{auto m : Ref MD Metadata} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto opts : Ref ROpts REPLOpts} ->
String ->
REPLResult ->
Core REPLResult
outputSyntaxHighlighting fname loadResult = do
opts <- get ROpts
when (opts.synHighlightOn) $ do
meta <- get MD
modIdent <- ctxtPathToNS fname
let allNames = filter ((PhysicalIdrSrc modIdent ==) . fst . fst)
$ toList meta.nameLocMap
--decls <- filter (inFile fname) . tydecls <$> get MD
--_ <- traverse outputNameSyntax allNames -- ++ decls)
let semHigh = meta.semanticHighlighting
log "ide-mode.highlight" 19 $
"Semantic metadata is: " ++ show semHigh
let aliases
: List ASemanticDecoration
= flip foldMap meta.semanticAliases $ \ (from, to) =>
let decors = uncurry exactRange (snd to) semHigh in
map (\ ((fnm, loc), rest) => ((fnm, snd from), rest)) decors
log "ide-mode.highlight.alias" 19 $
"Semantic metadata from aliases is: " ++ show aliases
let defaults
: List ASemanticDecoration
= flip foldMap meta.semanticDefaults $ \ decor@((_, range), _) =>
case uncurry exactRange range semHigh of
[] => [decor]
_ => []
traverse_ {b = Unit}
(\(nfc, decor, mn) =>
case mn of
Nothing => lwOutputHighlight (fname, nfc, decor)
Just n => outputNameSyntax (fname, nfc, decor, n))
(defaults ++ aliases ++ toList semHigh)
pure loadResult