-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnostics.fs
More file actions
77 lines (66 loc) · 2.82 KB
/
Diagnostics.fs
File metadata and controls
77 lines (66 loc) · 2.82 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
/// <summary>
/// Diagnostics are information/warnings/errors that show in a problems view and usually some visial indicator like
/// a colored underline or highlight for the relevant range of the file if known.
/// </summary>
/// <remarks>This example uses pull diagnostics which is newer than the original push diagnostics.
/// You may want to support push diagnostics so that your server is compatible with older clients.</remarks>
/// <see href="https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_pullDiagnostics"/>
module LspExample.Diagnostics
open System
open Ionide.LanguageServerProtocol.Types
open LspExample.Types
open LspExample.Types.DemoLang
let private diagnostic severity range message =
{ Range = range
Severity = Some severity
Code = None
CodeDescription = None
Source = Some "demolang"
Message = message
Tags = None
RelatedInformation = None
Data = None }
let private generateDiagnosticsForLine
{ Contents = contents
Line = line
Value = value }
: Diagnostic seq =
seq {
if contents.Length > 80 then
diagnostic
DiagnosticSeverity.Warning
(range (line, 0) (line, contents.Length - 1))
$"Line too long: {contents.Length} columns"
if String.IsNullOrWhiteSpace contents && contents.Length > 0 then
diagnostic
DiagnosticSeverity.Warning
(range (line, 0) (line, contents.Length))
"Line is non-empty and contains whitespace"
else
let noLeadingSpaces = contents.TrimStart()
if noLeadingSpaces <> contents then
diagnostic
DiagnosticSeverity.Error
(range (line, 0) (line, contents.Length - noLeadingSpaces.Length))
"Line has leading whitespace"
let noTrailingSpaces = contents.TrimEnd()
if noTrailingSpaces <> contents then
diagnostic
DiagnosticSeverity.Warning
(range (line, noTrailingSpaces.Length) (line, contents.Length))
"Line has trailing whitespace"
match value with
| Number number when number < 0 ->
diagnostic
DiagnosticSeverity.Error
(range (line, 0) (line, contents.Length))
"Negative numbers are not supported"
| _ -> ()
}
/// <summary>
/// Generate diagnostics for file contents: info, warnings, and errors
/// </summary>
/// <param name="contents">The file contents to generate diagnostics for</param>
let DocumentDiagnostics (contents: string) : Diagnostic[] =
let documentLines = Parser.ParseFile contents
documentLines |> Seq.collect generateDiagnosticsForLine |> Seq.toArray