Skip to content

Commit

Permalink
Merge branch 'felipebz-issue-160'
Browse files Browse the repository at this point in the history
  • Loading branch information
giggio committed Dec 15, 2014
2 parents 0ae1572 + 8136f14 commit 5638cd1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Expand Up @@ -67,7 +67,15 @@ private async Task<Document> CreateVariable(Document document, InvocationExpress

var root = await document.GetSyntaxRootAsync(ct);

var newRoot = root.ReplaceNode(invocation.Parent, invocation.Parent.WithAdditionalAnnotations(new SyntaxAnnotation(SyntaxAnnotatinKind)));
var oldNode = invocation.Parent;
var newNode = invocation.Parent.WithAdditionalAnnotations(new SyntaxAnnotation(SyntaxAnnotatinKind));

if (oldNode.Parent.IsEmbeddedStatementOwner())
{
newNode = SyntaxFactory.Block((StatementSyntax)newNode);
}

var newRoot = root.ReplaceNode(oldNode, newNode);
newRoot = newRoot.InsertNodesAfter(GetMark(newRoot), new[] { variable as SyntaxNode, newInvocation as SyntaxNode });
newRoot = newRoot.RemoveNode(GetMark(newRoot), SyntaxRemoveOptions.KeepNoTrivia);

Expand Down
13 changes: 13 additions & 0 deletions src/CSharp/CodeCracker/Extensions/AnalyzerExtensions.cs
Expand Up @@ -64,5 +64,18 @@ public static SyntaxNode WithSameTriviaAs(this SyntaxNode target, SyntaxNode sou
.WithLeadingTrivia(source.GetLeadingTrivia())
.WithTrailingTrivia(source.GetTrailingTrivia());
}

public static bool IsEmbeddedStatementOwner(this SyntaxNode node)
{
return node is IfStatementSyntax ||
node is ElseClauseSyntax ||
node is ForStatementSyntax ||
node is ForEachStatementSyntax ||
node is WhileStatementSyntax ||
node is UsingStatementSyntax ||
node is DoStatementSyntax ||
node is LockStatementSyntax ||
node is FixedStatementSyntax;
}
}
}
Expand Up @@ -207,5 +207,38 @@ public void Execute()

await VerifyCSharpFixAsync(source, fixtest, 0);
}

[Fact]
public async void FixWhenInvocationIsInsideABlockWithoutBraces()
{
const string source = @"
public class MyClass
{
public event System.EventHandler MyEvent;
public void Execute()
{
if (raiseEvents) MyEvent(this, System.EventArgs.Empty);
}
}";

const string fixtest = @"
public class MyClass
{
public event System.EventHandler MyEvent;
public void Execute()
{
if (raiseEvents)
{
var handler = MyEvent;
if (handler != null)
handler(this, System.EventArgs.Empty);
}
}
}";

await VerifyCSharpFixAsync(source, fixtest, 0);
}
}
}

0 comments on commit 5638cd1

Please sign in to comment.