Skip to content

Commit

Permalink
Merge pull request #6 from sharwell/testallfixall
Browse files Browse the repository at this point in the history
Complete fix all providers
  • Loading branch information
pdelvo committed Aug 6, 2015
2 parents 5e5a852 + a66c1ef commit aea2268
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,24 @@ public void Bar(int i)
}
}";

await this.VerifyCSharpFixAsync(testCode, fixedTestCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
// The batch fixer does correct all problems in one pass.
var batchFixedTestCode = @"using System.Diagnostics;
public class Foo
{
public void Bar(int i)
{
if (i == 0)
{
Debug.Assert(true);
}
else
{
Debug.Assert(false);
}
}
}";

await this.VerifyCSharpFixAsync(testCode, fixedTestCode, batchNewSource: batchFixedTestCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void TestMethod()

await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, 0).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, codeFixIndex: 0).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -113,7 +113,7 @@ public void TestMethod()

await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, 1).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, codeFixIndex: 1).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -185,7 +185,7 @@ orderby g.Key

await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, 1).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, codeFixIndex: 1).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -235,7 +235,7 @@ public void TestMethod()

await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, 0).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, codeFixIndex: 0).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -287,7 +287,7 @@ public void TestMethod()

await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, 1).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, codeFixIndex: 1).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -347,7 +347,7 @@ public void TestMethod()

await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, 0).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, codeFixIndex: 0).ConfigureAwait(false);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ protected virtual CodeFixProvider GetCSharpCodeFixProvider()
/// </summary>
/// <param name="oldSource">A class in the form of a string before the code fix was applied to it.</param>
/// <param name="newSource">A class in the form of a string after the code fix was applied to it.</param>
/// <param name="batchNewSource">A class in the form of a string after the batch fixer was applied to it.</param>
/// <param name="codeFixIndex">Index determining which code fix to apply if there are multiple.</param>
/// <param name="allowNewCompilerDiagnostics">A value indicating whether or not the test will fail if the code fix introduces other warnings after being applied.</param>
/// <param name="maxNumberOfIterations">Defines an upper limit for the number of iterations the fixer will be called.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
protected async Task VerifyCSharpFixAsync(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false, int maxNumberOfIterations = int.MaxValue, CancellationToken cancellationToken = default(CancellationToken))
protected async Task VerifyCSharpFixAsync(string oldSource, string newSource, string batchNewSource = null, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false, int maxNumberOfIterations = int.MaxValue, CancellationToken cancellationToken = default(CancellationToken))
{
var t1 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics, maxNumberOfIterations, GetSingleAnalyzerDocumentAsync, cancellationToken).ConfigureAwait(false);

Expand All @@ -51,7 +52,7 @@ protected virtual CodeFixProvider GetCSharpCodeFixProvider()
}
else
{
var t2 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics, maxNumberOfIterations, GetFixAllAnalyzerDocumentAsync, cancellationToken).ConfigureAwait(false);
var t2 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSource, batchNewSource ?? newSource, codeFixIndex, allowNewCompilerDiagnostics, maxNumberOfIterations, GetFixAllAnalyzerDocumentAsync, cancellationToken).ConfigureAwait(false);
await t1;
cancellationToken.ThrowIfCancellationRequested();
await t2;
Expand Down Expand Up @@ -184,24 +185,34 @@ private static async Task<Document> GetFixAllAnalyzerDocumentAsync(ImmutableArra
Assert.True(false, "The upper limit for the number of fix all iterations was exceeded");
}

string equivalenceKey = null;
foreach (var diagnostic in analyzerDiagnostics)
{
var actions = new List<CodeAction>();
var context = new CodeFixContext(document, diagnostic, (a, d) => actions.Add(a), cancellationToken);
await codeFixProvider.RegisterCodeFixesAsync(context).ConfigureAwait(false);
if (actions.Count > (codeFixIndex ?? 0))
{
equivalenceKey = actions[codeFixIndex ?? 0].EquivalenceKey;
break;
}
}

previousDiagnostics = analyzerDiagnostics;

done = true;

FixAllContext.DiagnosticProvider fixAllDiagnosticProvider = TestDiagnosticProvider.Create(analyzerDiagnostics);

string equivalenceKey = codeFixProvider.GetType().Name;
FixAllContext fixAllContext = new FixAllContext(document, codeFixProvider, FixAllScope.Document, equivalenceKey, codeFixProvider.FixableDiagnosticIds, fixAllDiagnosticProvider, cancellationToken);

CodeAction action = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);

if (action == null)
{
return document;
}

var fixedDocument = await ApplyFixAsync(document, action, cancellationToken).ConfigureAwait(false);

if (fixedDocument != document)
{
done = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using StyleCop.Analyzers.Helpers;

/// <summary>
Expand Down Expand Up @@ -51,20 +52,41 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen

var stringBuilder = new StringBuilder();

var column = violatingTrivia.GetLineSpan().StartLinePosition.Character;
foreach (var c in violatingTrivia.ToFullString())
int firstTriviaIndex = violatingTrivia.GetLineSpan().StartLinePosition.Character;
string relevantText;
if (firstTriviaIndex == 0)
{
relevantText = violatingTrivia.ToFullString();
}
else
{
SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
relevantText = sourceText.ToString(new TextSpan(violatingTrivia.FullSpan.Start - firstTriviaIndex, firstTriviaIndex + violatingTrivia.FullSpan.Length));
}

int column = 0;
for (int i = 0; i < relevantText.Length; i++)
{
char c = relevantText[i];
if (c == '\t')
{
var offsetWithinTabColumn = column % indentationOptions.TabSize;
var spaceCount = indentationOptions.TabSize - offsetWithinTabColumn;

stringBuilder.Append(' ', spaceCount);
if (i >= firstTriviaIndex)
{
stringBuilder.Append(' ', spaceCount);
}

column += spaceCount;
}
else
{
stringBuilder.Append(c);
if (i >= firstTriviaIndex)
{
stringBuilder.Append(c);
}

column++;
}
}
Expand Down

0 comments on commit aea2268

Please sign in to comment.