-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepRenameHandler.cs
More file actions
470 lines (409 loc) · 24.6 KB
/
Copy pathStepRenameHandler.cs
File metadata and controls
470 lines (409 loc) · 24.6 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using Reqnroll.IdeSupport.Common.Logging;
using Reqnroll.IdeSupport.LSP.Core.Bindings;
using Reqnroll.IdeSupport.LSP.Core.Documents;
using Reqnroll.IdeSupport.LSP.Core.Matching;
using Reqnroll.IdeSupport.LSP.Core.Rename;
using Reqnroll.IdeSupport.LSP.Server.Discovery;
using Reqnroll.IdeSupport.LSP.Server.Features.TextSync;
using Reqnroll.IdeSupport.LSP.Server.Hosting;
using Reqnroll.IdeSupport.LSP.Server.Performance;
using Reqnroll.IdeSupport.LSP.Server.Protocol;
using Reqnroll.IdeSupport.LSP.Server.Protocol.Documents;
using Reqnroll.IdeSupport.LSP.Server.Registry;
using Reqnroll.IdeSupport.LSP.Server.Telemetry;
using Reqnroll.IdeSupport.LSP.Server.Workspace;
using LspRange = OmniSharp.Extensions.LanguageServer.Protocol.Models.Range;
namespace Reqnroll.IdeSupport.LSP.Server.Features.Rename;
/// <summary>
/// Handles <c>textDocument/prepareRename</c>, <c>textDocument/rename</c>, and
/// <c>reqnroll/selectRenameTarget</c> for the Step Rename refactoring feature.
/// <c>reqnroll/renameTargets</c> is handled separately by <see cref="RenameTargetsHandler"/>.
/// </summary>
public sealed class StepRenameHandler
{
private readonly IBindingMatchService _matchService;
private readonly ILspWorkspaceScopeManager _scopeManager;
private readonly IProjectBindingRegistryLookup _registryLookup;
private readonly IIdeSupportLogger _logger;
private readonly IDocumentBufferService _documentBuffer;
private readonly RenameSessionManager _sessionManager;
private readonly ICSharpFileTextCache _csharpFileTextCache;
private readonly ICSharpBindingDiscoveryService _csharpDiscoveryService;
private readonly ILanguageServerFacade _languageServer;
private readonly ClientIdeContext _clientIdeContext;
private readonly ILspTelemetryService? _telemetryService;
private readonly IOperationDurationRecorder _recorder;
private readonly CSharpAttributeLiteralResolver _attributeLiteralResolver;
private readonly RenameBindingResolver _bindingResolver;
private readonly NewNameReconciler _nameReconciler;
private readonly RenamePostApplyCoordinator _postApplyCoordinator;
/// <summary>Initializes a new instance of the <see cref="StepRenameHandler"/> class.</summary>
public StepRenameHandler(
IBindingMatchService matchService,
ILspWorkspaceScopeManager scopeManager,
IProjectBindingRegistryLookup registryLookup,
IIdeSupportLogger logger,
IDocumentBufferService documentBuffer,
ICSharpFileTextCache csharpFileTextCache,
ICSharpBindingDiscoveryService csharpDiscoveryService,
ILanguageServerFacade languageServer,
ClientIdeContext clientIdeContext,
ILspTelemetryService? telemetryService = null,
IOperationDurationRecorder? recorder = null)
{
_matchService = matchService;
_scopeManager = scopeManager;
_registryLookup = registryLookup;
_logger = logger;
_documentBuffer = documentBuffer;
_sessionManager = new RenameSessionManager();
_csharpFileTextCache = csharpFileTextCache;
_csharpDiscoveryService = csharpDiscoveryService;
_languageServer = languageServer;
_clientIdeContext = clientIdeContext;
_telemetryService = telemetryService;
_recorder = recorder ?? NullOperationDurationRecorder.Instance;
_attributeLiteralResolver = new CSharpAttributeLiteralResolver(csharpFileTextCache, documentBuffer, logger);
_bindingResolver = new RenameBindingResolver(matchService, scopeManager, _sessionManager, logger);
_nameReconciler = new NewNameReconciler(logger);
_postApplyCoordinator = new RenamePostApplyCoordinator(
languageServer, clientIdeContext, matchService, documentBuffer,
csharpDiscoveryService, csharpFileTextCache, logger);
}
// ── textDocument/prepareRename ──────────────────────────────────────────────
/// <summary>
/// Validates that the cursor is on a renameable binding. Returns the range of the
/// renameable text (attribute string or step text) — for <c>.feature</c> files, paired
/// with a <see cref="PlaceholderRange.Placeholder"/> carrying the binding's abstract
/// expression (e.g. <c>"the second number is {int}"</c>) instead of the concrete step
/// text — or <c>null</c> if rename is not available at this position.
/// </summary>
/// <remarks>
/// Seeding the rename box with the abstract expression, rather than the concrete text
/// literally in the buffer, is deliberate (issue #33 follow-up): a spec-compliant client
/// has no buffer text to anchor a partial in-place edit against when the placeholder
/// text doesn't appear in the document, so it can only submit the box's full edited
/// content as <c>newName</c> — turning an inherently ambiguous "did the user edit the
/// wording, the parameter value, or an arbitrary fragment?" problem into an unambiguous
/// one: <c>newName</c> is always the complete new abstract expression.
/// </remarks>
public async Task<RangeOrPlaceholderRange?> HandlePrepareRenameAsync(
PrepareRenameParams request,
CancellationToken cancellationToken)
{
var uri = request.TextDocument.Uri;
var path = uri.GetFileSystemPath();
// Performance Verification (Layer 4): time the prepareRename cursor-validation round-trip.
using var _perf = _recorder.Measure(LspMethodNames.TextDocumentPrepareRename, uri);
if (string.IsNullOrEmpty(path))
return null;
// Rule 1: validate cursor position (file type)
var posError = StepRenameValidator.ValidateCursorPosition((Uri)uri);
if (posError != null)
{
_logger.LogVerbose($"StepRenameHandler: prepareRename — {posError.Message}");
return null;
}
// Rule 7: validate project state via the registry lookup
var registry = _registryLookup.GetRegistryForUri(uri);
bool isInitialized = registry != ProjectBindingRegistry.Invalid;
bool hasFeatureFiles = false;
if (isInitialized)
{
var project = _scopeManager.GetProjectForUri(uri);
hasFeatureFiles = project != null &&
(_scopeManager.GetIndexedFeatureFiles(project).Count > 0
|| _scopeManager.ResolveOwners(uri).Count > 0);
}
var projError = StepRenameValidator.ValidateProjectState(isInitialized, hasFeatureFiles);
if (projError != null)
{
_logger.LogVerbose($"StepRenameHandler: prepareRename — {projError.Message}");
return null;
}
// For .cs files: check if the cursor resolves to a single binding
if (path.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
{
var line = request.Position.Line + 1;
var column = request.Position.Character + 1;
var bindingLocation = new SourceLocation(path, line, column);
if (registry == ProjectBindingRegistry.Invalid)
return null;
var binding = registry.FindBindingAtLocation(bindingLocation);
if (binding == null)
return null;
// Rule 2: validate expression is a string literal
var exprError = StepRenameValidator.ValidateExpressionIsStringLiteral(binding.Expression);
if (exprError != null)
{
_logger.LogVerbose($"StepRenameHandler: prepareRename — {exprError.Message}");
return null;
}
// Return the range of the string literal's INNER text only, excluding the
// surrounding quote characters. Returning the whole line/token (quotes included)
// seeds the client's rename box with the quotes; if the user leaves them untouched
// (a natural interaction — they look like part of the placeholder), `newName`
// arrives already quoted, and BuildCSharpEdit's unconditional `"` + text + `"`
// wrapping then doubles them, producing a stray trailing quote (issue #55).
var literal = await _attributeLiteralResolver.FindAttributeLiteralAsync(uri, binding);
if (literal == null)
{
_logger.LogVerbose("StepRenameHandler: prepareRename — could not resolve attribute literal for binding");
return null;
}
return CSharpAttributeLiteralResolver.GetLiteralInnerRange(literal);
}
// For .feature files: only offer rename when the cursor is on a step that is
// actually defined in the match cache. Returning null here tells VS Code
// "rename not available at this position" — same as prepareRename for a C# cursor
// not on a binding attribute — which suppresses the rename dialog cleanly.
// Without this check, prepareRename would succeed for undefined steps, and the
// subsequent textDocument/rename would fail with "Internal Error".
if (path.EndsWith(".feature", StringComparison.OrdinalIgnoreCase))
{
var featureBindings = _bindingResolver.FindBindingsAtFeatureStep(uri, path, request.Position, out var stepRange);
if (featureBindings.Count == 0)
{
_logger.LogVerbose("StepRenameHandler: prepareRename — no defined binding at feature step position");
return null;
}
if (stepRange == null)
{
// Should not happen alongside a non-empty featureBindings, but refuse rather
// than fall back to a whole-line range: that used to seed the dialog with the
// keyword/indentation, which then got duplicated when the resulting edit was
// applied at the step-text-only range HandleRenameAsync actually replaces.
_logger.LogVerbose("StepRenameHandler: prepareRename — matched a binding but could not resolve the step's text range");
return null;
}
// When ambiguous (2+ candidate bindings), a plain F2 rename would fall back to the
// first candidate anyway (see HandleRenameAsync's position-based fallback) — pick
// the same one here so the placeholder shown matches what would actually be renamed.
var matchedBinding = featureBindings[0];
var sourceLiteral = await _attributeLiteralResolver.FindAttributeLiteralAsync(uri, matchedBinding);
var sourceExpression = sourceLiteral?.Token.ValueText ?? matchedBinding.Expression ?? string.Empty;
// Known cosmetic quirk (confirmed live in VS and VS Code, issue #33 follow-up): when
// a user pre-selects a sub-span of the concrete step text before invoking F2 (e.g.
// "added" in "the two numbers are added"), the client computes that selection's
// offset relative to Range.Start and reapplies the same numeric offset into
// Placeholder to decide what to pre-highlight in the rename box. Placeholder is a
// different string than the concrete text whenever a parameter's rendered width
// differs from its abstract token (here "are" → "{Verb}", +3 chars), so everything
// after the parameter shifts and the pre-highlighted substring lands a few characters
// off from what the user actually selected (e.g. "b} summed" instead of "summed").
// This is inherent to the offset math being reused across two different-length
// strings — the LSP PrepareRenameResult protocol has no field to specify which
// sub-span of Placeholder to highlight independently of Range — and is harmless: the
// box's full content is still correct, and whatever the user submits becomes newName
// in full regardless of what was pre-highlighted.
return new PlaceholderRange
{
Range = stepRange,
Placeholder = sourceExpression
};
}
return null;
}
// ── textDocument/rename ────────────────────────────────────────────────────
/// <summary>
/// Executes the rename. Validates the new name, resolves all feature step locations,
/// resolves the C# attribute string range, and returns a WorkspaceEdit covering all files.
/// </summary>
public async Task<WorkspaceEdit?> HandleRenameAsync(
RenameParams request,
CancellationToken cancellationToken)
{
var uri = request.TextDocument.Uri;
var path = uri.GetFileSystemPath();
var newName = request.NewName;
// Performance Verification (Layer 4): time the full rename — the highest-blast-radius,
// most complex operation in the server (workspace-wide applyEdit).
using var _perf = _recorder.Measure(LspMethodNames.TextDocumentRename, uri);
if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(newName))
return null;
_logger.LogVerbose($"StepRenameHandler: rename at {path}, newName='{newName}'");
// ── 1. Resolve binding ─────────────────────────────────────────────────
var line = request.Position.Line + 1;
var column = request.Position.Character + 1;
var registry = _registryLookup.GetRegistryForUri(uri);
if (registry == ProjectBindingRegistry.Invalid)
{
_logger.LogVerbose("StepRenameHandler: registry is invalid");
return null;
}
// Resolves a pending reqnroll/selectRenameTarget session first (multi-attribute picker
// flow), then falls back to feature-match-cache or registry position lookup — see
// RenameBindingResolver.ResolveBindingForRename for the full precedence order.
var binding = _bindingResolver.ResolveBindingForRename(uri, path, request.Position, registry);
if (binding == null)
return null;
SourceLocation bindingLocation;
// Use the binding's C# source location for FindUsages so we can find
// feature steps that reference this binding. For .feature-originated
// renames, the request path is the .feature file, not the .cs file.
if (!path.EndsWith(".cs", StringComparison.OrdinalIgnoreCase) &&
binding.Implementation?.SourceLocation?.SourceFile != null)
{
bindingLocation = new SourceLocation(
binding.Implementation.SourceLocation.SourceFile,
binding.Implementation.SourceLocation.SourceFileLine,
binding.Implementation.SourceLocation.SourceFileColumn);
_logger.LogVerbose($"StepRenameHandler: using binding source location for FindUsages: {bindingLocation}");
}
else
{
bindingLocation = new SourceLocation(path, line, column);
}
var expression = binding.Expression ?? string.Empty;
// ── 2. Resolve feature step locations ──────────────────────────────────
var owners = _scopeManager.ResolveOwners(uri);
var projectFilter = owners.Count > 0
? owners.Select(p => new ProjectOwner(p.ProjectFullName, p.TargetFrameworkMoniker)).ToArray()
: null;
var usages = _matchService.FindUsages(bindingLocation, projectFilter);
// Resolve the live source expression once (preserves the original parameter syntax).
// For a .cs-invoked rename this is the attribute string literal; otherwise it falls back
// to the registry expression. It anchors both the feature edits (static-segment
// substitution) and the C# attribute edit.
var sourceLiteral = await _attributeLiteralResolver.FindAttributeLiteralAsync(uri, binding);
var sourceExpression = sourceLiteral?.Token.ValueText ?? expression;
// Reconciles concrete step text (VS Code's native F2) against the binding's abstract
// expression (VS's "Rename Step" command) — see NewNameReconciler.Reconcile for the full
// rationale. Null means the edited text couldn't be reconciled with the binding's
// parameter positions; the rename is rejected.
var effectiveNewName = _nameReconciler.Reconcile(
path, uri, request.Position, usages, sourceExpression, newName, ReadStepText);
if (effectiveNewName == null)
return null;
// ── 3. Validate new name ───────────────────────────────────────────────
var nameError = StepRenameValidator.ValidateNewName(expression, effectiveNewName);
if (nameError != null)
{
_logger.LogVerbose($"StepRenameHandler: validation failed — {nameError.Message}");
return null;
}
// Change-annotation negotiation (issue #70): a client that advertises both
// `documentChanges` and `changeAnnotationSupport` gets a grouped, labelled rename
// preview; everyone else (VS, per Phase 0's capability survey — see
// docs/Rename-ChangeAnnotations-Implementation-Plan.md) gets the legacy `Changes`
// shape, byte-identical to before this feature existed.
var workspaceEditCapability = _languageServer.ClientSettings?.Capabilities?.Workspace?.WorkspaceEdit;
var supportsChangeAnnotations = workspaceEditCapability is not null &&
workspaceEditCapability.Value.IsSupported &&
workspaceEditCapability.Value.Value?.DocumentChanges == true &&
workspaceEditCapability.Value.Value?.ChangeAnnotationSupport is not null;
// A rename that touches more than one .feature file crosses file boundaries the user may
// not have anticipated from a single step's rename prompt — ask the client to confirm
// before applying, if it renders that confirmation (see WorkspaceEditBuilder's shape
// negotiation; unsupported clients never see this flag).
var featureFileCount = usages.Select(u => u.FeatureDocumentId).Distinct().Count();
var builder = new WorkspaceEditBuilder(supportsChangeAnnotations);
builder.DeclareAnnotation(RenameChangeAnnotations.Feature,
new ChangeAnnotation
{
Label = $"Rename step usages → \"{effectiveNewName}\"",
NeedsConfirmation = featureFileCount > 1
});
builder.DeclareAnnotation(RenameChangeAnnotations.Binding,
new ChangeAnnotation { Label = "Update step-definition attribute" });
// ── 4. Build .feature file edits ───────────────────────────────────────
foreach (var usage in usages)
{
var featureUri = DocumentUri.Parse(usage.FeatureDocumentId);
// Read the feature step text to preserve parameter values / placeholders
string? stepText = null;
if (usage.Range != null)
{
var stepRange = usage.Range.ToLspRange();
stepText = ReadStepText(featureUri, stepRange);
}
var featureNewText = FeatureStepTextBuilder.Build(effectiveNewName, sourceExpression, binding.Regex, stepText);
builder.Add(featureUri, usage.Range!.ToLspRange(), featureNewText, RenameChangeAnnotations.Feature);
}
// ── 5. Build .cs file edit ────────────────────────────────────────────
DocumentUri? csFileUri = null;
string? newCsText = null;
if (sourceLiteral != null)
{
var csEdit = _attributeLiteralResolver.BuildEdit(sourceLiteral, effectiveNewName);
if (csEdit != null)
{
csFileUri = path.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)
? uri
: DocumentUri.FromFileSystemPath(binding.Implementation!.SourceLocation!.SourceFile);
builder.Add(csFileUri, csEdit.Range, csEdit.NewText, RenameChangeAnnotations.Binding);
// Computed directly from the same Roslyn span BuildCSharpEdit used, so this is
// exact regardless of whether the .cs file is open or closed in the editor.
var sourceText = await sourceLiteral.SyntaxTree!.GetTextAsync(cancellationToken);
newCsText = sourceText
.WithChanges(new Microsoft.CodeAnalysis.Text.TextChange(sourceLiteral.Token.Span, csEdit.NewText))
.ToString();
}
}
if (builder.IsEmpty)
return null;
var workspaceEdit = builder.Build();
// The push is awaited and its Applied flag checked *before* touching any server-side
// cache below: if VS rejects or fails to apply the edit (e.g. a locked/read-only file, or
// the user having closed the document with unsaved conflicting changes), the actual
// buffer/file content never changed, so self-refreshing the registry or invalidating the
// match cache here would desync server state from reality — the registry would claim the
// rename succeeded while the source still has the old text. See
// RenamePostApplyCoordinator.PushEditIfVisualStudioAsync for why VS needs this push at all.
if (!await _postApplyCoordinator.PushEditIfVisualStudioAsync(builder, cancellationToken))
return null;
_postApplyCoordinator.InvalidateClosedFeatureCaches(builder);
await _postApplyCoordinator.RefreshCSharpRegistryAsync(csFileUri, newCsText, cancellationToken);
// Telemetry
_telemetryService?.SendEvent("Rename step command executed", new()
{
["Erroneous"] = false,
["ChangeAnnotationsUsed"] = supportsChangeAnnotations,
["EditedFileCount"] = builder.TouchedUris.Count,
});
return workspaceEdit;
}
// ── Custom request handlers ─────────────────────────────────────────────────
/// <summary>
/// Handles <c>reqnroll/selectRenameTarget</c> — stores the selected attribute
/// for the next <c>textDocument/rename</c> call.
/// </summary>
public Task HandleSelectRenameTargetAsync(
SelectRenameTargetParams request,
CancellationToken cancellationToken)
{
using var _perf = _recorder.Measure(LspMethodNames.ReqnrollSelectRenameTarget, request.Uri);
_sessionManager.SetSession(request.Uri.ToString(), request.Version, request.AttributeIndex);
return Task.CompletedTask;
}
// ── Helpers ────────────────────────────────────────────────────────────────
// ── Feature step text parameter preservation ─────────────────────────────
/// <summary>
/// Reads the step text from a feature file at the given range, using the
/// document buffer if available (open file) or reading from disk.
/// </summary>
private string? ReadStepText(DocumentUri featureUri, LspRange range)
{
string? fileText = null;
if (_documentBuffer.TryGet(featureUri, out var buffer) && buffer?.Text != null)
fileText = buffer.Text;
else
{
var path = featureUri.GetFileSystemPath();
if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
fileText = System.IO.File.ReadAllText(path);
}
if (fileText == null)
return null;
var lines = fileText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
if (range.Start.Line < 0 || range.Start.Line >= lines.Length)
return null;
var line = lines[range.Start.Line];
var start = Math.Min(range.Start.Character, line.Length);
var end = Math.Min(range.End.Character, line.Length);
return start < end ? line.Substring(start, end - start) : null;
}
}