-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsynchronization.pas
140 lines (113 loc) · 4.36 KB
/
synchronization.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
// 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/>.
unit synchronization;
{$mode objfpc}{$H+}
interface
uses
Classes, URIParser, CodeToolManager, CodeCache,
lsp, basic;
type
{ TDidOpenTextDocumentParams }
TDidOpenTextDocumentParams = class(TPersistent)
private
fTextDocument: TTextDocumentItem;
published
// The document that was opened.
property textDocument: TTextDocumentItem read fTextDocument write fTextDocument;
end;
{ TDidOpenTextDocument }
TDidOpenTextDocument = class(specialize TLSPNotification<TDidOpenTextDocumentParams>)
procedure Process(var Params : TDidOpenTextDocumentParams); override;
end;
{ TTextDocumentContentChangeEvent }
// An event describing a change to a text document. If range and
// rangeLength are omitted the new text is considered to be the full
// content of the document.
TTextDocumentContentChangeEvent = class(TCollectionItem)
private
fText: string;
published
// The new text of the whole document.
property text: string read fText write fText;
end;
{ TDidChangeTextDocumentParams }
TDidChangeTextDocumentParams = class(TPersistent)
private
fTextDocument: TVersionedTextDocumentIdentifier;
fContentChanges: TCollection;
public
constructor Create;
published
// The document that did change. The version number points to the
// version after all provided content changes have been applied.
property textDocument: TVersionedTextDocumentIdentifier read fTextDocument write fTextDocument;
// The actual content changes. The content changes describe single
// state changes to the document. So if there are two content
// changes c1 (at array index 0) and c2 (at array index 1) for a
// document in state S then c1 moves the document from S to S' and
// c2 from S' to S''. So c1 is computed on the state S and c2 is
// computed on the state S'.
//
// To mirror the content of a document using change events use the
// following approach:
// - start with the same initial content
// - apply the 'textDocument/didChange' notifications in the order
// you recevie them.
// - apply the `TextDocumentContentChangeEvent`s in a single
// notification in the order you receive them.
property contentChanges: TCollection read fContentChanges write fContentChanges;
end;
{ TDidChangeTextDocument }
TDidChangeTextDocument = class(specialize TLSPNotification<TDidChangeTextDocumentParams>)
procedure Process(var Params : TDidChangeTextDocumentParams); override;
end;
implementation
{ TDidChangeTextDocumentParams }
constructor TDidChangeTextDocumentParams.Create;
begin
contentChanges := TCollection.Create(TTextDocumentContentChangeEvent);
end;
{ TDidOpenTextDocument }
procedure TDidOpenTextDocument.Process(var Params : TDidOpenTextDocumentParams);
var
URI: TURI;
Code: TCodeBuffer;
begin with Params do
begin
URI := ParseURI(textDocument.uri);
Code := CodeToolBoss.LoadFile(URI.Path + URI.Document, false, false);
Code.Source := textDocument.text;
end;
end;
{ TDidChangeTextDocument }
procedure TDidChangeTextDocument.Process(var Params : TDidChangeTextDocumentParams);
var
URI: TURI;
Code: TCodeBuffer;
Change: TCollectionItem;
begin with Params do
begin
URI := ParseURI(textDocument.uri);
Code := CodeToolBoss.FindFile(URI.Path + URI.Document);
for Change in contentChanges do
begin
Code.Source := TTextDocumentContentChangeEvent(Change).text;
end;
end;
end;
initialization
LSPHandlerManager.RegisterHandler('textDocument/didOpen', TDidOpenTextDocument);
LSPHandlerManager.RegisterHandler('textDocument/didChange', TDidChangeTextDocument);
end.