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 @@ -667,7 +667,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
23 changes: 23 additions & 0 deletions lib/ruby_lsp/requests/on_type_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def run
elsif @document.syntax_error?
handle_statement_end
end
when "d"
auto_indent_after_end_keyword
end

@edits
Expand Down Expand Up @@ -171,6 +173,27 @@ 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 = T.cast(
@document.locate_node(@position, node_types: [YARP::StatementsNode]),
[T.nilable(YARP::StatementsNode), T.nilable(YARP::Node), T::Array[String]],
)
return unless target

target.body.each do |node|
start_line = node.location.start_line
unless @lines[start_line]&.start_with?(" ")
add_edit_with_text(" ", { line: start_line, character: 0 })
end
end

move_cursor_to(@position[:line], @position[:character])
end
end
end
end
28 changes: 28 additions & 0 deletions test/requests/on_type_formatting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,32 @@ 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::Document.new(source: +"", version: 1, uri: URI("file:///fake.rb"))

document.push_edits(
[{
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
text: "if true\nputs \"a\"\nend",
}],
version: 2,
)
document.parse

edits = RubyLsp::Requests::OnTypeFormatting.new(document, { line: 2, character: 0 }, "d").run

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

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