-
Notifications
You must be signed in to change notification settings - Fork 16
/
PasLS.TextLoop.pas
242 lines (198 loc) · 5.92 KB
/
PasLS.TextLoop.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
// Pascal Language Server
// Copyright 2023 Michael Van Canneyt
// LSP Text/File based protocol - in particular, Standard Input/Output/Error files.
// 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 PasLS.TextLoop;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, LSP.Base, LSP.Messages, fpjson;
Type
{ TTextLSPContext }
PText = ^Text;
{ TLSPTextTransport }
TLSPTextTransport = class(TMessageTransport)
FOutput : PText;
FError : PText;
Protected
Procedure DoSendMessage(aMessage: TJSONData); override;
Procedure DoSendDiagnostic(const aMessage: UTF8String); override;
Public
constructor Create(aOutput,aError : PText); reintroduce;
Procedure EmitMessage(aMessage: TJSONStringType);
end;
Procedure SetupTextLoop(var aInput,aOutput,aError : Text);
Procedure RunMessageLoop(var aInput,aOutput,aError : Text; aContext : TLSPContext);
procedure DebugSendMessage(var aFile : Text; aContext : TLSPContext; const aMethod, aParams: String);
implementation
Procedure SetupTextLoop(var aInput,aOutput,aError : Text);
begin
TJSONData.CompressedJSON := True;
SetTextLineEnding(aInput, #13#10);
SetTextLineEnding(aOutput, #13#10);
SetTextLineEnding(aError, #13#10);
end;
procedure DebugSendMessage(var aFile : Text; aContext : TLSPContext; const aMethod, aParams: String);
var
Content: TJSONStringType;
Request: TJSONData;
Response: TJSONData;
begin
Response:=Nil;
Writeln(aFile,'▶️ ', aMethod);
Content := '{"jsonrpc": "2.0","id": '+aContext.NextMessageID.ToString+', "method": "'+aMethod+'","params": '+aParams+'}';
Request := GetJSON(Content, True);
try
Response := aContext.Execute(Request);
if Assigned(Response) then
begin
writeln(aFile,'◀️ response: ');
writeln(aFile,Response.FormatJSON);
Flush(aFile);
end;
finally
Request.Free;
Response.Free;
end;
end;
Function ReadRequest(var aFile : text; aContext : TLSPContext) : TJSONData;
Var
Header,Name,Value: String;
Content : TJSONStringType;
I,ContentLength : Integer;
P : PJSONCharType;
begin
Result:=Nil;
aContext.Log('Reading request');
ReadLn(aFile,Header);
while Header <> '' do
begin
aContext.Log('Read header: %s',[Header]);
I := Pos(':', Header);
Name := Copy(Header, 1, I - 1);
Delete(Header, 1, i);
Value := Trim(Header);
if Name = 'Content-Length' then
ContentLength := StrToIntDef(Value,0);
ReadLn(aFile,Header);
end;
Content:='';
SetLength(Content,ContentLength);
P:=PJSONCharType(Content);
for I:=1 to ContentLength do
begin
Read(aFile,P^);
inc(P);
end;
if Content<>'' then
Result:=GetJSON(Content, True);
end;
Procedure SendResponse(aTransport : TMessageTransport; aContext : TLSPContext; aResponse : TJSONData; aFreeResponse : Boolean = True);
Var
Content : TJSONStringType;
begin
try
if not IsResponseValid(aResponse) then
begin
aContext.Log('Response not valid: %s',[aResponse.AsJSON]);
aTransport.SendDiagnostic('invalid response -> '+aResponse.AsJSON);
exit;
end;
Content := aResponse.AsJSON;
(aTransport as TLSPTextTransport).EmitMessage(Content);
aContext.Log('Wrote response to request');
finally
if aFreeResponse then
aResponse.Free;
end;
end;
Procedure RunMessageLoop(var aInput,aOutput,aError : Text; aContext : TLSPContext);
var
Request, Response: TJSONData;
VerboseDebugging: boolean = false;
IO : TLSPTextTransport;
begin
IO:=Nil;
Request:=Nil;
try
if aContext.Transport is TLSPTextTransport then
IO:=aContext.Transport as TLSPTextTransport
else
IO:=TLSPTextTransport.Create(@aOutput,@aError);
while not EOF(aInput) do
begin
Request:=ReadRequest(aInput,aContext);
// log request payload
if VerboseDebugging then
begin
Writeln(aError, Request.FormatJSON);
Flush(aError);
end;
Response := aContext.Execute(Request);
if Assigned(Response) then
begin
// log response payload
if VerboseDebugging then
begin
writeln(aError, Response.asJSON);
Flush(aError);
end;
SendResponse(IO, aContext, Response,True);
end
else
aContext.Log('No response to request');
FreeAndNil(Request);
end;
finally
if IO<>aContext.Transport then
IO.Free;
Request.Free;
end;
end;
{ TTextLSPContext }
constructor TLSPTextTransport.Create(aOutput, aError: PText);
begin
FOutput:=aOutput;
FError:=aError;
end;
procedure TLSPTextTransport.EmitMessage(aMessage: TJSONStringType);
begin
Try
WriteLn(Foutput^,'Content-Type: ', ContentType);
WriteLn(Foutput^,'Content-Length: ', Length(aMessage));
WriteLn(Foutput^);
Write(Foutput^,aMessage);
Flush(Foutput^);
except
on e : exception do
DoLog('Exception %s during output: %s',[E.ClassName,E.Message]);
end;
end;
procedure TLSPTextTransport.DoSendMessage(aMessage: TJSONData);
Var
Content : TJSONStringType;
begin
Content:=aMessage.AsJSON;
EmitMessage(Content);
end;
procedure TLSPTextTransport.DoSendDiagnostic(const aMessage: UTF8String);
begin
Try
WriteLn(FError^,aMessage);
Flush(FError^);
except
on e : exception do
DoLog('Exception %s during diagnostic output: %s',[E.ClassName,E.Message]);
end;
end;
end.