private static void ApplyObserveEventMethodFix( DocumentEditor editor, AssignmentExpressionSyntax assignment, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken) { var usesArg = false; if (methodDeclaration.ParameterList.Parameters.Any()) { using (var pooled = IdentifierNameWalker.Borrow((SyntaxNode)methodDeclaration.Body ?? methodDeclaration.ExpressionBody)) { foreach (var name in pooled.IdentifierNames) { if (name.Identifier.ValueText == methodDeclaration.ParameterList.Parameters[0].Identifier.ValueText) { if (methodDeclaration.ParameterList.Parameters.Count == 1) { usesArg = true; continue; } return; } if (methodDeclaration.ParameterList.Parameters.Count == 2 && name.Identifier.ValueText == methodDeclaration.ParameterList.Parameters[1] .Identifier.ValueText) { usesArg = true; } } } } var eventSymbol = (IEventSymbol)editor.SemanticModel.GetSymbolSafe(assignment.Left, cancellationToken); var observeSubscribe = GetObservableFromEventString(eventSymbol) .Replace("HANDLERTYPE", eventSymbol.Type.ToMinimalDisplayString(editor.SemanticModel, assignment.SpanStart)) .Replace("ARGTYPE", ArgType(eventSymbol)) .Replace("LEFT", assignment.Left.ToString()) .Replace("LAMBDA", Lambda(methodDeclaration, usesArg)); editor.AddUsing(SystemReactiveLinq) .ReplaceNode( assignment, SyntaxFactory.ParseExpression(observeSubscribe) .WithTriviaFrom(assignment) .WithSimplifiedNames() .WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation)); if (methodDeclaration.ParameterList.Parameters.Count == 2) { editor.RemoveNode(methodDeclaration.ParameterList.Parameters[0]); } if (!usesArg && methodDeclaration.ParameterList.Parameters.Any()) { editor.RemoveNode(methodDeclaration.ParameterList.Parameters.Last()); } }
internal static DocumentEditor AddIDisposableInterface(this DocumentEditor editor, TypeDeclarationSyntax type) { if (IsMissing()) { editor.AddUsing(UsingSystem) .AddInterfaceType(type, IDisposableFactory.SystemIDisposable); } return(editor); bool IsMissing() { return(type switch { { BaseList : { Types : { } types } } => !types.TryFirst(x => x == KnownSymbol.IDisposable, out _), _ => true, });
private static void ApplyObserveEventLambdaFix( DocumentEditor editor, AssignmentExpressionSyntax assignment, bool usesArg, CancellationToken cancellationToken) { var eventSymbol = (IEventSymbol)editor.SemanticModel.GetSymbolSafe(assignment.Left, cancellationToken); var observeSubscribe = GetObservableFromEventString(eventSymbol) .Replace("HANDLERTYPE", eventSymbol.Type.ToMinimalDisplayString(editor.SemanticModel, assignment.SpanStart)) .Replace("ARGTYPE", ArgType(eventSymbol)) .Replace("LEFT", assignment.Left.ToString()) .Replace("LAMBDA", Lambda((ParenthesizedLambdaExpressionSyntax)assignment.Right, usesArg)); editor.AddUsing(SystemReactiveLinq) .ReplaceNode( assignment, SyntaxFactory.ParseExpression(observeSubscribe) .WithTriviaFrom(assignment) .WithSimplifiedNames() .WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation)); }