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

Generically handle all method visibility modifiers #369

Merged
merged 3 commits into from
Dec 3, 2022
Merged
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
9 changes: 0 additions & 9 deletions fixtures/small/private_def_actual.rb

This file was deleted.

9 changes: 0 additions & 9 deletions fixtures/small/private_def_expected.rb

This file was deleted.

23 changes: 23 additions & 0 deletions fixtures/small/visibility_modifier_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Foo
private def bar
42
end

private def baz(a, b, c)
a + b + c
end
end

module WhiteTeaBowl
sig do
params(walking_this: Path)
end
module_function def i_choose_one; end

sig do
void
end
patch_of def sunlight
"after another"
end
end
24 changes: 24 additions & 0 deletions fixtures/small/visibility_modifier_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Foo
private def bar
42
end

private def baz(a, b, c)
a + b + c
end
end

module WhiteTeaBowl
sig do
params(walking_this: Path)
end
module_function def i_choose_one
end

sig do
void
end
patch_of def sunlight
"after another"
end
end
7 changes: 7 additions & 0 deletions librubyfmt/src/intermediary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ impl Intermediary {
}
}

pub fn insert_blankline_from_end(&mut self, index_from_end: usize) {
self.tokens.insert(
self.tokens.len() - index_from_end,
ConcreteLineToken::HardNewLine,
)
}

pub fn insert_trailing_blankline(&mut self, _bl: BlanklineReason) {
if self.index_of_last_hard_newline <= 2 {
self.tokens.insert(
Expand Down
13 changes: 0 additions & 13 deletions librubyfmt/src/line_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,6 @@ impl ConcreteLineToken {
}
}

pub fn is_method_visibility_modifier(&self) -> bool {
match self {
Self::DirectPart { part } => {
part == "public"
|| part == "private"
|| part == "protected"
|| part == "public_class_method"
|| part == "private_class_method"
}
_ => false,
}
}

pub fn is_single_line_breakable_garbage(&self) -> bool {
match self {
Self::DirectPart { part } => part == &"".to_string(),
Expand Down
21 changes: 17 additions & 4 deletions librubyfmt/src/render_queue_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,29 @@ impl RenderQueueWriter {
}

if let Some(
[&ConcreteLineToken::End, &ConcreteLineToken::AfterCallChain, &ConcreteLineToken::HardNewLine, &ConcreteLineToken::Indent { .. }, x],
) = accum.last::<5>()
[&ConcreteLineToken::End, &ConcreteLineToken::AfterCallChain, &ConcreteLineToken::HardNewLine, &ConcreteLineToken::Indent { .. }, x, maybe_space, maybe_def],
) = accum.last::<7>()
{
match x {
ConcreteLineToken::DefKeyword => {}
_ => {
if x.is_in_need_of_a_trailing_blankline()
&& !x.is_method_visibility_modifier()
&& !matches!(
(maybe_space, maybe_def),
(ConcreteLineToken::Space, ConcreteLineToken::DefKeyword)
)
{
accum.insert_trailing_blankline(BlanklineReason::ComesAfterEnd);
// If we're here, the last few tokens must look like this:
// | token | index_from_end |
// | End | 6 |
// |. AfterCallChain | 5 |
// | HardNewline | 4 | <-- insert after this token
// |. Indent | 3 . |
// | (ArbitraryToken) | 2 |
// | (ArbitraryToken) | 1 |
// | (ArbitraryToken) | 0 |
const LAST_NEWLINE_INDEX_FROM_END: usize = 4;
accum.insert_blankline_from_end(LAST_NEWLINE_INDEX_FROM_END);
}
}
}
Expand Down