Skip to content
Merged
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
52 changes: 23 additions & 29 deletions crates/rue-formatter/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use rue_parser::{SyntaxKind, SyntaxNode, T};
use crate::{
FormatError,
ordering::{DocumentPlan, ImportGroupPlan, plan_document, plan_import_groups},
token_stream::{TokenId, TokenStream},
token_stream::{TokenId, TokenStream, significant_tokens},
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DelimiterStyle {
pub enum DelimiterStyle {
Block,
Braced,
ConditionalBraced,
Expand All @@ -26,25 +26,25 @@ enum SingleArgumentLayout {
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ItemBoundary {
pub enum ItemBoundary {
Document,
Module,
Block,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ContinuationOperator {
pub(crate) token: TokenId,
pub(crate) depth: usize,
pub struct ContinuationOperator {
pub token: TokenId,
pub depth: usize,
}

#[derive(Debug, Clone, Default)]
pub(crate) struct TokenFacts {
pub(crate) pair: Option<TokenId>,
pub(crate) delimiter_style: Option<DelimiterStyle>,
pub(crate) item_boundary: Option<ItemBoundary>,
pub(crate) group_end: Option<TokenId>,
pub(crate) continuation_operators: Vec<ContinuationOperator>,
pub struct TokenFacts {
pub pair: Option<TokenId>,
pub delimiter_style: Option<DelimiterStyle>,
pub item_boundary: Option<ItemBoundary>,
pub group_end: Option<TokenId>,
pub continuation_operators: Vec<ContinuationOperator>,
flags: TokenFlags,
}

Expand All @@ -69,40 +69,40 @@ impl TokenFlags {
}

impl TokenFacts {
pub(crate) fn supports_trailing_comma(&self) -> bool {
pub fn supports_trailing_comma(&self) -> bool {
self.flags.contains(TokenFlags::TRAILING_COMMA)
}

pub(crate) fn is_generic(&self) -> bool {
pub fn is_generic(&self) -> bool {
self.flags.contains(TokenFlags::GENERIC)
}

pub(crate) fn is_prefix_operator(&self) -> bool {
pub fn is_prefix_operator(&self) -> bool {
self.flags.contains(TokenFlags::PREFIX_OPERATOR)
}

pub(crate) fn is_attached_opener(&self) -> bool {
pub fn is_attached_opener(&self) -> bool {
self.flags.contains(TokenFlags::ATTACHED_OPENER)
}

pub(crate) fn is_absolute_path_start(&self) -> bool {
pub fn is_absolute_path_start(&self) -> bool {
self.flags.contains(TokenFlags::ABSOLUTE_PATH_START)
}

pub(crate) fn is_conditional_group(&self) -> bool {
pub fn is_conditional_group(&self) -> bool {
self.flags.contains(TokenFlags::CONDITIONAL_GROUP)
}
}

#[derive(Debug)]
pub(crate) struct Layout {
pub struct Layout {
facts: Vec<TokenFacts>,
pub(crate) document: DocumentPlan,
pub(crate) import_groups: std::collections::HashMap<TokenId, ImportGroupPlan>,
pub document: DocumentPlan,
pub import_groups: std::collections::HashMap<TokenId, ImportGroupPlan>,
}

impl Layout {
pub(crate) fn new(document: &AstDocument, stream: &TokenStream) -> Result<Self, FormatError> {
pub fn new(document: &AstDocument, stream: &TokenStream) -> Result<Self, FormatError> {
let mut facts = vec![TokenFacts::default(); stream.len()];
pair_delimiters(stream, &mut facts)?;
analyze_nodes(document.syntax(), stream, &mut facts)?;
Expand All @@ -116,7 +116,7 @@ impl Layout {
})
}

pub(crate) fn facts(&self, token: TokenId) -> &TokenFacts {
pub fn facts(&self, token: TokenId) -> &TokenFacts {
&self.facts[token.index()]
}
}
Expand Down Expand Up @@ -607,12 +607,6 @@ fn collect_union_operators(node: &SyntaxNode, operators: &mut Vec<OperatorOffset
}
}

fn significant_tokens(node: &SyntaxNode) -> impl Iterator<Item = rue_parser::SyntaxToken> + '_ {
node.descendants_with_tokens()
.filter_map(rowan::NodeOrToken::into_token)
.filter(|token| !token.kind().is_trivia())
}

fn is_generic_punctuation(kind: SyntaxKind) -> bool {
matches!(kind, T![<] | T![>] | T![,])
}
Expand Down
28 changes: 14 additions & 14 deletions crates/rue-formatter/src/document.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A deliberately small, private pretty-printing document model.

#[derive(Debug, Clone)]
pub(crate) enum Doc {
pub enum Doc {
Nil,
Text(String),
Concat(Vec<Self>),
Expand All @@ -25,18 +25,18 @@ pub(crate) enum Doc {
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LineKind {
pub enum LineKind {
Soft,
Hard,
Empty,
}

impl Doc {
pub(crate) fn text(text: impl Into<String>) -> Self {
pub fn text(text: impl Into<String>) -> Self {
Self::Text(text.into())
}

pub(crate) fn concat(docs: impl IntoIterator<Item = Self>) -> Self {
pub fn concat(docs: impl IntoIterator<Item = Self>) -> Self {
let mut flattened = Vec::new();
for doc in docs {
match doc {
Expand All @@ -48,15 +48,15 @@ impl Doc {
Self::Concat(flattened)
}

pub(crate) fn space() -> Self {
pub fn space() -> Self {
Self::text(" ")
}

pub(crate) fn soft_line() -> Self {
pub fn soft_line() -> Self {
Self::Line(LineKind::Soft)
}

pub(crate) fn fill(doc: Self, indent_levels_on_break: usize) -> Self {
pub fn fill(doc: Self, indent_levels_on_break: usize) -> Self {
Self::Fill {
flat: Box::new(doc.clone()),
broken: Box::new(doc),
Expand All @@ -65,7 +65,7 @@ impl Doc {
}
}

pub(crate) fn fill_choice(
pub fn fill_choice(
flat: Self,
broken: Self,
space_when_flat: bool,
Expand All @@ -79,27 +79,27 @@ impl Doc {
}
}

pub(crate) fn hard_line() -> Self {
pub fn hard_line() -> Self {
Self::Line(LineKind::Hard)
}

pub(crate) fn empty_line() -> Self {
pub fn empty_line() -> Self {
Self::Line(LineKind::Empty)
}

pub(crate) fn indent(self) -> Self {
pub fn indent(self) -> Self {
Self::Indent(Box::new(self))
}

pub(crate) fn outdent(self) -> Self {
pub fn outdent(self) -> Self {
Self::Outdent(Box::new(self))
}

pub(crate) fn group(self) -> Self {
pub fn group(self) -> Self {
Self::Group(Box::new(self))
}

pub(crate) fn if_break(broken: Self, flat: Self) -> Self {
pub fn if_break(broken: Self, flat: Self) -> Self {
Self::IfBreak {
broken: Box::new(broken),
flat: Box::new(flat),
Expand Down
Loading
Loading