无法获得我的Code-Fix以产生所需的结果

时间:2014-11-23 17:43:49

标签: vb.net roslyn

我编写了一个Code Fix来插入Null Guard检查方法参数,同时保留参数的顺序。

Public Sub ex(p0 As Example, p1 As Example, p2 As Integer, p3 As Example) ' Comment 1
  If p0 Is Nothing Then Throw New System.ArgumentNullException("p0") 'comment

  Dim a = 0
End Sub

假设我使用代码修复为参数p3添加空保护。这应该是以下结果。

Public Sub ex(p0 As Example, p1 As Example, p2 As Integer, p3 As Example) ' Comment 1
  If p0 Is Nothing Then Throw New System.ArgumentNullException("p0") 'comment
  If p3 Is Nothing Then Throw New System.ArgumentNullException("p3")

  Dim a = 0
End Sub

但我实际得到的是

Public Sub ex(p0 As Example, p1 As Example, p2 As Integer, p3 As Example) ' Comment 1
  If p0 Is Nothing Then Throw New System.ArgumentNullException("p0") If p3 Is Nothing Then Throw New System.ArgumentNullException("p3")         'comment

    Dim a = 0
End Sub

Public Sub ex(p0 As Example, p1 As Example, p2 As Integer, p3 As Example) ' Comment 1
  If p0 Is Nothing Then Throw New System.ArgumentNullException("p0")
  If p3 Is Nothing Then Throw New System.ArgumentNullException("p3")         'comment

    Dim a = 0
End Sub

使用的代码可以在GitHub上找到(repo) *使用的算法可能是垃圾,但它的工作原理。 *

无论我尝试过什么,我都无法得到正确的结果。

  • 所有守卫最终都在同一条线上
  • 所有守卫最终分开,但评论被删除/移至最后一名后卫。

任何人都可以看到我错过了产生预期效果吗?


其他

  Dim ifStatements = method.Statements.Where(Function(s) (TypeOf s Is MultiLineIfBlockSyntax)
                     OrElse (TypeOf s Is SingleLineIfStatementSyntax))
  Dim ExistingGuards = ifStatements.Where(Function(s)
    If TypeOf s Is SingleLineIfStatementSyntax Then
      Dim singleIF = DirectCast(s, SingleLineIfStatementSyntax)
      Dim isExpr = TryCast(singleIF.Condition, BinaryExpressionSyntax)
      Return CheckIfCondition(isExpr)
    ElseIf TypeOf s Is MultiLineIfBlockSyntax Then
      Dim multiIF = DirectCast(s, MultiLineIfBlockSyntax)
      Dim isExpr = TryCast(multiIF.IfStatement.Condition, BinaryExpressionSyntax)
      Return CheckIfCondition(isExpr)
    Else
      Return False
    End If
  End Function).ToList()

我希望每个陈述都能保留这个琐事,但它不是。

1 个答案:

答案 0 :(得分:0)

亚当,

您是否尝试过格式化程序注释?

         replacement = ifStatement.WithStatement(
                 SyntaxFactory.Block(ifStatement.Statement)
                    .WithAdditionalAnnotations(Formatter.Annotation));

你是如何将它们固定在同一条线上的?

评论需要在该行的最后一个标记 - 括号

上尾随琐事