RenameLocation.ReferenceProcessing.cs 26.0 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7 8 9 10

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.LanguageServices;
T
Tomas Matousek 已提交
11
using Microsoft.CodeAnalysis.PooledObjects;
P
Pilchie 已提交
12 13 14
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
15
using System.Text.RegularExpressions;
16
using System.Collections.Immutable;
P
Pilchie 已提交
17 18 19 20 21 22 23

namespace Microsoft.CodeAnalysis.Rename
{
    /// <summary>
    /// A helper class that contains some of the methods and filters that must be used when
    /// processing the raw results from the FindReferences API.
    /// </summary>
24
    internal sealed partial class RenameLocations
P
Pilchie 已提交
25 26 27 28 29 30 31
    {
        internal static class ReferenceProcessing
        {
            /// <summary>
            /// Given a symbol in a document, returns the "right" symbol that should be renamed in
            /// the case the name binds to things like aliases _and_ the underlying type at once.
            /// </summary>
32 33
            public static async Task<SymbolAndProjectId> GetRenamableSymbolAsync(
                Document document, int position, CancellationToken cancellationToken)
P
Pilchie 已提交
34 35 36 37
            {
                var symbol = await SymbolFinder.FindSymbolAtPositionAsync(document, position, cancellationToken: cancellationToken).ConfigureAwait(false);
                if (symbol == null)
                {
C
CyrusNajmabadi 已提交
38
                    return default;
P
Pilchie 已提交
39 40
                }

41 42 43
                var symbolAndProjectId = SymbolAndProjectId.Create(symbol, document.Project.Id);
                var definitionSymbol = await FindDefinitionSymbolAsync(symbolAndProjectId, document.Project.Solution, cancellationToken).ConfigureAwait(false);
                Contract.ThrowIfNull(definitionSymbol.Symbol);
P
Pilchie 已提交
44 45 46 47 48 49 50

                return definitionSymbol;
            }

            /// <summary>
            /// Given a symbol, finds the symbol that actually defines the name that we're using.
            /// </summary>
51 52
            public static async Task<SymbolAndProjectId> FindDefinitionSymbolAsync(
                SymbolAndProjectId symbolAndProjectId, Solution solution, CancellationToken cancellationToken)
P
Pilchie 已提交
53
            {
54
                var symbol = symbolAndProjectId.Symbol;
P
Pilchie 已提交
55 56 57 58
                Contract.ThrowIfNull(symbol);
                Contract.ThrowIfNull(solution);

                // Make sure we're on the original source definition if we can be
59 60
                var foundSymbolAndProjectId = await SymbolFinder.FindSourceDefinitionAsync(
                    symbolAndProjectId, solution, cancellationToken).ConfigureAwait(false);
P
Pilchie 已提交
61

62 63 64 65
                var bestSymbolAndProjectId = foundSymbolAndProjectId.Symbol != null
                    ? foundSymbolAndProjectId
                    : symbolAndProjectId;
                symbol = bestSymbolAndProjectId.Symbol;
P
Pilchie 已提交
66 67 68 69 70 71 72 73

                // If we're renaming a property, it might be a synthesized property for a method
                // backing field.
                if (symbol.Kind == SymbolKind.Parameter)
                {
                    if (symbol.ContainingSymbol.Kind == SymbolKind.Method)
                    {
                        var containingMethod = (IMethodSymbol)symbol.ContainingSymbol;
74
                        if (containingMethod.AssociatedSymbol is IPropertySymbol)
P
Pilchie 已提交
75
                        {
76
                            var associatedPropertyOrEvent = (IPropertySymbol)containingMethod.AssociatedSymbol;
P
Pilchie 已提交
77 78 79
                            var ordinal = containingMethod.Parameters.IndexOf((IParameterSymbol)symbol);
                            if (ordinal < associatedPropertyOrEvent.Parameters.Length)
                            {
80 81
                                return bestSymbolAndProjectId.WithSymbol(
                                    associatedPropertyOrEvent.Parameters[ordinal]);
P
Pilchie 已提交
82 83 84 85 86 87 88 89 90
                            }
                        }
                    }
                }

                // if we are renaming a compiler generated delegate for an event, cascade to the event
                if (symbol.Kind == SymbolKind.NamedType)
                {
                    var typeSymbol = (INamedTypeSymbol)symbol;
91
                    if (typeSymbol.IsImplicitlyDeclared && typeSymbol.IsDelegateType() && typeSymbol.AssociatedSymbol != null)
P
Pilchie 已提交
92
                    {
93 94
                        return bestSymbolAndProjectId.WithSymbol(
                            typeSymbol.AssociatedSymbol);
P
Pilchie 已提交
95 96 97 98 99 100 101 102 103 104 105
                    }
                }

                // If we are renaming a constructor or destructor, we wish to rename the whole type
                if (symbol.Kind == SymbolKind.Method)
                {
                    var methodSymbol = (IMethodSymbol)symbol;
                    if (methodSymbol.MethodKind == MethodKind.Constructor ||
                        methodSymbol.MethodKind == MethodKind.StaticConstructor ||
                        methodSymbol.MethodKind == MethodKind.Destructor)
                    {
106 107
                        return bestSymbolAndProjectId.WithSymbol(
                            methodSymbol.ContainingType);
P
Pilchie 已提交
108 109 110
                    }
                }

111
                // If we are renaming a backing field for a property, cascade to the property
112 113 114
                if (symbol.Kind == SymbolKind.Field)
                {
                    var fieldSymbol = (IFieldSymbol)symbol;
J
Jared Parsons 已提交
115
                    if (fieldSymbol.IsImplicitlyDeclared &&
116 117
                        fieldSymbol.AssociatedSymbol.IsKind(SymbolKind.Property))
                    {
118 119
                        return bestSymbolAndProjectId.WithSymbol(
                            fieldSymbol.AssociatedSymbol);
120 121 122
                    }
                }

P
Pilchie 已提交
123
                // in case this is e.g. an overridden property accessor, we'll treat the property itself as the definition symbol
124
                var propertyAndProjectId = await GetPropertyFromAccessorOrAnOverride(bestSymbolAndProjectId, solution, cancellationToken).ConfigureAwait(false);
P
Pilchie 已提交
125

126 127 128
                return propertyAndProjectId.Symbol != null
                    ? propertyAndProjectId
                    : bestSymbolAndProjectId;
P
Pilchie 已提交
129 130
            }

131 132
            private static async Task<bool> ShouldIncludeSymbolAsync(
                ISymbol referencedSymbol, ISymbol originalSymbol, Solution solution, bool considerSymbolReferences, CancellationToken cancellationToken)
P
Pilchie 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            {
                if (referencedSymbol.IsPropertyAccessor())
                {
                    return considerSymbolReferences;
                }

                if (referencedSymbol.Equals(originalSymbol))
                {
                    return true;
                }

                // Parameters of properties and methods can cascade to each other in
                // indexer scenarios.
                if (originalSymbol.Kind == SymbolKind.Parameter && referencedSymbol.Kind == SymbolKind.Parameter)
                {
                    return true;
                }

                // If the original symbol is a property, cascade to the backing field
152
                if (referencedSymbol.Kind == SymbolKind.Field && originalSymbol.Equals(((IFieldSymbol)referencedSymbol).AssociatedSymbol))
P
Pilchie 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166
                {
                    return true;
                }

                // If the symbol doesn't actually exist in source, we never want to rename it
                if (referencedSymbol.IsImplicitlyDeclared)
                {
                    return considerSymbolReferences;
                }

                // We can cascade from members to other members only if the names match. The example
                // where the names might be different is explicit interface implementations in
                // Visual Basic and VB's identifiers are case insensitive. 
                // Do not cascade to symbols that are defined only in metadata.
167 168 169
                if (referencedSymbol.Kind == originalSymbol.Kind &&
                    string.Compare(TrimNameToAfterLastDot(referencedSymbol.Name), TrimNameToAfterLastDot(originalSymbol.Name), StringComparison.OrdinalIgnoreCase) == 0 &&
                    referencedSymbol.Locations.Any(loc => loc.IsInSource))
P
Pilchie 已提交
170
                {
171
                    return true;
P
Pilchie 已提交
172 173 174 175 176 177 178 179
                }

                // If the original symbol is an alias, then the referenced symbol will be where we
                // actually see references.
                if (originalSymbol.Kind == SymbolKind.Alias)
                {
                    var target = ((IAliasSymbol)originalSymbol).Target;

C
CyrusNajmabadi 已提交
180 181 182 183 184 185
                    switch (target)
                    {
                        case INamedTypeSymbol nt: return nt.ConstructedFrom.Equals(referencedSymbol);
                        case INamespaceOrTypeSymbol s: return s.Equals(referencedSymbol);
                        default: return false;
                    }
P
Pilchie 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                }

                // cascade from property accessor to property (someone in C# renames base.get_X, or the accessor override)
                if (await IsPropertyAccessorOrAnOverride(referencedSymbol, solution, cancellationToken).ConfigureAwait(false) ||
                    await IsPropertyAccessorOrAnOverride(originalSymbol, solution, cancellationToken).ConfigureAwait(false))
                {
                    return true;
                }

                if (referencedSymbol.ContainingSymbol != null &&
                    referencedSymbol.ContainingSymbol.Kind == SymbolKind.NamedType &&
                    ((INamedTypeSymbol)referencedSymbol.ContainingSymbol).TypeKind == TypeKind.Interface &&
                    !originalSymbol.ExplicitInterfaceImplementations().Any(s => s.Equals(referencedSymbol)))
                {
                    return true;
                }

                return false;
            }

206 207
            internal static async Task<SymbolAndProjectId> GetPropertyFromAccessorOrAnOverride(
                SymbolAndProjectId symbolAndProjectId, Solution solution, CancellationToken cancellationToken)
P
Pilchie 已提交
208
            {
209
                var symbol = symbolAndProjectId.Symbol;
P
Pilchie 已提交
210 211
                if (symbol.IsPropertyAccessor())
                {
212 213
                    return symbolAndProjectId.WithSymbol(
                        ((IMethodSymbol)symbol).AssociatedSymbol);
P
Pilchie 已提交
214 215 216 217
                }

                if (symbol.IsOverride && symbol.OverriddenMember() != null)
                {
218
                    var originalSourceSymbol = await SymbolFinder.FindSourceDefinitionAsync(
219
                        symbolAndProjectId.WithSymbol(symbol.OverriddenMember()),
220
                        solution, cancellationToken).ConfigureAwait(false);
P
Pilchie 已提交
221

222
                    if (originalSourceSymbol.Symbol != null)
P
Pilchie 已提交
223 224 225 226 227 228 229 230
                    {
                        return await GetPropertyFromAccessorOrAnOverride(originalSourceSymbol, solution, cancellationToken).ConfigureAwait(false);
                    }
                }

                if (symbol.Kind == SymbolKind.Method &&
                    symbol.ContainingType.TypeKind == TypeKind.Interface)
                {
231 232
                    var methodImplementors = await SymbolFinder.FindImplementationsAsync(
                        symbolAndProjectId, solution, cancellationToken: cancellationToken).ConfigureAwait(false);
P
Pilchie 已提交
233 234 235 236

                    foreach (var methodImplementor in methodImplementors)
                    {
                        var propertyAccessorOrAnOverride = await GetPropertyFromAccessorOrAnOverride(methodImplementor, solution, cancellationToken).ConfigureAwait(false);
237
                        if (propertyAccessorOrAnOverride.Symbol != null)
P
Pilchie 已提交
238 239 240 241 242 243
                        {
                            return propertyAccessorOrAnOverride;
                        }
                    }
                }

C
CyrusNajmabadi 已提交
244
                return default;
P
Pilchie 已提交
245 246
            }

247 248
            private static async Task<bool> IsPropertyAccessorOrAnOverride(
                ISymbol symbol, Solution solution, CancellationToken cancellationToken)
P
Pilchie 已提交
249
            {
250 251 252 253
                var result = await GetPropertyFromAccessorOrAnOverride(
                    SymbolAndProjectId.Create(symbol, projectId: null),
                    solution, cancellationToken).ConfigureAwait(false);
                return result.Symbol != null;
P
Pilchie 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
            }

            private static string TrimNameToAfterLastDot(string name)
            {
                int position = name.LastIndexOf('.');

                if (position == -1)
                {
                    return name;
                }
                else
                {
                    return name.Substring(position + 1);
                }
            }

            /// <summary>
271
            /// Given a ISymbol, returns the renameable locations for a given symbol.
P
Pilchie 已提交
272
            /// </summary>
273 274
            public static async Task<ImmutableArray<RenameLocation>> GetRenamableDefinitionLocationsAsync(
                ISymbol referencedSymbol, ISymbol originalSymbol, Solution solution, CancellationToken cancellationToken)
P
Pilchie 已提交
275 276 277 278
            {
                var shouldIncludeSymbol = await ShouldIncludeSymbolAsync(referencedSymbol, originalSymbol, solution, false, cancellationToken).ConfigureAwait(false);
                if (!shouldIncludeSymbol)
                {
279
                    return ImmutableArray<RenameLocation>.Empty;
P
Pilchie 已提交
280 281 282 283 284 285
                }

                // Namespaces are definitions and references all in one. Since every definition
                // location is also a reference, we'll ignore it's definitions.
                if (referencedSymbol.Kind == SymbolKind.Namespace)
                {
286
                    return ImmutableArray<RenameLocation>.Empty;
P
Pilchie 已提交
287 288
                }

289
                var results = ArrayBuilder<RenameLocation>.GetInstance();
P
Pilchie 已提交
290 291 292 293 294 295 296

                // If the original symbol was an alias, then the definitions will just be the
                // location of the alias, always
                if (originalSymbol.Kind == SymbolKind.Alias)
                {
                    var location = originalSymbol.Locations.Single();
                    results.Add(new RenameLocation(location, solution.GetDocument(location.SourceTree).Id));
297
                    return results.ToImmutableAndFree();
P
Pilchie 已提交
298 299 300 301 302 303 304
                }

                var isRenamableAccessor = await IsPropertyAccessorOrAnOverride(referencedSymbol, solution, cancellationToken).ConfigureAwait(false);
                foreach (var location in referencedSymbol.Locations)
                {
                    if (location.IsInSource)
                    {
305 306 307 308
                        results.Add(new RenameLocation(
                            location, 
                            solution.GetDocument(location.SourceTree).Id, 
                            isRenamableAccessor: isRenamableAccessor));
P
Pilchie 已提交
309 310 311 312 313
                    }
                }

                // If we're renaming a named type, we'll also have to find constructors and
                // destructors declarations that match the name
314
                if (referencedSymbol.Kind == SymbolKind.NamedType && referencedSymbol.Locations.All(l => l.IsInSource))
P
Pilchie 已提交
315
                {
316
                    var syntaxFacts = solution.GetDocument(referencedSymbol.Locations[0].SourceTree).GetLanguageService<ISyntaxFactsService>();
P
Pilchie 已提交
317

318
                    var namedType = (INamedTypeSymbol)referencedSymbol;
P
Pilchie 已提交
319 320 321 322 323 324 325 326 327 328 329
                    foreach (var method in namedType.GetMembers().OfType<IMethodSymbol>())
                    {
                        if (!method.IsImplicitlyDeclared && (method.MethodKind == MethodKind.Constructor ||
                                                      method.MethodKind == MethodKind.StaticConstructor ||
                                                      method.MethodKind == MethodKind.Destructor))
                        {
                            foreach (var location in method.Locations)
                            {
                                if (location.IsInSource)
                                {
                                    var token = location.FindToken(cancellationToken);
330
                                    if (!syntaxFacts.IsKeyword(token) && token.ValueText == referencedSymbol.Name)
P
Pilchie 已提交
331 332 333 334 335 336 337 338 339
                                    {
                                        results.Add(new RenameLocation(location, solution.GetDocument(location.SourceTree).Id));
                                    }
                                }
                            }
                        }
                    }
                }

340
                return results.ToImmutableAndFree();
P
Pilchie 已提交
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
            }

            internal static async Task<IEnumerable<RenameLocation>> GetRenamableReferenceLocationsAsync(ISymbol referencedSymbol, ISymbol originalSymbol, ReferenceLocation location, Solution solution, CancellationToken cancellationToken)
            {
                var shouldIncludeSymbol = await ShouldIncludeSymbolAsync(referencedSymbol, originalSymbol, solution, true, cancellationToken).ConfigureAwait(false);
                if (!shouldIncludeSymbol)
                {
                    return SpecializedCollections.EmptyEnumerable<RenameLocation>();
                }

                // Implicit references are things like a foreach referencing GetEnumerator. We don't
                // want to consider those as part of the set
                if (location.IsImplicit)
                {
                    return SpecializedCollections.EmptyEnumerable<RenameLocation>();
                }

                var results = new List<RenameLocation>();

                // If we were originally naming an alias, then we'll only use the location if was
                // also bound through the alias
                if (originalSymbol.Kind == SymbolKind.Alias)
                {
                    if (originalSymbol.Equals(location.Alias))
                    {
                        results.Add(new RenameLocation(location, location.Document.Id));

                        // We also need to add the location of the alias
                        // itself
                        var aliasLocation = location.Alias.Locations.Single();
                        results.Add(new RenameLocation(aliasLocation, solution.GetDocument(aliasLocation.SourceTree).Id));
                    }
                }
                else
                {
                    // If we bound through an alias, we'll only rename if the alias's name matches
                    // the name of symbol it points to. We do this because it's common to see things
378
                    // like "using Goo = System.Goo" where people want to import a single type
P
Pilchie 已提交
379 380 381 382 383
                    // rather than a whole namespace of stuff.
                    if (location.Alias != null)
                    {
                        if (location.Alias.Name == referencedSymbol.Name)
                        {
384
                            results.Add(new RenameLocation(location.Location, location.Document.Id,
385
                                candidateReason: location.CandidateReason, isRenamableAliasUsage: true, isWrittenTo: location.IsWrittenTo));
P
Pilchie 已提交
386 387 388 389 390 391 392 393 394

                            // We also need to add the location of the alias itself
                            var aliasLocation = location.Alias.Locations.Single();
                            results.Add(new RenameLocation(aliasLocation, solution.GetDocument(aliasLocation.SourceTree).Id));
                        }
                    }
                    else
                    {
                        // The simple case, so just the single location and we're done
395
                        results.Add(new RenameLocation(
J
Jared Parsons 已提交
396 397
                            location.Location,
                            location.Document.Id,
398
                            isWrittenTo: location.IsWrittenTo,
399
                            candidateReason: location.CandidateReason,
400
                            isRenamableAccessor: await IsPropertyAccessorOrAnOverride(referencedSymbol, solution, cancellationToken).ConfigureAwait(false)));
P
Pilchie 已提交
401 402 403 404 405 406 407 408 409
                    }
                }

                return results;
            }

            internal static async Task<Tuple<IEnumerable<RenameLocation>, IEnumerable<RenameLocation>>> GetRenamableLocationsInStringsAndCommentsAsync(
                ISymbol originalSymbol,
                Solution solution,
410
                ISet<RenameLocation> renameLocations,
P
Pilchie 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423
                bool renameInStrings,
                bool renameInComments,
                CancellationToken cancellationToken)
            {
                if (!renameInStrings && !renameInComments)
                {
                    return new Tuple<IEnumerable<RenameLocation>, IEnumerable<RenameLocation>>(null, null);
                }

                var renameText = originalSymbol.Name;
                List<RenameLocation> stringLocations = renameInStrings ? new List<RenameLocation>() : null;
                List<RenameLocation> commentLocations = renameInComments ? new List<RenameLocation>() : null;

424
                foreach (var documentsGroupedByLanguage in RenameUtilities.GetDocumentsAffectedByRename(originalSymbol, solution, renameLocations).GroupBy(d => d.Project.Language))
P
Pilchie 已提交
425
                {
426
                    var syntaxFactsLanguageService = solution.Workspace.Services.GetLanguageServices(documentsGroupedByLanguage.Key).GetService<ISyntaxFactsService>();
427 428

                    if (syntaxFactsLanguageService != null)
P
Pilchie 已提交
429
                    {
430
                        foreach (var document in documentsGroupedByLanguage)
P
Pilchie 已提交
431
                        {
432 433 434 435 436
                            if (renameInStrings)
                            {
                                await AddLocationsToRenameInStringsAsync(document, renameText, syntaxFactsLanguageService,
                                    stringLocations, cancellationToken).ConfigureAwait(false);
                            }
P
Pilchie 已提交
437

438 439 440 441
                            if (renameInComments)
                            {
                                await AddLocationsToRenameInCommentsAsync(document, renameText, commentLocations, cancellationToken).ConfigureAwait(false);
                            }
P
Pilchie 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455
                        }
                    }
                }

                return new Tuple<IEnumerable<RenameLocation>, IEnumerable<RenameLocation>>(stringLocations, commentLocations);
            }

            private static async Task AddLocationsToRenameInStringsAsync(Document document, string renameText, ISyntaxFactsService syntaxFactsService, List<RenameLocation> renameLocations, CancellationToken cancellationToken)
            {
                var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
                var renameTextLength = renameText.Length;

                var renameStringsAndPositions = root
                    .DescendantTokens()
456
                    .Where(t => syntaxFactsService.IsStringLiteralOrInterpolatedStringLiteral(t) && t.Span.Length >= renameTextLength)
P
Pilchie 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
                    .Select(t => Tuple.Create(t.ToString(), t.Span.Start, t.Span));

                if (renameStringsAndPositions.Any())
                {
                    AddLocationsToRenameInStringsAndComments(document, root.SyntaxTree, renameText,
                        renameStringsAndPositions, renameLocations, isRenameInStrings: true, isRenameInComments: false);
                }
            }

            private static async Task AddLocationsToRenameInCommentsAsync(Document document, string renameText, List<RenameLocation> renameLocations, CancellationToken cancellationToken)
            {
                var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
                var renameTextLength = renameText.Length;

                var renameStringsAndPositions = root
                    .DescendantTrivia(descendIntoTrivia: true)
                    .Where(t => t.Span.Length >= renameTextLength)
                    .Select(t => Tuple.Create(t.ToString(), t.Span.Start, t.Token.Span));

                if (renameStringsAndPositions.Any())
                {
                    AddLocationsToRenameInStringsAndComments(document, root.SyntaxTree, renameText,
                        renameStringsAndPositions, renameLocations, isRenameInStrings: false, isRenameInComments: true);
                }
            }

            private static void AddLocationsToRenameInStringsAndComments(
                Document document,
                SyntaxTree tree,
                string renameText,
                IEnumerable<Tuple<string, int, TextSpan>> renameStringsAndPositions,
                List<RenameLocation> renameLocations,
                bool isRenameInStrings,
                bool isRenameInComments)
            {
                var regex = GetRegexForMatch(renameText);
                foreach (var renameStringAndPosition in renameStringsAndPositions)
                {
                    string renameString = renameStringAndPosition.Item1;
                    int renameStringPosition = renameStringAndPosition.Item2;
                    var containingSpan = renameStringAndPosition.Item3;

                    MatchCollection matches = regex.Matches(renameString);

                    foreach (Match match in matches)
                    {
                        int start = renameStringPosition + match.Index;
                        Debug.Assert(renameText.Length == match.Length);
                        var matchTextSpan = new TextSpan(start, renameText.Length);
                        var matchLocation = tree.GetLocation(matchTextSpan);
                        var renameLocation = new RenameLocation(matchLocation, document.Id, containingLocationForStringOrComment: containingSpan);
                        renameLocations.Add(renameLocation);
                    }
                }
            }

            private static Regex GetRegexForMatch(string matchText)
            {
                var matchString = string.Format(@"\b{0}\b", matchText);
                return new Regex(matchString, RegexOptions.CultureInvariant);
            }

            internal static string ReplaceMatchingSubStrings(string replaceInsideString, string matchText, string replacementText)
            {
                var regex = GetRegexForMatch(matchText);
                return regex.Replace(replaceInsideString, replacementText);
            }
        }
    }
526
}