This repository has been archived by the owner on May 30, 2024. It is now read-only.
Add on type formatting with snippet support #193
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When investigating some other aspects of the specification, I stumbled upon
onTypeFormatting
and couldn't stop myself from implementing this for automatically closing Ruby tokens - which is something I miss a lot using VS Code.What is on type formatting
We register specific characters that we want VS Code to send
onTypeFormatting
requests for on the server side. When we receive the requests, we can send back text edits to automatically format the code around that edit.One common way of using this is adding
end
tokens automatically when adding a line break, automatically closing pipes for block arguments or closing braces for string interpolation.VS Code extension changes
Theoretically, we wouldn't need any changes other than the ones in
package.json
and everything would be implemented in the server. However, VS Code currently does not support specifying the final cursor position for a text edit.For example, if we add an
end
automatically when typingif
, the cursor would not be placed between theif
and theend
, but would always be at the last character ofend
. This is not an optimal experience, as we'd like to place be able to decide where the cursor should end up.To get around that, this PR adds a middleware for
onTypeFormatting
to process$0
snippet anchors that come from text edits. This anchor makes VS Code move the cursor to the desired position, which results in the experience we'd like to provide.The logic is:
$0
in theonTypeFormatting
response, just return the response and let VS Code handle it$0
anchor, then apply the edits regular edits first and then apply the$0
snippet at the end to move the cursor to the desired positionManual tests
"editor.formatOnType": true
if something
and press returnend
is added automaticallydo
,unless
,while
,class
...