Skip to content

Commit

Permalink
Auto indent after end keyword (#1014)
Browse files Browse the repository at this point in the history
* audo indent after end keyword

* fix code review appointments

* fix sorbet appointments

* solve code review appointments

* code review updates

* add sorbet signature

* solve linter report problems
  • Loading branch information
giovannism20 authored Dec 6, 2023
1 parent e5ff372 commit 57bd213
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
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
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
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

0 comments on commit 57bd213

Please sign in to comment.