-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathLSP.Messages.pas
More file actions
162 lines (125 loc) · 4.52 KB
/
LSP.Messages.pas
File metadata and controls
162 lines (125 loc) · 4.52 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
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
// Copyright 2023 Michael Van Canneyt
// 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 LSP.Messages;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LSP.Streaming, fpJSON, LSP.BaseTypes;
Type
// We cannot assume stdout to send out-of-band messages.
{ TMessageTransport }
TMessageLog = procedure(sender : TObject; Const Msg : UTF8String) of object;
TMessageTransport = class
Protected
Procedure DoSendMessage(aMessage : TJSONData); virtual; abstract;
Procedure DoSendDiagnostic(const aMessage : UTF8String); virtual; abstract;
Procedure DoLog(Const Msg : UTF8String); overload;
Procedure DoLog(Const Fmt : UTF8String; Const args : array of const); overload;
Public
Class Var OnLog : TMessageLog;
Public
Procedure SendMessage(aMessage : TJSONData);
Procedure SendDiagnostic(const aMessage : UTF8String);
Procedure SendDiagnostic(const Fmt : String; const args : Array of const); overload;
end;
{ TAbstractMessage
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#abstractMessage
A general message as defined by JSON-RPC. The language server
protocol always uses “2.0” as the jsonrpc version. }
TAbstractMessage = class(TLSPStreamable)
private
function GetJSONRPC: String;
published
property jsonrpc: String read GetJSONRPC;
public
procedure Send(aTransport : TMessageTransport);
end;
{ TRequestMessage
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#requestMessage
A request message to describe a request between the client and the server.
Every processed request must send a response back to the sender of the request. }
TRequestMessage = class(TAbstractMessage)
protected
fID: TOptionalAny; // integer | string
fMethod: string;
fParams: TLSPStreamable;
published
// The request id.
property id: TOptionalAny read fID write fID;
// The method to be invoked.
property method: string read fMethod write fMethod;
// The notification's params. Not freed when message is freed.
property params: TLSPStreamable read fParams write fParams;
end;
{ TNotificationMessage
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#notificationMessage
A notification message. A processed notification message
must not send a response back. They work like events. }
TNotificationMessage = class(TAbstractMessage)
protected
fMethod: string;
fParams: TLSPStreamable;
published
// The method to be invoked.
property method: string read fMethod write fMethod;
// The notification's params.
property params: TLSPStreamable read fParams write fParams;
end;
const
ContentType = 'application/vscode-jsonrpc; charset=utf-8';
implementation
{ TMessageTransport }
procedure TMessageTransport.DoLog(const Msg: UTF8String);
begin
If Assigned(OnLog) then
OnLog(Self,Msg);
end;
procedure TMessageTransport.DoLog(const Fmt: UTF8String;
const args: array of const);
begin
DoLog(Format(Fmt,Args));
end;
procedure TMessageTransport.SendMessage(aMessage: TJSONData);
begin
DoLog('Sending message: %s',[aMessage.AsJSON]);
DoSendMessage(aMessage);
end;
procedure TMessageTransport.SendDiagnostic(const aMessage: UTF8String);
begin
DoLog('Sending diagnostic: %s',[aMessage]);
DoSendDiagnostic(aMessage);
end;
procedure TMessageTransport.SendDiagnostic(const Fmt: String;
const args: array of const);
begin
SendDiagnostic(Format(Fmt,Args));
end;
{ TAbstractMessage }
function TAbstractMessage.GetJSONRPC: String;
begin
result := '2.0';
end;
procedure TAbstractMessage.Send(aTransport : TMessageTransport);
var
Data: TJSONData;
begin
Data := specialize
TLSPStreaming<TAbstractMessage>.ToJSON(self);
if Data <> nil then
begin
aTransport.SendMessage(Data);
Data.Free;
end;
end;
end.