Skip to content

Format loops #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ impl<'a> ChangeSet<'a> {
self.push_str(&file_name, text)
}

// Fetch the output buffer for the given file name.
// Panics on unknown files.
pub fn get(&mut self, file_name: &str) -> &StringBuffer {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should document that this panics if file_name is unknown (also for get_mut)

self.file_map.get(file_name).unwrap()
}

// Fetch a mutable reference to the output buffer for the given file name.
// Panics on unknown files.
pub fn get_mut(&mut self, file_name: &str) -> &mut StringBuffer {
self.file_map.get_mut(file_name).unwrap()
}
Expand Down
31 changes: 30 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ use rewrite::{Rewrite, RewriteContext};
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic};
use string::{StringFormat, rewrite_string};
use utils::{span_after, make_indent};
use visitor::FmtVisitor;

use syntax::{ast, ptr};
use syntax::codemap::{Pos, Span, BytePos};
use syntax::codemap::{Pos, Span, BytePos, mk_sp};
use syntax::parse::token;
use syntax::print::pprust;
use syntax::visit::Visitor;

impl Rewrite for ast::Expr {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
Expand Down Expand Up @@ -53,11 +55,38 @@ impl Rewrite for ast::Expr {
ast::Expr_::ExprTup(ref items) => {
rewrite_tuple_lit(context, items, self.span, width, offset)
}
ast::Expr_::ExprLoop(ref block, _) => {
// FIXME: this drops any comment between "loop" and the block.
// TODO: format label
block.rewrite(context, width, offset).map(|result| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DO you need to adjust the width here to take account of "loop "?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Blocks go to the new line immediately after the opening brace.. That's why width and offset are disregarded in the implementation of Rewrite for ast::Block. It only needs the current block indentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, of course

format!("loop {}", result)
})
}
_ => context.codemap.span_to_snippet(self.span).ok()
}
}
}

impl Rewrite for ast::Block {
fn rewrite(&self, context: &RewriteContext, _: usize, _: usize) -> Option<String> {
let mut visitor = FmtVisitor::from_codemap(context.codemap, context.config);
visitor.last_pos = self.span.lo;
visitor.block_indent = context.block_indent;

visitor.visit_block(self);

// Push text between last block item and end of block
let snippet = visitor.snippet(mk_sp(visitor.last_pos, self.span.hi));
visitor.changes.push_str_span(self.span, &snippet);

// Stringify visitor
let file_name = context.codemap.span_to_filename(self.span);
let string_buffer = visitor.changes.get(&file_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying you need to make the changes as part of this PR, but just going to note some odd-ness about the FmtVisitor API here:

  • We've just filled the visitor with local changes, and yet now we need a filename to get them out. Furthermore, we have to reach inside the visitor to its changes field to get them. I'd prefer some API to just dump everything collected so far into a String (or StringBuffer).
  • With the calls to add any missed text, this seems like it should be an internal operation on the visitor - there should be a function which takes a position (self.span.hi, in this case), and does the rest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought a bit about that when dealing with molules issue. In fact, there are only two places where we need the multiple-file aspect of the visitor: visiting a module and when we print the result. For all other tasks, complicated changeset is only noise. For example, there is no obvious reason for ChangeSet::push_str_span to need a span, the text should be added to the file we're working on automatically (we never make a push_str_span on more than one file).
A simple option to fix that would be to add a currentFile: StringBuffer to the visitor, all functions would add text on this, and format_mod would exchange this when needed.
Eventually, format_mod could become an entry point for the visit, and we could remove entierely the changeset of the visitor.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_mut is surprizing here. Maybe write a ChangeSet::get or simply visitor.changes.file_map.get(&filename).unwrap ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed; fixed.

Some(string_buffer.to_string())
}
}

fn rewrite_string_lit(context: &RewriteContext,
s: &str,
span: Span,
Expand Down
1 change: 1 addition & 0 deletions src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ pub trait Rewrite {
pub struct RewriteContext<'a> {
pub codemap: &'a CodeMap,
pub config: &'a Config,
pub block_indent: usize,
}
4 changes: 3 additions & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.codemap.lookup_char_pos(ex.span.hi));
self.format_missing(ex.span.lo);
let offset = self.changes.cur_offset_span(ex.span);
let context = RewriteContext { codemap: self.codemap, config: self.config };
let context = RewriteContext { codemap: self.codemap,
config: self.config,
block_indent: self.block_indent, };
let rewrite = ex.rewrite(&context, self.config.max_width - offset, offset);

if let Some(new_str) = rewrite {
Expand Down
11 changes: 11 additions & 0 deletions tests/source/loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

fn main() {
loop
{ return some_val;}

let x = loop { do_forever(); };

loop {
// Just comments
}
}
14 changes: 14 additions & 0 deletions tests/target/loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

fn main() {
loop {
return some_val;
}

let x = loop {
do_forever();
};

loop {
// Just comments
}
}