forked from coolchyni/pascal-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasls.lpr
181 lines (148 loc) · 4.5 KB
/
pasls.lpr
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
// Pascal Language Server
// Copyright 2020 Arjan Adriaanse
// 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/>.
program pasls;
{$mode objfpc}{$H+}
uses
{ RTL }
SysUtils, fpjson, jsonparser, jsonscanner,
{ LSP }
lsp, general,
{ Protocols }
basic, synchronization, completion, gotoDeclaration, gotoDefinition,
gotoImplementation, gotoTypeDefinition, hover, signatureHelp, references, codeAction, rename,
documentHighlight, documentSymbol, workspace, window, diagnostics, settings,
{Other}
LazLoggerBase;
const
ContentType = 'application/vscode-jsonrpc; charset=utf-8';
type
TTestNotification = class(specialize TLSPNotification<TShowMessageParams>)
procedure Process(var Params : TShowMessageParams); override;
end;
{ TMyLogger }
TMyLogger=class(TLazLogger)
procedure DoDebugLn(s: string; AGroup: PLazLoggerLogGroup = nil); override;
end;
{ TMyLogger }
procedure TMyLogger.DoDebugLn(s: string; AGroup: PLazLoggerLogGroup);
begin
WriteLn(StdErr,s);
Flush(StdErr);
end;
procedure TTestNotification.Process(var Params : TShowMessageParams);
begin
writeln('got params: ', Params.ClassName)
end;
procedure TestNotifications;
var
params: TShowMessageParams;
//notification: TTestNotification;
data: TJSONData;
Dispatcher: TLSPDispatcher;
begin
params := TShowMessageParams.Create;
params.&type := TMessageType.Error;
params.message := 'Some Error Message';
Dispatcher := TLSPDispatcher.Create(nil);
//notification := TTestNotification.Create(nil);
//function TLSPDispatcher.ExecuteMethod(const AClassName, AMethodName: TJSONStringType;
// Params, ID: TJSONData; AContext: TJSONRPCCallContext): TJSONData;
data := specialize TLSPStreaming<TShowMessageParams>.ToJSON(params);
writeln(data.AsJSON);
data := Dispatcher.Execute(data);
if data <> nil then
writeln(data.AsJSON);
end;
procedure RunConsole;
var
Dispatcher: TLSPDispatcher;
Header, Name, Value, Content: string;
I, Length: Integer;
Request, Response: TJSONData;
VerboseDebugging: boolean = false;
begin
//TestNotifications;
//halt;
Length:=0;
Dispatcher := TLSPDispatcher.Create(nil);
TJSONData.CompressedJSON := True;
SetTextLineEnding(Input, #13#10);
SetTextLineEnding(Output, #13#10);
SetTextCodePage(Input,CP_UTF8);
SetTextCodePage(Output,CP_UTF8);
while not EOF do
begin
ReadLn(Header);
while Header <> '' do
begin
I := Pos(':', Header);
Name := Copy(Header, 1, I - 1);
Delete(Header, 1, i);
Value := Trim(Header);
if Name = 'Content-Length' then Length := StrToInt(Value);
ReadLn(Header);
end;
Content := '';
SetLength(Content, Length);
I := 1;
while I <= Length do
begin
Read(Content[I]);
Inc(I);
end;
Request := TJSONParser.Create(Content, DefaultOptions).Parse;
if TJSONObject(request).Find('params')=nil then
begin
TJSONObject(request).Add('params',TJSONObject.Create);
end;
// log request payload
if VerboseDebugging then
begin
writeln(StdErr, Request.FormatJSON);
Flush(StdErr);
end;
Response := Dispatcher.Execute(Request);
if Assigned(Response) then
begin
if not IsResponseValid(Response) then
begin
Writeln(StdErr, 'invalid response -> ', response.AsJSON);
Flush(StdErr);
continue;
end;
Content := Response.AsJSON;
WriteLn('Content-Type: ', ContentType);
WriteLn('Content-Length: ', Content.Length);
WriteLn;
Write(Content);
Flush(Output);
// log response payload
if VerboseDebugging then
begin
writeln(StdErr, Content);
Flush(StdErr);
end;
Response.Free;
end;
Request.Free;
end;
end;
var log:TMyLogger;
begin
log:=TMyLogger.Create;
SetDebugLogger(log);
RunConsole;
log.Free;
end.