-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Renamer.cs
108 lines (92 loc) · 4.28 KB
/
Renamer.cs
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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Rename.ConflictEngine;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Rename
{
public static class Renamer
{
public static Task<Solution> RenameSymbolAsync(
Solution solution, ISymbol symbol, string newName, OptionSet optionSet, CancellationToken cancellationToken = default)
{
return RenameSymbolAsync(
solution,
SymbolAndProjectId.Create(symbol, projectId: null),
newName, optionSet, cancellationToken);
}
internal static Task<Solution> RenameSymbolAsync(
Solution solution, SymbolAndProjectId symbolAndProjectId, string newName, OptionSet optionSet, CancellationToken cancellationToken = default)
{
return RenameSymbolAsync(solution, symbolAndProjectId, newName, optionSet, filter: null, cancellationToken: cancellationToken);
}
internal static Task<RenameLocations> GetRenameLocationsAsync(
Solution solution, SymbolAndProjectId symbolAndProjectId, OptionSet options, CancellationToken cancellationToken)
{
if (solution == null)
{
throw new ArgumentNullException(nameof(solution));
}
if (symbolAndProjectId.Symbol == null)
{
throw new ArgumentNullException(nameof(symbolAndProjectId));
}
cancellationToken.ThrowIfCancellationRequested();
options = options ?? solution.Options;
return RenameLocations.FindAsync(
symbolAndProjectId, solution, options, cancellationToken);
}
internal static async Task<Solution> RenameAsync(
RenameLocations locations,
string newName,
Func<Location, bool> filter = null,
Func<IEnumerable<ISymbol>, bool?> hasConflict = null,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(newName))
{
throw new ArgumentException(nameof(newName));
}
cancellationToken.ThrowIfCancellationRequested();
var symbolAndProjectId = locations.SymbolAndProjectId;
if (filter != null)
{
locations = new RenameLocations(
locations.Locations.Where(loc => filter(loc.Location)).ToSet(),
symbolAndProjectId, locations.Solution,
locations.ReferencedSymbols, locations.ImplicitLocations.Where(loc => filter(loc.Location)),
locations.Options);
}
var conflictResolution = await ConflictResolver.ResolveConflictsAsync(
locations, symbolAndProjectId.Symbol.Name, newName, locations.Options, hasConflict, cancellationToken).ConfigureAwait(false);
return conflictResolution.NewSolution;
}
internal static async Task<Solution> RenameSymbolAsync(
Solution solution,
SymbolAndProjectId symbolAndProjectId,
string newName,
OptionSet options,
Func<Location, bool> filter,
Func<IEnumerable<ISymbol>, bool?> hasConflict = null,
CancellationToken cancellationToken = default)
{
if (solution == null)
{
throw new ArgumentNullException(nameof(solution));
}
if (symbolAndProjectId.Symbol == null)
{
throw new ArgumentNullException(nameof(symbolAndProjectId));
}
cancellationToken.ThrowIfCancellationRequested();
options = options ?? solution.Workspace.Options;
var renameLocations = await GetRenameLocationsAsync(solution, symbolAndProjectId, options, cancellationToken).ConfigureAwait(false);
return await RenameAsync(renameLocations, newName, filter, hasConflict, cancellationToken).ConfigureAwait(false);
}
}
}