I did a little searching here and I am surprised that no one has asked this question. Is Visual Studio 2010's VB smart indenting just horribly broken for multi-line statements? Or am I doing/setting something incorrectly? I have it set to 4-character indents with keep tabs. Typing a multi-line statement in VB with natural returns yields the following (property names changed to protect the innocent).
Public Sub Sub1()
Dim foo As New MyClassA With {.FileName = "test",
.Format = MyEnumForImageFormat.jpg,
.IsReallySpecial = False,
.Name = "testN",
.SourceId = Guid.NewGuid(),
.VariantName = "TestV",
.Width = 800,
.Height = 600}
End Sub
How could anyone want to format their code this way? I thought, maybe I need to reformat (Ctrl+K Ctrl+D):
Public Sub Sub1()
Dim foo As New ImageBase With {.FileName = "test",
.Format = MyEnumForImageFormat.jpg,
.IsReallySpecial = False,
.Name = "testN",
.SourceId = Guid.NewGuid(),
.VariantName = "TestV",
.Width = 800,
.Height = 600}
End Sub
For good measure, I tried Reformat again. Lo and behold, it pushed it further left. I pushed it again, and it moved some more. Finally, after four reformats, my code looks like:
Public Sub Sub1()
Dim foo As New ImageBase With {.FileName = "test",
.Format = MyEnumForImageFormat.jpg,
.IsReallySpecial = False,
.Name = "testN",
.SourceId = Guid.NewGuid(),
.VariantName = "TestV",
.Width = 800,
.Height = 600}
End Sub
Better, but we really want the continuing lines indented by only a space? And why should I have to reformat four times to get what I want? To top it off, trying to add one more property definition above does not keep anything aligned--the default indent is to cascade offset to the right by a randomly-determined number of space and tab characters.
Using old-style continuation characters doesn't help. How can I have smart indenting with any degree of sanity?
You will find the automatic indenting to be better or worse depending on exactly where you choose to split a line. In your example my style is to bring the opening brace and first item to the next line, after which the indenting will behave itself better.
Public Sub Sub1()
Dim foo As New MyClassA With
{.FileName = "test",
.Format = MyEnumForImageFormat.jpg,
.IsReallySpecial = False,
.Name = "testN",
.SourceId = Guid.NewGuid(),
.VariantName = "TestV",
.Width = 800,
.Height = 600}
End Sub
Without wanting to assume too much, I believe the indenting of each line on a multi-line statement is governed by the preceding line. With this choice of style however, you will find adding items using the natural return to position themselves correctly. When copying any pasting a snippet into a target of different indentation level this will still cause indentation issues, but since the return positions itself correctly, I haven't had too much problem with resetting each line manually (repetitions of Down, End, Delete, Enter.)
A similar solution is useful with multi-line lambda functions, however these behave better than straight multi-line statements. Correctly positioning the opening call automatically corrects the indentation of the whole routine.
This isn't so good and the indent floats:
Public Sub Sub1()
Dim a As New Action(Sub()
Dim b As Double = 4.0
Dim c As Double = 5.0
Dim d As Double = b * c
End Sub)
End Sub
This behaved nicely with the indenter:
Public Sub Sub1()
Dim a As New Action(
Sub()
Dim b As Double = 4.0
Dim c As Double = 5.0
Dim d As Double = b * c
End Sub)
End Sub
Just as a personal style I prefer these, others may get the same result with a different appearance.
I found this an annoyance aswell. It is possible to turn off Pretty Listing for Vb.Net - have a look at this question: Turn off auto formatting in Visual Studio
In my opinion it goes too far the other way, but it should stop the problem you're talking about.
Edit: you might also want to play around with the auto-indent settings; I think the default is set to 'smart'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With