Correctly apply anchor indentation adjustments in the presence of trivia#59654
Correctly apply anchor indentation adjustments in the presence of trivia#59654sharwell merged 5 commits intodotnet:mainfrom
Conversation
1a99eb9 to
b41f0e9
Compare
b41f0e9 to
1a45d64
Compare
| 2 + | ||
| 3) | ||
| 2 + | ||
| 3) |
There was a problem hiding this comment.
i'm curious what happened here.
There was a problem hiding this comment.
These lines previously anchored to from, but now anchor to the first token on the line containing from (var). Since var didn't move, these lines were not altered.
There was a problem hiding this comment.
Note that anchoring is different from relative indentation. The latter applies to the select keyword on the next line.
There was a problem hiding this comment.
hrmmmmmm.
i am worried about this. are we sure we don't want this to be anchored to the 'from'. While i genuinely think the anchoring change makes sense for many cases, queries are so special that it seems like we would want them them to follow that. (lambdas may be similar in that regard).
There was a problem hiding this comment.
Also note that the new behavior is only problematic when all of the following hold:
- The original anchor token is not the first token on the line
- The first token on the line with the anchor token is in the correct location
- The anchor token is not in the correct location (i.e. spaces need to be added or removed to correct the position of the anchor token)
- The now position of the anchor token continues to be on the same line
- The anchored token on a subsequent line is correctly aligned with the mispositioned anchor token, and needs to retain its position after the anchor adjustment
Of all the tests that needed updating, this is the only one which could be argued to meet all these conditions. However, it's also easy to argue that this case does not meet condition (5), and therefore the new behavior is equally acceptable.
There was a problem hiding this comment.
➡️ Testing greatly increased by adding an idempotency check for format document tests
| if (existingWhitespaceBetween.Spaces != this.Spaces) | ||
| return LineColumnRule.PreserveWithGivenSpaces(spaces: this.Spaces); | ||
| else | ||
| return LineColumnRule.PreserveLinesWithDefaultIndentation(lines: 0); |
There was a problem hiding this comment.
i would doc this line. i think i get it, but it would be nice to ahve confirmation of intent.
There was a problem hiding this comment.
➡️ This block now eliminated
|
|
||
| var originalSpace = _tokenStream.GetOriginalColumn(operation.StartToken); | ||
| var data = new AnchorData(operation, originalSpace); | ||
| var anchorToken = _tokenStream.FirstTokenOfBaseTokenLine(operation.AnchorToken); |
| Sub Method() | ||
| Dim a = From q In | ||
| {1, 3, 5} | ||
| {1, 3, 5} |
There was a problem hiding this comment.
this does not not seem good. what happened here?
There was a problem hiding this comment.
ok. based on your previous comment, this is explained. that said, this is so weird too. why did this value end up on the next line? that feels like a bug itself.
There was a problem hiding this comment.
This was already on its own line, which is a strange situation from the start.
| Where q > 10 | ||
| Select q | ||
| End Sub | ||
| End Class</Code> |
There was a problem hiding this comment.
i'll reiterate that this seems not good. we could try taking this PR though and seeing if people have problems with it.
That said, i medium-strongly feel that within a query we should be keeping hte anchor attached to the start of hte query, not the start of hte line.
Is there a way we could have the best of both worlds? Your approach for most anchors, but allow an achor to override that with a flag that says "no, do not attach to start of line, keep me attached to this actual node".
There was a problem hiding this comment.
Is there a way we could have the best of both worlds?
There is, possibly. In the original form, I had a new flag to control the anchor selection. All of them used "first token on line", and when I came back to query expressions I was worried about the following situation:
-
User has a correctly formatted expression:
Dim a = From q In {1, 3, 5} Where q > 10 Select q
-
User accidentally hits tab between
=andFrom:Dim a = From q In {1, 3, 5} Where q > 10 Select q
-
User presses Ctrl+K, Ctrl+D to format document¹. Do to movement of the anchor, a change is made to a line that was already correct:
Dim a = From q In {1, 3, 5} Where q > 10 Select q
Eventually I concluded that it's more likely for an entire block to be indented incorrectly than for a vertically-aligned subsection to be indented incorrectly, and kept the anchor pointing to the first token on the line.
¹ Moving to a different line and/or saving the document alone is not sufficient to trigger the problematic behavior, because these actions instruct the formatter to only touch "dirty" lines, and that doesn't include the line with the inline array.
Fixes #57465