-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnostics.go
163 lines (151 loc) · 5.45 KB
/
diagnostics.go
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
package server
import (
"context"
"errors"
"fmt"
"strings"
"github.com/huderlem/poryscript-pls/lsp"
"github.com/huderlem/poryscript-pls/parse"
"github.com/huderlem/poryscript/ast"
"github.com/huderlem/poryscript/lexer"
"github.com/huderlem/poryscript/parser"
)
// Checks the given Poryscript file content for diagnostic errors.
// Any diagnostics are immediately published to the client.
func (s *poryscriptServer) validatePoryscriptFile(ctx context.Context, fileUri string) error {
diagnostics := lsp.PublishDiagnosticsParams{
URI: lsp.DocumentURI(fileUri),
Diagnostics: []lsp.Diagnostic{},
}
// Only publish diagnostics for Poryscript files.
// The language server also has tenuous support for script.inc and text.inc files.
if !strings.HasSuffix(fileUri, ".pory") {
s.connection.Notify(ctx, "textDocument/publishDiagnostics", diagnostics)
return nil
}
content, err := s.getDocumentContent(ctx, fileUri)
if err != nil {
// TODO: log error?
s.connection.Notify(ctx, "textDocument/publishDiagnostics", diagnostics)
return err
}
// TODO: should this potential error be ignored?
commandConfig, _ := s.getAutovarCommands(ctx, fileUri)
p := parser.NewLintParser(lexer.New(content), commandConfig)
program, err := p.ParseProgram()
if err == nil {
// The poryscript file is syntactically correct. Check for warnings.
diagnostics.Diagnostics = append(diagnostics.Diagnostics, s.getPoryscriptWarnings(ctx, program, fileUri)...)
} else {
var parsedErr parser.ParseError
if !errors.As(err, &parsedErr) {
// TODO: this is an unknown error type, so we can't
// do anything with it. Log it?
s.connection.Notify(ctx, "textDocument/publishDiagnostics", diagnostics)
return nil
}
diagnostics.Diagnostics = append(diagnostics.Diagnostics,
lsp.Diagnostic{
Range: lsp.Range{
Start: lsp.Position{Line: parsedErr.LineNumberStart - 1, Character: parsedErr.Utf8CharStart},
End: lsp.Position{Line: parsedErr.LineNumberEnd - 1, Character: parsedErr.Utf8CharEnd},
},
Severity: lsp.Error,
Source: "Poryscript",
Message: parsedErr.Message,
},
)
}
s.connection.Notify(ctx, "textDocument/publishDiagnostics", diagnostics)
return nil
}
func (s *poryscriptServer) getPoryscriptWarnings(ctx context.Context, program *ast.Program, fileUri string) []lsp.Diagnostic {
diagnostics := []lsp.Diagnostic{}
for _, topStatement := range program.TopLevelStatements {
switch statement := topStatement.(type) {
case *ast.ScriptStatement:
diagnostics = append(diagnostics, s.getScriptWarnings(ctx, fileUri, statement)...)
case *ast.MovementStatement:
diagnostics = append(diagnostics, s.getMovementWarnings(ctx, fileUri, statement)...)
}
}
return diagnostics
}
// Finds any diagnostic warnings inside a Poryscript script statement.
func (s *poryscriptServer) getScriptWarnings(ctx context.Context, fileUri string, script *ast.ScriptStatement) []lsp.Diagnostic {
diagnostics := []lsp.Diagnostic{}
for _, statement := range script.AllChildren() {
cmd, ok := statement.(*ast.CommandStatement)
if !ok {
continue
}
// Check to see if the number of arguments given to the script command is valid.
commands, _ := s.getCommands(ctx, fileUri)
if command, ok := commands[cmd.Name.Value]; ok && command.Kind == parse.CommandScriptMacro {
numRequiredParams := 0
for _, p := range command.Parameters {
if p.Kind == parse.CommandParamRequired {
numRequiredParams++
}
}
var message string
if len(cmd.Args) < numRequiredParams {
message = fmt.Sprintf("%s requires at least %s, but %s provided", cmd.Name.Value, getArgumentsString(numRequiredParams), getWasWereString(len(cmd.Args)))
} else if len(cmd.Args) > len(command.Parameters) && !command.HasVarargParam() {
word := "were"
if len(cmd.Args) == 1 {
word = "was"
}
message = fmt.Sprintf("%s expects a maximum of %s, but %d %s provided", cmd.Name.Value, getArgumentsString(len(command.Parameters)), len(cmd.Args), word)
}
if len(message) > 0 {
diagnostics = append(diagnostics,
lsp.Diagnostic{
Range: lsp.Range{
Start: lsp.Position{Line: cmd.Token.LineNumber - 1, Character: cmd.Token.StartUtf8CharIndex},
End: lsp.Position{Line: cmd.Token.EndLineNumber - 1, Character: cmd.Token.EndUtf8CharIndex},
},
Severity: lsp.Warning,
Source: "Poryscript",
Message: message,
})
}
}
}
return diagnostics
}
// Finds any diagnostic warnings inside a Poryscript movement statement.
func (s *poryscriptServer) getMovementWarnings(ctx context.Context, fileUri string, script *ast.MovementStatement) []lsp.Diagnostic {
diagnostics := []lsp.Diagnostic{}
commands, _ := s.getCommands(ctx, fileUri)
for _, cmd := range script.MovementCommands {
if _, ok := commands[cmd.Literal]; !ok {
diagnostics = append(diagnostics,
lsp.Diagnostic{
Range: lsp.Range{
Start: lsp.Position{Line: cmd.LineNumber - 1, Character: cmd.StartUtf8CharIndex},
End: lsp.Position{Line: cmd.EndLineNumber - 1, Character: cmd.EndUtf8CharIndex},
},
Severity: lsp.Warning,
Source: "Poryscript",
Message: fmt.Sprintf("Unrecognized movement command \"%s\"", cmd.Literal),
})
}
}
return diagnostics
}
func getArgumentsString(n int) string {
if n == 1 {
return "1 argument"
}
return fmt.Sprintf("%d arguments", n)
}
func getWasWereString(n int) string {
if n == 0 {
return "none were"
} else if n == 1 {
return "only 1 was"
} else {
return fmt.Sprintf("only %d were", n)
}
}