Skip to content

Commit

Permalink
Merge pull request #35 from naminodarie/feature/create_operator
Browse files Browse the repository at this point in the history
Update CreateOperatorCodeFixProvider
  • Loading branch information
kzrnm authored Feb 11, 2021
2 parents 7105cec + 566fff7 commit 252b174
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - 2021-02-11
### Changed
- Update CreateOperatorCodeFixProvider
- Use Span<T>.Fill
- CeilPow2 AggressiveInlining

Expand Down
2 changes: 1 addition & 1 deletion Source/AtCoderAnalyzer/CreateOperatorCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var writtenTypeSyntax = writtenTypeSyntaxes[i];
var originalType = originalTypes[i];
var constraintTypes = originalType.ConstraintTypes;
var writtenType = writtenTypes[i] as INamedTypeSymbol;
var writtenType = writtenTypes[i] as ITypeSymbol;

if (!constraintTypes
.OfType<INamedTypeSymbol>()
Expand Down
13 changes: 5 additions & 8 deletions Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ internal class SimplifyTypeSyntaxRewriter : CSharpSyntaxRewriter
private ImmutableHashSet<string> Usings { get; }
public SimplifyTypeSyntaxRewriter(ImmutableHashSet<string> usings) => Usings = usings;

public override SyntaxNode Visit(SyntaxNode node)
public override SyntaxNode VisitQualifiedName(QualifiedNameSyntax node)
{
if (node is QualifiedNameSyntax qualified)
{
if (Usings.Contains(qualified.Left.ToString()))
return qualified.Right;
return qualified;
}
return base.Visit(node);
var right = (SimpleNameSyntax)Visit(node.Right);
if (Usings.Contains(node.Left.ToString()))
return right;
return SyntaxFactory.QualifiedName(node.Left, right);
}
}
}
98 changes: 95 additions & 3 deletions Test/AtCoderAnalyzer.Test/CreateOperatorCodeFixProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ struct Op : System.Collections.Generic.IComparer<short>
public int Compare(short x, short y) => default;
}";
await VerifyCS.VerifyCodeFixAsync(source,
VerifyCS.Diagnostic().WithSpan(6, 5, 6, 23).WithArguments("Op"),
VerifyCS.Diagnostic("AC0008").WithSpan(6, 5, 6, 23).WithArguments("Op"),
fixedSource);
}

Expand Down Expand Up @@ -716,7 +716,7 @@ public void Init(ref (int, long) val, out bool success, params int[] nums)
public (int, long) Prop2 { set; get; }
}";
await VerifyCS.VerifyCodeFixAsync(source,
VerifyCS.Diagnostic().WithSpan(12, 5, 12, 29).WithArguments("Op"),
VerifyCS.Diagnostic("AC0008").WithSpan(12, 5, 12, 29).WithArguments("Op"),
fixedSource);
}

Expand Down Expand Up @@ -770,8 +770,100 @@ public void Init()
public (int n, long m) Prop2 { set; get; }
}";
await VerifyCS.VerifyCodeFixAsync(source,
VerifyCS.Diagnostic().WithSpan(14, 9, 14, 31).WithArguments("Op"),
VerifyCS.Diagnostic("AC0008").WithSpan(14, 9, 14, 31).WithArguments("Op"),
fixedSource);
}

[Fact]
public async Task Array()
{
var source = @"
using AtCoder;
using System.Runtime.CompilerServices;
[IsOperator]
public interface IAny<T> {
T Prop { get; }
}
class Program
{
static void M<T, TOp>() where TOp : struct, IAny<T> {}
static void Run()
{
M<System.Numerics.BigInteger[], BigOp>();
}
}
";
var fixedSource = @"
using AtCoder;
using System.Runtime.CompilerServices;
[IsOperator]
public interface IAny<T> {
T Prop { get; }
}
class Program
{
static void M<T, TOp>() where TOp : struct, IAny<T> {}
static void Run()
{
M<System.Numerics.BigInteger[], BigOp>();
}
}
struct BigOp : IAny<System.Numerics.BigInteger[]>
{
public System.Numerics.BigInteger[] Prop => default;
}";
await VerifyCS.VerifyCodeFixAsync(source,
VerifyCS.Diagnostic("AC0008").WithSpan(14, 9, 14, 47).WithArguments("BigOp"),
fixedSource);
}

[Fact]
public async Task Generic()
{
var source = @"
using AtCoder;
using System.Runtime.CompilerServices;
[IsOperator]
public interface IAny<T> {
T Prop { get; }
}
class Program
{
static void M<T, TOp>() where TOp : struct, IAny<T> {}
static void Run()
{
M<StaticModInt<Mod1000000007>, ModOp>();
}
}
";
var fixedSource = @"
using AtCoder;
using System.Runtime.CompilerServices;
[IsOperator]
public interface IAny<T> {
T Prop { get; }
}
class Program
{
static void M<T, TOp>() where TOp : struct, IAny<T> {}
static void Run()
{
M<StaticModInt<Mod1000000007>, ModOp>();
}
}
struct ModOp : IAny<StaticModInt<Mod1000000007>>
{
public StaticModInt<Mod1000000007> Prop => default;
}";
await VerifyCS.VerifyCodeFixAsync(source,
VerifyCS.Diagnostic("AC0008").WithSpan(14, 9, 14, 46).WithArguments("ModOp"),
fixedSource);
}
}
}

0 comments on commit 252b174

Please sign in to comment.