-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FormatAsync removes whitespace outside the requested bounds #50129
Comments
Due to dotnet/roslyn#50129, `FormatAsync` will give us changes past the location that we wanted if there were changes to be made on the current line. This can lead to a frustrating typing experience now that we listen for `\n` in vscode to trigger formatting. I've implemented a simple workaround for this while the bug is fixed upstream.
Due to dotnet/roslyn#50129, `FormatAsync` will give us changes past the location that we wanted if there were changes to be made on the current line. This can lead to a frustrating typing experience now that we listen for `\n` in vscode to trigger formatting. I've implemented a simple workaround for this while the bug is fixed upstream.
IN general, we walk a token forward/backward from the span requested as that's how we thought the intended behavior of this API should be. i.e. you're giving the span of nodes/tokens to format, so of course you'd need to touch the whitespace that node/tokens are touching. It's highly unlikely we could change this as too much is built with this assumption. Note: if you do want to filter this stuff out, i would imagine you could write your own method that just takes the text-changes produced and clamps that as appropriate to whatever span you must limit things to. |
I'm not certain what tihs means. what is 'on type formatting'? |
Put another way, the idea of formatting was that particular nodes/tokens would be annotated. We'd then just grab the span of the node/token and pass that into the formatter (so everythign is consistent). Since the node is what is annotated (and what span is being asked for) the actual region to be formatted extends before/after that node so that node is properly formatted in the context of everything else. |
VSCode has a setting that will format as you type. We use it to format the line you just typed, but you don't want it to format the next line at all.
It is very much not that simple. The new changes include newlines that were outside the requested boundaries, and I can't easily map them back to the original location. |
If the existing behavior can't be changed (it's certainly non-obvious), then I'd like to see a new mode added for it. |
Then i would filter out the result to only the region you care about. The text changes are just data that are passed out. I don't see a need for a mode for this. It's very specifically written around a need of a particular vscode feature, and seems like it would be a 1-liner to support. So i would just do it on that end. |
As another example of where this had odd consequences, consider the scenario where, in VS, I select the |
Definitely still by design here. I get that it's not desirable from your perspective, and you have a rational and cohesive alternate design. However, that design can always be layered on what we have here pretty simpler afaict :) i.e. the existing api is effectively "please smartly format the node this span encompasses" and you can easily write the "and definitely clamp to a particular region" on top of that if you want :) |
To summarize the conversation @CyrusNajmabadi and I had on discord:
Any other points I missed Cyrus? |
that seems like a reasonable place to start. :) |
@CyrusNajmabadi I took a look at moving |
I agree that the formatter should filter changes to only affect the requested span. However, given the potentially large amount of work involved with making any progress on this edge case, I'm not sure we'll ever see a resolution on it. With that said, the elimination of 8 spaces is not a bug. On that blank line, the caret is placed in virtual space 8 positions to the right of the last character on the line. There are no spaces to its left. If the IDE using Roslyn does not understand virtual spaces, you'll need to simulate it somehow by reinserting unwanted spaces after every formatting operation when the indentation service requests positioning in virtual space. |
Moved the public API request to #50221. Leaving this open to track the unexpected formatting changes. |
Version Used: 3.8.0
Steps to Reproduce:
Consider the following code:
If I make a FormatAsync call, with the bounds
[109..130)
, I get back a text change removing both the space on the end of thevar
line and the spaces on the line after, with a changed span of[127..138)
. This span is outside the bounds of what I asked for. In practice, what this means that if I have on-type formatting turned on in vscode and I have a trailing space, FormatAsync will remove the auto-indentation added in to put my cursor, when all that was asked to be formatted was the previous line.The text was updated successfully, but these errors were encountered: