Skip to content
Merged
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
18 changes: 10 additions & 8 deletions crates/oxc_formatter/src/utils/member_chain/groups.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;

use oxc_span::GetSpan;

Expand All @@ -8,7 +9,7 @@ use crate::formatter::{Format, Formatter, prelude::*};
#[derive(Default)]
pub(super) struct MemberChainGroupsBuilder<'a, 'b> {
/// keeps track of the groups created
groups: Vec<MemberChainGroup<'a, 'b>>,
groups: VecDeque<MemberChainGroup<'a, 'b>>,
/// keeps track of the current group that is being created/updated
current_group: Option<MemberChainGroup<'a, 'b>>,
}
Expand All @@ -33,15 +34,15 @@ impl<'a, 'b> MemberChainGroupsBuilder<'a, 'b> {
/// clears the current group, and adds it to the groups collection
pub fn close_group(&mut self) {
if let Some(group) = self.current_group.take() {
self.groups.push(group);
self.groups.push_back(group);
}
}

pub(super) fn finish(self) -> TailChainGroups<'a, 'b> {
let mut groups = self.groups;

if let Some(group) = self.current_group {
groups.push(group);
groups.push_back(group);
}

TailChainGroups { groups }
Expand All @@ -53,7 +54,7 @@ impl<'a, 'b> MemberChainGroupsBuilder<'a, 'b> {
/// May be empty if all members are part of the head group
#[derive(Debug)]
pub(super) struct TailChainGroups<'a, 'b> {
groups: Vec<MemberChainGroup<'a, 'b>>,
groups: VecDeque<MemberChainGroup<'a, 'b>>,
}

impl<'a, 'b> TailChainGroups<'a, 'b> {
Expand All @@ -69,17 +70,17 @@ impl<'a, 'b> TailChainGroups<'a, 'b> {

/// Returns the first group
pub(crate) fn first(&self) -> Option<&MemberChainGroup<'a, 'b>> {
self.groups.first()
self.groups.front()
}

/// Returns the last group
pub(crate) fn last(&self) -> Option<&MemberChainGroup<'a, 'b>> {
self.groups.last()
self.groups.back()
}

/// Removes the first group and returns it
pub(super) fn pop_first(&mut self) -> Option<MemberChainGroup<'a, 'b>> {
if self.groups.is_empty() { None } else { Some(self.groups.remove(0)) }
self.groups.pop_front()
}

/// Here we check if the length of the groups exceeds the cutoff or there are comments
Expand All @@ -96,7 +97,8 @@ impl<'a, 'b> TailChainGroups<'a, 'b> {

/// Test if any group except the last group [break](FormatElements::will_break).
pub(super) fn any_except_last_will_break(&self, f: &Formatter<'_, 'a>) -> bool {
for group in &self.groups[..self.groups.len().saturating_sub(1)] {
let count = self.groups.len().saturating_sub(1);
for group in self.groups.iter().take(count) {
if group.will_break(f) {
return true;
}
Expand Down
Loading