Skip to content

Commit

Permalink
fmt: fix item_fn
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Aug 8, 2024
1 parent 20c7016 commit 74c5ef7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/rune/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) use self::unit_builder::UnitBuilder;
pub(crate) mod v1;

mod options;
#[cfg(any(all(test, feature = "fmt"), feature = "languageserver"))]
#[cfg(any(feature = "fmt", feature = "languageserver"))]
pub(crate) use self::options::FmtOptions;
pub use self::options::{Options, ParseOptionError};

Expand Down
38 changes: 10 additions & 28 deletions crates/rune/src/fmt/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1419,39 +1419,21 @@ fn tuple_body<'a>(o: &mut Output<'a>, p: &mut Stream<'a>) -> Result<()> {
fn item_fn<'a>(o: &mut Output<'a>, p: &mut Stream<'a>) -> Result<()> {
o.write(p.expect(K![fn])?)?;

let mut has_arguments = false;
let mut has_body = false;

for node in p.by_ref() {
match node.kind() {
K![ident] => {
o.ws()?;
o.write(node)?;
}
FnArgs => {
has_arguments = true;
node.parse(|p| fn_args(o, p))?;
}
Block => {
if !has_arguments {
o.lit("()")?;
has_arguments = true;
}

has_body = true;

o.ws()?;
node.parse(|p| block(o, p))?;
}
_ => {}
}
if matches!(p.peek(), K![ident]) {
o.ws()?;
o.write(p.pump()?)?;
}

if !has_arguments {
if let Some(node) = p.try_pump(FnArgs)? {
node.parse(|p| fn_args(o, p))?;
} else {
o.lit("()")?;
}

if !has_body {
if let Some(node) = p.try_pump(Block)? {
o.ws()?;
node.parse(|p| block(o, p))?;
} else {
o.ws()?;
o.lit("{")?;
o.nl(1)?;
Expand Down

0 comments on commit 74c5ef7

Please sign in to comment.