提交 332880ea 编写于 作者: C Cyrus Najmabadi

Properly handle trivia when performign code action.

上级 fe7b4dab
......@@ -309,8 +309,7 @@ void Foo()
}
}
}",
@"
class C
@"class C
{
System.Action a;
void Foo()
......@@ -337,8 +336,7 @@ void Foo()
}
}
}",
@"
class C
@"class C
{
System.Action a;
void Foo()
......
......@@ -10,6 +10,7 @@
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Roslyn.Utilities;
......@@ -65,6 +66,7 @@ private Document HandleSingleIfStatementForm(Document document, SyntaxNode root,
invocationExpression.Expression,
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberBindingExpression(SyntaxFactory.IdentifierName(nameof(Action.Invoke))), invocationExpression.ArgumentList)));
newStatement = newStatement.WithPrependedLeadingTrivia(ifStatement.GetLeadingTrivia());
if (ifStatement.Parent.IsKind(SyntaxKind.ElseClause) && ifStatement.Statement.IsKind(SyntaxKind.Block))
{
......@@ -98,12 +100,12 @@ private static Document HandVariableAndIfStatementFormAsync(Document document, S
newStatement = newStatement.WithAdditionalAnnotations(Formatter.Annotation);
var newStatements = parentBlock.Statements.TakeWhile(s => s != localDeclarationStatement)
.Concat(newStatement)
.Concat(parentBlock.Statements.SkipWhile(s => s != ifStatement).Skip(1));
var editor = new SyntaxEditor(root, document.Project.Solution.Workspace);
editor.ReplaceNode(ifStatement, newStatement);
editor.RemoveNode(localDeclarationStatement, SyntaxRemoveOptions.KeepLeadingTrivia | SyntaxRemoveOptions.AddElasticMarker);
var newBlock = parentBlock.WithStatements(SyntaxFactory.List(newStatements));
return document.WithSyntaxRoot(root.ReplaceNode(parentBlock, newBlock));
var newRoot = editor.GetChangedRoot();
return document.WithSyntaxRoot(newRoot);
}
private class MyCodeAction : CodeAction.DocumentChangeAction
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册