From a801c78fc194b8f7e80efa28c1c1b37c6b50d224 Mon Sep 17 00:00:00 2001 From: Astrale Date: Sat, 9 Sep 2023 14:37:43 +0200 Subject: [PATCH] fix on off in content and markup --- lib/src/markup.rs | 21 ++++++++++++++++++- ...ib__tests__snippets__on_off__snapshot.snap | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/src/markup.rs b/lib/src/markup.rs index 9fe611f..81504fb 100644 --- a/lib/src/markup.rs +++ b/lib/src/markup.rs @@ -17,6 +17,10 @@ pub(crate) fn format_content_blocks( for (s, node) in children.iter().zip(parent.children()) { match node.kind() { _ if ctx.off => res.push_str(node.text()), + LineComment | BlockComment => { + let buf = format_comment_handling_disable(&node, &[], ctx); + ctx.push_raw_in(&buf, &mut res); + } RightBracket if spaced => { let space_type = if first_space.unwrap().text().contains('\n') { '\n' @@ -48,8 +52,23 @@ pub(crate) fn format_markup(parent: &LinkedNode, children: &[String], ctx: &mut for (idx, (s, node)) in children.iter().zip(parent.children()).enumerate() { match node.kind() { - _ if ctx.off => res.push_str(node.text()), // todo, interaction with line below? + _ if ctx.off => res.push_str(&deep_no_format(&node)), // todo, interaction with line below? _ if skip_until.is_some_and(|skip| idx <= skip) => {} + LineComment | BlockComment => { + let buf = format_comment_handling_disable(&node, &[], ctx); + if ctx.off + && [Space, Parbreak] + .map(Some) + .contains(&utils::get_prev_ignoring(&node, &[]).map(|x| x.kind())) + { + let s = utils::get_prev_ignoring(&node, &[]) + .map(|x| x.text().to_string()) + .unwrap_or_default(); + let s = s.split('\n').last().unwrap_or_default(); + ctx.push_raw_in(s, &mut res); + } + ctx.push_raw_in(&buf, &mut res); + } Space => { if idx == 0 || idx == children.len() diff --git a/lib/src/tests/snapshots/typstfmt_lib__tests__snippets__on_off__snapshot.snap b/lib/src/tests/snapshots/typstfmt_lib__tests__snippets__on_off__snapshot.snap index 2eeef4c..8f4aaf1 100644 --- a/lib/src/tests/snapshots/typstfmt_lib__tests__snippets__on_off__snapshot.snap +++ b/lib/src/tests/snapshots/typstfmt_lib__tests__snippets__on_off__snapshot.snap @@ -1,6 +1,6 @@ --- source: lib/src/tests/snippets.rs -description: "INPUT\n===\n\"// typstfmt::off\\n#{{4}}\\n// typstfmt::on\\n#{{4}}\\n\"\n===\n// typstfmt::off\n#{{4}}\n// typstfmt::on\n#{{4}}\n\n===\nFORMATTED\n===\n// typstfmt::off\n#{{4}}\n// typstfmt::on\n#{ { 4 } }\n" +description: "INPUT\n===\n\"// typstfmt::off\\n#{{4}}\\n// typstfmt::on\\n#{{4}}\\n\"\n===\n// typstfmt::off\n#{{4}}\n// typstfmt::on\n#{{4}}\n\n===\nFORMATTED\n===\n// typstfmt::off\n#{{4}}\n// typstfmt::on\n#{{4}}\n" expression: formatted --- -"// typstfmt::off\n#{{4}}\n// typstfmt::on\n#{ { 4 } }\n" +"// typstfmt::off\n#{{4}}\n// typstfmt::on\n#{{4}}\n"