Skip to content

Commit 3aa03dc

Browse files
committed
Use new module code from libsyntax
1 parent 609e621 commit 3aa03dc

File tree

5 files changed

+49
-64
lines changed

5 files changed

+49
-64
lines changed

src/comment.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ fn format_comments() {
101101
12));
102102

103103
let input = "// comment";
104-
let expected_output = "/* com\n \
105-
* men\n \
106-
* t */";
107-
assert_eq!(expected_output, rewrite_comment(input, true, 9, 69));
104+
let expected = "/* com\n \
105+
* men\n * \
106+
t */";
107+
assert_eq!(expected, rewrite_comment(input, true, 9, 69));
108108

109109
assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100, 100));
110110
}

src/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,11 @@ fn rewrite_binary_op(context: &RewriteContext,
347347
result.push(' ');
348348
result.push_str(&operator_str);
349349

350+
// 1 = space between operator and rhs
351+
let used_width = result.len() + operator_str.len() + 1;
350352
let remaining_width = match result.rfind('\n') {
351-
Some(idx) => (offset + width + idx).checked_sub(result.len()).unwrap_or(0),
352-
None => width.checked_sub(result.len()).unwrap_or(0)
353+
Some(idx) => (offset + width + idx).checked_sub(used_width).unwrap_or(0),
354+
None => width.checked_sub(used_width).unwrap_or(0)
353355
};
354356

355357
// Get "full width" rhs and see if it fits on the current line. This

src/visitor.rs

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
use syntax::ast;
1212
use syntax::codemap::{self, CodeMap, Span, BytePos};
1313
use syntax::visit;
14-
use syntax::parse::token;
15-
use syntax::attr;
14+
use syntax::parse::{token, parser};
1615
use std::path::PathBuf;
1716

1817
use utils;
1918
use config::Config;
20-
use comment::FindUncommented;
2119

2220
use changes::ChangeSet;
2321
use rewrite::{Rewrite, RewriteContext};
@@ -363,68 +361,48 @@ impl<'a> FmtVisitor<'a> {
363361

364362
fn format_mod(&mut self, m: &ast::Mod, s: Span, ident: ast::Ident, attrs: &[ast::Attribute]) {
365363
debug!("FmtVisitor::format_mod: ident: {:?}, span: {:?}", ident, s);
364+
366365
// Decide whether this is an inline mod or an external mod.
367-
// There isn't any difference between inline and external mod in AST,
368-
// so we use the trick of searching for an opening brace.
369-
// We can't use the inner span of the mod since it is weird when it
370-
// is empty (no items).
371-
// FIXME Use the inner span once rust-lang/rust#26755 is fixed.
372-
let open_brace = self.codemap.span_to_snippet(s).unwrap().find_uncommented("{");
373-
match open_brace {
374-
None => {
375-
debug!("FmtVisitor::format_mod: external mod");
376-
let file_path = self.module_file(ident, attrs, s);
377-
let filename = file_path.to_str().unwrap();
378-
if self.changes.is_changed(filename) {
379-
// The file has already been reformatted, do nothing
380-
} else {
381-
self.format_separate_mod(m, filename);
382-
}
383-
// TODO Should rewrite properly `mod X;`
384-
}
385-
Some(open_brace) => {
386-
debug!("FmtVisitor::format_mod: internal mod");
387-
debug!("... open_brace: {}, str: {:?}",
388-
open_brace,
389-
self.codemap.span_to_snippet(s));
390-
// Format everything until opening brace
391-
// TODO Shoud rewrite properly
392-
self.format_missing(s.lo + BytePos(open_brace as u32));
393-
self.block_indent += self.config.tab_spaces;
394-
visit::walk_mod(self, m);
395-
debug!("... last_pos after: {:?}", self.last_pos);
396-
self.block_indent -= self.config.tab_spaces;
366+
let local_file_name = self.codemap.span_to_filename(s);
367+
let is_internal = local_file_name == self.codemap.span_to_filename(m.inner);
368+
369+
// TODO Should rewrite properly `mod X;`
370+
371+
if is_internal {
372+
debug!("FmtVisitor::format_mod: internal mod");
373+
self.block_indent += self.config.tab_spaces;
374+
visit::walk_mod(self, m);
375+
debug!("... last_pos after: {:?}", self.last_pos);
376+
self.block_indent -= self.config.tab_spaces;
377+
} else {
378+
debug!("FmtVisitor::format_mod: external mod");
379+
let file_path = self.module_file(ident, attrs, local_file_name);
380+
let filename = file_path.to_str().unwrap();
381+
if self.changes.is_changed(filename) {
382+
// The file has already been reformatted, do nothing
383+
} else {
384+
self.format_separate_mod(m, filename);
397385
}
398386
}
399-
self.format_missing(s.hi);
387+
400388
debug!("FmtVisitor::format_mod: exit");
401389
}
402390

403391
/// Find the file corresponding to an external mod
404-
/// Same algorithm as syntax::parse::eval_src_mod
405-
fn module_file(&self, id: ast::Ident, outer_attrs: &[ast::Attribute], id_sp: Span) -> PathBuf {
406-
// FIXME use libsyntax once rust-lang/rust#26750 is merged
407-
let mut prefix = PathBuf::from(&self.codemap.span_to_filename(id_sp));
408-
prefix.pop();
409-
let mod_string = token::get_ident(id);
410-
match attr::first_attr_value_str_by_name(outer_attrs, "path") {
411-
Some(d) => prefix.join(&*d),
412-
None => {
413-
let default_path_str = format!("{}.rs", mod_string);
414-
let secondary_path_str = format!("{}/mod.rs", mod_string);
415-
let default_path = prefix.join(&default_path_str);
416-
let secondary_path = prefix.join(&secondary_path_str);
417-
let default_exists = self.codemap.file_exists(&default_path);
418-
let secondary_exists = self.codemap.file_exists(&secondary_path);
419-
if default_exists {
420-
default_path
421-
} else if secondary_exists {
422-
secondary_path
423-
} else {
424-
// Should never appens since rustc parsed everything sucessfully
425-
panic!("Didn't found module {}", mod_string);
426-
}
427-
}
392+
fn module_file(&self, id: ast::Ident, attrs: &[ast::Attribute], filename: String) -> PathBuf {
393+
let dir_path = {
394+
let mut path = PathBuf::from(&filename);
395+
path.pop();
396+
path
397+
};
398+
399+
if let Some(path) = parser::Parser::submod_path_from_attr(attrs, &dir_path) {
400+
return path;
401+
}
402+
403+
match parser::Parser::default_submod_path(id, &dir_path, &self.codemap).result {
404+
Ok(parser::ModulePathSuccess { path, .. }) => path,
405+
_ => panic!("Couldn't find module {}", token::get_ident(id))
428406
}
429407
}
430408

tests/source/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ fn foo() -> bool {
44
let very_long_variable_name = ( a + first + simple + test );
55
let very_long_variable_name = (a + first + simple + test + AAAAAAAAAAAAA + BBBBBBBBBBBBBBBBB + b + c);
66

7+
let is_internalxxxx = self.codemap.span_to_filename(s) == self.codemap.span_to_filename(m.inner);
8+
79
let some_val = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa * bbbb / (bbbbbb -
810
function_call(x, *very_long_pointer, y))
911
+ 1000;

tests/target/expr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ fn foo() -> bool {
55
let very_long_variable_name = (a + first + simple + test + AAAAAAAAAAAAA + BBBBBBBBBBBBBBBBB +
66
b + c);
77

8+
let is_internalxxxx = self.codemap.span_to_filename(s) ==
9+
self.codemap.span_to_filename(m.inner);
10+
811
let some_val = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa * bbbb /
912
(bbbbbb - function_call(x, *very_long_pointer, y)) + 1000;
1013

0 commit comments

Comments
 (0)