Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions lib/ruby_lsp/requests/code_action_resolve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,42 @@ def perform
#: -> (Interface::CodeAction)
def switch_block_style
source_range = @code_action.dig(:data, :range)
raise EmptySelectionError, "Invalid selection for refactor" if source_range[:start] == source_range[:end]
if source_range[:start] == source_range[:end]
block_context = @document.locate_node(
source_range[:start],
node_types: [Prism::BlockNode],
)
node = block_context.node
unless node.is_a?(Prism::BlockNode)
raise InvalidTargetRangeError, "Cursor is not inside a block"
end

target = @document.locate_first_within_range(
@code_action.dig(:data, :range),
node_types: [Prism::CallNode],
)
# Find the call node at the block node's start position.
# This should be the call node whose block the cursor is inside of.
call_context = RubyDocument.locate(
@document.ast,
node.location.cached_start_code_units_offset(@document.code_units_cache),
node_types: [Prism::CallNode],
code_units_cache: @document.code_units_cache,
)
target = call_context.node
unless target.is_a?(Prism::CallNode) && target.block == node
raise InvalidTargetRangeError, "Couldn't find an appropriate location to place extracted refactor"
end
else
target = @document.locate_first_within_range(
@code_action.dig(:data, :range),
node_types: [Prism::CallNode],
)

unless target.is_a?(Prism::CallNode)
raise InvalidTargetRangeError, "Couldn't find an appropriate location to place extracted refactor"
end
unless target.is_a?(Prism::CallNode)
raise InvalidTargetRangeError, "Couldn't find an appropriate location to place extracted refactor"
end

node = target.block
unless node.is_a?(Prism::BlockNode)
raise InvalidTargetRangeError, "Couldn't find an appropriate location to place extracted refactor"
node = target.block
unless node.is_a?(Prism::BlockNode)
raise InvalidTargetRangeError, "Couldn't find an appropriate location to place extracted refactor"
end
end

indentation = " " * target.location.start_column unless node.opening_loc.slice == "do"
Expand Down
25 changes: 20 additions & 5 deletions lib/ruby_lsp/requests/code_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ def perform
kind: Constant::CodeActionKind::REFACTOR_EXTRACT,
data: { range: @range, uri: @uri.to_s },
)
code_actions << Interface::CodeAction.new(
title: TOGGLE_BLOCK_STYLE_TITLE,
kind: Constant::CodeActionKind::REFACTOR_REWRITE,
data: { range: @range, uri: @uri.to_s },
)
end
code_actions.concat(toggle_block_style_action)
code_actions.concat(attribute_actions)

code_actions
Expand Down Expand Up @@ -113,6 +109,25 @@ def attribute_actions
),
]
end

#: -> Array[Interface::CodeAction]
def toggle_block_style_action
if @range[:start] == @range[:end]
block_context = @document.locate_node(
Copy link
Member

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.

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_nodes to NodeContext
  • In CodeActions#perform, if there's no selection, call @document.locate_node(@range[:start], node_types: INSTANCE_VARIABLE_NODES) (the filter that we need in attribute_actions)
  • Pass the returned NodeContext to attribute_actions and toggle_block_style_action as a nilable param
  • In toggle_block_style_action, if that param is provided, only return the toggle action if the context's nesting_nodes contains a BlockNode
  • In the if node.nil? branch of attribute_actions, use the NodeContext passed by the caller if it's present; if not, we're in case (b) above and need to invoke locate

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?

@range[:start],
node_types: [Prism::BlockNode],
)
return [] unless block_context.node.is_a?(Prism::BlockNode)
end

[
Interface::CodeAction.new(
title: TOGGLE_BLOCK_STYLE_TITLE,
kind: Constant::CodeActionKind::REFACTOR_REWRITE,
data: { range: @range, uri: @uri.to_s },
),
]
end
end
end
end
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"
}
]
}
]
}
}
}
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| } }"
}
]
}
]
}
}
}
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| }"
}
]
}
]
}
}
}
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"
}
]
}
]
}
}
}
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"
}
]
}
]
}
}
}
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": []
}
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"
}
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/aref_call_aref_assign_without_selection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
object["attributes"].find { |a| a["field"] == "expected" }["value"] = "changed"
Loading