Skip to content
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

Auto indent after end keyword #1014

Merged
merged 13 commits into from
Dec 6, 2023
2 changes: 1 addition & 1 deletion lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def initialize_request(options)
on_type_formatting_provider = if enabled_features["onTypeFormatting"]
Interface::DocumentOnTypeFormattingOptions.new(
first_trigger_character: "{",
more_trigger_character: ["\n", "|"],
more_trigger_character: ["\n", "|", "d"],
)
end

Expand Down
28 changes: 28 additions & 0 deletions lib/ruby_lsp/requests/on_type_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def run
handle_statement_end
end
end
when "d"
auto_indent_after_end_keyword
end

@edits
Expand Down Expand Up @@ -185,6 +187,32 @@ def find_indentation(line)

count
end

sig { void }
def auto_indent_after_end_keyword
giovannism20 marked this conversation as resolved.
Show resolved Hide resolved
giovannism20 marked this conversation as resolved.
Show resolved Hide resolved
current_line = @lines[@position[:line]]
return unless current_line&.strip == "end"

target, _parent, _nesting = @document.locate_node({
line: @position[:line],
character: @position[:character] - 1,
})

statements = case target
when Prism::IfNode, Prism::UnlessNode, Prism::ForNode, Prism::WhileNode, Prism::UntilNode
target.statements
end
return unless statements

statements.body.each do |node|
loc = node.location
next unless loc.start_column == @indentation

add_edit_with_text(" ", { line: loc.start_line - 1, character: 0 })
end

move_cursor_to(@position[:line], @position[:character])
end
end
end
end
18 changes: 18 additions & 0 deletions test/requests/on_type_formatting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,24 @@ def test_breaking_line_immediately_after_keyword
assert_equal(expected_edits.to_json, T.must(edits).to_json)
end

def test_auto_indent_after_end_keyword
giovannism20 marked this conversation as resolved.
Show resolved Hide resolved
document = RubyLsp::RubyDocument.new(source: +"if foo\nbar\nend", version: 1, uri: URI("file:///fake.rb"))
edits = RubyLsp::Requests::OnTypeFormatting.new(document, { line: 2, character: 2 }, "d").run

expected_edits = [
{
range: { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } },
newText: " ",
},
{
range: { start: { line: 2, character: 2 }, end: { line: 2, character: 2 } },
newText: "$0",
},
]

assert_equal(expected_edits.to_json, T.must(edits).to_json)
end

def test_breaking_line_if_a_keyword_is_part_of_method_call
document = RubyLsp::RubyDocument.new(source: +" force({", version: 1, uri: URI("file:///fake.rb"))
edits = RubyLsp::Requests::OnTypeFormatting.new(document, { line: 1, character: 2 }, "\n").run
Expand Down
Loading