-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathConversions.fs
More file actions
294 lines (266 loc) · 11.8 KB
/
Conversions.fs
File metadata and controls
294 lines (266 loc) · 11.8 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
module FSharpLanguageServer.Conversions
open LSP.Log
open FSharp.Compiler
open FSharp.Compiler.SourceCodeServices
open System
open System.IO
open LSP.Types
open FSharp.Data
/// Convert an F# Compiler Services 'FSharpErrorInfo' to an LSP 'Range'
let private errorAsRange(err: FSharpDiagnostic): Range =
{
// Got error "The field, constructor or member 'StartLine' is not defined"
start = {line=err.StartLineAlternate-1; character=err.StartColumn}
``end`` = {line=err.EndLineAlternate-1; character=err.EndColumn}
}
/// Convert an F# `Range.pos` to an LSP `Position`
let private asPosition(p: Text.pos): Position =
{
line=p.Line-1
character=p.Column
}
/// Convert an F# `Range.range` to an LSP `Range`
let asRange(r: Text.range): Range =
{
start=asPosition r.Start
``end``=asPosition r.End
}
/// Convert an F# `Range.range` to an LSP `Location`
let private asLocation(l: Text.range): Location =
{
uri=Uri("file://" + l.FileName)
range = asRange l
}
/// Convert an F# Compiler Services 'FSharpErrorSeverity' to an LSP 'DiagnosticSeverity'
let private asDiagnosticSeverity(s: FSharpDiagnosticSeverity): DiagnosticSeverity =
match s with
| FSharpDiagnosticSeverity.Warning -> DiagnosticSeverity.Warning
| FSharpDiagnosticSeverity.Error -> DiagnosticSeverity.Error
| FSharpDiagnosticSeverity.Info -> DiagnosticSeverity.Information
/// Convert an F# Compiler Services 'FSharpErrorInfo' to an LSP 'Diagnostic'
let asDiagnostic(err: FSharpDiagnostic): Diagnostic =
{
range = errorAsRange(err)
severity = Some(asDiagnosticSeverity(err.Severity))
code = Some(sprintf "%d: %s" err.ErrorNumber err.Subcategory)
source = None
message = err.Message
}
/// Create a Diagnostic
let diagnostic(message: string, range: Text.range, severity: DiagnosticSeverity): Diagnostic =
{
range = asRange(range)
severity = Some(severity)
code = None
source = None
message = message
}
/// Some compiler errors have no location in the file and should be displayed at the top of the file
let private hasNoLocation(err: FSharpDiagnostic): bool =
err.StartLineAlternate-1 = 0 &&
err.StartColumn = 0 &&
err.EndLineAlternate-1 = 0 &&
err.EndColumn = 0
/// A special error message that shows at the top of the file
let errorAtTop(message: string): Diagnostic =
{
range = { start = {line=0; character=0}; ``end`` = {line=0; character=1} }
severity = Some(DiagnosticSeverity.Error)
code = None
source = None
message = message
}
/// Convert a list of F# Compiler Services 'FSharpErrorInfo' to LSP 'Diagnostic'
let asDiagnostics(errors: FSharpDiagnostic seq): Diagnostic list =
[
for err in errors do
if hasNoLocation(err) then
yield errorAtTop(sprintf "%s: %s" err.Subcategory err.Message)
else
yield asDiagnostic(err)
]
/// Convert an F# `FSharpToolTipElement` to an LSP `Hover`
let asHover(FSharpToolTipText tips): Hover =
let elements =
[ for t in tips do
match t with
| FSharpToolTipElement.CompositionError(e) -> dprintfn "Error rendering tooltip: %s" e
| FSharpToolTipElement.None -> ()
| FSharpToolTipElement.Group(elements) ->
yield! elements ]
let contents =
match elements with
| [] -> []
| [one] ->
[ yield HighlightedString(one.MainDescription, "fsharp")
match TipFormatter.docComment(one.XmlDoc) with
| None -> ()
| Some(markdown) -> yield PlainString(markdown + "\n\n")
match one.Remarks with
| None | Some("") -> ()
| Some(remarks) ->
yield PlainString("*" + remarks + "*\n\n") ]
| many ->
let last = List.last(many)
[ for e in many do
yield HighlightedString(e.MainDescription, "fsharp")
match TipFormatter.docSummaryOnly(last.XmlDoc) with
| None -> ()
| Some(markdown) -> yield PlainString(markdown)
match last.Remarks with
| None | Some("") -> ()
| Some(remarks) ->
yield PlainString("*" + remarks + "*\n\n") ]
{contents=contents; range=None}
/// Convert an F# `FSharpGlyph` to an LSP `CompletionItemKind`
let private asCompletionItemKind(k: FSharpGlyph): CompletionItemKind =
match k with
| FSharpGlyph.Class -> CompletionItemKind.Class
| FSharpGlyph.Constant -> CompletionItemKind.Constant
| FSharpGlyph.Delegate -> CompletionItemKind.Property // ?
| FSharpGlyph.Enum -> CompletionItemKind.Enum
| FSharpGlyph.EnumMember -> CompletionItemKind.EnumMember
| FSharpGlyph.Event -> CompletionItemKind.Event
| FSharpGlyph.Exception -> CompletionItemKind.Class // ?
| FSharpGlyph.Field -> CompletionItemKind.Field
| FSharpGlyph.Interface -> CompletionItemKind.Interface
| FSharpGlyph.Method -> CompletionItemKind.Method
| FSharpGlyph.OverridenMethod -> CompletionItemKind.Method
| FSharpGlyph.Module -> CompletionItemKind.Module
| FSharpGlyph.NameSpace -> CompletionItemKind.Module // ?
| FSharpGlyph.Property -> CompletionItemKind.Property
| FSharpGlyph.Struct -> CompletionItemKind.Struct
| FSharpGlyph.Typedef -> CompletionItemKind.Interface // ?
| FSharpGlyph.Type -> CompletionItemKind.Class // ?
| FSharpGlyph.Union -> CompletionItemKind.Enum // ?
| FSharpGlyph.Variable -> CompletionItemKind.Variable
| FSharpGlyph.ExtensionMethod -> CompletionItemKind.Method
| FSharpGlyph.Error -> CompletionItemKind.Class // ?
/// Convert an F# `FSharpDeclarationListItem` to an LSP `CompletionItem`
let private asCompletionItem(i: FSharpDeclarationListItem): CompletionItem =
{ defaultCompletionItem with
label = i.Name
insertText = Some(i.NameInCode)
kind = Some(asCompletionItemKind(i.Glyph))
detail = Some(i.FullName)
// Stash FullName in data so we can use it later in ResolveCompletionItem
data = JsonValue.Record [|"FullName", JsonValue.String(i.FullName)|]
}
/// Convert an F# `FSharpDeclarationListInfo` to an LSP `CompletionList`
/// Used in rendering autocomplete lists
let asCompletionList(ds: FSharpDeclarationListInfo): CompletionList =
let items = [for i in ds.Items do yield asCompletionItem(i)]
{isIncomplete=List.isEmpty(items); items=items}
/// Convert an F# `FSharpMethodGroupItemParameter` to an LSP `ParameterInformation`
let private asParameterInformation(p: FSharpMethodGroupItemParameter): ParameterInformation =
{
label = p.ParameterName
documentation = Some p.Display
}
/// Convert an F# method name + `FSharpMethodGroupItem` to an LSP `SignatureInformation`
/// Used in providing signature help after autocompleting
let asSignatureInformation(methodName: string, s: FSharpMethodGroupItem): SignatureInformation =
let doc = match s.Description with
| FSharpToolTipText [FSharpToolTipElement.Group [tip]] -> Some tip.MainDescription
| _ ->
dprintfn "Can't render documentation %A" s.Description
None
let parameterName(p: FSharpMethodGroupItemParameter) = p.ParameterName
let parameterNames = Array.map parameterName s.Parameters
{
label = sprintf "%s(%s)" methodName (String.concat ", " parameterNames)
documentation = doc
parameters = Array.map asParameterInformation s.Parameters |> List.ofArray
}
/// Get the lcation where `s` was declared
let declarationLocation(s: FSharpSymbol): Location option =
match s.DeclarationLocation with
| None ->
dprintfn "Symbol %s has no declaration" s.FullName
None
| Some l ->
Some(asLocation(l))
/// Get the location where `s` was used
let useLocation(s: FSharpSymbolUse): Location =
asLocation(s.RangeAlternate)
/// Convert an F# `FSharpNavigationDeclarationItemKind` to an LSP `SymbolKind`
/// `FSharpNavigationDeclarationItemKind` is the level of symbol-type information you get when parsing without typechecking
let private asSymbolKind(k: FSharpNavigationDeclarationItemKind): SymbolKind =
match k with
| NamespaceDecl -> SymbolKind.Namespace
| ModuleFileDecl -> SymbolKind.Module
| ExnDecl -> SymbolKind.Class
| ModuleDecl -> SymbolKind.Module
| TypeDecl -> SymbolKind.Interface
| MethodDecl -> SymbolKind.Method
| PropertyDecl -> SymbolKind.Property
| FieldDecl -> SymbolKind.Field
| OtherDecl -> SymbolKind.Variable
/// Convert an F# `NavigationDeclarationItem` to an LSP `SymbolInformation`
/// `NavigationDeclarationItem` is the parsed AST representation of a symbol without typechecking
/// `container` is present when `d` is part of a module or type
let asSymbolInformation(d: NavigationItem, container: NavigationItem option): SymbolInformation =
let declarationName(d: NavigationItem) = d.Name
{
name=d.Name
kind=asSymbolKind d.Kind
location=asLocation d.Range
containerName=Option.map declarationName container
}
/// Convert symbols declared in an .fsi file to a CodeLens that helps the user navigate to the definition
let asGoToImplementation(name: string list, file: FileInfo, range: Text.range): CodeLens =
let jsonFile = JsonValue.String(file.FullName)
let jsonName = JsonValue.Array([|for i in name do yield JsonValue.String(i)|])
{
range=asRange(range)
command=None
data=JsonValue.Array([|jsonFile; jsonName|])
}
let goToImplementationData(goTo: CodeLens) =
match goTo.data with
| JsonValue.Array([|JsonValue.String(file); JsonValue.Array(jsonNames)|]) ->
FileInfo(file), [for JsonValue.String(j) in jsonNames do yield j ]
let resolveGoToImplementation(unresolved: CodeLens, file: FileInfo, range: Text.range): CodeLens =
let command =
{
title=sprintf "%s(%d)" file.Name range.StartLine
command="fsharp.command.goto"
arguments=[
JsonValue.String(file.FullName)
JsonValue.Number(decimal(range.StartLine - 1))
JsonValue.Number(decimal(range.StartColumn))
JsonValue.Number(decimal(range.EndLine - 1))
JsonValue.Number(decimal(range.EndColumn))
]
}
{ unresolved with command = Some(command) }
let resolveMissingGoToImplementation(unresolved: CodeLens, file: FileInfo): CodeLens =
let command =
{
title="Not Found"
command="fsharp.command.goto"
arguments=[
JsonValue.String(file.FullName)
JsonValue.Number(decimal(unresolved.range.start.line))
JsonValue.Number(decimal(unresolved.range.start.character))
JsonValue.Number(decimal(unresolved.range.``end``.line))
JsonValue.Number(decimal(unresolved.range.``end``.character))
]
}
{ unresolved with command = Some(command) }
let asRunTest(fsproj: FileInfo, fullyQualifiedName: string list, test: SyntaxTree.SynBinding): CodeLens =
{
range=asRange(test.RangeOfBindingSansRhs)
command=Some({ title="Run Test"
command="fsharp.command.test.run"
arguments=[JsonValue.String(fsproj.FullName); JsonValue.String(String.concat "." fullyQualifiedName)] })
data=JsonValue.Null
}
let asDebugTest(fsproj: FileInfo, fullyQualifiedName: string list, test: SyntaxTree.SynBinding): CodeLens =
{
range=asRange(test.RangeOfBindingSansRhs)
command=Some({ title="Debug Test"
command="fsharp.command.test.debug"
arguments=[JsonValue.String(fsproj.FullName); JsonValue.String(String.concat "." fullyQualifiedName)] })
data=JsonValue.Null
}