Skip to content

Commit

Permalink
chore: add constructor formatter (#3243)
Browse files Browse the repository at this point in the history
  • Loading branch information
kek kek kek authored Oct 23, 2023
1 parent c82c43f commit 4bcff8c
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 117 deletions.
2 changes: 1 addition & 1 deletion tooling/nargo_fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ config! {
tab_spaces: usize, 4, "Number of spaces per tab";
remove_nested_parens: bool, true, "Remove nested parens";
short_array_element_width_threshold: usize, 10, "Width threshold for an array element to be considered short";
array_width: usize, 60, "Maximum width of an array literal before falling back to vertical formatting";
array_width: usize, 100, "Maximum width of an array literal before falling back to vertical formatting";
}

impl Config {
Expand Down
3 changes: 1 addition & 2 deletions tooling/nargo_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
/// in both placement and content during the formatting process.
mod config;
pub mod errors;
#[macro_use]
mod visitor;
mod utils;
mod visitor;

use noirc_frontend::ParsedModule;
use visitor::FmtVisitor;
Expand Down
74 changes: 57 additions & 17 deletions tooling/nargo_fmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::visitor::FmtVisitor;
use noirc_frontend::hir::resolution::errors::Span;
use noirc_frontend::lexer::Lexer;
use noirc_frontend::token::Token;
use noirc_frontend::Expression;
use noirc_frontend::{Expression, Ident};

pub(crate) fn recover_comment_removed(original: &str, new: String) -> String {
if changed_comment_content(original, &new) {
Expand Down Expand Up @@ -46,19 +46,15 @@ impl Expr {
}
}

pub(crate) struct Exprs<'me> {
pub(crate) struct Exprs<'me, T> {
pub(crate) visitor: &'me FmtVisitor<'me>,
pub(crate) elements: std::iter::Peekable<std::vec::IntoIter<Expression>>,
pub(crate) elements: std::iter::Peekable<std::vec::IntoIter<T>>,
pub(crate) last_position: u32,
pub(crate) end_position: u32,
}

impl<'me> Exprs<'me> {
pub(crate) fn new(
visitor: &'me FmtVisitor<'me>,
span: Span,
elements: Vec<Expression>,
) -> Self {
impl<'me, T: Item> Exprs<'me, T> {
pub(crate) fn new(visitor: &'me FmtVisitor<'me>, span: Span, elements: Vec<T>) -> Self {
Self {
visitor,
last_position: span.start() + 1, /*(*/
Expand All @@ -68,33 +64,33 @@ impl<'me> Exprs<'me> {
}
}

impl Iterator for Exprs<'_> {
impl<T: Item> Iterator for Exprs<'_, T> {
type Item = Expr;

fn next(&mut self) -> Option<Self::Item> {
let element = self.elements.next()?;
let element_span = element.span;
let element_span = element.span();

let start = self.last_position;
let end = element_span.start();

let is_last = self.elements.peek().is_none();
let next_start = self.elements.peek().map_or(self.end_position, |expr| expr.span.start());
let next_start = self.elements.peek().map_or(self.end_position, |expr| expr.start());

let (leading, different_line) = self.leading(start, end);
let expr = self.visitor.format_expr(element);
let expr = element.format(self.visitor);
let trailing = self.trailing(element_span.end(), next_start, is_last);

Expr { leading, value: expr, trailing, different_line }.into()
}
}

impl<'me> Exprs<'me> {
impl<'me, T> Exprs<'me, T> {
pub(crate) fn leading(&mut self, start: u32, end: u32) -> (String, bool) {
let mut different_line = false;

let leading = slice!(self.visitor, start, end);
let leading_trimmed = slice!(self.visitor, start, end).trim();
let leading = self.visitor.slice(start..end);
let leading_trimmed = leading.trim();

let starts_with_block_comment = leading_trimmed.starts_with("/*");
let ends_with_block_comment = leading_trimmed.ends_with("*/");
Expand All @@ -114,7 +110,7 @@ impl<'me> Exprs<'me> {
}

pub(crate) fn trailing(&mut self, start: u32, end: u32, is_last: bool) -> String {
let slice = slice!(self.visitor, start, end);
let slice = self.visitor.slice(start..end);
let comment_end = find_comment_end(slice, is_last);
let trailing = slice[..comment_end].trim_matches(',').trim();
self.last_position = start + (comment_end as u32);
Expand Down Expand Up @@ -201,3 +197,47 @@ fn comment_len(comment: &str) -> usize {
}
}
}

pub(crate) trait Item {
fn span(&self) -> Span;

fn format(self, visitor: &FmtVisitor) -> String;

fn start(&self) -> u32 {
self.span().start()
}

fn end(&self) -> u32 {
self.span().end()
}
}

impl Item for Expression {
fn span(&self) -> Span {
self.span
}

fn format(self, visitor: &FmtVisitor) -> String {
visitor.format_expr(self)
}
}

impl Item for (Ident, Expression) {
fn span(&self) -> Span {
let (name, value) = self;
(name.span().start()..value.span.end()).into()
}

fn format(self, visitor: &FmtVisitor) -> String {
let (name, expr) = self;

let name = name.0.contents;
let expr = visitor.format_expr(expr);

if name == expr {
name
} else {
format!("{name}: {expr}")
}
}
}
30 changes: 18 additions & 12 deletions tooling/nargo_fmt/src/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
/// A macro to create a slice from a given data source, helping to avoid borrow checker errors.
#[macro_export]
macro_rules! slice {
($this:expr, $start:expr, $end:expr) => {
&$this.source[$start as usize..$end as usize]
};
}

mod expr;
mod item;
mod stmt;

use noirc_frontend::hir::resolution::errors::Span;
use noirc_frontend::{hir::resolution::errors::Span, token::Token};

use crate::config::Config;
use crate::{config::Config, utils::FindToken};

pub(crate) struct FmtVisitor<'me> {
config: &'me Config,
Expand All @@ -33,6 +25,20 @@ impl<'me> FmtVisitor<'me> {
}
}

pub(crate) fn slice(&self, span: impl Into<Span>) -> &'me str {
let span = span.into();
&self.source[span.start() as usize..span.end() as usize]
}

fn span_before(&self, span: impl Into<Span>, token: Token) -> Span {
let span = span.into();

let slice = self.slice(span);
let offset = slice.find_token(token).unwrap();

(span.start() + offset..span.end()).into()
}

fn shape(&self) -> Shape {
Shape {
width: self.config.max_width.saturating_sub(self.indent.width()),
Expand Down Expand Up @@ -110,7 +116,7 @@ impl<'me> FmtVisitor<'me> {
return;
}

let slice = slice!(self, start, end);
let slice = self.slice(start..end);
self.last_position = end;

if slice.trim().is_empty() && !self.at_start() {
Expand Down Expand Up @@ -147,7 +153,7 @@ impl<'me> FmtVisitor<'me> {
}

pub(crate) fn format_comment(&self, span: Span) -> String {
let slice = slice!(self, span.start(), span.end()).trim();
let slice = self.slice(span).trim();
let pos = slice.find('/');

if !slice.is_empty() && pos.is_some() {
Expand Down
Loading

0 comments on commit 4bcff8c

Please sign in to comment.