Skip to content

Commit

Permalink
Generically handle all method visibility modifiers (#369)
Browse files Browse the repository at this point in the history
* Fixtures

* Properly handle end spacing for all method-modifiers

* Make index a constant and add some more explanatory comments

Co-authored-by: Reese Williams <[email protected]>
  • Loading branch information
reese and reese-stripe authored Dec 3, 2022
1 parent d6ee1c7 commit cc74bbb
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 35 deletions.
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

0 comments on commit cc74bbb

Please sign in to comment.