-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPasLS.Rename.pas
More file actions
265 lines (215 loc) · 8.27 KB
/
PasLS.Rename.pas
File metadata and controls
265 lines (215 loc) · 8.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Pascal Language Server
// Copyright 2026 Simon Hsu
// 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.Rename;
{$mode objfpc}{$H+}
interface
uses
{ RTL }
SysUtils, Classes,
{ Code Tools }
CodeToolManager, CodeCache, CTUnitGraph, BasicCodeTools,
{ LazUtils }
LazFileUtils, AVL_Tree,
{ Protocol }
LSP.BaseTypes, LSP.Base, LSP.Basic, LSP.Rename;
type
{ TRenameRequest }
{ The rename request is sent from the client to the server to perform
a workspace-wide rename of a symbol. }
TRenameRequest = class(specialize TLSPRequest<TRenameParams, TWorkspaceEdit>)
public
function Process(var Params: TRenameParams): TWorkspaceEdit; override;
end;
{ TPrepareRenameRequest }
{ The prepare rename request is sent from the client to the server to
setup and test the validity of a rename operation at a given location. }
TPrepareRenameRequest = class(specialize TLSPRequest<TPrepareRenameParams, TPrepareRenameResult>)
public
function Process(var Params: TPrepareRenameParams): TPrepareRenameResult; override;
end;
implementation
uses
PasLS.Settings, PasLS.Diagnostics, PasLS.CodeUtils;
{ TRenameRequest }
function TRenameRequest.Process(var Params: TRenameParams): TWorkspaceEdit;
var
Path, Identifier, MainFilename: String;
X, Y: Integer;
DeclCode, StartSrcCode: TCodeBuffer;
DeclX, DeclY, DeclTopLine: Integer;
DeclCaretXY: TPoint;
TreeOfPCodeXYPosition: TAVLTree;
Files: TStringList;
ANode, Node: TAVLTreeNode;
CodePos: PCodeXYPosition;
CurrentURI: String;
TextDocumentEdit: TTextDocumentEdit;
TextEdit: TTextEdit;
Graph: TUsesGraph;
Completed: Boolean;
UGUnit: TUGUnit;
begin
Result := nil;
with Params do
begin
Path := textDocument.LocalPath;
X := position.character + 1; // Convert to 1-based
Y := position.line + 1; // Convert to 1-based
// Step 1: Load the file
StartSrcCode := CodeToolBoss.LoadFile(Path, false, false);
if StartSrcCode = nil then
begin
DoLog('Unable to load file: %s', [Path]);
exit;
end;
// Step 2: Find the main declaration
if not CodeToolBoss.FindMainDeclaration(StartSrcCode, X, Y,
DeclCode, DeclX, DeclY, DeclTopLine) then
begin
PublishCodeToolsError(Transport, 'FindMainDeclaration failed in ' + StartSrcCode.FileName + ' at ' + IntToStr(Y) + ':' + IntToStr(X));
exit;
end;
// Step 3: Get the identifier at the declaration
CodeToolBoss.GetIdentifierAt(DeclCode, DeclX, DeclY, Identifier);
if Identifier = '' then
begin
DoLog('No identifier found at position');
exit;
end;
DoLog('Found identifier: %s', [Identifier]);
// Step 4: Collect all modules of program
Files := TStringList.Create;
TreeOfPCodeXYPosition := nil;
try
Files.Add(DeclCode.Filename);
if CompareFilenames(DeclCode.Filename, StartSrcCode.Filename) <> 0 then
Files.Add(StartSrcCode.Filename);
// Determine main filename
if ServerSettings.&program <> '' then
MainFilename := ServerSettings.&program
else
MainFilename := Path;
// Collect project units and pre-load them before building the uses graph
GetProjectUnits(MainFilename, Files, False, Transport);
// Step 5: Find references in all files using FindReferencesInFiles
DoLog('Searching references in %d files...', [Files.Count]);
DeclCaretXY.X := DeclX;
DeclCaretXY.Y := DeclY;
if not CodeToolBoss.FindReferencesInFiles(Files, DeclCode, DeclCaretXY,
False, TreeOfPCodeXYPosition) then
begin
PublishCodeToolsError(Transport, 'FindReferencesInFiles failed');
exit;
end;
// Step 6: Check if any references found
if (TreeOfPCodeXYPosition = nil) or (TreeOfPCodeXYPosition.Count = 0) then
begin
DoLog('No references found for identifier: %s', [Identifier]);
exit;
end;
DoLog('Found %d reference(s)', [TreeOfPCodeXYPosition.Count]);
// 6. Create workspace edit with all changes
Result := TWorkspaceEdit.Create;
CurrentURI := '';
TextDocumentEdit := nil;
// Process all references from lowest to highest
ANode := TAVLTreeNode(TreeOfPCodeXYPosition.FindLowest);
while ANode <> nil do
begin
CodePos := PCodeXYPosition(ANode.Data);
// Convert file path to URI
Path := PathToURI(CodePos^.Code.Filename);
// Create new TextDocumentEdit for each different file
if Path <> CurrentURI then
begin
CurrentURI := Path;
TextDocumentEdit := Result.documentChanges.Add;
TextDocumentEdit.textDocument.uri := CurrentURI;
if ClientInfo.name = TClients.SublimeTextLSP then
TextDocumentEdit.textDocument.version := nil
else
TextDocumentEdit.textDocument.version := 0;
end;
// Create text edit for this reference
TextEdit := TextDocumentEdit.edits.Add;
TextEdit.range.start.line := CodePos^.Y - 1; // Convert to 0-based
TextEdit.range.start.character := CodePos^.X - 1; // Convert to 0-based
TextEdit.range.&end.line := CodePos^.Y - 1;
TextEdit.range.&end.character := CodePos^.X - 1 + Length(Identifier);
TextEdit.newText := newName;
DoLog('Rename at: %s @ %d,%d', [CodePos^.Code.Filename, CodePos^.Y, CodePos^.X]);
ANode := TAVLTreeNode(TreeOfPCodeXYPosition.FindSuccessor(ANode));
end;
finally
CodeToolBoss.FreeTreeOfPCodeXYPosition(TreeOfPCodeXYPosition);
Files.Free;
end;
end;
end;
{ TPrepareRenameRequest }
function TPrepareRenameRequest.Process(var Params: TPrepareRenameParams): TPrepareRenameResult;
var
Code: TCodeBuffer;
X, Y: Integer;
DeclCode: TCodeBuffer;
DeclX, DeclY, DeclTopLine: Integer;
Identifier: string;
begin
Result := nil;
with Params do
begin
Code := CodeToolBoss.FindFile(textDocument.LocalPath);
if Code = nil then
Code := CodeToolBoss.LoadFile(textDocument.LocalPath, false, false);
if Code = nil then
begin
DoLog('Cannot prepare rename: file not found');
exit;
end;
X := position.character + 1; // Convert to 1-based
Y := position.line + 1; // Convert to 1-based
try
// Find the main declaration to verify this is a valid identifier
if not CodeToolBoss.FindMainDeclaration(Code, X, Y,
DeclCode, DeclX, DeclY, DeclTopLine) then
begin
DoLog('Cannot prepare rename: no declaration found');
exit;
end;
// Get the identifier name at the declaration
CodeToolBoss.GetIdentifierAt(DeclCode, DeclX, DeclY, Identifier);
if Identifier = '' then
begin
DoLog('Cannot prepare rename: no identifier at position');
exit;
end;
// Create result with range and placeholder (0-based coordinates for LSP)
Result := TPrepareRenameResult.Create;
Result.range.start.line := position.line;
Result.range.start.character := position.character;
Result.range.&end.line := position.line;
Result.range.&end.character := position.character + Length(Identifier);
Result.placeholder := Identifier;
DoLog('Prepare rename for: %s', [Identifier]);
except
on E: Exception do
begin
LogError('PrepareRename Error', E);
FreeAndNil(Result);
end;
end;
end;
end;
end.