-
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
Open
rolandcrosby-columntax
wants to merge
2
commits into
Shopify:main
Choose a base branch
from
rolandcrosby-columntax:toggle-block-without-selection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
test/expectations/code_action_resolve/aref_call_aref_assign_without_selection.exp.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "params": { | ||
| "kind": "refactor.rewrite", | ||
| "title": "Refactor: Toggle block style", | ||
| "data": { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 29 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 29 | ||
| } | ||
| }, | ||
| "uri": "file:///fake" | ||
| } | ||
| }, | ||
| "result": { | ||
| "title": "Refactor: Toggle block style", | ||
| "edit": { | ||
| "documentChanges": [ | ||
| { | ||
| "textDocument": { | ||
| "uri": "file:///fake", | ||
| "version": null | ||
| }, | ||
| "edits": [ | ||
| { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 26 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 58 | ||
| } | ||
| }, | ||
| "newText": "do |a|\n a[\"field\"] == \"expected\"\nend" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
test/expectations/code_action_resolve/nested_block_calls_without_selection.exp.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "params": { | ||
| "kind": "refactor.rewrite", | ||
| "title": "Refactor: Toggle block style", | ||
| "data": { | ||
| "range": { | ||
| "start": { | ||
| "line": 1, | ||
| "character": 30 | ||
| }, | ||
| "end": { | ||
| "line": 1, | ||
| "character": 30 | ||
| } | ||
| }, | ||
| "uri": "file:///fake" | ||
| } | ||
| }, | ||
| "result": { | ||
| "title": "Refactor: Toggle block style", | ||
| "edit": { | ||
| "documentChanges": [ | ||
| { | ||
| "textDocument": { | ||
| "uri": "file:///fake", | ||
| "version": null | ||
| }, | ||
| "edits": [ | ||
| { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 29 | ||
| }, | ||
| "end": { | ||
| "line": 3, | ||
| "character": 3 | ||
| } | ||
| }, | ||
| "newText": "{ |a| nested_call(fourth_call).each { |b| } }" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
test/expectations/code_action_resolve/nested_block_calls_without_selection_inner.exp.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "params": { | ||
| "kind": "refactor.rewrite", | ||
| "title": "Refactor: Toggle block style", | ||
| "data": { | ||
| "range": { | ||
| "start": { | ||
| "line": 1, | ||
| "character": 37 | ||
| }, | ||
| "end": { | ||
| "line": 1, | ||
| "character": 37 | ||
| } | ||
| }, | ||
| "uri": "file:///fake" | ||
| } | ||
| }, | ||
| "result": { | ||
| "title": "Refactor: Toggle block style", | ||
| "edit": { | ||
| "documentChanges": [ | ||
| { | ||
| "textDocument": { | ||
| "uri": "file:///fake", | ||
| "version": null | ||
| }, | ||
| "edits": [ | ||
| { | ||
| "range": { | ||
| "start": { | ||
| "line": 1, | ||
| "character": 32 | ||
| }, | ||
| "end": { | ||
| "line": 2, | ||
| "character": 5 | ||
| } | ||
| }, | ||
| "newText": "{ |b| }" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
test/expectations/code_action_resolve/nested_oneline_blocks_without_selection.exp.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "params": { | ||
| "kind": "refactor.rewrite", | ||
| "title": "Refactor: Toggle block style", | ||
| "data": { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 31 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 31 | ||
| } | ||
| }, | ||
| "uri": "file:///fake" | ||
| } | ||
| }, | ||
| "result": { | ||
| "title": "Refactor: Toggle block style", | ||
| "edit": { | ||
| "documentChanges": [ | ||
| { | ||
| "textDocument": { | ||
| "uri": "file:///fake", | ||
| "version": null | ||
| }, | ||
| "edits": [ | ||
| { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 29 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 74 | ||
| } | ||
| }, | ||
| "newText": "do |a|\n nested_call(fourth_call).each do |b|\n \n end\nend" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
test/expectations/code_action_resolve/nested_oneline_blocks_without_selection_inner.exp.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "params": { | ||
| "kind": "refactor.rewrite", | ||
| "title": "Refactor: Toggle block style", | ||
| "data": { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 68 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 68 | ||
| } | ||
| }, | ||
| "uri": "file:///fake" | ||
| } | ||
| }, | ||
| "result": { | ||
| "title": "Refactor: Toggle block style", | ||
| "edit": { | ||
| "documentChanges": [ | ||
| { | ||
| "textDocument": { | ||
| "uri": "file:///fake", | ||
| "version": null | ||
| }, | ||
| "edits": [ | ||
| { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 65 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 72 | ||
| } | ||
| }, | ||
| "newText": "do |b|\n \n end" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
test/expectations/code_actions/toggle_block_outside_block.exp.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "params": { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 0 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 0 | ||
| } | ||
| }, | ||
| "textDocument": { | ||
| "uri": "file:///fake", | ||
| "version": null | ||
| }, | ||
| "context": { | ||
| "diagnostics": [] | ||
| } | ||
| }, | ||
| "result": [] | ||
| } |
40 changes: 40 additions & 0 deletions
40
test/expectations/code_actions/toggle_block_without_selection.exp.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "params": { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 22 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 22 | ||
| } | ||
| }, | ||
| "textDocument": { | ||
| "uri": "file:///fake", | ||
| "version": null | ||
| }, | ||
| "context": { | ||
| "diagnostics": [] | ||
| } | ||
| }, | ||
| "result": [ | ||
| { | ||
| "title": "Refactor: Toggle block style", | ||
| "kind": "refactor.rewrite", | ||
| "data": { | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 22 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 22 | ||
| } | ||
| }, | ||
| "uri": "file:///fake" | ||
| } | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| object["attributes"].find { |a| a["field"] == "expected" }["value"] = "changed" |
Oops, something went wrong.
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.
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_typesfiltering and then we can pass the node to this method andattribute_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
locatehave differentnode_typesfilters.attribute_actionsis trying to findINSTANCE_VARIABLE_NODEScovering the cursor, whiletoggle_block_style_actionis looking forBlockNodes. Additionally, inattribute_actions, there are two different cases where we calllocatewith 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 matchesINSTANCE_VARIABLE_NODES.Since
locatehappens to build an array ofClassNode,DefNode,BlockNode, etc. in theNodeContext's@nesting_nodes, we could potentially reuse theattribute_actionslocate result fortoggle_block_style_actionas follows:attr_reader :nesting_nodestoNodeContextCodeActions#perform, if there's no selection, call@document.locate_node(@range[:start], node_types: INSTANCE_VARIABLE_NODES)(the filter that we need inattribute_actions)NodeContexttoattribute_actionsandtoggle_block_style_actionas a nilable paramtoggle_block_style_action, if that param is provided, only return the toggle action if the context'snesting_nodescontains aBlockNodeif 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 invokelocateThis 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
locatecalls. What do you think we should do?