Skip to content
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 @@ -29,15 +29,34 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
if (nodeToFix is null)
return;

// Check if C# 14 or later is available
var isCSharp14OrAbove = false;
if (context.Document.Project.ParseOptions is CSharpParseOptions parseOptions)
{
isCSharp14OrAbove = parseOptions.LanguageVersion.IsCSharp14OrAbove();
}

// Always offer partial method
context.RegisterCodeFix(
CodeAction.Create(
"Use Regex Source Generator",
cancellationToken => ConvertToSourceGenerator(context.Document, context.Diagnostics[0], cancellationToken),
equivalenceKey: "Use Regex Source Generator"),
"Use Regex Source Generator (partial method)",
cancellationToken => ConvertToSourceGenerator(context.Document, context.Diagnostics[0], usePartialProperty: false, cancellationToken),
equivalenceKey: "Use Regex Source Generator (partial method)"),
context.Diagnostics);

// Offer partial property if C# 14 or later
if (isCSharp14OrAbove)
{
context.RegisterCodeFix(
CodeAction.Create(
"Use Regex Source Generator (partial property)",
cancellationToken => ConvertToSourceGenerator(context.Document, context.Diagnostics[0], usePartialProperty: true, cancellationToken),
equivalenceKey: "Use Regex Source Generator (partial property)"),
context.Diagnostics);
}
}

private static async Task<Document> ConvertToSourceGenerator(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
private static async Task<Document> ConvertToSourceGenerator(Document document, Diagnostic diagnostic, bool usePartialProperty, CancellationToken cancellationToken)
{
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
var generator = editor.Generator;
Expand Down Expand Up @@ -97,11 +116,19 @@ private static async Task<Document> ConvertToSourceGenerator(Document document,

var newTypeDeclaration = typeDeclaration;

// Use new method
// Use new method or property
if (operation is IObjectCreationOperation)
{
var invokeMethod = generator.InvocationExpression(generator.IdentifierName(methodName));
newTypeDeclaration = newTypeDeclaration.ReplaceNode(nodeToFix, invokeMethod);
if (usePartialProperty)
{
var accessProperty = generator.IdentifierName(methodName);
newTypeDeclaration = newTypeDeclaration.ReplaceNode(nodeToFix, accessProperty);
}
else
{
var invokeMethod = generator.InvocationExpression(generator.IdentifierName(methodName));
newTypeDeclaration = newTypeDeclaration.ReplaceNode(nodeToFix, invokeMethod);
}
}
else if (operation is IInvocationOperation invocationOperation)
{
Expand All @@ -117,10 +144,18 @@ private static async Task<Document> ConvertToSourceGenerator(Document document,
arguments = arguments.RemoveAt(index.GetValueOrDefault());
}

var createRegexMethod = generator.InvocationExpression(generator.IdentifierName(methodName));
var method = generator.InvocationExpression(generator.MemberAccessExpression(createRegexMethod, invocationOperation.TargetMethod.Name), [.. arguments.Select(arg => arg.Syntax)]);

newTypeDeclaration = newTypeDeclaration.ReplaceNode(nodeToFix, method);
if (usePartialProperty)
{
var accessProperty = generator.IdentifierName(methodName);
var method = generator.InvocationExpression(generator.MemberAccessExpression(accessProperty, invocationOperation.TargetMethod.Name), [.. arguments.Select(arg => arg.Syntax)]);
newTypeDeclaration = newTypeDeclaration.ReplaceNode(nodeToFix, method);
}
else
{
var createRegexMethod = generator.InvocationExpression(generator.IdentifierName(methodName));
var method = generator.InvocationExpression(generator.MemberAccessExpression(createRegexMethod, invocationOperation.TargetMethod.Name), [.. arguments.Select(arg => arg.Syntax)]);
newTypeDeclaration = newTypeDeclaration.ReplaceNode(nodeToFix, method);
}
}

// Generate method
Expand Down Expand Up @@ -150,25 +185,62 @@ private static async Task<Document> ConvertToSourceGenerator(Document document,
regexOptionsValue = generator.MemberAccessExpression(generator.TypeExpression(compilation.GetBestTypeByMetadataName("System.Text.RegularExpressions.RegexOptions")!), "None");
}

var newMethod = (MethodDeclarationSyntax)generator.MethodDeclaration(
name: methodName,
returnType: generator.TypeExpression(regexSymbol),
modifiers: DeclarationModifiers.Static | DeclarationModifiers.Partial,
accessibility: Accessibility.Private);
SyntaxNode newMember;

if (usePartialProperty)
{
// Generate partial property manually to ensure proper syntax
var propertyType = (TypeSyntax)generator.TypeExpression(regexSymbol);
var accessorList = AccessorList(
List([
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
]));

var newProperty = PropertyDeclaration(propertyType, methodName)
.WithModifiers(TokenList(
Token(SyntaxKind.PrivateKeyword),
Token(SyntaxKind.StaticKeyword),
Token(SyntaxKind.PartialKeyword)))
.WithAccessorList(accessorList);

newMethod = newMethod.ReplaceToken(newMethod.Identifier, Identifier(methodName).WithAdditionalAnnotations(RenameAnnotation.Create()));
newProperty = newProperty.ReplaceToken(newProperty.Identifier, Identifier(methodName).WithAdditionalAnnotations(RenameAnnotation.Create()));

// Extract arguments (pattern,options,timeout)
var attributes = generator.Attribute(generator.TypeExpression(regexGeneratorAttributeSymbol), attributeArguments: (patternValue, regexOptionsValue, timeoutValue) switch
// Extract arguments (pattern,options,timeout)
var attributes = generator.Attribute(generator.TypeExpression(regexGeneratorAttributeSymbol), attributeArguments: (patternValue, regexOptionsValue, timeoutValue) switch
{
({ }, null, null) => [patternValue],
({ }, { }, null) => [patternValue, regexOptionsValue],
({ }, { }, { }) => [patternValue, regexOptionsValue, AttributeArgument((ExpressionSyntax)timeoutValue).WithNameColon(NameColon(IdentifierName("matchTimeoutMilliseconds")))],
_ => Array.Empty<SyntaxNode>(),
});

newMember = (PropertyDeclarationSyntax)generator.AddAttributes(newProperty, attributes);
}
else
{
({ }, null, null) => [patternValue],
({ }, { }, null) => [patternValue, regexOptionsValue],
({ }, { }, { }) => [patternValue, regexOptionsValue, AttributeArgument((ExpressionSyntax)timeoutValue).WithNameColon(NameColon(IdentifierName("matchTimeoutMilliseconds")))],
_ => Array.Empty<SyntaxNode>(),
});
// Generate partial method
var newMethod = (MethodDeclarationSyntax)generator.MethodDeclaration(
name: methodName,
returnType: generator.TypeExpression(regexSymbol),
modifiers: DeclarationModifiers.Static | DeclarationModifiers.Partial,
accessibility: Accessibility.Private);

newMethod = newMethod.ReplaceToken(newMethod.Identifier, Identifier(methodName).WithAdditionalAnnotations(RenameAnnotation.Create()));

// Extract arguments (pattern,options,timeout)
var attributes = generator.Attribute(generator.TypeExpression(regexGeneratorAttributeSymbol), attributeArguments: (patternValue, regexOptionsValue, timeoutValue) switch
{
({ }, null, null) => [patternValue],
({ }, { }, null) => [patternValue, regexOptionsValue],
({ }, { }, { }) => [patternValue, regexOptionsValue, AttributeArgument((ExpressionSyntax)timeoutValue).WithNameColon(NameColon(IdentifierName("matchTimeoutMilliseconds")))],
_ => Array.Empty<SyntaxNode>(),
});

newMember = (MethodDeclarationSyntax)generator.AddAttributes(newMethod, attributes);
}

newMethod = (MethodDeclarationSyntax)generator.AddAttributes(newMethod, attributes);
newTypeDeclaration = newTypeDeclaration.AddMembers(newMethod);
newTypeDeclaration = newTypeDeclaration.AddMembers((MemberDeclarationSyntax)newMember);
return document.WithSyntaxRoot(root.ReplaceNode(typeDeclaration, newTypeDeclaration));
}

Expand Down
Loading