Skip to content

Commit

Permalink
Fix comment rendering for sclasses (#420)
Browse files Browse the repository at this point in the history
* Fixtures

* Use the same constant body renderer as classes/modules for sclasses
  • Loading branch information
reese committed May 14, 2023
1 parent a6afe44 commit 3b06f3c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
13 changes: 13 additions & 0 deletions fixtures/small/sclass_actual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@ class << self
end
end

# The moon beams white on the peach blossoms,
# the Milky Way moored silvering at midnight.
class MoonBeam
# I wonder if the nightingale has noticed
# the spring spirit already alive in the branch?
class << self
# I can hardly get to sleep
# as if tenderness were a sickness.
def show
end
end
end

Foo.machine
13 changes: 13 additions & 0 deletions fixtures/small/sclass_expected.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@ class << self
end
end

# The moon beams white on the peach blossoms,
# the Milky Way moored silvering at midnight.
class MoonBeam
# I wonder if the nightingale has noticed
# the spring spirit already alive in the branch?
class << self
# I can hardly get to sleep
# as if tenderness were a sickness.
def show
end
end
end

Foo.machine
5 changes: 3 additions & 2 deletions librubyfmt/rubyfmt_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def initialize(file_data)
"return" => [],
"when" => [],
"case" => [],
"class" => [],
"yield" => [],
"break" => [],
"super" => [],
Expand Down Expand Up @@ -406,11 +407,11 @@ def on_defs(*args)
end

def on_class(*args)
with_lineno { super }
super + [start_end_for_keyword("class")]
end

def on_sclass(*args)
with_lineno { super }
super + [start_end_for_keyword("class")]
end

def on_module(*args)
Expand Down
24 changes: 3 additions & 21 deletions librubyfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3581,6 +3581,8 @@ pub fn format_sclass(ps: &mut dyn ConcreteParserState, sc: SClass) {
let body = sc.2;
let end_line = sc.3.end_line();

ps.on_line(sc.3.start_line());

ps.with_start_of_line(
false,
Box::new(|ps| {
Expand All @@ -3589,29 +3591,9 @@ pub fn format_sclass(ps: &mut dyn ConcreteParserState, sc: SClass) {
ps.emit_ident("<<".to_string());
ps.emit_space();
format_expression(ps, *expr);
ps.emit_newline();
ps.new_block(Box::new(|ps| {
ps.with_start_of_line(
true,
Box::new(|ps| {
format_bodystmt(ps, body, end_line);
}),
);
}));
format_constant_body(ps, body, end_line);
}),
);
ps.with_start_of_line(
true,
Box::new(|ps| {
ps.emit_end();
}),
);

ps.on_line(end_line);

if ps.at_start_of_line() {
ps.emit_newline();
}
}

pub fn format_stabby_lambda(ps: &mut dyn ConcreteParserState, sl: StabbyLambda) {
Expand Down

0 comments on commit 3b06f3c

Please sign in to comment.