/// <summary> /// Returns 'true' if a multi-line conditional was created, and thus should be /// formatted specially. /// </summary> protected override async Task FixOneAsync( Document document, Diagnostic diagnostic, SyntaxEditor editor, CancellationToken cancellationToken) { var syntaxFacts = document.GetLanguageService <ISyntaxFactsService>(); var ifStatement = diagnostic.AdditionalLocations[0].FindNode(cancellationToken); var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); var ifOperation = (IConditionalOperation)semanticModel.GetOperation(ifStatement); if (!UseConditionalExpressionForAssignmentHelpers.TryMatchPattern( syntaxFacts, ifOperation, out var trueAssignment, out var falseAssignment)) { return; } var conditionalExpression = await CreateConditionalExpressionAsync( document, ifOperation, trueAssignment.Parent, falseAssignment.Parent, trueAssignment.Value, falseAssignment.Value, trueAssignment.IsRef, cancellationToken).ConfigureAwait(false); // See if we're assigning to a variable declared directly above the if statement. If so, // try to inline the conditional directly into the initializer for that variable. if (!TryConvertWhenAssignmentToLocalDeclaredImmediateAbove( syntaxFacts, editor, ifOperation, trueAssignment, falseAssignment, conditionalExpression)) { // If not, just replace the if-statement with a single assignment of the new // conditional. ConvertOnlyIfToConditionalExpression( editor, ifOperation, trueAssignment, falseAssignment, conditionalExpression); } }
protected override bool TryMatchPattern(IConditionalOperation ifOperation) => UseConditionalExpressionForAssignmentHelpers.TryMatchPattern( GetSyntaxFactsService(), ifOperation, out _, out _);