未验证 提交 37f74eb3 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #28043 from sharwell/refactoring-only

Change 'None' in code style options to 'Refactoring Only'
......@@ -1539,8 +1539,10 @@ static void M()
[|var|] n1 = new C();
}
}";
await TestMissingInRegularAndScriptAsync(source,
new TestParameters(options: ExplicitTypeSilentEnforcement()));
await TestDiagnosticInfoAsync(source,
options: ExplicitTypeSilentEnforcement(),
diagnosticId: IDEDiagnosticIds.UseExplicitTypeDiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Hidden);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseImplicitType)]
......
......@@ -1524,8 +1524,10 @@ static void M()
[|C|] n1 = new C();
}
}";
await TestMissingInRegularAndScriptAsync(source,
new TestParameters(options: ImplicitTypeSilentEnforcement()));
await TestDiagnosticInfoAsync(source,
options: ImplicitTypeSilentEnforcement(),
diagnosticId: IDEDiagnosticIds.UseImplicitTypeDiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Hidden);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseImplicitType)]
......
......@@ -84,6 +84,7 @@ static void Main(string[] args)
return AssertCodeCleanupResult(expected, code,
(CodeCleanupOptions.AreCodeCleanupRulesConfigured, enabled: true),
(CodeCleanupOptions.SortImports, enabled: true),
(CodeCleanupOptions.ApplyImplicitExplicitTypePreferences, enabled: false),
(CodeCleanupOptions.AddAccessibilityModifiers, enabled: false));
}
......
......@@ -888,9 +888,9 @@ void Handler(object sender, EventArgs args)
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsQualifyMemberAccess)]
public async Task QualifyMemberAccessNotPresentOnNotificationOptionSilent()
public async Task QualifyMemberAccessOnNotificationOptionSilent()
{
await TestMissingAsyncWithOptionAndNotificationOption(
await TestAsyncWithOptionAndNotificationOption(
@"class Class
{
int Property { get; set; };
......@@ -900,6 +900,15 @@ void M()
[|Property|] = 1;
}
}",
@"class Class
{
int Property { get; set; };
void M()
{
this.Property = 1;
}
}",
CodeStyleOptions.QualifyPropertyAccess, NotificationOption.Silent);
}
......
......@@ -434,9 +434,10 @@ CodeStyleOptions.QualifyEventAccess)
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsQualifyMemberAccess)>
Public Async Function QualifyMemberAccessNotPresentOnNotificationOptionSilent() As Task
Await TestMissingAsyncWithOptionAndNotification(
Public Async Function QualifyMemberAccessOnNotificationOptionSilent() As Task
Await TestAsyncWithOptionAndNotification(
"Class C : Property I As Integer : Sub M() : [|I|] = 1 : End Sub : End Class",
"Class C : Property I As Integer : Sub M() : Me.I = 1 : End Sub : End Class",
CodeStyleOptions.QualifyPropertyAccess, NotificationOption.Silent)
End Function
......
// 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.Diagnostics;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle
......@@ -62,9 +58,7 @@ private void HandleVariableDeclaration(SyntaxNodeAnalysisContext context)
var typeStyle = Helper.AnalyzeTypeName(
declaredType, semanticModel, optionSet, cancellationToken);
if (!typeStyle.IsStylePreferred ||
typeStyle.Severity.WithDefaultSeverity(DiagnosticSeverity.Hidden) >= ReportDiagnostic.Hidden ||
!typeStyle.CanConvert())
if (!typeStyle.IsStylePreferred || !typeStyle.CanConvert())
{
return;
}
......
......@@ -31,7 +31,7 @@ public UseExpressionBodyCodeFixProvider()
}
protected override bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic)
=> diagnostic.Severity != DiagnosticSeverity.Hidden ||
=> !diagnostic.IsSuppressed ||
diagnostic.Properties.ContainsKey(UseExpressionBodyDiagnosticAnalyzer.FixesError);
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
......
......@@ -30,7 +30,7 @@ public override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray.Create(IDEDiagnosticIds.UseLocalFunctionDiagnosticId);
protected override bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic)
=> diagnostic.Severity != DiagnosticSeverity.Hidden && !diagnostic.IsSuppressed;
=> !diagnostic.IsSuppressed;
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
......
......@@ -2,7 +2,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
......@@ -118,7 +117,7 @@ void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
var namingStyleRules = namingPreferences.Rules;
if (!namingStyleRules.TryGetApplicableRule(symbol, out var applicableRule) ||
applicableRule.EnforcementLevel.WithDefaultSeverity(DiagnosticSeverity.Hidden) >= ReportDiagnostic.Hidden)
applicableRule.EnforcementLevel == ReportDiagnostic.Suppress)
{
return null;
}
......
......@@ -128,6 +128,6 @@ private bool IsFrameworkTypePreferred(OptionSet optionSet, PerLanguageOption<Cod
/// </summary>
/// <remarks>if predefined type is not preferred, it implies the preference is framework type.</remarks>
private static bool OptionSettingPrefersFrameworkType(CodeStyleOption<bool> optionValue, ReportDiagnostic severity)
=> !optionValue.Value && severity.WithDefaultSeverity(DiagnosticSeverity.Hidden) < ReportDiagnostic.Hidden;
=> !optionValue.Value && severity != ReportDiagnostic.Suppress;
}
}
......@@ -121,19 +121,20 @@ private void AnalyzeOperation(OperationAnalysisContext context, IOperation opera
var optionValue = optionSet.GetOption(applicableOption, context.Operation.Syntax.Language);
var shouldOptionBePresent = optionValue.Value;
var isQualificationPresent = IsAlreadyQualifiedMemberAccess(simpleName);
if (shouldOptionBePresent && !isQualificationPresent)
var severity = optionValue.Notification.Severity;
if (!shouldOptionBePresent || severity == ReportDiagnostic.Suppress)
{
var severity = optionValue.Notification.Severity;
if (severity.WithDefaultSeverity(DiagnosticSeverity.Hidden) < ReportDiagnostic.Hidden)
{
context.ReportDiagnostic(DiagnosticHelper.Create(
Descriptor,
GetLocation(operation),
severity,
additionalLocations: null,
properties: null));
}
return;
}
if (!IsAlreadyQualifiedMemberAccess(simpleName))
{
context.ReportDiagnostic(DiagnosticHelper.Create(
Descriptor,
GetLocation(operation),
severity,
additionalLocations: null,
properties: null));
}
}
......
// 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.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
......@@ -41,7 +39,7 @@ private void AnalyzeOperation(OperationAnalysisContext context)
var option = optionSet.GetOption(CodeStyleOptions.PreferExplicitTupleNames, context.Compilation.Language);
var severity = option.Notification.Severity;
if (severity.WithDefaultSeverity(DiagnosticSeverity.Hidden) >= ReportDiagnostic.Hidden)
if (severity == ReportDiagnostic.Suppress)
{
return;
}
......
......@@ -48,14 +48,17 @@ void Method()
var analyzerType = typeof(CSharpUseExplicitTypeDiagnosticAnalyzer);
var analyzerResult = await AnalyzeAsync(workspace, workspace.CurrentSolution.ProjectIds.First(), analyzerType);
Assert.True(analyzerResult.IsEmpty);
var diagnostics = analyzerResult.SemanticLocals[analyzerResult.DocumentIds.First()];
Assert.Equal(IDEDiagnosticIds.UseExplicitTypeDiagnosticId, diagnostics[0].Id);
Assert.Equal(DiagnosticSeverity.Hidden, diagnostics[0].Severity);
// set option
workspace.Options = workspace.Options.WithChangedOption(CSharpCodeStyleOptions.UseImplicitTypeWhereApparent, new CodeStyleOption<bool>(false, NotificationOption.Suggestion));
analyzerResult = await AnalyzeAsync(workspace, workspace.CurrentSolution.ProjectIds.First(), analyzerType);
var diagnostics = analyzerResult.SemanticLocals[analyzerResult.DocumentIds.First()];
diagnostics = analyzerResult.SemanticLocals[analyzerResult.DocumentIds.First()];
Assert.Equal(IDEDiagnosticIds.UseExplicitTypeDiagnosticId, diagnostics[0].Id);
Assert.Equal(DiagnosticSeverity.Info, diagnostics[0].Severity);
}
}
......
......@@ -18,16 +18,17 @@ internal struct TypeStyleResult
private readonly CancellationToken _cancellationToken;
/// <summary>
/// Whether or not converting would transition the code to the style the user prefers. i.e.
/// if the user likes 'var' for everything, and you have 'int i = 0' then IsStylePreffered
/// will be true. however, if the user likes 'var' for everything and you have 'var i = 0',
/// then it's still possible to convert that, it would just be 'false' for IsStylePreferred
/// because it goes against the user's preferences.
///
/// In general, most features should only convert the type if IsStylePreferred is true. The
/// one exception is the refactoring, which is explicitly there to still let people convert
/// things quickly, even if it's going against their stated style.
/// Whether or not converting would transition the code to the style the user prefers. i.e. if the user likes
/// <c>var</c> for everything, and you have <c>int i = 0</c> then <see cref="IsStylePreferred"/> will be
/// <see langword="true"/>. However, if the user likes <c>var</c> for everything and you have <c>var i = 0</c>,
/// then it's still possible to convert that, it would just be <see langword="false"/> for
/// <see cref="IsStylePreferred"/> because it goes against the user's preferences.
/// </summary>
/// <remarks>
/// <para>In general, most features should only convert the type if <see cref="IsStylePreferred"/> is
/// <see langword="true"/>. The one exception is the refactoring, which is explicitly there to still let people
/// convert things quickly, even if it's going against their stated style.</para>
/// </remarks>
public readonly bool IsStylePreferred;
public readonly ReportDiagnostic Severity;
......
......@@ -98,6 +98,7 @@ public static bool TryParseNotification(string value, out NotificationOption not
notification = NotificationOption.None;
return true;
case EditorConfigSeverityStrings.Refactoring:
case EditorConfigSeverityStrings.Silent:
notification = NotificationOption.Silent;
return true;
......
......@@ -31,7 +31,7 @@ public DiagnosticSeverity Value
}
public static readonly NotificationOption None = new NotificationOption(WorkspacesResources.None, ReportDiagnostic.Suppress);
public static readonly NotificationOption Silent = new NotificationOption(WorkspacesResources.None, ReportDiagnostic.Hidden);
public static readonly NotificationOption Silent = new NotificationOption(WorkspacesResources.Refactoring_Only, ReportDiagnostic.Hidden);
public static readonly NotificationOption Suggestion = new NotificationOption(WorkspacesResources.Suggestion, ReportDiagnostic.Info);
public static readonly NotificationOption Warning = new NotificationOption(WorkspacesResources.Warning, ReportDiagnostic.Warn);
public static readonly NotificationOption Error = new NotificationOption(WorkspacesResources.Error, ReportDiagnostic.Error);
......
......@@ -52,6 +52,7 @@ private static ReportDiagnostic ParseEnforcementLevel(string ruleSeverity)
case EditorConfigSeverityStrings.None:
return ReportDiagnostic.Suppress;
case EditorConfigSeverityStrings.Refactoring:
case EditorConfigSeverityStrings.Silent:
return ReportDiagnostic.Hidden;
......
......@@ -5,6 +5,7 @@ namespace Microsoft.CodeAnalysis
internal static class EditorConfigSeverityStrings
{
public const string None = "none";
public const string Refactoring = "refactoring";
public const string Silent = "silent";
public const string Suggestion = "suggestion";
public const string Warning = "warning";
......
......@@ -1052,6 +1052,15 @@ internal class WorkspacesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Refactoring Only.
/// </summary>
internal static string Refactoring_Only {
get {
return ResourceManager.GetString("Refactoring_Only", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Removed:.
/// </summary>
......
......@@ -616,4 +616,7 @@
<data name="Changing_document_property_is_not_supported" xml:space="preserve">
<value>Changing document properties is not supported</value>
</data>
<data name="Refactoring_Only" xml:space="preserve">
<value>Refactoring Only</value>
</data>
</root>
\ No newline at end of file
......@@ -7,6 +7,11 @@
<target state="translated">Došlo k chybě při čtení zadaného konfiguračního souboru: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">Symbol {0} nepochází ze zdroje.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">Beim Lesen der angegebenen Konfigurationsdatei ist ein Fehler aufgetreten: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">Symbol "{0}" ist nicht aus Quelle.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">Error al leer el archivo de configuración especificado: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">El símbolo "{0}" no procede del código fuente.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">Une erreur s'est produite lors de la lecture du fichier de configuration spécifié : {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">Le symbole "{0}" ne provient pas de la source.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">Si è verificato un errore durante la lettura del file di configurazione specificato: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">Il simbolo "{0}" non proviene dall'origine.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">指定した構成ファイルの読み取り中にエラーが発生しました: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">シンボル "{0}" は、ソースからではありません。</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">지정한 구성 파일을 읽는 동안 오류가 발생했습니다({0}).</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">"{0}" 기호가 소스에 없습니다.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">Wystąpił błąd podczas odczytywania określonego pliku konfiguracji: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">Symbol „{0}” nie pochodzi ze źródła.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">Ocorreu um erro ao ler o arquivo de configuração especificado: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">Símbolo "{0}" não é da fonte.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">Произошла ошибка при чтении указанного файла конфигурации: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">Символ "{0}" не из источника.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">Belirtilen yapılandırma dosyası okunurken bir hata oluştu: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">"{0}" sembolü kaynağa ait değil.</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">读取指定的配置文件时出错: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">符号“{0}”不是来自源。</target>
......
......@@ -7,6 +7,11 @@
<target state="translated">讀取指定的組態檔時發生錯誤: {0}</target>
<note />
</trans-unit>
<trans-unit id="Refactoring_Only">
<source>Refactoring Only</source>
<target state="new">Refactoring Only</target>
<note />
</trans-unit>
<trans-unit id="Symbol_0_is_not_from_source">
<source>Symbol "{0}" is not from source.</source>
<target state="translated">符號 "{0}" 非來自來源。</target>
......
......@@ -14,12 +14,14 @@ public class EditorConfigCodeStyleParserTests
{
[Theory]
[InlineData("true:none", true, ReportDiagnostic.Suppress)]
[InlineData("true:refactoring", true, ReportDiagnostic.Hidden)]
[InlineData("true:silent", true, ReportDiagnostic.Hidden)]
[InlineData("true:suggestion", true, ReportDiagnostic.Info)]
[InlineData("true:warning", true, ReportDiagnostic.Warn)]
[InlineData("true:error", true, ReportDiagnostic.Error)]
[InlineData("true", false, ReportDiagnostic.Hidden)]
[InlineData("false:none", false, ReportDiagnostic.Suppress)]
[InlineData("false:refactoring", false, ReportDiagnostic.Hidden)]
[InlineData("false:silent", false, ReportDiagnostic.Hidden)]
[InlineData("false:suggestion", false, ReportDiagnostic.Info)]
[InlineData("false:warning", false, ReportDiagnostic.Warn)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册