forked from genericptr/pascal-language-server
-
Notifications
You must be signed in to change notification settings - Fork 3
/
diagnostics.pas
251 lines (211 loc) · 7.83 KB
/
diagnostics.pas
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
// Pascal Language Server
// Copyright 2020 Ryan Joseph
// This file is part of Pascal Language Server.
// Pascal Language Server is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Pascal Language Server is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pascal Language Server. If not, see
// <https://www.gnu.org/licenses/>.
unit diagnostics;
{$mode objfpc}{$H+}
interface
uses
Classes,
CodeToolManager, CodeCache,
lsp, basic, window;
type
{ TPublishDiagnosticsParams }
TPublishDiagnosticsParams = class(TPersistent)
private
fUri: TDocumentUri;
fVersion: integer;
fDiagnostics: TDiagnosticItems;
published
// The URI for which diagnostic information is reported.
property uri: TDocumentUri read fUri write fUri;
// The version number of the document the diagnostics are published for.
// todo: this must be optional
//property version: integer read fVersion write fVersion;
// An array of diagnostic information items.
property diagnostics: TDiagnosticItems read fDiagnostics write fDiagnostics;
public
procedure AfterConstruction; override;
end;
{ TPublishDiagnostics }
{ Diagnostics notification are sent from the server to the client to signal results of validation runs.
Diagnostics are “owned” by the server so it is the server’s responsibility to clear them if necessary.
The following rule is used for VS Code servers that generate diagnostics:
if a language is single file only (for example HTML) then diagnostics are cleared by the server when the file is closed.
if a language has a project system (for example C#) diagnostics are not cleared when a file closes. When a project is
opened all diagnostics for all files are recomputed (or read from a cache).
When a file changes it is the server’s responsibility to re-compute diagnostics and push them to the client. If the
computed set is empty it has to push the empty array to clear former diagnostics. Newly pushed diagnostics always replace
previously pushed diagnostics. There is no merging that happens on the client side. }
TPublishDiagnostics = class(TNotificationMessage)
public
constructor Create;
destructor Destroy; override;
procedure Add(fileName, message: string; line, column: integer; code:Int64; severity: TDiagnosticSeverity);
procedure Clear(fileName: string);
end;
{ Code Tools Error Handling }
procedure CheckSyntax(Code: TCodeBuffer);
procedure PublishDiagnostic(UserMessage: String = '');
procedure ClearDiagnostics(Code: TCodeBuffer);
implementation
uses
SysUtils, settings;
{ Publish the last code tools error as a diagnostics }
procedure PublishDiagnostic(UserMessage: String = '');
var
Notification: TPublishDiagnostics;
ShowMessage: TShowMessageNotification;
FileName,
MessageString: String;
hasfile:Boolean;
begin
//if UserMessage <> '' then
// writeln(stderr, UserMessage)
//else
// begin
// if CodeToolBoss.ErrorCode <> nil then
// begin
// MessageString := CodeToolBoss.ErrorCode.FileName+' "'+CodeToolBoss.ErrorMessage+'" @ '+IntToStr(CodeToolBoss.ErrorLine-1)+':'+IntToStr(CodeToolBoss.ErrorColumn-1);
// hasfile:=True;
// end
// else if CodeToolBoss.ErrorMessage <> '' then
// begin
// MessageString := '"'+CodeToolBoss.ErrorMessage+'" @ '+IntToStr(CodeToolBoss.ErrorLine-1)+':'+IntToStr(CodeToolBoss.ErrorColumn-1);
// hasfile:=False;
// end
// else
// // there's no error to show so bail
// // probably PublishDiagnostic should not have been called
// exit;
//
// // print the erro to StdErr
// writeln(StdErr, 'Syntax Error -> '+MessageString);
//
// // Show message in the gui also
// if ServerSettings.showSyntaxErrors then
// begin
// ShowMessage := TShowMessageNotification.Create(TMessageType.Error, MessageString,hasfile);
// ShowMessage.Send;
// ShowMessage.Free;
// end;
// end;
//Flush(stderr);
if not ServerSettings.publishDiagnostics then
exit;
if UserMessage <> '' then
begin
Notification := TPublishDiagnostics.Create;
Notification.Add('',
UserMessage,
0,
0,
CodeToolBoss.ErrorID,
TDiagnosticSeverity.Error);
Notification.Send;
Notification.Free;
end
else if CodeToolBoss.ErrorCode <> nil then
begin
Notification := TPublishDiagnostics.Create;
Notification.Add(CodeToolBoss.ErrorCode.FileName,
CodeToolBoss.ErrorMessage,
CodeToolBoss.ErrorLine - 1,
CodeToolBoss.ErrorColumn - 1,
CodeToolBoss.ErrorID,
TDiagnosticSeverity.Error);
Notification.Send;
Notification.Free;
end;
end;
{ Checks syntax for code buffer and publishes errors as diagnostics }
procedure CheckSyntax(Code: TCodeBuffer);
var
Tool: TCodeTool;
Notification: TPublishDiagnostics;
newCode:TCodeBuffer;
newX,newY,newTopLine:Integer;
errorMsg:String;
begin
if not ServerSettings.checkSyntax then
exit;
if not CodeToolBoss.Explore(Code,Tool,true) then
PublishDiagnostic
else if ServerSettings.publishDiagnostics then
begin
// if not CodeToolBoss.CheckSyntax(Code,newCode,newx,newY,newTopLine,errorMsg) then
// begin
// // todo: when we have a document store we can check to see
// // if we actually have any errors.
Notification := TPublishDiagnostics.Create;
Notification.Clear(Code.FileName);
// Notification.Add(Code.Filename,
// errorMsg,
// newY+1,
// newX+1,
// CodeToolBoss.ErrorID,
// TDiagnosticSeverity.Error);
Notification.Send;
Notification.Free;
// end;
end;
end;
procedure ClearDiagnostics(Code: TCodeBuffer);
var
Notification: TPublishDiagnostics;
begin
if ServerSettings.publishDiagnostics then
begin
// todo: when we have a document store we can check to see
// if we actually have any errors.
Notification := TPublishDiagnostics.Create;
Notification.Clear(Code.FileName);
Notification.Send;
Notification.Free;
end;
end;
{ TPublishDiagnostics }
procedure TPublishDiagnostics.Clear(fileName: string);
begin
TPublishDiagnosticsParams(params).uri := PathToURI(fileName);
TPublishDiagnosticsParams(params).diagnostics.Clear;
end;
procedure TPublishDiagnostics.Add(fileName, message: string; line, column: integer;code:Int64; severity: TDiagnosticSeverity);
var
Diagnostic: TDiagnostic;
begin
TPublishDiagnosticsParams(params).uri := PathToURI(fileName);
Diagnostic := TDiagnostic(TPublishDiagnosticsParams(params).diagnostics.Add);
Diagnostic.range := TRange.Create(line, column);
Diagnostic.severity := severity;
Diagnostic.code := IntToStr(code);
Diagnostic.source := 'Free Pascal Compiler';
Diagnostic.message := message;
end;
constructor TPublishDiagnostics.Create;
begin
params := TPublishDiagnosticsParams.Create;
method := 'textDocument/publishDiagnostics';
end;
destructor TPublishDiagnostics.Destroy;
begin
params.Free;
inherited;
end;
{ TPublishDiagnosticsParams }
procedure TPublishDiagnosticsParams.AfterConstruction;
begin
inherited;
diagnostics := TDiagnosticItems.Create;
end;
end.