use crate::roles::{BraceKind, Roles};
use crate::tokens::Stream;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Definition {
pub start: usize,
pub open: usize,
pub close: usize,
pub single_statement: bool,
pub inside_a_record: bool,
pub nested_block: bool,
}
pub fn definitions(stream: &Stream<'_>, roles: &Roles) -> Vec<Definition> {
use cpp_parser::CppTokenKind as K;
let mut definitions = Vec::new();
let mut open: Vec<Option<(usize, BraceKind)>> = Vec::new();
for index in 0..stream.tokens.len() {
match stream.tokens[index].kind {
K::LeftBrace => {
let kind = roles.brace_kind.get(&stream.tokens[index].start).copied();
open.push(kind.map(|kind| (index, kind)));
}
K::RightBrace => {
let Some(Some((open_index, kind))) = open.pop() else {
continue;
};
if !matches!(kind, BraceKind::Function | BraceKind::Lambda) {
continue;
}
definitions.push(Definition {
start: definition_start(stream, open_index),
open: open_index,
close: index,
single_statement: single_statement(stream, open_index, index),
nested_block: has_a_nested_block(stream, open_index, index),
inside_a_record: open.iter().flatten().any(|(_, kind)| {
matches!(
kind,
BraceKind::Class | BraceKind::Struct | BraceKind::Union
)
}),
});
}
_ => {}
}
}
definitions
}
fn definition_start(stream: &Stream<'_>, open: usize) -> usize {
use cpp_parser::CppTokenKind as K;
let mut index = open;
let mut depth = 0usize;
while index > 0 {
let previous = index - 1;
if stream.gaps[index].newlines > 1 {
break;
}
match stream.tokens[previous].kind {
K::RightParen | K::RightBracket => depth += 1,
K::LeftParen | K::LeftBracket => depth = depth.saturating_sub(1),
K::Semicolon | K::LeftBrace | K::RightBrace if depth == 0 => break,
K::Colon
if depth == 0
&& previous > 0
&& matches!(
stream.tokens[previous - 1].kind,
K::PublicKeyword | K::PrivateKeyword | K::ProtectedKeyword
) =>
{
break;
}
K::Hash if depth == 0 => break,
_ => {}
}
index = previous;
}
index
}
fn has_a_nested_block(stream: &Stream<'_>, open: usize, close: usize) -> bool {
use cpp_parser::CppTokenKind as K;
stream.tokens[open + 1..close]
.iter()
.any(|token| token.kind == K::LeftBrace)
}
fn single_statement(stream: &Stream<'_>, open: usize, close: usize) -> bool {
use cpp_parser::CppTokenKind as K;
let mut depth = 0usize;
let mut statements = 0usize;
let mut tokens_at_the_top = 0usize;
let mut index = open + 1;
while index < close {
let token = &stream.tokens[index];
match token.kind {
K::LeftParen | K::LeftBracket | K::LeftBrace => {
if depth == 0 {
tokens_at_the_top += 1;
if token.kind == K::LeftBrace {
statements += 1;
}
}
depth += 1;
}
K::RightParen | K::RightBracket | K::RightBrace => depth = depth.saturating_sub(1),
K::Semicolon if depth == 0 => statements += 1,
K::Colon
if depth == 0
&& stream
.gaps
.get(index + 1)
.is_some_and(|gap| gap.newlines > 0) =>
{
return false;
}
_ => {
if depth == 0 {
tokens_at_the_top += 1;
}
}
}
if statements > 1 {
return false;
}
index += 1;
}
statements == 1 || (statements == 0 && tokens_at_the_top == 0)
}
pub fn body_width(
stream: &Stream<'_>,
roles: &Roles,
style: &crate::config::Style,
definition: &Definition,
) -> usize {
let mut width = 0;
for index in definition.open..=definition.close {
if index > definition.open {
width += crate::space::between(stream, index, roles, style);
}
width += stream.tokens[index].text.chars().count();
}
width
}
pub fn one_line_width(
stream: &Stream<'_>,
roles: &Roles,
style: &crate::config::Style,
definition: &Definition,
indent: usize,
) -> usize {
let mut width = indent;
for index in definition.start..=definition.close {
if index > definition.start {
width += crate::space::between(stream, index, roles, style);
}
width += stream.tokens[index].text.chars().count();
}
width
}
#[cfg(test)]
mod tests {
use super::*;
fn definitions_of(source: &str) -> Vec<Definition> {
let stream = crate::tokens::stream(source);
let roles = crate::roles::of(source);
definitions(&stream, &roles)
}
#[test]
fn a_definition_spans_from_the_return_type_to_the_closing_brace() {
let source = "void f() {\n g();\n}\n";
let found = definitions_of(source);
assert_eq!(found.len(), 1);
assert_eq!(found[0].start, 0, "the definition starts at `void`");
assert!(found[0].single_statement);
}
#[test]
fn two_statements_are_not_one() {
let source = "void f() {\n g();\n h();\n}\n";
assert!(!definitions_of(source)[0].single_statement);
}
#[test]
fn a_nested_block_is_one_statement_and_a_second_one_is_not() {
assert!(definitions_of("void f() {\n if (x) {\n g();\n }\n}\n")[0].single_statement);
assert!(
!definitions_of("void f() {\n while (x) {\n g();\n }\n h();\n}\n")[0]
.single_statement
);
}
#[test]
fn a_class_method_is_inside_a_record() {
let source = "class C {\n void m() {\n g();\n }\n};\n";
let found = definitions_of(source);
assert_eq!(found.len(), 1, "{found:?}");
assert!(found[0].inside_a_record);
assert!(found[0].single_statement);
}
#[test]
fn an_out_of_line_constructor_starts_at_its_qualified_name() {
let source = "A::A(int x) : x_(x) {\n init();\n}\n";
let found = definitions_of(source);
assert_eq!(found.len(), 1);
assert_eq!(found[0].start, 0);
assert!(found[0].single_statement);
}
}