forked from genericptr/pascal-language-server
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrename.pas
230 lines (184 loc) · 6.51 KB
/
rename.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
// Pascal Language Server
// 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 rename;
{$mode objfpc}{$H+}
interface
uses
Classes, URIParser, CodeToolManager, CodeCache, BasicCodeTools,
lsp, basic,fpjson,fpjsonrpc,avl_tree, CTUnitGraph ,settings,
{ LazUtils }
LazFileUtils, Laz_AVL_Tree;
type
{ TRename }
TRename = class(specialize TLSPRequest<TRenameParams, TWorkspaceEdit>)
function DoExecute(const Params: TJSONData; AContext: TJSONRPCCallContext): TJSONData; override;
//function Process(var Params: TRenameParams): TWorkspaceEdit; override;
end;
implementation
uses
diagnostics;
function FindReferences(Filename, MainFilename: String; X, Y: Integer;out Identifier:string):TAVLTree;
var
DeclCode, StartSrcCode, Code: TCodeBuffer;
ListOfPCodeXYPosition: TFPList;
DeclX, DeclY, DeclTopLine, i: Integer;
CurLine: string;
Graph: TUsesGraph;
Cache: TFindIdentifierReferenceCache;
ANode, Node: TAVLTreeNode;
CodePos: PCodeXYPosition;
Files: TStringList;
Completed: boolean;
UGUnit: TUGUnit;
begin
// Step 1: load the file
StartSrcCode:=CodeToolBoss.LoadFile(Filename,false,false);
// Step 2: find the main declaration
if not CodeToolBoss.FindMainDeclaration(StartSrcCode,
X,Y,
DeclCode,DeclX,DeclY,DeclTopLine) then
begin
//PublishDiagnostic('FindMainDeclaration failed in '+StartSrcCode.FileName+' at '+IntToStr(Y)+':'+IntToStr(X));
ExitCode:=-1;
exit;
end;
// Step 3: get identifier
CodeToolBoss.GetIdentifierAt(DeclCode,DeclX,DeclY,Identifier);
writeln(StdErr, 'Found identifier: ',Identifier);
// Step 4: collect all modules of program
Files:=TStringList.Create;
ListOfPCodeXYPosition:=nil;
Result:=nil;
Cache:=nil;
try
Files.Add(DeclCode.Filename);
if CompareFilenames(DeclCode.Filename,StartSrcCode.Filename)<>0 then
Files.Add(DeclCode.Filename);
// parse all used units
Graph:=CodeToolBoss.CreateUsesGraph;
try
Graph.AddStartUnit(MainFilename);
Graph.AddTargetUnit(DeclCode.Filename);
Graph.Parse(true,Completed);
Node:=Graph.FilesTree.FindLowest;
while Node<>nil do begin
UGUnit:=TUGUnit(Node.Data);
Files.Add(UGUnit.Filename);
Node:=Node.Successor;
end;
finally
Graph.Free;
end;
// Step 5: find references in all files
for i:=0 to Files.Count-1 do begin
writeln(Stderr, 'Searching ', Files[i], '...');
Code:=CodeToolBoss.LoadFile(Files[i],true,false);
if Code=nil then begin
writeln(stderr, 'unable to load "',Files[i],'"');
continue;
end;
// search references
CodeToolBoss.FreeListOfPCodeXYPosition(ListOfPCodeXYPosition);
if not CodeToolBoss.FindReferences(
DeclCode,DeclX,DeclY,
Code, true, ListOfPCodeXYPosition, Cache) then
begin
PublishDiagnostic('FindReferences failed in "'+Code.Filename+'"');
continue;
end;
if ListOfPCodeXYPosition=nil then continue;
// In order to show all references after any parser error, they are
// collected in a tree
if Result=nil then
Result:=CodeToolBoss.CreateTreeOfPCodeXYPosition;
CodeToolBoss.AddListToTreeOfPCodeXYPosition(ListOfPCodeXYPosition,
Result,true,false);
end;
finally
Files.Free;
CodeToolBoss.FreeListOfPCodeXYPosition(ListOfPCodeXYPosition);
Cache.Free;
Flush(stderr);
end;
end;
//todo: use documentChanges to support needsConfirmation
function TRename.DoExecute(const Params: TJSONData;
AContext: TJSONRPCCallContext): TJSONData;
var
Input: TRenameParams;
Path,Identifier: String;
we:TWorkspaceEdit;
edit:TTextEdit;
items:TChangeItems;
X, Y: Integer;
av_tree:TAVLTree;
code:TCodeBuffer;
pos:TPoint;
ANode, Node: TAVLTreeNode;
CodePos: PCodeXYPosition;
begin
Input := specialize TLSPStreaming<TRenameParams>.ToObject(Params);
we:=TWorkspaceEdit.Create;
try
Path := UriToFilenameEx(input.textDocument.uri);
X := input.position.character;
Y := input.position.line;
Code := CodeToolBoss.FindFile(Path);
// if the main program file was provided via initializationOptions -> program
// then use this unit as the root for searching, otherwise default to the
// current text document
if ServerSettings.&program <> '' then
av_tree:=FindReferences(Path, ServerSettings.&program, X + 1, Y + 1,identifier)
else
av_tree:=FindReferences(Path, Path, X + 1, Y + 1,identifier);
if not assigned(av_tree) then exit;
ANode:=av_tree.FindHighest;
while ANode<>nil do begin
CodePos:=PCodeXYPosition(ANode.Data);
path:=PathToURI(CodePos^.Code.Filename);
if not we.changes.TryGetData(path,items) then
begin
items:=TChangeItems.Create;
we.changes.Add(path,items);
end;
edit:=items.Add;
edit.range.start.line:=CodePos^.Y - 1;
edit.range.start.character:=CodePos^.X - 1;
edit.range.&end.line:=CodePos^.Y - 1;
edit.range.&end.character:=CodePos^.X-1 +length(identifier);
edit.newText:=input.newName;
//writeln(StdErr, ' Found: ', CodePos^.Code.Filename, ' @ ', CodePos^.Y, ',',CodePos^.X);
ANode:=av_tree.FindPrecessor(ANode);
end;
finally
Result:=we.ToJson();
we.Free;
Input.Free;
end;
end;
// function TRename.Process(var Params: TRenameParams): TWorkspaceEdit;
// var edit:TTextEdit;
// begin
// edit:=TTextEdit.Create(Nil);
// result:=TWorkspaceEdit.Create;
// edit.range:=TRange.Create(Params.position.line,Params.position.character);
// edit.newText:='test';
// Result.changes:= TChangeDictory.Create;
// Result.changes.Add(Params.textDocument.uri,edit);
// edit:=Result.changes.values.ToArray[0];
// end;
initialization
LSPHandlerManager.RegisterHandler('textDocument/rename', TRename);
end.