Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update inline rename code following completion of TypeScript external access work #43634

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Options;
Expand Down Expand Up @@ -33,11 +34,13 @@ public FailureInlineRenameInfo(string localizedErrorMessage)

public Glyph Glyph => Glyph.None;

public ImmutableArray<DocumentSpan> DefinitionLocations => default;

public string GetFinalSymbolName(string replacementText) => null;

public TextSpan GetReferenceEditSpan(InlineRenameLocation location, CancellationToken cancellationToken) => default;
public TextSpan GetReferenceEditSpan(InlineRenameLocation location, string triggerText, CancellationToken cancellationToken) => default;

public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string replacementText, CancellationToken cancellationToken) => null;
public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string triggerText, string replacementText, CancellationToken cancellationToken) => null;

public Task<IInlineRenameLocationSet> FindRenameLocationsAsync(OptionSet optionSet, CancellationToken cancellationToken) => null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Rename;
Expand Down Expand Up @@ -60,6 +59,7 @@ public SymbolInlineRenameInfo(
IEnumerable<IRefactorNotifyService> refactorNotifyServices,
Document document,
TextSpan triggerSpan,
string triggerText,
ISymbol renameSymbol,
bool forceRenameOverloads,
ImmutableArray<DocumentSpan> definitionLocations,
Expand All @@ -74,13 +74,13 @@ public SymbolInlineRenameInfo(
this.HasOverloads = RenameLocations.GetOverloadedSymbols(this.RenameSymbol).Any();
this.ForceRenameOverloads = forceRenameOverloads;

_isRenamingAttributePrefix = CanRenameAttributePrefix(document, triggerSpan, cancellationToken);
this.TriggerSpan = GetReferenceEditSpan(new InlineRenameLocation(document, triggerSpan), cancellationToken);
_isRenamingAttributePrefix = CanRenameAttributePrefix(document, triggerSpan, triggerText, cancellationToken);
this.TriggerSpan = GetReferenceEditSpan(new InlineRenameLocation(document, triggerSpan), triggerText, cancellationToken);

this.DefinitionLocations = definitionLocations;
}

private bool CanRenameAttributePrefix(Document document, TextSpan triggerSpan, CancellationToken cancellationToken)
private bool CanRenameAttributePrefix(Document document, TextSpan triggerSpan, string triggerText, CancellationToken cancellationToken)
{
// if this isn't an attribute, or it doesn't have the 'Attribute' suffix, then clearly
// we can't rename just the attribute prefix.
Expand All @@ -94,7 +94,6 @@ private bool CanRenameAttributePrefix(Document document, TextSpan triggerSpan, C
// we need to rename the entire attribute).
var nameWithoutAttribute = GetWithoutAttributeSuffix(this.RenameSymbol.Name);

var triggerText = GetSpanText(document, triggerSpan, cancellationToken);
return triggerText.StartsWith(triggerText); // TODO: Always true? What was it supposed to do?
}

Expand All @@ -111,7 +110,7 @@ private bool CanRenameAttributePrefix(Document document, TextSpan triggerSpan, C
/// - Compiler-generated EventHandler suffix XEventHandler => X
/// - Compiler-generated get_ and set_ prefixes get_X => X
/// </summary>
public TextSpan GetReferenceEditSpan(InlineRenameLocation location, CancellationToken cancellationToken)
public TextSpan GetReferenceEditSpan(InlineRenameLocation location, string triggerText, CancellationToken cancellationToken)
{
var searchName = this.RenameSymbol.Name;
if (_isRenamingAttributePrefix)
Expand All @@ -121,9 +120,7 @@ public TextSpan GetReferenceEditSpan(InlineRenameLocation location, Cancellation
searchName = GetWithoutAttributeSuffix(this.RenameSymbol.Name);
}

var spanText = GetSpanText(location.Document, location.TextSpan, cancellationToken);
var index = spanText.LastIndexOf(searchName, StringComparison.Ordinal);

var index = triggerText.LastIndexOf(searchName, StringComparison.Ordinal);
if (index < 0)
{
// Couldn't even find the search text at this reference location. This might happen
Expand All @@ -135,16 +132,15 @@ public TextSpan GetReferenceEditSpan(InlineRenameLocation location, Cancellation
return new TextSpan(location.TextSpan.Start + index, searchName.Length);
}

public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string replacementText, CancellationToken cancellationToken)
public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string triggerText, string replacementText, CancellationToken cancellationToken)
{
var spanText = GetSpanText(location.Document, location.TextSpan, cancellationToken);
var position = spanText.LastIndexOf(replacementText, StringComparison.Ordinal);
var position = triggerText.LastIndexOf(replacementText, StringComparison.Ordinal);

if (_isRenamingAttributePrefix)
{
// We're only renaming the attribute prefix part. We want to adjust the span of
// the reference we've found to only update the prefix portion.
var index = spanText.LastIndexOf(replacementText + AttributeSuffix, StringComparison.Ordinal);
var index = triggerText.LastIndexOf(replacementText + AttributeSuffix, StringComparison.Ordinal);
position = index >= 0 ? index : position;
}

Expand All @@ -162,15 +158,6 @@ private string GetWithoutAttributeSuffix(string value)
private bool HasAttributeSuffix(string value)
=> value.TryGetWithoutAttributeSuffix(isCaseSensitive: _document.GetLanguageService<ISyntaxFactsService>().IsCaseSensitive, result: out var _);

private static string GetSpanText(Document document, TextSpan triggerSpan, CancellationToken cancellationToken)
{
// TO-DO: Add new 'triggerText' parameter to the callers of this method via IVT so that we can get the text asynchronously instead.
// https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1080161
var sourceText = document.GetTextSynchronously(cancellationToken);
var triggerText = sourceText.ToString(triggerSpan);
return triggerText;
}

internal bool IsRenamingAttributeTypeWithAttributeSuffix()
{
if (this.RenameSymbol.IsAttribute() || (this.RenameSymbol.Kind == SymbolKind.Alias && ((IAliasSymbol)this.RenameSymbol).Target.IsAttribute()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@ internal static async Task<IInlineRenameInfo> GetRenameInfoAsync(
}
}

var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var triggerText = sourceText.ToString(triggerToken.Span);

return new SymbolInlineRenameInfo(
refactorNotifyServices, document, triggerToken.Span,
refactorNotifyServices, document, triggerToken.Span, triggerText,
symbol, forceRenameOverloads, documentSpans.ToImmutableAndFree(), cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ public InlineRenameSessionInfo StartInlineSession(

static InlineRenameSessionInfo? IsReadOnlyOrCannotNavigateToSpan(IInlineRenameInfo renameInfo, Document document, CancellationToken cancellationToken)
{
if (renameInfo is IInlineRenameInfoWithFileRename renameInfoWithFileRename)
if (renameInfo is IInlineRenameInfo inlineRenameInfo && inlineRenameInfo.DefinitionLocations != default)
{
var workspace = document.Project.Solution.Workspace;
var navigationService = workspace.Services.GetRequiredService<IDocumentNavigationService>();
foreach (var documentSpan in renameInfoWithFileRename.DefinitionLocations)

foreach (var documentSpan in inlineRenameInfo.DefinitionLocations)
{
var sourceText = documentSpan.Document.GetTextSynchronously(cancellationToken);
var textSnapshot = sourceText.FindCorrespondingEditorTextSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ internal void SetReferenceSpans(IEnumerable<TextSpan> spans)
_referenceSpanToLinkedRenameSpanMap.Clear();
foreach (var span in spans)
{
var document = _baseDocuments.First();
var renameableSpan = _session._renameInfo.GetReferenceEditSpan(
new InlineRenameLocation(_baseDocuments.First(), span), CancellationToken.None);
new InlineRenameLocation(document, span), GetTriggerText(document, span), CancellationToken.None);
var trackingSpan = new RenameTrackingSpan(
_subjectBuffer.CurrentSnapshot.CreateTrackingSpan(renameableSpan.ToSpan(), SpanTrackingMode.EdgeInclusive, TrackingFidelityMode.Forward),
RenameSpanKind.Reference);
Expand All @@ -208,6 +209,12 @@ internal void SetReferenceSpans(IEnumerable<TextSpan> spans)
RaiseSpansChanged();
}

private static string GetTriggerText(Document document, TextSpan span)
{
var sourceText = document.GetTextSynchronously(CancellationToken.None);
return sourceText.ToString(span);
}

private void OnTextBufferChanged(object sender, TextContentChangedEventArgs args)
{
AssertIsForeground();
Expand Down Expand Up @@ -465,7 +472,10 @@ internal void ApplyConflictResolutionEdits(IInlineRenameReplacementInfo conflict
if (_referenceSpanToLinkedRenameSpanMap.ContainsKey(replacement.OriginalSpan) && kind != RenameSpanKind.Complexified)
{
var linkedRenameSpan = _session._renameInfo.GetConflictEditSpan(
new InlineRenameLocation(newDocument, replacement.NewSpan), GetWithoutAttributeSuffix(_session.ReplacementText, document.GetLanguageService<LanguageServices.ISyntaxFactsService>().IsCaseSensitive), cancellationToken);
new InlineRenameLocation(newDocument, replacement.NewSpan), GetTriggerText(newDocument, replacement.NewSpan),
GetWithoutAttributeSuffix(_session.ReplacementText,
document.GetLanguageService<LanguageServices.ISyntaxFactsService>().IsCaseSensitive), cancellationToken);

if (linkedRenameSpan.HasValue)
{
if (!mergeConflictComments.Any(s => replacement.NewSpan.IntersectsWith(s)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<IInlineRenameLocationSet> FindRenameLocationsAsync(OptionSet o
}
}

public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string replacementText, CancellationToken cancellationToken)
public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string triggerText, string replacementText, CancellationToken cancellationToken)
{
return _info.GetConflictEditSpan(new VSTypeScriptInlineRenameLocationWrapper(
new InlineRenameLocation(location.Document, location.TextSpan)), replacementText, cancellationToken);
Expand All @@ -63,7 +63,7 @@ public async Task<IInlineRenameLocationSet> FindRenameLocationsAsync(OptionSet o
public string GetFinalSymbolName(string replacementText)
=> _info.GetFinalSymbolName(replacementText);

public TextSpan GetReferenceEditSpan(InlineRenameLocation location, CancellationToken cancellationToken)
public TextSpan GetReferenceEditSpan(InlineRenameLocation location, string triggerText, CancellationToken cancellationToken)
{
return _info.GetReferenceEditSpan(new VSTypeScriptInlineRenameLocationWrapper(
new InlineRenameLocation(location.Document, location.TextSpan)), cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ internal interface IInlineRenameInfo
/// </summary>
Glyph Glyph { get; }

/// <summary>
/// The locations of the potential rename candidates for the symbol.
/// </summary>
ImmutableArray<DocumentSpan> DefinitionLocations { get; }

/// <summary>
/// Gets the final name of the symbol if the user has typed the provided replacement text
/// in the editor. Normally, the final name will be same as the replacement text. However,
Expand All @@ -205,13 +210,13 @@ internal interface IInlineRenameInfo
/// Returns the actual span that should be edited in the buffer for a given rename reference
/// location.
/// </summary>
TextSpan GetReferenceEditSpan(InlineRenameLocation location, CancellationToken cancellationToken);
TextSpan GetReferenceEditSpan(InlineRenameLocation location, string triggerText, CancellationToken cancellationToken);

/// <summary>
/// Returns the actual span that should be edited in the buffer for a given rename conflict
/// location.
/// </summary>
TextSpan? GetConflictEditSpan(InlineRenameLocation location, string replacementText, CancellationToken cancellationToken);
TextSpan? GetConflictEditSpan(InlineRenameLocation location, string triggerText, string replacementText, CancellationToken cancellationToken);

/// <summary>
/// Determine the set of locations to rename given the provided options. May be called
Expand Down Expand Up @@ -240,10 +245,6 @@ internal interface IInlineRenameInfoWithFileRename : IInlineRenameInfo
/// an inline rename
/// </summary>
InlineRenameFileRenameInfo GetFileRenameInfo();

// TO-DO: Move property to IInlineRenameInfo once Typescript moves to the correct IVT layering
// https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1077984
ImmutableArray<DocumentSpan> DefinitionLocations { get; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;

Expand Down Expand Up @@ -130,6 +130,11 @@ internal interface IFSharpInlineRenameInfo
/// </summary>
FSharpGlyph Glyph { get; }

/// <summary>
/// The locations of the potential rename candidates for the symbol.
/// </summary>
ImmutableArray<DocumentSpan> DefinitionLocations { get; }

/// <summary>
/// Gets the final name of the symbol if the user has typed the provided replacement text
/// in the editor. Normally, the final name will be same as the replacement text. However,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using System.Collections.Immutable;

namespace Microsoft.CodeAnalysis.ExternalAccess.FSharp.Internal.Editor
{
Expand Down Expand Up @@ -130,6 +131,8 @@ public FSharpInlineRenameInfo(IFSharpInlineRenameInfo info)

public Glyph Glyph => FSharpGlyphHelpers.ConvertTo(_info.Glyph);

public ImmutableArray<DocumentSpan> DefinitionLocations => _info.DefinitionLocations;

public async Task<IInlineRenameLocationSet> FindRenameLocationsAsync(OptionSet optionSet, CancellationToken cancellationToken)
{
var set = await _info.FindRenameLocationsAsync(optionSet, cancellationToken).ConfigureAwait(false);
Expand All @@ -143,7 +146,7 @@ public async Task<IInlineRenameLocationSet> FindRenameLocationsAsync(OptionSet o
}
}

public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string replacementText, CancellationToken cancellationToken)
public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string triggerText, string replacementText, CancellationToken cancellationToken)
{
return _info.GetConflictEditSpan(new FSharpInlineRenameLocation(location.Document, location.TextSpan), replacementText, cancellationToken);
}
Expand All @@ -153,7 +156,7 @@ public string GetFinalSymbolName(string replacementText)
return _info.GetFinalSymbolName(replacementText);
}

public TextSpan GetReferenceEditSpan(InlineRenameLocation location, CancellationToken cancellationToken)
public TextSpan GetReferenceEditSpan(InlineRenameLocation location, string triggerText, CancellationToken cancellationToken)
{
return _info.GetReferenceEditSpan(new FSharpInlineRenameLocation(location.Document, location.TextSpan), cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -31,9 +32,10 @@ public FailureInlineRenameInfo(string localizedErrorMessage)
public string DisplayName => null;
public string FullDisplayName => null;
public Glyph Glyph => Glyph.None;
public ImmutableArray<DocumentSpan> DefinitionLocations => default;
public string GetFinalSymbolName(string replacementText) => null;
public TextSpan GetReferenceEditSpan(InlineRenameLocation location, CancellationToken cancellationToken) => default;
public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string replacementText, CancellationToken cancellationToken) => null;
public TextSpan GetReferenceEditSpan(InlineRenameLocation location, string triggerText, CancellationToken cancellationToken) => default;
public TextSpan? GetConflictEditSpan(InlineRenameLocation location, string triggerText, string replacementText, CancellationToken cancellationToken) => null;
public Task<IInlineRenameLocationSet> FindRenameLocationsAsync(OptionSet optionSet, CancellationToken cancellationToken) => null;
public bool TryOnAfterGlobalSymbolRenamed(CodeAnalysis.Workspace workspace, IEnumerable<DocumentId> changedDocumentIDs, string replacementText) => false;
public bool TryOnBeforeGlobalSymbolRenamed(CodeAnalysis.Workspace workspace, IEnumerable<DocumentId> changedDocumentIDs, string replacementText) => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -42,6 +43,11 @@ internal interface IXamlRenameInfo
/// </summary>
SymbolKind Kind { get; }

/// <summary>
/// The locations of the potential rename candidates for the symbol.
/// </summary>
ImmutableArray<CodeAnalysis.DocumentSpan> DefinitionLocations { get; }

/// <summary>
/// Find all locations to be renamed.
/// </summary>
Expand Down
Loading