-
Notifications
You must be signed in to change notification settings - Fork 231
Enable "toggle block style" refactor when there's no selection #3818
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
base: main
Are you sure you want to change the base?
Enable "toggle block style" refactor when there's no selection #3818
Conversation
How to use the Graphite Merge QueueAdd the label graphite-merge to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
|
I have signed the CLA! |
vinistock
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, this is great. Let's just ensure we're not traversing the AST multiple times for discovering code actions and we should be good to ship
| #: -> Array[Interface::CodeAction] | ||
| def toggle_block_style_action | ||
| if @range[:start] == @range[:end] | ||
| block_context = @document.locate_node( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Locating nodes is expensive because it needs to traverse the AST. Let's start locating the node under the cursor with no node_types filtering and then we can pass the node to this method and attribute_actions.
That way, both can check what type of node is under the cursor without traversing twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that could be slightly complicated, because the two calls to locate have different node_types filters. attribute_actions is trying to find INSTANCE_VARIABLE_NODES covering the cursor, while toggle_block_style_action is looking for BlockNodes. Additionally, in attribute_actions, there are two different cases where we call locate with the range start position: (a) the case where there's no selection, and (b) the case where there's nothing fully contained within the selection that matches INSTANCE_VARIABLE_NODES.
Since locate happens to build an array of ClassNode, DefNode, BlockNode, etc. in the NodeContext's @nesting_nodes, we could potentially reuse the attribute_actions locate result for toggle_block_style_action as follows:
- Add
attr_reader :nesting_nodestoNodeContext - In
CodeActions#perform, if there's no selection, call@document.locate_node(@range[:start], node_types: INSTANCE_VARIABLE_NODES)(the filter that we need inattribute_actions) - Pass the returned
NodeContexttoattribute_actionsandtoggle_block_style_actionas a nilable param - In
toggle_block_style_action, if that param is provided, only return the toggle action if the context'snesting_nodescontains aBlockNode - In the
if node.nil?branch ofattribute_actions, use the NodeContext passed by the caller if it's present; if not, we're in case (b) above and need to invokelocate
This optimization would only work because the conditional actions only care about these two kinds of nodes; if additional conditional code actions were added which needed to match other node types, we'd be back where we started and would need multiple locate calls. What do you think we should do?
Motivation
The "toggle block style" refactor action currently only works if there is a selection in the editor which covers a call node and its associated block. When there's no selection but the cursor is inside a block, it would be nice to be able to toggle the style of the block that the cursor is inside.
Implementation
CodeActionsrequest: add a quick check using@document.locate_nodeto conditionally offer the "toggle block style" action if the cursor is inside aBlockNodeCodeActionResolverequest: add a similar call to find the current block node when there's no selection. The actual rewrite also needs theCallNodeassociated with the block, and I couldn't figure out a good way to do this without adding a secondRubyDocument.locatecall. (When you calllocate_nodeto get the BlockNode, you don't get any reference to theCallNodethat theBlockNodeis attached to. You also can't just look for aCallNodecovering the cursor - that could return aCallNodethat's actually inside the block that you want to toggle.)Automated Tests
Added some expectation tests:
Manual Tests