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(
@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