Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Added

- Added support for `not in` and `is not` compound operators.

## [0.7.0] - 2025-11-11

### Added
Expand Down
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Added support for `not in` and `is not` compound operators.

## 0.7.0 - 2025-11-11

### Added
Expand Down
84 changes: 84 additions & 0 deletions core/datatests/generators/optimising_line_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ mod comments {
child_lines::generate(root_dir);
conditional_directives::generate(root_dir);
individual_block::generate(root_dir);
compound_operators::generate(root_dir);
}

mod midline_line {
Expand Down Expand Up @@ -680,6 +681,44 @@ mod comments {
);
}
}

mod compound_operators {
use super::*;

pub fn generate(root_dir: &Path) {
generate_test_cases!(
root_dir,
not_in = "
AA := AAA {} not {} in {} BBB;
AAA :=
AAA {} not {} in {} BBB;
AAA :=
AAAAA {}
not {} in {} BBBB;
AAA :=
AAAAA {}
not {} in {} BBBBBBBBB;
AAA :=
AAAAA
{}
not
{} in
{} BBBBBBBBB;
AAA :=
AAAAA
{
}
not
{
}
in
{
}
BBBBBBBBB;
",
);
}
}
}

mod anonymous {
Expand Down Expand Up @@ -4312,6 +4351,51 @@ mod expressions {
and DDDDDDD;

",
compound = "
A := AAAAAAAA not in BBBBBBBB;
A :=
AAAAAAAA not in BBBBBBBBB;
A :=
AAAAAAAAA
not in BBBBBBBBB;
A :=
AAAAAAAAA
not in BBBBBBBBBBBBBBB;
A :=
AAAAAA + BBBBBB + CCCCCCCC
not in DDDDDDDDDDDDDD;
A :=
AAAAAA + BBBBBB + CCCCCCCC
not in DDDDD + EEEEEE;
A :=
AAAAAAA
+ BBBBBBB
+ CCCCCCC
not in DDDDDDDDDDDDDD;
A :=
AAAAAA + BBBBBB + CCCCCCCC
not in DDDDDD
+ EEEEEE;
A := AAAAAAAA is not BBBBBBBB;
A :=
AAAAAAAA is not BBBBBBBBB;
A :=
AAAAAAAAA
is not BBBBBBBBB;
A :=
AAAAAAAAA
is not BBBBBBBBBBBBBBB;
A := (AAA not in [DDD + EEE]);
A :=
(AAAA not in [DDD + EEE]);
A :=
(AAAAA
not in [DDD + EEE]);
A :=
(AAAAA
not in [
DDDDD + EEEE]);
",
);
}
}
Expand Down
155 changes: 121 additions & 34 deletions core/src/rules/optimising_line_formatter/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,15 @@ impl<'a> SpecificContextStack<'a> {
)
.cloned()
}
fn get_next_real_token_type_from_line_index(&self, line_index: u32) -> Option<TokenType> {
self.formatting_contexts
.line
.get_tokens()
.iter()
.skip(line_index as usize + 1)
.map(|index| self.formatting_contexts.token_types[*index])
.find(|token_type| !token_type.is_comment_or_compiler_directive())
}

/// Updates all contexts to reflect the decision provided.
pub(super) fn update_contexts(&self, node: &mut FormattingNode, decision: RawDecision) {
Expand Down Expand Up @@ -647,8 +656,20 @@ impl<'a> SpecificContextStack<'a> {
_ => {}
}
}
(Some(op1), Some(op2)) if (op1, op2).get_operator_precedence().is_some() => {
// In the middle of a compound operator, do nothing
}
(_, Some(op @ (TT::Op(_) | TT::Keyword(_))))
if self
.get_next_real_token_type_from_line_index(line_index)
.is_some_and(|token_type| {
(op, token_type).get_operator_precedence().is_some()
}) =>
{
self.update_operator_precedences(node, is_break);
}
(prev, Some(op @ (TT::Op(_) | TT::Keyword(_))))
if super::get_operator_precedence(op).is_some() && is_binary(op, prev) =>
if op.get_operator_precedence().is_some() && is_binary(op, prev) =>
{
self.update_operator_precedences(node, is_break);
}
Expand Down Expand Up @@ -775,12 +796,6 @@ impl<'a> LineFormattingContexts<'a> {
token_types: &'a [TokenType],
context_tree: &'a ParentPointerTree<FormattingContext>,
) -> Self {
let get_token_type_from_line_index = |line_index| {
token_types
.get(*line.get_tokens().get(line_index as usize)?)
.cloned()
};

let builder_context_tree = Self::new_tree();
let mut contexts = LineFormattingContextsBuilder::new(&builder_context_tree);

Expand Down Expand Up @@ -822,18 +837,44 @@ impl<'a> LineFormattingContexts<'a> {
}
}

let mut prev_prev_token_type = None;
let mut prev_token_type = None;
let mut prev_semantic_token_type = None;
let mut current = get_token_type_from_line_index(0);
let mut next_token_type = get_token_type_from_line_index(1);
let mut prev_token_types: Vec<TokenType> = Vec::with_capacity(line.get_tokens().len());
macro_rules! last_semantic_token_type {
() => {
last_semantic_token_type!(0)
};
($i: expr) => {
prev_token_types
.iter()
.rev()
.filter(|tt| !tt.is_comment_or_directive())
.nth($i)
};
}
let mut next_token_types = line
.get_tokens()
.iter()
.rev()
.map(|id| token_types[*id])
.collect::<Vec<_>>();
let mut current = next_token_types.pop();

fn next_real_token_type(token_types: &[TokenType]) -> Option<TokenType> {
token_types
.iter()
.rev()
.find(|token_type| !token_type.is_comment_or_compiler_directive())
.cloned()
}

while let Some(current_token_type) = current {
if !current_token_type.is_comment_or_compiler_directive() {
let last_context_type = contexts.current_context.get().context_type;
// New contexts relating to the previous token are pushed here
// to avoid including any leading comments
if let (Some(prev_token_type), Some(prev_directive_token_type)) =
(prev_token_type, prev_semantic_token_type)
if let Some(prev_token_type) = prev_token_types
.iter()
.rev()
.find(|tt| !tt.is_comment_or_compiler_directive())
{
match (prev_token_type, last_context_type) {
(TT::Op(OK::LParen | OK::LBrack | OK::LessThan(ChK::Generic)), _)
Expand All @@ -858,7 +899,7 @@ impl<'a> LineFormattingContexts<'a> {
}
_ => {}
}
match prev_directive_token_type {
match prev_token_type {
TT::Keyword(KK::Of) => {
contexts.push(CT::Subject);
contexts.push_expression();
Expand Down Expand Up @@ -908,7 +949,10 @@ impl<'a> LineFormattingContexts<'a> {
contexts.push_expression();
}
TT::Keyword(KK::Abstract)
if matches!(prev_prev_token_type, Some(TT::Keyword(KK::Class))) => {}
if matches!(
last_semantic_token_type!(1),
Some(TT::Keyword(KK::Class))
) => {}
TT::Keyword(kk) if kk.is_directive() => {
contexts.push_expression();
}
Expand All @@ -933,8 +977,24 @@ impl<'a> LineFormattingContexts<'a> {
TT::ConditionalDirective(kind) if kind.is_else() => {
contexts.push_operators();
}
op if super::get_operator_precedence(op).is_some()
&& is_binary(op, prev_prev_token_type) =>
op if (*op, current_token_type)
.get_operator_precedence()
.is_some() =>
{
// In the middle of a compound operator, do nothing
}
op if prev_token_types
.iter()
.rev()
.nth(1)
.cloned()
.and_then(|prev| (prev, *op).get_operator_precedence())
.is_some() =>
{
contexts.push_operators();
}
op if op.get_operator_precedence().is_some()
&& is_binary(*op, last_semantic_token_type!(1).cloned()) =>
{
contexts.push_operators();
}
Expand All @@ -952,7 +1012,7 @@ impl<'a> LineFormattingContexts<'a> {
TT::Op(OK::LessThan(ChevronKind::Generic)) => BracketKind::Angle,
_ => BracketKind::Round,
};
let (typ, cont_delta) = match prev_token_type {
let (typ, cont_delta) = match last_semantic_token_type!() {
// routine invocations
Some(TT::Identifier | TT::Op(OK::GreaterThan(ChevronKind::Generic))) => {
(BracketStyle::BreakClose, 1)
Expand Down Expand Up @@ -1094,7 +1154,10 @@ impl<'a> LineFormattingContexts<'a> {
}
TT::Op(OK::Dot) if CT::Precedence(0) == last_context_type => {
contexts.retain_current();
if matches!(prev_token_type, Some(TT::Op(OK::RParen | OK::RBrack))) {
if matches!(
last_semantic_token_type!(),
Some(TT::Op(OK::RParen | OK::RBrack))
) {
/*
Fluency is considered after () and [] because
they allow for arbitrary computation which will
Expand All @@ -1108,13 +1171,33 @@ impl<'a> LineFormattingContexts<'a> {
contexts.fluent(contexts.current_context.clone());
}
}
op if super::get_operator_precedence(op).is_some()
&& is_binary(op, prev_token_type) =>

op if prev_token_types
.last()
.cloned()
.and_then(|prev| (prev, op).get_operator_precedence())
.is_some() =>
{
// We are in the middle of a compound operator, do nothing
}
op if next_real_token_type(&next_token_types)
.and_then(|next| (op, next).get_operator_precedence())
.is_some() =>
{
let op_prec = super::get_operator_precedence(op).unwrap();
let op_prec = next_real_token_type(&next_token_types)
.and_then(|next| (op, next).get_operator_precedence())
.unwrap();
contexts.pop_until_and_retain(CT::Precedence(op_prec));
}
TT::Keyword(KK::Of) if matches!(next_token_type, Some(TT::Keyword(KK::Object))) => {
op if op.get_operator_precedence().is_some()
&& is_binary(op, last_semantic_token_type!().cloned()) =>
{
let op_prec = op.get_operator_precedence().unwrap();
contexts.pop_until_and_retain(CT::Precedence(op_prec));
}
TT::Keyword(KK::Of)
if matches!(next_token_types.last(), Some(TT::Keyword(KK::Object))) =>
{
contexts.pop_until_after(CT::AnonHeader);
}
TT::Keyword(KK::Then | KK::Do | KK::Of) => {
Expand All @@ -1135,7 +1218,7 @@ impl<'a> LineFormattingContexts<'a> {
contexts.pop_until_after(CT::AnonHeader);
}
TT::Keyword(KK::Abstract)
if matches!(prev_token_type, Some(TT::Keyword(KK::Class))) => {}
if matches!(last_semantic_token_type!(), Some(TT::Keyword(KK::Class))) => {}
TT::Keyword(kk) if kk.is_directive() => {
if contexts.pop_until(CT::DirectiveList) != Some(CT::DirectiveList) {
if contexts
Expand Down Expand Up @@ -1180,15 +1263,8 @@ impl<'a> LineFormattingContexts<'a> {
_ => {}
}

if !current_token_type.is_comment_or_directive() {
prev_prev_token_type = prev_token_type;
prev_token_type = current;
}
if !current_token_type.is_comment_or_compiler_directive() {
prev_semantic_token_type = current;
}
current = next_token_type;
next_token_type = get_token_type_from_line_index(contexts.line_index + 1);
prev_token_types.extend(current);
current = next_token_types.pop();
}

contexts.finalise();
Expand Down Expand Up @@ -2160,6 +2236,17 @@ mod tests {
1 Precedence(3) ^-----------
1 Precedence(2) ^-----$
"},
not_in_operator = {"
AA + BB not in CC
1 Base ^----------------
1 Precedence(4) ^----------------
1 Precedence(3) ^-----$
"},
is_not_operator = {"
AA is not BB
1 Base ^-----------
1 Precedence(4) ^-----------
"},
routine_arguments = {"
AA(BB, CC) + DD
1 Base ^--------------
Expand Down
Loading
Loading