From 3b06f3c2594571cdfc5f605be69ad74f33fa2637 Mon Sep 17 00:00:00 2001 From: Reese Williams Date: Sun, 14 May 2023 09:32:22 -0700 Subject: [PATCH] Fix comment rendering for sclasses (#420) * Fixtures * Use the same constant body renderer as classes/modules for sclasses --- fixtures/small/sclass_actual.rb | 13 +++++++++++++ fixtures/small/sclass_expected.rb | 13 +++++++++++++ librubyfmt/rubyfmt_lib.rb | 5 +++-- librubyfmt/src/format.rs | 24 +++--------------------- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/fixtures/small/sclass_actual.rb b/fixtures/small/sclass_actual.rb index ed572a29d..b3fc4aef1 100644 --- a/fixtures/small/sclass_actual.rb +++ b/fixtures/small/sclass_actual.rb @@ -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 diff --git a/fixtures/small/sclass_expected.rb b/fixtures/small/sclass_expected.rb index ed572a29d..b3fc4aef1 100644 --- a/fixtures/small/sclass_expected.rb +++ b/fixtures/small/sclass_expected.rb @@ -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 diff --git a/librubyfmt/rubyfmt_lib.rb b/librubyfmt/rubyfmt_lib.rb index 385fa1eda..c83f2fd53 100644 --- a/librubyfmt/rubyfmt_lib.rb +++ b/librubyfmt/rubyfmt_lib.rb @@ -45,6 +45,7 @@ def initialize(file_data) "return" => [], "when" => [], "case" => [], + "class" => [], "yield" => [], "break" => [], "super" => [], @@ -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) diff --git a/librubyfmt/src/format.rs b/librubyfmt/src/format.rs index 51bcf06ff..941c020a1 100644 --- a/librubyfmt/src/format.rs +++ b/librubyfmt/src/format.rs @@ -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| { @@ -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) {