Skip to content

Commit

Permalink
Support Unity (#1349)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Jan 9, 2024
1 parent 2354f1f commit 77688a2
Show file tree
Hide file tree
Showing 112 changed files with 13,095 additions and 413 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ jobs:
working-directory: src/${{ matrix.component.name }}.CodeFixes
steps:
- uses: actions/checkout@v3
- run: dotnet restore
- run: dotnet build --no-restore /p:Roslynator${{ matrix.component.propertyName }}NuGet=true
- run: dotnet restore --force /p:RoslynVersion=roslyn3.8
- run: dotnet build --no-restore /p:Roslynator${{ matrix.component.propertyName }}NuGet=true /p:RoslynVersion=roslyn3.8
- run: dotnet restore --force /p:RoslynVersion=roslyn4.7
- run: dotnet build --no-restore /p:Roslynator${{ matrix.component.propertyName }}NuGet=true /p:RoslynVersion=roslyn4.7
- run: dotnet pack --no-build
- uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -158,7 +160,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: dotnet restore
- run: dotnet build --no-restore /p:DefineConstants=VSCODE
- run: dotnet build --no-restore
- run: |
mkdir package/roslyn/analyzers
mkdir package/roslyn/refactorings
Expand Down
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add support for Unity ([PR](https://github.com/dotnet/roslynator/pull/1349))
- [Unity uses Roslyn 3.8](https://docs.unity3d.com/Manual/roslyn-analyzers.html) and this version is now supported by Roslynator NuGet packages with analyzers (Roslynator.Analyzers etc.)

### Fixed

- Fix analyzer [RCS0034](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0034) ([PR](https://github.com/dotnet/roslynator/pull/1351))
Expand Down
6 changes: 5 additions & 1 deletion src/Analyzers.CodeFixes/Analyzers.CodeFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(RoslynVersion)' != ''">
<BaseOutputPath>bin\$(RoslynVersion)\</BaseOutputPath>
</PropertyGroup>

<PropertyGroup>
<Version>$(RoslynatorAnalyzersVersion)</Version>
<AssemblyName>Roslynator.CSharp.Analyzers.CodeFixes</AssemblyName>
Expand All @@ -12,7 +16,7 @@
<NuspecProperties>configuration=$(Configuration);version=$(RoslynatorAnalyzersPackageVersion)</NuspecProperties>
<IsPackable>true</IsPackable>
<IncludeSymbols>false</IncludeSymbols>
</PropertyGroup>
</PropertyGroup>

<ItemGroup>
<None Remove="tools\install.ps1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ private static async Task<Document> RefactorAsync(
newTypeDeclaration = recordDeclaration.WithBaseList(baseList.WithTypes(baseTypes));
}
else if (kind == SyntaxKind.StructDeclaration
|| kind == SyntaxKind.RecordStructDeclaration)
#if ROSLYN_4_0
|| kind == SyntaxKind.RecordStructDeclaration
#endif
)
{
var structDeclaration = (StructDeclarationSyntax)newTypeDeclaration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

CodeAction codeAction = CodeAction.Create(
$"Rename '{oldName}' to '{newName}'",
ct => Renamer.RenameSymbolAsync(
context.Document.Solution(),
parameterSymbol,
default(SymbolRenameOptions),
newName,
ct),
ct =>
#if ROSLYN_4_4
Renamer.RenameSymbolAsync(context.Document.Solution(), parameterSymbol, default(SymbolRenameOptions), newName, ct),
#else
Renamer.RenameSymbolAsync(context.Document.Solution(), parameterSymbol, newName, default(Microsoft.CodeAnalysis.Options.OptionSet), ct),
#endif
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
case SyntaxKind.EmptyStatement:
case SyntaxKind.FinallyClause:
case SyntaxKind.NamespaceDeclaration:
#if ROSLYN_4_0
case SyntaxKind.FileScopedNamespaceDeclaration:
#endif
case SyntaxKind.ObjectCreationExpression:
case SyntaxKind.RegionDirectiveTrivia:
return true;
Expand Down Expand Up @@ -98,6 +100,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(codeAction, diagnostic);
break;
}
#if ROSLYN_4_0
case BaseNamespaceDeclarationSyntax namespaceDeclaration:
{
CodeAction codeAction = CodeAction.Create(
Expand All @@ -108,6 +111,18 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(codeAction, diagnostic);
break;
}
#else
case NamespaceDeclarationSyntax namespaceDeclaration:
{
CodeAction codeAction = CodeAction.Create(
"Remove empty namespace declaration",
ct => document.RemoveNodeAsync(namespaceDeclaration, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
#endif
case RegionDirectiveTriviaSyntax regionDirective:
{
CodeAction codeAction = CodeAction.Create(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if ROSLYN_4_2
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Composition;
Expand Down Expand Up @@ -96,3 +97,4 @@ private static Task<Document> RefactorAsync(
return document.WithTextChangeAsync(interpolatedString.Span, newText, cancellationToken);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ private static async Task<Solution> RefactorAsync(

string newName = NameGenerators.UnderscoreSuffix.EnsureUniqueParameterName("_", anonymousFunctionSymbol, semanticModel, cancellationToken: cancellationToken);

#if ROSLYN_4_4
return await Renamer.RenameSymbolAsync(document.Solution(), parameterSymbol, default(SymbolRenameOptions), newName, cancellationToken).ConfigureAwait(false);
#else
return await Renamer.RenameSymbolAsync(document.Solution(), parameterSymbol, newName, default(Microsoft.CodeAnalysis.Options.OptionSet), cancellationToken).ConfigureAwait(false);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public override ImmutableArray<string> FixableDiagnosticIds
get { return ImmutableArray.Create(DiagnosticIdentifiers.UseExplicitlyOrImplicitlyTypedArray); }
}

#if ROSLYN_4_0
public override FixAllProvider GetFixAllProvider()
{
return FixAllProvider.Create(async (context, document, diagnostics) => await FixAllAsync(document, diagnostics, context.CancellationToken).ConfigureAwait(false));
Expand All @@ -51,6 +52,7 @@ static async Task<Document> FixAllAsync(
return document;
}
}
#endif

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
Expand Down Expand Up @@ -80,39 +82,50 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
diagnostic.Location.SourceSpan,
out SyntaxNode node,
predicate: f => f.IsKind(
#if ROSLYN_4_7
SyntaxKind.CollectionExpression,
#endif
SyntaxKind.ImplicitArrayCreationExpression,
SyntaxKind.ArrayCreationExpression,
SyntaxKind.CollectionExpression)))
SyntaxKind.ArrayCreationExpression)))
{
throw new InvalidOperationException();
}

if (node is ArrayCreationExpressionSyntax arrayCreation)
{
#if ROSLYN_4_7
if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.ExplicitToCollectionExpression))
{
return (ct => ConvertToCollectionExpressionAsync(document, arrayCreation, ct), UseCollectionExpressionTitle);
}
else
{
#endif
return (ct => ConvertToImplicitAsync(document, arrayCreation, ct), UseImplicitlyTypedArrayTitle);
#if ROSLYN_4_7
}
#endif
}
else if (node is ImplicitArrayCreationExpressionSyntax implicitArrayCreation)
{
if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.VarToExplicit))
{
return (ct => ConvertToExplicitAndUseVarAsync(document, implicitArrayCreation, ct), UseCollectionExpressionTitle);
}
#if ROSLYN_4_7
else if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.ImplicitToCollectionExpression))
{
return (ct => ConvertToCollectionExpressionAsync(document, implicitArrayCreation, ct), UseCollectionExpressionTitle);
}
else
{
#endif
return (ct => ConvertToExplicitAsync(document, implicitArrayCreation, ct), UseExplicitlyTypedArrayTitle);
#if ROSLYN_4_7
}
#endif
}
#if ROSLYN_4_7
else if (node is CollectionExpressionSyntax collectionExpression)
{
if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.CollectionExpressionToImplicit))
Expand All @@ -124,6 +137,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
return (ct => ConvertToExplicitAsync(document, collectionExpression, ct), UseExplicitlyTypedArrayTitle);
}
}
#endif
else
{
throw new InvalidOperationException();
Expand Down Expand Up @@ -180,23 +194,6 @@ private static async Task<ArrayCreationExpressionSyntax> CreateArrayCreationAsyn
newInitializer);
}

private static async Task<Document> ConvertToExplicitAsync(
Document document,
CollectionExpressionSyntax collectionExpression,
CancellationToken cancellationToken)
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(collectionExpression, cancellationToken).ConvertedType;

ArrayCreationExpressionSyntax arrayCreation = ArrayCreationExpression(
Token(SyntaxKind.NewKeyword),
(ArrayTypeSyntax)typeSymbol.ToTypeSyntax().WithSimplifierAnnotation(),
ConvertCollectionExpressionToInitializer(collectionExpression, SyntaxKind.ArrayInitializerExpression))
.WithTriviaFrom(collectionExpression);

return await document.ReplaceNodeAsync(collectionExpression, arrayCreation, cancellationToken).ConfigureAwait(false);
}

private static async Task<Document> ConvertToImplicitAsync(
Document document,
ArrayCreationExpressionSyntax arrayCreation,
Expand Down Expand Up @@ -233,6 +230,24 @@ private static async Task<Document> ConvertToImplicitAsync(
return await document.ReplaceNodeAsync(arrayCreation, implicitArrayCreation, cancellationToken).ConfigureAwait(false);
}

#if ROSLYN_4_7
private static async Task<Document> ConvertToExplicitAsync(
Document document,
CollectionExpressionSyntax collectionExpression,
CancellationToken cancellationToken)
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(collectionExpression, cancellationToken).ConvertedType;

ArrayCreationExpressionSyntax arrayCreation = ArrayCreationExpression(
Token(SyntaxKind.NewKeyword),
(ArrayTypeSyntax)typeSymbol.ToTypeSyntax().WithSimplifierAnnotation(),
ConvertCollectionExpressionToInitializer(collectionExpression, SyntaxKind.ArrayInitializerExpression))
.WithTriviaFrom(collectionExpression);

return await document.ReplaceNodeAsync(collectionExpression, arrayCreation, cancellationToken).ConfigureAwait(false);
}

private static async Task<Document> ConvertToImplicitAsync(
Document document,
CollectionExpressionSyntax collectionExpression,
Expand Down Expand Up @@ -269,4 +284,5 @@ private static async Task<Document> ConvertToCollectionExpressionAsync(

return await document.ReplaceNodeAsync(implicitArrayCreation, collectionExpression, cancellationToken).ConfigureAwait(false);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.Span,
out SyntaxNode node,
predicate: f => f.IsKind(
#if ROSLYN_4_7
SyntaxKind.CollectionExpression,
#endif
SyntaxKind.ObjectCreationExpression,
SyntaxKind.ImplicitObjectCreationExpression,
SyntaxKind.CollectionExpression)))
SyntaxKind.ImplicitObjectCreationExpression)))

{
return;
}
Expand All @@ -52,27 +55,35 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

if (node is ObjectCreationExpressionSyntax objectCreation)
{
#if ROSLYN_4_7
bool useCollectionExpression = diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.ExplicitToCollectionExpression);

#endif
CodeAction codeAction = CodeAction.Create(
#if ROSLYN_4_7
(useCollectionExpression)
? UseCollectionExpressionTitle
: UseImplicitObjectCreationTitle,
#else
UseImplicitObjectCreationTitle,
#endif
ct =>
{
SyntaxNode newNode;
#if ROSLYN_4_7
if (useCollectionExpression)
{
newNode = ConvertInitializerToCollectionExpression(objectCreation.Initializer).WithFormatterAnnotation();
}
else
{
#endif
newNode = ImplicitObjectCreationExpression(
objectCreation.NewKeyword.WithTrailingTrivia(objectCreation.NewKeyword.TrailingTrivia.EmptyIfWhitespace()),
objectCreation.ArgumentList ?? ArgumentList().WithTrailingTrivia(objectCreation.Type.GetTrailingTrivia()),
objectCreation.Initializer);
#if ROSLYN_4_7
}
#endif
if (objectCreation.IsParentKind(SyntaxKind.EqualsValueClause)
&& objectCreation.Parent.IsParentKind(SyntaxKind.VariableDeclarator)
Expand All @@ -90,7 +101,14 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return document.ReplaceNodeAsync(objectCreation, newNode, ct);
}
},
GetEquivalenceKey(diagnostic, (useCollectionExpression) ? UseCollectionExpressionEquivalenceKey : null));
GetEquivalenceKey(
diagnostic,
#if ROSLYN_4_7
(useCollectionExpression) ? UseCollectionExpressionEquivalenceKey : null
#else
null
#endif
));

context.RegisterCodeFix(codeAction, diagnostic);
}
Expand Down Expand Up @@ -126,6 +144,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

context.RegisterCodeFix(codeAction, diagnostic);
}
#if ROSLYN_4_7
else if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.ImplicitToCollectionExpression))
{
CodeAction codeAction = CodeAction.Create(
Expand All @@ -146,6 +165,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

context.RegisterCodeFix(codeAction, diagnostic);
}
#endif
else
{
CodeAction codeAction = CodeAction.Create(
Expand Down Expand Up @@ -174,6 +194,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(codeAction, diagnostic);
}
}
#if ROSLYN_4_7
else if (node is CollectionExpressionSyntax collectionExpression)
{
if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.CollectionExpressionToImplicit))
Expand Down Expand Up @@ -228,5 +249,6 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(codeAction, diagnostic);
}
}
#endif
}
}
5 changes: 3 additions & 2 deletions src/Analyzers.CodeFixes/Roslynator.Analyzers.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A collection of 200+ analyzers for C#, powered by Roslyn.

- This package is dependent on Microsoft.CodeAnalysis.CSharp.Workspaces 4.7.0.</description>
- This package requires Roslyn 3.8 or higher.</description>
<summary>A collection of 200+ analyzers for C#, powered by Roslyn.</summary>
<copyright>Copyright (c) 2016-2023 Josef Pihrt</copyright>
<tags>Roslyn Analyzer Refactoring Productivity CodeAnalysis C# CSharp</tags>
Expand All @@ -20,7 +20,8 @@
<readme>docs\README.md</readme>
</metadata>
<files>
<file src="bin\$Configuration$\netstandard2.0\Roslynator*.dll" target="analyzers\dotnet\cs" />
<file src="bin\roslyn3.8\$Configuration$\netstandard2.0\Roslynator*.dll" target="analyzers/dotnet/roslyn3.8/cs" />
<file src="bin\roslyn4.7\$Configuration$\netstandard2.0\Roslynator*.dll" target="analyzers/dotnet/roslyn4.7/cs" />
<file src="tools\*.ps1" target="tools\" />
<file src="..\..\images\roslynator-logo-small.png" target="icon.png" />
<file src="docs\NuGetReadme.md" target="docs\README.md" />
Expand Down
Loading

0 comments on commit 77688a2

Please sign in to comment.