Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to strike out obsolete symbols #72156

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,8 @@ class C
""",
testHost,
Namespace("System"),
Class("Obsolete"));
Class("Obsolete"),
Obsolete("C"));
}

[Theory, CombinatorialData]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.UnitTests.ObsoleteSymbol;
using Microsoft.CodeAnalysis.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ObsoleteSymbol;

public class CSharpObsoleteSymbolTests : AbstractObsoleteSymbolTests
{
protected override EditorTestWorkspace CreateWorkspace(string markup)
=> EditorTestWorkspace.CreateCSharp(markup);

[Theory]
[InlineData("class")]
[InlineData("struct")]
[InlineData("record")]
[InlineData("record class")]
[InlineData("record struct")]
[InlineData("interface")]
[InlineData("enum")]
public async Task TestObsoleteTypeDefinition(string keyword)
{
await TestAsync(
$$"""
[System.Obsolete]
{{keyword}} [|ObsoleteType|]
{
}

{{keyword}} NonObsoleteType
{
}
""");
}

[Fact]
public async Task TestObsoleteDelegateTypeDefinition()
{
await TestAsync(
"""
[System.Obsolete]
delegate void [|ObsoleteType|]();

delegate void NonObsoleteType();
""");
}

[Fact]
public async Task TestDeclarationAndUseOfObsoleteAlias()
{
await TestAsync(
"""
using [|ObsoleteAlias|] = [|ObsoleteType|];

[System.Obsolete]
class [|ObsoleteType|];

/// <seealso cref="[|ObsoleteType|]"/>
/// <seealso cref="[|ObsoleteAlias|]"/>
class NonObsoleteType
{
[|ObsoleteAlias|] field = new [|ObsoleteType|]();
}
""");
}

[Fact]
public async Task TestParametersAndReturnTypes()
{
await TestAsync(
"""
[System.Obsolete]
class [|ObsoleteType|];

class NonObsoleteType([|ObsoleteType|] field2)
{
[|ObsoleteType|] Method([|ObsoleteType|] arg) => [|new|]();

System.Func<[|ObsoleteType|], [|ObsoleteType|]> field = [|ObsoleteType|] ([|ObsoleteType|] arg) => [|new|]();
}
""");
}

[Fact]
public async Task TestImplicitType()
{
await TestAsync(
"""
[System.Obsolete]
class [|ObsoleteType|]
{
public ObsoleteType() { }

[System.Obsolete]
public [|ObsoleteType|](int x) { }
}

class ObsoleteCtor
{
public ObsoleteCtor() { }

[System.Obsolete]
public [|ObsoleteCtor|](int x) { }
}

class C
{
void Method()
{
[|var|] t1 = new [|ObsoleteType|]();
[|var|] t2 = [|new|] [|ObsoleteType|](3);
[|ObsoleteType|] t3 = [|new|]();
[|ObsoleteType|] t4 = [|new|](3);
[|var|] t5 = CreateObsoleteType();
var t6 = nameof([|ObsoleteType|]);

var u1 = new ObsoleteCtor();
var u2 = [|new|] ObsoleteCtor(3);
ObsoleteCtor u3 = new();
ObsoleteCtor u4 = [|new|](3);
var u6 = nameof(ObsoleteCtor);

[|ObsoleteType|] CreateObsoleteType() => [|new|]();
}
}
""");
}

[Fact]
public async Task TestExtensionMethods()
{
await TestAsync(
"""
[System.Obsolete]
static class [|ObsoleteType|]
{
public static void ObsoleteMember1(this C ignored) { }

[System.Obsolete]
public static void [|ObsoleteMember2|](this C ignored) { }
}

class C
{
void Method()
{
this.ObsoleteMember1();
this.[|ObsoleteMember2|]();
[|ObsoleteType|].ObsoleteMember1(this);
[|ObsoleteType|].[|ObsoleteMember2|](this);
}
}
""");
}

[Fact]
public async Task TestGenerics()
{
await TestAsync(
"""
[System.Obsolete]
class [|ObsoleteType|];

[System.Obsolete]
struct [|ObsoleteValueType|];

class G<T>
{
}

class C
{
void M<T>() { }

/// <summary>
/// This looks like a reference to an obsolete type, but it's actually just an identifier alias for the
/// generic type parameter 'T'.
/// </summary>
/// <seealso cref="G{ObsoleteType}"/>
void Method()
{
_ = new G<[|ObsoleteType|]>();
_ = new G<G<[|ObsoleteType|]>>();
M<[|ObsoleteType|]>();
M<G<[|ObsoleteType|]>>();
M<G<G<[|ObsoleteType|]>>>();

// Mark 'var' as obsolete even when it points to Nullable<T> where T is obsolete
[|var|] nullableValue = CreateNullableValueType();

[|ObsoleteValueType|]? CreateNullableValueType() => new [|ObsoleteValueType|]();
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ public ReassignedVariableFormatDefinition()
}
#endregion

#region Obsolete Symobl
[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = ClassificationTypeNames.ObsoleteSymbol)]
[Name(ClassificationTypeNames.ObsoleteSymbol)]
[Order(After = Priority.High)]
[UserVisible(false)]
[ExcludeFromCodeCoverage]
private class ObsoleteSymbolFormatDefinition : ClassificationFormatDefinition
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public ObsoleteSymbolFormatDefinition()
{
this.DisplayName = EditorFeaturesResources.Obsolete_symbol;
this.TextDecorations = System.Windows.TextDecorations.Strikethrough;
}
}
#endregion

#region Symbol - Static
[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = ClassificationTypeNames.StaticSymbol)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ internal sealed class ClassificationTypeDefinitions
internal readonly ClassificationTypeDefinition ReassignedVariableTypeDefinition;
#endregion

#region Obsolete Symbol
[Export]
[Name(ClassificationTypeNames.ObsoleteSymbol)]
[BaseDefinition(PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition ObsoleteSymbolTypeDefinition;
#endregion

#region Static Symbol
[Export]
[Name(ClassificationTypeNames.StaticSymbol)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ protected sealed override ITaggerEventSource CreateEventSource(ITextView textVie
TaggerEventSources.OnViewSpanChanged(ThreadingContext, textView),
TaggerEventSources.OnWorkspaceChanged(subjectBuffer, AsyncListener),
TaggerEventSources.OnDocumentActiveContextChanged(subjectBuffer),
TaggerEventSources.OnGlobalOptionChanged(_globalOptions, ClassificationOptionsStorage.ClassifyReassignedVariables));
TaggerEventSources.OnGlobalOptionChanged(_globalOptions, ClassificationOptionsStorage.ClassifyReassignedVariables),
TaggerEventSources.OnGlobalOptionChanged(_globalOptions, ClassificationOptionsStorage.ClassifyObsoleteSymbols));
}

protected sealed override Task ProduceTagsAsync(
Expand Down
3 changes: 3 additions & 0 deletions src/EditorFeatures/Core/EditorFeaturesResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -923,4 +923,7 @@ Do you want to proceed?</value>
<data name="Roslyn_Test_Code_Markup" xml:space="preserve">
<value>Roslyn Test Code Markup</value>
</data>
<data name="Obsolete_symbol" xml:space="preserve">
<value>Obsolete symbol</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading