CodeConverter
CodeConverter copied to clipboard
VB -> C#: Preserve line breaks and spacing within expressions
Whitespace trivia gets lost when converting VB to C#
Input code
Class Program
Public Shared Sub Main(ByVal args As String())
Dim x = "a" & "b" &
"c" & "d" &
"e" & "f" &
"g" & "h" &
"i" & "j" &
"k" & "l" &
"m" & "n" &
"o" & "p"
End Sub
End Class
Erroneous output
namespace ConsoleApp4
{
class Program
{
public static void Main(string[] args)
{
var x = "a" + "b" + "c" + "d" + "e" + "f" + "g" + "h" + "i" + "j" + "k" + "l" + "m" + "n" + "o" + "p";
}
}
}
Expected output
(formatted manually, so the whitespace is probably wrong, despite being visually correct...)
namespace ConsoleApp4
{
class Program
{
public static void Main(string[] args)
{
var x = "a" + "b" +
"c" + "d" +
"e" + "f" +
"g" + "h" +
"i" + "j" +
"k" + "l" +
"m" + "n" +
"o" + "p";
}
}
}
Details
Version in use: 7.0.0.0
At the moment I think whitespace trivia is dropped within expressions in general. Or possibly just when there isn't an accompanying comment.
With the simplifier and formatter running at the end, even if it was converted, it would still often change I think.
I'm reluctant to make any changes to the trivia code until its been tidied up a bit. See #74
Whitespace conversion is definitely at the very bottom of my personal list of priorities, but if anyone's keen I'm happy to help with PRs as always 😊
The tidying has now occurred. This definitely makes most sense within expressions. #448 may override some of the formatting anyway
Now that the simpler case of between-statement newlines is covered, I think it's more plausible for someone to approach this task. The main tasks here are:
- Figure out how to get the formatter to give reasonable indentation (unlike https://github.com/icsharpcode/CodeConverter/pull/880#discussion_r855317472). It may be just that converted whitespace and generated whitespace are both being converted and hence throwing everything off. "Elastic" trivia may also be key to this.
- Find a bunch of cases where this would be useful and look for the commonality. e.g. AcceptSeparatedListAsync is a generalisation that covers a few useful cases without complicating the core conversion code for the related types.