use crate::formatting::actions;
use crate::formatting::condition_helpers;
use super::super::Arena;
use super::super::conditions;
use super::super::print_items::*;
pub fn surround_with_new_lines(item: PrintItems<'_>) -> PrintItems<'_> {
if item.is_empty() {
return item;
}
let mut items = PrintItems::new(item.arena());
items.push_signal(Signal::NewLine);
items.extend(item);
items.push_signal(Signal::NewLine);
items
}
pub fn with_indent(item: PrintItems<'_>) -> PrintItems<'_> {
with_indent_times(item, 1)
}
pub fn with_queued_indent(item: PrintItems<'_>) -> PrintItems<'_> {
if item.is_empty() {
return item;
}
let mut items = PrintItems::new(item.arena());
items.push_signal(Signal::QueueStartIndent);
items.extend(item);
items.push_signal(Signal::FinishIndent);
items
}
pub fn with_indent_times(item: PrintItems<'_>, times: u32) -> PrintItems<'_> {
if item.is_empty() {
return item;
}
let mut items = PrintItems::new(item.arena());
for _ in 0..times {
items.push_signal(Signal::StartIndent);
}
items.extend(item);
for _ in 0..times {
items.push_signal(Signal::FinishIndent);
}
items
}
pub fn with_no_new_lines(item: PrintItems<'_>) -> PrintItems<'_> {
if item.is_empty() {
return item;
}
let mut items = PrintItems::new(item.arena());
items.push_signal(Signal::StartForceNoNewLines);
items.extend(item);
items.push_signal(Signal::FinishForceNoNewLines);
items
}
pub fn new_line_group(item: PrintItems<'_>) -> PrintItems<'_> {
if item.is_empty() {
return item;
}
let mut items = PrintItems::new(item.arena());
items.push_signal(Signal::StartNewLineGroup);
items.extend(item);
items.push_signal(Signal::FinishNewLineGroup);
items
}
pub fn gen_from_raw_string<'a>(arena: &'a Arena, text: &'a str) -> PrintItems<'a> {
gen_from_raw_string_lines(arena, text, gen_from_string_line)
}
pub fn gen_from_raw_string_trim_line_ends<'a>(arena: &'a Arena, text: &'a str) -> PrintItems<'a> {
gen_from_raw_string_lines(arena, text, |arena, line_text| gen_from_string_line(arena, line_text.trim_end()))
}
fn gen_from_raw_string_lines<'a>(arena: &'a Arena, text: &'a str, gen_line: impl Fn(&'a Arena, &'a str) -> PrintItems<'a>) -> PrintItems<'a> {
let has_newline = text.contains('\n');
let mut items = PrintItems::new(arena);
if has_newline {
items.push_signal(Signal::StartIgnoringIndent);
items.extend(gen_string_lines(arena, text, gen_line));
items.push_signal(Signal::FinishIgnoringIndent);
} else {
items.extend(gen_line(arena, text));
}
items
}
pub fn gen_from_string<'a>(arena: &'a Arena, text: &'a str) -> PrintItems<'a> {
gen_string_lines(arena, text, gen_from_string_line)
}
pub fn gen_from_string_trim_line_ends<'a>(arena: &'a Arena, text: &'a str) -> PrintItems<'a> {
gen_string_lines(arena, text, |arena, line_text| gen_from_string_line(arena, line_text.trim_end()))
}
fn gen_string_lines<'a>(arena: &'a Arena, text: &'a str, gen_line: impl Fn(&'a Arena, &'a str) -> PrintItems<'a>) -> PrintItems<'a> {
let mut items = PrintItems::new(arena);
for (i, line) in text.lines().enumerate() {
if i > 0 {
items.push_signal(Signal::NewLine);
}
items.extend(gen_line(arena, line));
}
if text.ends_with('\n') {
items.push_signal(Signal::NewLine)
}
items
}
fn gen_from_string_line<'a>(arena: &'a Arena, line: &'a str) -> PrintItems<'a> {
let mut items = PrintItems::new(arena);
push_string_line(&mut items, line);
items
}
fn push_string_line<'a>(items: &mut PrintItems<'a>, line: &'a str) {
for (i, part) in line.split('\t').enumerate() {
if i > 0 {
items.push_signal(Signal::Tab);
}
if !part.is_empty() {
items.push_str_runtime_width_computed(part);
}
}
}
pub fn surround_with_newlines_indented_if_multi_line(inner_items: PrintItems<'_>, indent_width: u8) -> PrintItems<'_> {
if inner_items.is_empty() {
return inner_items;
}
let arena = inner_items.arena();
let mut items = PrintItems::new(arena);
let start_name = "surroundWithNewLinesIndentedIfMultiLineStart";
let start_ln = LineNumber::new(start_name);
let end_ln = LineNumber::new("surroundWithNewLinesIndentedIfMultiLineEnd");
let inner_items = inner_items.into_rc_path();
items.push_info(start_ln);
items.push_anchor(LineNumberAnchor::new(end_ln));
items.extend(actions::if_column_number_changes(arena, move |context| {
context.clear_info(end_ln);
}));
let mut condition = Condition::new(
"newlineIfMultiLine",
ConditionProperties {
true_path: Some(surround_with_new_lines(with_indent(inner_items.into_print_items(arena)))),
false_path: Some({
let mut items = PrintItems::new(arena);
items.push_condition(conditions::if_above_width(indent_width, Signal::PossibleNewLine.into_print_items(arena)));
items.extend(inner_items.into_print_items(arena));
items
}),
condition: arena.alloc_resolver(move |context| condition_helpers::is_multiple_lines(context, start_ln, end_ln)),
},
);
let condition_reevaluation = condition.create_reevaluation();
items.push_condition(condition);
items.push_info(end_ln);
items.push_reevaluation(condition_reevaluation);
items
}
pub fn gen_js_like_comment_line<'a>(arena: &'a Arena, text: &'a str, force_space_after_slashes: bool) -> PrintItems<'a> {
const DOUBLE_SLASH: StringContainer = StringContainer::proc_macro_new_with_char_count("//", 2);
let non_slash_index = text.find(|c| c != '/').unwrap_or(text.len());
let slashes = &text[..non_slash_index];
let skip_space = force_space_after_slashes && text[non_slash_index..].starts_with(' ');
let comment_text = text[if skip_space { non_slash_index + 1 } else { non_slash_index }..].trim_end();
let mut items = PrintItems::new(arena);
items.push_sc(&DOUBLE_SLASH);
if !slashes.is_empty() {
items.push_str_runtime_width_computed(slashes);
}
if !comment_text.is_empty() {
if force_space_after_slashes {
items.push_space();
}
push_string_line(&mut items, comment_text);
}
items.push_signal(Signal::ExpectNewLine);
with_no_new_lines(items)
}
pub fn gen_js_like_comment_block<'a>(arena: &'a Arena, text: &'a str) -> PrintItems<'a> {
const LEADING_STAR_SLASH: StringContainer = StringContainer::proc_macro_new_with_char_count("/*", 2);
const TRAILING_STAR_SLASH: StringContainer = StringContainer::proc_macro_new_with_char_count("*/", 2);
let mut items = PrintItems::new(arena);
let add_ignore_indent = text.contains('\n');
let last_line_trailing_whitespace = get_last_line_trailing_whitespace(text);
items.push_sc(&LEADING_STAR_SLASH);
if add_ignore_indent {
items.push_signal(Signal::StartIgnoringIndent);
}
items.extend(gen_from_string_trim_line_ends(arena, text));
if !last_line_trailing_whitespace.is_empty() {
items.extend(gen_from_raw_string(arena, last_line_trailing_whitespace));
}
if add_ignore_indent {
items.push_signal(Signal::FinishIgnoringIndent);
}
items.push_sc(&TRAILING_STAR_SLASH);
return items;
fn get_last_line_trailing_whitespace(text: &str) -> &str {
let end_text = &text[text.trim_end().len()..];
if let Some(last_index) = end_text.rfind('\n') {
&end_text[last_index + 1..]
} else {
end_text
}
}
}
pub fn text_has_dprint_ignore(text: &str, searching_text: &str) -> bool {
let pos = text.find(searching_text);
if let Some(pos) = pos {
let end = pos + searching_text.len();
if pos > 0 && is_alpha_numeric_at_pos(text, pos - 1) {
return false;
}
if is_alpha_numeric_at_pos(text, end) {
return false;
}
return true;
} else {
return false;
}
fn is_alpha_numeric_at_pos(text: &str, pos: usize) -> bool {
if let Some(chars_after) = text.get(pos..)
&& let Some(char_after) = chars_after.chars().next()
{
return char_after.is_alphanumeric();
}
false
}
}