use std::ops::Range;
use cpp_parser::CppTokenKind;
use crate::tokens::Stream;
pub fn logical_lines(stream: &Stream<'_>) -> Vec<Range<usize>> {
let mut lines = Vec::new();
let mut start = 0usize;
let mut parens = 0isize;
let mut in_directive = false;
for index in 0..stream.tokens.len() {
let kind = stream.tokens[index].kind;
if in_directive {
if stream.gaps[index].newlines > 0 {
in_directive = false;
lines.push(start..index);
start = index;
} else {
continue;
}
}
if kind == CppTokenKind::Hash && (index == 0 || stream.gaps[index].newlines > 0) {
in_directive = true;
continue;
}
if kind == CppTokenKind::Identifier
&& is_a_macro_name(stream.tokens[index].text)
&& stream
.gaps
.get(index + 1)
.is_some_and(|gap| gap.newlines > 0)
&& index + 1 > start
{
lines.push(start..index + 1);
start = index + 1;
continue;
}
match kind {
CppTokenKind::LeftParen | CppTokenKind::LeftBracket => parens += 1,
CppTokenKind::RightParen | CppTokenKind::RightBracket => parens -= 1,
_ => {}
}
if matches!(kind, CppTokenKind::LeftBrace | CppTokenKind::RightBrace) && parens == 0 {
if start < index {
lines.push(start..index);
}
lines.push(index..index + 1);
start = index + 1;
continue;
}
let ends = match kind {
CppTokenKind::Semicolon if parens == 0 => true,
CppTokenKind::Colon if parens == 0 => is_a_label(stream, index),
CppTokenKind::RightParen if parens == 0 => starts_a_control_statement(stream, index),
_ => false,
};
if ends && index + 1 > start {
lines.push(start..index + 1);
start = index + 1;
}
}
if start < stream.tokens.len() {
lines.push(start..stream.tokens.len());
}
lines
}
fn is_a_macro_name(text: &str) -> bool {
text.chars().count() >= 2
&& text.chars().all(|character| {
character.is_ascii_uppercase() || character.is_ascii_digit() || character == '_'
})
}
fn is_a_label(stream: &Stream<'_>, index: usize) -> bool {
use CppTokenKind::*;
let mut cursor = index;
while cursor > 0 {
cursor -= 1;
match stream.tokens[cursor].kind {
CaseKeyword | DefaultKeyword | PublicKeyword | PrivateKeyword | ProtectedKeyword => {
return true;
}
Semicolon | LeftBrace | RightBrace | Question => return false,
_ => {}
}
}
false
}
pub(crate) fn starts_a_control_statement(stream: &Stream<'_>, index: usize) -> bool {
use CppTokenKind::*;
let mut depth = 0isize;
let mut cursor = index;
let mut open: Option<usize> = Option::None;
loop {
match stream.tokens[cursor].kind {
RightParen => depth += 1,
LeftParen => {
depth -= 1;
if depth == 0 {
open = Some(cursor);
break;
}
}
_ => {}
}
if cursor == 0 {
break;
}
cursor -= 1;
}
let Some(open) = open else {
return false;
};
if open == 0 {
return false;
}
if stream.tokens[open - 1].kind == WhileKeyword
&& open >= 2
&& stream.tokens[open - 2].kind == RightBrace
{
return false;
}
matches!(
stream.tokens[open - 1].kind,
IfKeyword | ForKeyword | WhileKeyword | SwitchKeyword | CatchKeyword
) && stream
.gaps
.get(index + 1)
.is_some_and(|gap| gap.newlines > 0 || gap.at_end)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::stream;
fn texts(source: &str) -> Vec<String> {
let stream = stream(source);
logical_lines(&stream)
.into_iter()
.map(|line| {
stream.tokens[line]
.iter()
.map(|token| token.text)
.collect::<Vec<_>>()
.join(" ")
})
.collect()
}
#[test]
fn a_statement_ends_at_its_semicolon() {
assert_eq!(
texts("int a = 1;\nint b = 2;\n"),
vec!["int a = 1 ;", "int b = 2 ;"]
);
}
#[test]
fn a_block_brace_ends_its_own_line() {
assert_eq!(
texts("void f()\n{\nreturn;\n}\n"),
vec!["void f ( )", "{", "return ;", "}"]
);
}
#[test]
fn a_case_label_ends_at_its_colon() {
assert_eq!(
texts("switch (x) {\ncase 1:\nbreak;\n}\n"),
vec!["switch ( x )", "{", "case 1 :", "break ;", "}"]
);
}
#[test]
fn a_control_head_ends_at_its_parenthesis_when_the_body_is_on_the_next_line() {
assert_eq!(texts("if (x)\nreturn;\n"), vec!["if ( x )", "return ;"]);
}
#[test]
fn every_token_belongs_to_exactly_one_line() {
let source = "void f()\n{\nif (x) {\nreturn;\n}\n}\n";
let stream = stream(source);
let lines = logical_lines(&stream);
let covered: usize = lines.iter().map(|line| line.len()).sum();
assert_eq!(covered, stream.tokens.len(), "{lines:?}");
}
}
#[derive(Debug, Clone, Copy)]
pub struct Penalties {
pub excess_character: i64,
pub break_before_first_parameter: i64,
pub break_assignment: i64,
pub plain: i64,
}
impl Penalties {
pub fn of(style: &crate::config::Style) -> Penalties {
use crate::options::StyleOption;
Penalties {
excess_character: style.number(StyleOption::PenaltyExcessCharacter).max(1),
break_before_first_parameter: style
.number(StyleOption::PenaltyBreakBeforeFirstCallParameter),
break_assignment: style.number(StyleOption::PenaltyBreakAssignment),
plain: 1,
}
}
}
pub fn break_line(
stream: &Stream<'_>,
roles: &crate::roles::Roles,
style: &crate::config::Style,
line: &Range<usize>,
start_column: usize,
extra_width: usize,
) -> Option<Vec<usize>> {
let limit = style.count(crate::options::StyleOption::ColumnLimit);
if limit == 0 {
return None;
}
let penalties = Penalties::of(style);
let continuation = style.count(crate::options::StyleOption::ContinuationIndentWidth);
let mut positions: Vec<usize> = vec![line.start];
for index in line.start + 1..line.end {
if is_breakable(stream, roles, style, index) {
positions.push(index);
}
}
positions.push(line.end);
if positions.len() <= 2 {
return None;
}
let mut enclosing: Vec<Option<usize>> = vec![None; stream.tokens.len()];
let mut joined_column: Vec<usize> = vec![0; stream.tokens.len()];
{
let mut open: Vec<usize> = Vec::new();
let mut column = start_column;
for index in line.start..line.end {
if index > line.start {
column += crate::space::between(stream, index, roles, style);
}
joined_column[index] = column;
enclosing[index] = open.last().copied();
match stream.tokens[index].kind {
CppTokenKind::LeftParen | CppTokenKind::LeftBracket | CppTokenKind::LeftBrace => {
open.push(index)
}
CppTokenKind::RightParen
| CppTokenKind::RightBracket
| CppTokenKind::RightBrace => {
open.pop();
}
_ => {}
}
column += stream.tokens[index].text.chars().count();
}
}
let aligns = matches!(
style.text(crate::options::StyleOption::AlignAfterOpenBracket),
"Align" | "AlwaysBreak"
) || style.flag(crate::options::StyleOption::AlignAfterOpenBracket);
let indent_at = |position: usize, first: bool| -> usize {
match enclosing[position] {
Some(open) if aligns && open + 1 < position => joined_column[open + 1],
Some(_) => start_column + continuation,
None if first => start_column,
None => start_column + continuation,
}
};
let width = |a: usize, b: usize, indent: usize| -> usize {
let mut column = indent;
for index in a..b {
if index > a {
column += crate::space::between(stream, index, roles, style);
}
column += stream.tokens[index].text.chars().count();
}
column
};
let count = positions.len();
let mut best = vec![i64::MAX; count];
let mut from = vec![0usize; count];
best[0] = 0;
for end in 1..count {
for start in 0..end {
if best[start] == i64::MAX {
continue;
}
let indent = indent_at(positions[start], start == 0);
let piece = width(positions[start], positions[end], indent)
+ if positions[end] == line.end {
extra_width
} else {
0
};
let excess = piece.saturating_sub(limit) as i64 * penalties.excess_character;
let break_cost = if start == 0 {
0
} else {
break_at(stream, positions[start], &penalties)
};
let cost = best[start] + excess + break_cost;
if cost < best[end] {
best[end] = cost;
from[end] = start;
}
}
}
let mut chosen = Vec::new();
let mut cursor = count - 1;
while cursor > 0 {
let previous = from[cursor];
if previous > 0 {
chosen.push(positions[previous]);
}
cursor = previous;
}
chosen.reverse();
if chosen.is_empty() {
None
} else {
Some(chosen)
}
}
pub fn laid_out(source: &str, style: &crate::config::Style) -> String {
let stream = crate::tokens::stream(source);
let lines = logical_lines(&stream);
let mut starts: std::collections::HashSet<usize> =
lines.iter().skip(1).map(|line| line.start).collect();
let ending = crate::tokens::line_ending(source);
let mut out = String::with_capacity(source.len() + starts.len() * ending.len());
let mut in_directive = false;
let attaches = style.text(crate::options::StyleOption::BreakBeforeBraces) == "Attach";
for index in 0..stream.tokens.len() {
let gap = &stream.gaps[index];
let text = source.get(gap.start..gap.end).unwrap_or("");
if stream.tokens[index].text == "#" && (index == 0 || gap.newlines > 0) {
in_directive = true;
}
let current = &stream.tokens[index];
let previous = stream.tokens.get(index.wrapping_sub(1));
let attaches_to_the_line_before = attaches
&& (current.kind == cpp_parser::CppTokenKind::LeftBrace
|| (previous.is_some_and(|previous| {
previous.kind == cpp_parser::CppTokenKind::RightBrace
}) && matches!(current.text, "else" | "catch" | "while")));
let needs_a_break = index > 0
&& !in_directive
&& !attaches_to_the_line_before
&& starts.remove(&index)
&& gap.newlines == 0
&& gap.comments.is_empty()
&& !gap.at_end;
if needs_a_break {
out.push_str(ending);
} else {
out.push_str(text);
}
out.push_str(stream.tokens[index].text);
if in_directive
&& stream
.gaps
.get(index + 1)
.is_some_and(|next| next.newlines > 0)
{
in_directive = false;
}
}
if let Some(trailing) = stream.gaps.last() {
out.push_str(source.get(trailing.start..trailing.end).unwrap_or(""));
}
out
}
pub fn is_breakable(
stream: &Stream<'_>,
roles: &crate::roles::Roles,
style: &crate::config::Style,
index: usize,
) -> bool {
use CppTokenKind::*;
let previous = stream.tokens[index - 1].kind;
let current = stream.tokens[index].kind;
let start = stream.tokens[index].start;
let previous_start = stream.tokens[index - 1].start;
if previous == Comma {
return true;
}
let break_before = match style.text(crate::options::StyleOption::BreakBeforeBinaryOperators) {
"All" => true,
"NonAssignment" => crate::layout::is_a_binary_operator(current) && current == Assign,
_ => false,
};
if crate::layout::is_a_binary_operator(current) && break_before {
return !roles.is_declarator(start) && !roles.is_template_angle(start);
}
if crate::layout::is_a_binary_operator(previous)
&& !break_before
&& !roles.is_declarator(previous_start)
&& !roles.is_template_angle(previous_start)
{
return true;
}
if roles.is_declarator(start) || roles.is_template_angle(start) {
return false;
}
match current {
Dot | Arrow => true,
_ if matches!(previous, LeftParen | LeftBracket) => true,
Question => true,
_ => false,
}
}
pub fn break_over_long_pieces(
stream: &Stream<'_>,
roles: &crate::roles::Roles,
style: &crate::config::Style,
line: &Range<usize>,
blocks: &[Option<usize>],
) -> Vec<usize> {
let limit = style.count(crate::options::StyleOption::ColumnLimit);
let mut breaks = Vec::new();
let mut start = line.start;
for index in line.start..line.end {
let ends_a_line = index + 1 == line.end || stream.gaps[index + 1].newlines > 0;
if !ends_a_line {
continue;
}
let column = blocks.get(start).copied().flatten().unwrap_or(0);
let mut width = column;
for inside in start..=index {
if inside > start {
width += crate::space::between(stream, inside, roles, style);
}
width += stream.tokens[inside].text.chars().count();
}
if width > limit
&& let Some(found) = break_line(stream, roles, style, &(start..index + 1), column, 0)
{
breaks.extend(found);
}
start = index + 1;
}
breaks
}
fn break_at(stream: &Stream<'_>, index: usize, penalties: &Penalties) -> i64 {
use CppTokenKind::*;
match stream.tokens[index].kind {
Assign => penalties.break_assignment,
_ if stream.tokens[index - 1].kind == LeftParen => penalties.break_before_first_parameter,
_ => penalties.plain,
}
}
#[cfg(test)]
mod break_tests {
use super::*;
use crate::config::Style;
fn broken(source: &str, limit: &str) -> Option<Vec<String>> {
let mut style = Style::default_style();
style.set_from_text("ColumnLimit", limit);
broken_with(style, source)
}
fn broken_with(style: Style, source: &str) -> Option<Vec<String>> {
let stream = crate::tokens::stream(source);
let roles = crate::roles::of(source);
let lines = logical_lines(&stream);
let line = lines.first().expect("a line").clone();
let breaks = break_line(&stream, &roles, &style, &line, 0, 0)?;
let mut pieces = Vec::new();
let mut start = line.start;
for at in breaks.iter().copied().chain(std::iter::once(line.end)) {
let mut text = String::new();
for (offset, token) in stream.tokens[start..at].iter().enumerate() {
if offset > 0 {
text.push(' ');
}
text.push_str(token.text);
}
pieces.push(text);
start = at;
}
Some(pieces)
}
#[test]
fn a_line_that_fits_is_not_broken() {
assert_eq!(broken("int a = 1;", "80"), None);
}
#[test]
fn a_long_argument_list_breaks_after_the_commas() {
let pieces = broken("f(aaaaaaaaaa, bbbbbbbbbb, cccccccccc);", "20").expect("broken");
assert!(pieces.len() > 1, "{pieces:?}");
for piece in &pieces {
assert!(piece.chars().count() <= 20, "{piece:?} in {pieces:?}");
}
}
#[test]
fn a_long_sum_breaks_after_the_operator_under_none() {
let pieces = broken("int x = aaaaaaaaaa + bbbbbbbbbb + cccccccccc;", "25").expect("broken");
assert!(
pieces.iter().any(|piece| piece.trim_end().ends_with('+')),
"{pieces:?}"
);
assert!(
pieces
.iter()
.skip(1)
.all(|piece| !piece.trim_start().starts_with('+')),
"{pieces:?}"
);
}
#[test]
fn a_long_sum_breaks_before_the_operator_under_all() {
let mut style = Style::default_style();
style.set_from_text("BreakBeforeBinaryOperators", "All");
style.set_from_text("ColumnLimit", "25");
let pieces =
broken_with(style, "int x = aaaaaaaaaa + bbbbbbbbbb + cccccccccc;").expect("broken");
assert!(
pieces
.iter()
.skip(1)
.any(|piece| piece.trim_start().starts_with('+')),
"{pieces:?}"
);
}
#[test]
fn the_search_is_deterministic() {
let first = broken("f(aaaaaaaaaa, bbbbbbbbbb, cccccccccc);", "20");
let second = broken("f(aaaaaaaaaa, bbbbbbbbbb, cccccccccc);", "20");
assert_eq!(first, second);
}
}