Skip to content

Commit

Permalink
bees (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
fables-tales committed Apr 6, 2020
1 parent 335fe82 commit 489ad3b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Makefile
@@ -1,4 +1,4 @@
.PHONY: clean clippy lint
.PHONY: clean clippy lint fmt

debug: target/rubyfmt_debug.bundle

Expand Down Expand Up @@ -29,3 +29,6 @@ target/release/librubyfmt.a: native/src/*.rs native/Cargo.toml

clean:
rm -rf target/

fmt:
cd native && cargo fmt && git add -u ./
7 changes: 7 additions & 0 deletions fixtures/small/memoist_actual.rb
@@ -0,0 +1,7 @@
memoize def foo
end

memoize def foo(x, y, z)
aboiqjfdw
foo(bar, 1, 2)
end
11 changes: 11 additions & 0 deletions fixtures/small/memoist_expected.rb
@@ -0,0 +1,11 @@
memoize(
def foo
end
)

memoize(
def foo(x, y, z)
aboiqjfdw
foo(bar, 1, 2)
end
)
7 changes: 7 additions & 0 deletions native/src/breakable_entry.rs
Expand Up @@ -62,4 +62,11 @@ impl BreakableEntry {
pub fn is_multiline(&self) -> bool {
self.line_numbers.len() > 1
}

pub fn last_token_is_a_hard_newline(&self) -> bool {
match self.tokens.last() {
Some(LineToken::HardNewLine) => true,
_ => false,
}
}
}
9 changes: 7 additions & 2 deletions native/src/format.rs
Expand Up @@ -7,7 +7,9 @@ pub fn format_def(ps: &mut ParserState, def: Def) {

let body = def.3;
ps.on_line((def_expression.1).0);
ps.emit_indent();
if ps.at_start_of_line() {
ps.emit_indent();
}
ps.emit_def(def_expression.0);
format_paren_or_params(ps, def.2);
ps.emit_newline();
Expand All @@ -20,7 +22,10 @@ pub fn format_def(ps: &mut ParserState, def: Def) {
});
});

ps.emit_end();
ps.with_start_of_line(true, |ps| {
ps.emit_end();
ps.wind_line_forward();
});
ps.emit_newline();
}

Expand Down
24 changes: 16 additions & 8 deletions native/src/parser_state.rs
Expand Up @@ -120,7 +120,7 @@ impl ParserState {
return;
}

if let Some(be) = self.breakable_entry_stack.last_mut() {
for be in self.breakable_entry_stack.iter_mut().rev() {
be.push_line_number(line_number);
}

Expand Down Expand Up @@ -268,7 +268,9 @@ impl ParserState {
}

pub fn emit_collapsing_newline(&mut self) {
self.push_token(LineToken::CollapsingNewLine);
if !self.last_token_is_a_hard_newline() {
self.push_token(LineToken::CollapsingNewLine);
}
}

pub fn emit_def(&mut self, def_name: String) {
Expand All @@ -285,12 +287,7 @@ impl ParserState {
}

pub fn emit_end(&mut self) {
if !self
.render_queue
.last()
.map(|x| x.is_newline())
.unwrap_or(false)
{
if !self.last_token_is_a_hard_newline() {
self.emit_newline();
}
if self.at_start_of_line() {
Expand All @@ -299,6 +296,17 @@ impl ParserState {
self.push_token(LineToken::End);
}

fn last_token_is_a_hard_newline(&self) -> bool {
if let Some(be) = self.breakable_entry_stack.last() {
be.last_token_is_a_hard_newline()
} else {
self.render_queue
.last()
.map(|x| x.is_newline())
.unwrap_or(false)
}
}

pub fn shift_comments(&mut self) {
let idx_of_prev_hard_newline = self.index_of_prev_hard_newline();

Expand Down
5 changes: 4 additions & 1 deletion native/src/render_queue_writer.rs
Expand Up @@ -22,7 +22,10 @@ impl RenderQueueWriter {
Err(_) => true,
Ok(x) => x != "1",
};

#[cfg(debug_assertions)]
{
eprintln!("first tokens {:?}", self.tokens);
}
if run {
Self::render_as(
&mut accum,
Expand Down

0 comments on commit 489ad3b

Please sign in to comment.