-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPasLS.Synchronization.pas
More file actions
183 lines (138 loc) · 5.27 KB
/
PasLS.Synchronization.pas
File metadata and controls
183 lines (138 loc) · 5.27 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Pascal Language Server
// Copyright 2020 Arjan Adriaanse
// 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 PasLS.Synchronization;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, DateUtils,
CodeToolManager, CodeCache,
LSP.BaseTypes, LSP.Base, LSP.Basic, PasLS.Symbols, LSP.Synchronization;
Type
{ TDidOpenTextDocument }
TDidOpenTextDocument = class(specialize TLSPNotification<TDidOpenTextDocumentParams>)
procedure Process(var Params : TDidOpenTextDocumentParams); override;
end;
{ TDidSaveTextDocument }
TDidSaveTextDocument = class(specialize TLSPNotification<TDidSaveTextDocumentParams>)
procedure Process(var Params : TDidSaveTextDocumentParams); override;
end;
{ TDidCloseTextDocument }
TDidCloseTextDocument = class(specialize TLSPNotification<TDidCloseTextDocumentParams>)
procedure Process(var Params : TDidCloseTextDocumentParams); override;
end;
{ TDidChangeTextDocument }
TDidChangeTextDocument = class(specialize TLSPNotification<TDidChangeTextDocumentParams>)
procedure Process(var Params : TDidChangeTextDocumentParams); override;
end;
implementation
uses PasLS.CheckInactiveRegions, PasLS.Diagnostics;
{ TDidChangeTextDocument }
procedure TDidChangeTextDocument.Process(var Params : TDidChangeTextDocumentParams);
var
Code: TCodeBuffer;
Change: TCollectionItem;
{ Range: TRange;
StartPos, EndPos: integer;}
begin with Params do
begin
Code := CodeToolBoss.FindFile(textDocument.LocalPath);
for Change in contentChanges do
begin
// note(ryan): can't get this working yet
// and I'm not even sure if it's worth it
{
Range := TTextDocumentContentChangeEvent(Change).range;
if Range <> nil then
begin
//Code.LineColToPosition(Range.start.line + 1, Range.start.character + 1, StartPos);
//Code.LineColToPosition(Range.&end.line + 1, Range.&end.character + 1, EndPos);
DoLog('insert: %d -> %d text=%s',[StartPos,EndPos, TTextDocumentContentChangeEvent(Change).text]);
//Code.Replace(StartPos, EndPos - StartPos, TTextDocumentContentChangeEvent(Change).text);
end
else }
Code.Source := TTextDocumentContentChangeEvent(Change).text;
// Ryan, uncomment this to have a syntax check at
// CheckSyntax(Self.Transport,Code);
if SymbolManager <> nil then
SymbolManager.FileModified(Code);
end;
// DoLog( 'Synched text in %d ms',[MilliSecondsBetween(Now, StartTime)]);
end;
end;
{ TDidCloseTextDocument }
procedure TDidCloseTextDocument.Process(var Params : TDidCloseTextDocumentParams);
var
FileName, FullPath: String;
begin
if SymbolManager = nil then
Exit;
FullPath := Params.textDocument.LocalPath;
FileName := ExtractFileName(FullPath);
// Distinguish between workspace files and external files
if SymbolManager.IsFileInWorkspace(FullPath) then
SymbolManager.UnloadFile(FileName) // Keep database symbols
else
SymbolManager.RemoveFile(FileName); // Remove database symbols
end;
{ TDidSaveTextDocument }
procedure TDidSaveTextDocument.Process(var Params : TDidSaveTextDocumentParams);
var
Code: TCodeBuffer;
FullPath: String;
begin
FullPath := Params.textDocument.LocalPath;
Code := CodeToolBoss.FindFile(FullPath);
if Code = nil then
Exit;
if SymbolManager <> nil then
begin
// Always mark as modified
SymbolManager.FileModified(Code);
// Proactively update database for workspace files
if SymbolManager.IsFileInWorkspace(FullPath) then
SymbolManager.Reload(Code, True); // Force reload and DB update
end;
DiagnosticsHandler.CheckSyntax(Transport, Code);
CheckInactiveRegions(Transport, Code, Params.textDocument.uri);
end;
{ TDidOpenTextDocument }
procedure TDidOpenTextDocument.Process(var Params : TDidOpenTextDocumentParams);
var
Path: String;
Code: TCodeBuffer;
begin
with Params do
begin
Path := textDocument.LocalPath;
Code := CodeToolBoss.FindFile(Path);
// the file was not found in search paths so
// it need to be loaded from disk
if Code = nil then
Code := CodeToolBoss.LoadFile(Path, False, False);
//rename file will failue ,need CreateFile ,It not create file in disk.(s)
if Code = nil then
begin
Code :=CodeToolBoss.CreateFile(Path);
end;
//allways update the source from the opened document
Code.Source:=textDocument.text;
DiagnosticsHandler.CheckSyntax(Transport,Code);
CheckInactiveRegions(Transport, Code, textDocument.uri);
if SymbolManager <> nil then
SymbolManager.Reload(Code, True);
end;
end;
end.