use std::{fmt::Write, ops::Range};
use sway_types::Spanned;
use crate::{formatter::FormattedCode, Formatter, FormatterError};
pub fn has_comments<I: Iterator>(comments: I) -> bool {
comments.peekable().peek().is_some()
}
pub fn maybe_write_comments_from_map(
formatted_code: &mut FormattedCode,
range: Range<usize>,
formatter: &mut Formatter,
) -> Result<bool, FormatterError> {
{
let mut comments_iter = formatter.comment_map.comments_between(&range).peekable();
if comments_iter.peek().is_none() {
return Ok(false);
};
if formatted_code.ends_with(&['{', '}']) && !formatted_code.ends_with('\n') {
writeln!(formatted_code)?;
}
for comment in comments_iter {
writeln!(
formatted_code,
"{}{}",
formatter.shape.indent.to_string(&formatter.config)?,
comment.span().as_str(),
)?;
}
}
formatter
.comment_map
.retain(|bs, _| !bs.contained_within(&range));
Ok(true)
}