/// <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.GetRequiredLanguageService <ISyntaxFactsService>(); var ifStatement = diagnostic.AdditionalLocations[0].FindNode(cancellationToken); var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false); var ifOperation = (IConditionalOperation)semanticModel.GetOperation(ifStatement, cancellationToken) !; if (!UseConditionalExpressionForAssignmentHelpers.TryMatchPattern( syntaxFacts, ifOperation, out var trueStatement, out var falseStatement, out var trueAssignment, out var falseAssignment)) { return; } var conditionalExpression = await CreateConditionalExpressionAsync( document, ifOperation, trueStatement, falseStatement, trueAssignment?.Value ?? trueStatement, falseAssignment?.Value ?? falseStatement, trueAssignment?.IsRef == true, 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)) { return; } // 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( GetSyntaxFacts(), ifOperation, out _, out _);