提交 2863a9ec 编写于 作者: Š Šimon Koníček

Adding CSharpUseAnotherLiteralCodeFixProvider

上级 a0be2920
......@@ -1243,6 +1243,15 @@ internal class CSharpFeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Use &apos;{0}&apos;.
/// </summary>
internal static string Use_0 {
get {
return ResourceManager.GetString("Use_0", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Use explicit type.
/// </summary>
......
......@@ -572,4 +572,7 @@
<data name="Remove_unused_variables" xml:space="preserve">
<value>Remove unused variables</value>
</data>
<data name="Use_0" xml:space="preserve">
<value>Use '{0}'</value>
</data>
</root>
\ No newline at end of file
// 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.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp.UseAnotherLiteral
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.UseAnotherLiteral), Shared]
internal sealed class CSharpUseAnotherLiteralCodeFixProvider : CodeFixProvider
{
private const string CS8313 = nameof(CS8313); // A default literal 'default' is not valid as a case constant. Use another literal (e.g. '0' or 'null') as appropriate. If you intended to write the default label, use 'default:' without 'case'.
private const string CS8363 = nameof(CS8363); // A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern 'var _'.
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
ImmutableArray.Create(CS8313, CS8363);
public override FixAllProvider GetFixAllProvider()
{
// This code fix addresses very specific compiler errors. It's unlikely there will be more than 1 of them at a time.
return null;
}
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var token = syntaxRoot.FindToken(context.Span.Start);
if (token.Span == context.Span &&
token.IsKind(SyntaxKind.DefaultKeyword) &&
token.Parent.IsKind(SyntaxKind.DefaultLiteralExpression))
{
var defaultLiteral = (LiteralExpressionSyntax)token.Parent;
var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
var constant = semanticModel.GetConstantValue(defaultLiteral, context.CancellationToken);
if (!constant.HasValue)
{
return;
}
var newLiteral = SyntaxGenerator.GetGenerator(context.Document).LiteralExpression(constant.Value);
context.RegisterCodeFix(
new MyCodeAction(
c => FixAsync(context.Document, context.Span, newLiteral, c),
newLiteral.ToString()),
context.Diagnostics);
}
}
private static async Task<Document> FixAsync(Document document, TextSpan span, SyntaxNode newLiteral, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var defaultToken = syntaxRoot.FindToken(span.Start);
var defaultLiteral = (LiteralExpressionSyntax)defaultToken.Parent;
var newRoot = syntaxRoot.ReplaceNode(defaultLiteral, newLiteral.WithTriviaFrom(defaultLiteral));
return document.WithSyntaxRoot(newRoot);
}
private sealed class MyCodeAction : CodeAction.DocumentChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument, string literal)
: base(string.Format(CSharpFeaturesResources.Use_0, literal), createChangedDocument, CSharpFeaturesResources.Use_0)
{
}
}
}
}
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -107,6 +107,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -54,6 +54,7 @@ internal static class PredefinedCodeFixProviderNames
public const string Suppression = nameof(Suppression);
public const string AddOverloads = nameof(AddOverloads);
public const string AddNew = nameof(AddNew);
public const string UseAnotherLiteral = nameof(UseAnotherLiteral);
public const string UseImplicitType = nameof(UseImplicitType);
public const string UseExplicitType = nameof(UseExplicitType);
public const string UseCollectionInitializer = nameof(UseCollectionInitializer);
......
......@@ -122,6 +122,7 @@ public static class Features
public const string CodeActionsSimplifyTypeNames = "CodeActions.SimplifyTypeNames";
public const string CodeActionsSpellcheck = "CodeActions.Spellcheck";
public const string CodeActionsSuppression = "CodeActions.Suppression";
public const string CodeActionsUseAnotherLiteral = "CodeActions.UseAnotherLiteral";
public const string CodeActionsUseInterpolatedVerbatimString = "CodeActions.UseInterpolatedVerbatimString";
public const string CodeActionsUseAutoProperty = "CodeActions.UseAutoProperty";
public const string CodeActionsUseCoalesceExpression = "CodeActions.UseCoalesceExpression";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册