-
-
Notifications
You must be signed in to change notification settings - Fork 255
/
RemoveParameterNameRefactoring.cs
55 lines (46 loc) · 1.95 KB
/
RemoveParameterNameRefactoring.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
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Roslynator.CSharp.Refactorings.AddOrRemoveParameterName
{
internal static class RemoveParameterNameRefactoring
{
public static void ComputeRefactoring(
RefactoringContext context,
ArgumentListSyntax argumentList,
SeparatedSyntaxListSelection<ArgumentSyntax> selection)
{
if (!CanRefactor(selection))
return;
context.RegisterRefactoring(
"Remove parameter name",
cancellationToken => RefactorAsync(context.Document, argumentList, selection, cancellationToken),
RefactoringIdentifiers.RemoveParameterNameFromArgument);
}
private static bool CanRefactor(SeparatedSyntaxListSelection<ArgumentSyntax> selection)
{
for (int i = 0; i < selection.Count; i++)
{
NameColonSyntax nameColon = selection[i].NameColon;
if (nameColon?.IsMissing == false)
return true;
}
return false;
}
private static Task<Document> RefactorAsync(
Document document,
ArgumentListSyntax argumentList,
SeparatedSyntaxListSelection<ArgumentSyntax> selection,
CancellationToken cancellationToken = default(CancellationToken))
{
var rewriter = new RemoveParameterNameRewriter(selection.ToImmutableArray());
SyntaxNode newNode = rewriter
.Visit(argumentList)
.WithFormatterAnnotation();
return document.ReplaceNodeAsync(argumentList, newNode, cancellationToken);
}
}
}