mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-24 18:39:50 -04:00
DEV: Handle comments in tuple field expressions
This commit is contained in:
parent
957d0c6102
commit
94ca738ee1
@ -88,9 +88,10 @@ where
|
|||||||
let empty: Vec<Token> = Vec::new();
|
let empty: Vec<Token> = Vec::new();
|
||||||
//eprintln!("comment line candidate: {}", line);
|
//eprintln!("comment line candidate: {}", line);
|
||||||
let cg = map.get(&line).unwrap_or(&empty);
|
let cg = map.get(&line).unwrap_or(&empty);
|
||||||
|
let indent = self.make_indent();
|
||||||
//eprintln!("comment_group: {:?}", cg);
|
//eprintln!("comment_group: {:?}", cg);
|
||||||
for c in cg.iter() {
|
for c in cg.iter() {
|
||||||
write!(self.w, "// {}\n", c.fragment.trim())?;
|
write!(self.w, "{}// {}\n", indent, c.fragment.trim())?;
|
||||||
}
|
}
|
||||||
self.comment_group_lines.pop();
|
self.comment_group_lines.pop();
|
||||||
}
|
}
|
||||||
@ -155,6 +156,12 @@ where
|
|||||||
write!(self.w, "\n")?;
|
write!(self.w, "\n")?;
|
||||||
}
|
}
|
||||||
for &(ref t, ref expr) in def.iter() {
|
for &(ref t, ref expr) in def.iter() {
|
||||||
|
let field_line = t.pos.line;
|
||||||
|
let expr_line = expr.pos().line;
|
||||||
|
self.render_comment_if_needed(field_line)?;
|
||||||
|
if expr_line != field_line {
|
||||||
|
self.render_comment_if_needed(expr_line)?;
|
||||||
|
}
|
||||||
write!(self.w, "{}", indent)?;
|
write!(self.w, "{}", indent)?;
|
||||||
if Self::is_bareword(&t.fragment) {
|
if Self::is_bareword(&t.fragment) {
|
||||||
write!(&mut self.w, "{} = ", t.fragment)?;
|
write!(&mut self.w, "{} = ", t.fragment)?;
|
||||||
@ -427,9 +434,9 @@ where
|
|||||||
self.render_stmt(v)?;
|
self.render_stmt(v)?;
|
||||||
}
|
}
|
||||||
if let Some(last_comment_line) = self.comment_group_lines.first() {
|
if let Some(last_comment_line) = self.comment_group_lines.first() {
|
||||||
eprintln!("last_comment_line is: {}", last_comment_line);
|
//eprintln!("last_comment_line is: {}", last_comment_line);
|
||||||
eprintln!("comment_map is: {:?}", self.comment_map);
|
//eprintln!("comment_map is: {:?}", self.comment_map);
|
||||||
eprintln!("coment_group_lines is: {:?}", self.comment_group_lines);
|
//eprintln!("coment_group_lines is: {:?}", self.comment_group_lines);
|
||||||
self.render_missed_comments(*last_comment_line + 1)?;
|
self.render_missed_comments(*last_comment_line + 1)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -402,3 +402,51 @@ fn test_statement_with_comment_printing_comments_at_end() {
|
|||||||
format!("{}\n", input.trim())
|
format!("{}\n", input.trim())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_expression_with_embedded_comment() {
|
||||||
|
let mut comment_map = BTreeMap::new();
|
||||||
|
let input = "{\n foo = bar,\n // a comment\n bar = foo,\n};";
|
||||||
|
let stmts = assert_parse(input, Some(&mut comment_map));
|
||||||
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
|
let mut printer = AstPrinter::new(2, &mut buffer).with_comment_map(&comment_map);
|
||||||
|
assert!(printer.render(&stmts).is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
String::from_utf8(buffer).unwrap(),
|
||||||
|
format!("{}\n", input.trim())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_expression_with_embedded_comment_mid_field_expr() {
|
||||||
|
let mut comment_map = BTreeMap::new();
|
||||||
|
let input = "{\n foo = bar,\n bar =\n// a comment\n foo\n};";
|
||||||
|
let stmts = assert_parse(input, Some(&mut comment_map));
|
||||||
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
|
let mut printer = AstPrinter::new(2, &mut buffer).with_comment_map(&comment_map);
|
||||||
|
assert!(printer.render(&stmts).is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
String::from_utf8(buffer).unwrap(),
|
||||||
|
format!(
|
||||||
|
"{}\n",
|
||||||
|
"{\n foo = bar,\n // a comment\n bar = foo,\n};".trim()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_expression_with_embedded_comment_and_mid_field_expr() {
|
||||||
|
let mut comment_map = BTreeMap::new();
|
||||||
|
let input = "{\n foo = bar,\n// a comment\n bar =\n// another comment\n foo\n};";
|
||||||
|
let stmts = assert_parse(input, Some(&mut comment_map));
|
||||||
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
|
let mut printer = AstPrinter::new(2, &mut buffer).with_comment_map(&comment_map);
|
||||||
|
assert!(printer.render(&stmts).is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
String::from_utf8(buffer).unwrap(),
|
||||||
|
format!(
|
||||||
|
"{}\n",
|
||||||
|
"{\n foo = bar,\n // a comment\n // another comment\n bar = foo,\n};".trim()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user