-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into zpedro/docs_0.19.3
- Loading branch information
Showing
20 changed files
with
433 additions
and
268 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
mod array; | ||
mod expr; | ||
mod infix; | ||
mod parenthesized; | ||
|
||
pub(crate) use array::rewrite as array; | ||
pub(crate) use expr::{rewrite as expr, rewrite_sub_expr as sub_expr}; | ||
pub(crate) use infix::rewrite as infix; | ||
pub(crate) use parenthesized::rewrite as parenthesized; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
use noirc_frontend::{token::Token, ArrayLiteral, Expression, ExpressionKind, Literal, UnaryOp}; | ||
|
||
use crate::visitor::{ | ||
expr::{format_brackets, format_parens}, | ||
ExpressionType, FmtVisitor, Indent, Shape, | ||
}; | ||
|
||
pub(crate) fn rewrite_sub_expr( | ||
visitor: &FmtVisitor, | ||
shape: Shape, | ||
expression: Expression, | ||
) -> String { | ||
rewrite(visitor, expression, ExpressionType::SubExpression, shape) | ||
} | ||
|
||
pub(crate) fn rewrite( | ||
visitor: &FmtVisitor, | ||
Expression { kind, span }: Expression, | ||
expr_type: ExpressionType, | ||
shape: Shape, | ||
) -> String { | ||
match kind { | ||
ExpressionKind::Block(block) => { | ||
let mut visitor = visitor.fork(); | ||
visitor.visit_block(block, span); | ||
visitor.finish() | ||
} | ||
ExpressionKind::Prefix(prefix) => { | ||
let op = match prefix.operator { | ||
UnaryOp::Minus => "-", | ||
UnaryOp::Not => "!", | ||
UnaryOp::MutableReference => "&mut ", | ||
UnaryOp::Dereference { implicitly_added } => { | ||
if implicitly_added { | ||
"" | ||
} else { | ||
"*" | ||
} | ||
} | ||
}; | ||
|
||
format!("{op}{}", rewrite_sub_expr(visitor, shape, prefix.rhs)) | ||
} | ||
ExpressionKind::Cast(cast) => { | ||
format!("{} as {}", rewrite_sub_expr(visitor, shape, cast.lhs), cast.r#type) | ||
} | ||
kind @ ExpressionKind::Infix(_) => { | ||
super::infix(visitor.fork(), Expression { kind, span }, shape) | ||
} | ||
ExpressionKind::Call(call_expr) => { | ||
let args_span = | ||
visitor.span_before(call_expr.func.span.end()..span.end(), Token::LeftParen); | ||
|
||
let callee = rewrite_sub_expr(visitor, shape, *call_expr.func); | ||
let args = format_parens( | ||
visitor.config.fn_call_width.into(), | ||
visitor.fork(), | ||
shape, | ||
false, | ||
call_expr.arguments, | ||
args_span, | ||
true, | ||
); | ||
|
||
format!("{callee}{args}") | ||
} | ||
ExpressionKind::MethodCall(method_call_expr) => { | ||
let args_span = visitor.span_before( | ||
method_call_expr.method_name.span().end()..span.end(), | ||
Token::LeftParen, | ||
); | ||
|
||
let object = rewrite_sub_expr(visitor, shape, method_call_expr.object); | ||
let method = method_call_expr.method_name.to_string(); | ||
let args = format_parens( | ||
visitor.config.fn_call_width.into(), | ||
visitor.fork(), | ||
shape, | ||
false, | ||
method_call_expr.arguments, | ||
args_span, | ||
true, | ||
); | ||
|
||
format!("{object}.{method}{args}") | ||
} | ||
ExpressionKind::MemberAccess(member_access_expr) => { | ||
let lhs_str = rewrite_sub_expr(visitor, shape, member_access_expr.lhs); | ||
format!("{}.{}", lhs_str, member_access_expr.rhs) | ||
} | ||
ExpressionKind::Index(index_expr) => { | ||
let index_span = visitor | ||
.span_before(index_expr.collection.span.end()..span.end(), Token::LeftBracket); | ||
|
||
let collection = rewrite_sub_expr(visitor, shape, index_expr.collection); | ||
let index = format_brackets(visitor.fork(), false, vec![index_expr.index], index_span); | ||
|
||
format!("{collection}{index}") | ||
} | ||
ExpressionKind::Tuple(exprs) => { | ||
format_parens(None, visitor.fork(), shape, exprs.len() == 1, exprs, span, false) | ||
} | ||
ExpressionKind::Literal(literal) => match literal { | ||
Literal::Integer(_) | Literal::Bool(_) | Literal::Str(_) | Literal::FmtStr(_) => { | ||
visitor.slice(span).to_string() | ||
} | ||
Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => { | ||
let repeated = rewrite_sub_expr(visitor, shape, *repeated_element); | ||
let length = rewrite_sub_expr(visitor, shape, *length); | ||
|
||
format!("[{repeated}; {length}]") | ||
} | ||
Literal::Array(ArrayLiteral::Standard(exprs)) => { | ||
super::array(visitor.fork(), exprs, span) | ||
} | ||
Literal::Unit => "()".to_string(), | ||
}, | ||
ExpressionKind::Parenthesized(sub_expr) => { | ||
super::parenthesized(visitor, shape, span, *sub_expr) | ||
} | ||
ExpressionKind::Constructor(constructor) => { | ||
let type_name = visitor.slice(span.start()..constructor.type_name.span().end()); | ||
let fields_span = visitor | ||
.span_before(constructor.type_name.span().end()..span.end(), Token::LeftBrace); | ||
|
||
visitor.format_struct_lit(type_name, fields_span, *constructor) | ||
} | ||
ExpressionKind::If(if_expr) => { | ||
let allow_single_line = expr_type == ExpressionType::SubExpression; | ||
|
||
if allow_single_line { | ||
let mut visitor = visitor.fork(); | ||
visitor.indent = Indent::default(); | ||
if let Some(line) = visitor.format_if_single_line(*if_expr.clone()) { | ||
return line; | ||
} | ||
} | ||
|
||
visitor.format_if(*if_expr) | ||
} | ||
ExpressionKind::Lambda(_) | ExpressionKind::Variable(_) => visitor.slice(span).to_string(), | ||
ExpressionKind::Error => unreachable!(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use noirc_frontend::{hir::resolution::errors::Span, Expression, ExpressionKind}; | ||
|
||
use crate::visitor::{FmtVisitor, Shape}; | ||
|
||
pub(crate) fn rewrite( | ||
visitor: &FmtVisitor<'_>, | ||
shape: Shape, | ||
mut span: Span, | ||
mut sub_expr: Expression, | ||
) -> String { | ||
let remove_nested_parens = visitor.config.remove_nested_parens; | ||
|
||
let mut leading; | ||
let mut trailing; | ||
|
||
loop { | ||
let leading_span = span.start() + 1..sub_expr.span.start(); | ||
let trailing_span = sub_expr.span.end()..span.end() - 1; | ||
|
||
leading = visitor.format_comment(leading_span.into()); | ||
trailing = visitor.format_comment(trailing_span.into()); | ||
|
||
if let ExpressionKind::Parenthesized(ref sub_sub_expr) = sub_expr.kind { | ||
if remove_nested_parens && leading.is_empty() && trailing.is_empty() { | ||
span = sub_expr.span; | ||
sub_expr = *sub_sub_expr.clone(); | ||
continue; | ||
} | ||
} | ||
|
||
break; | ||
} | ||
|
||
if !leading.contains("//") && !trailing.contains("//") { | ||
let sub_expr = super::sub_expr(visitor, shape, sub_expr); | ||
format!("({leading}{sub_expr}{trailing})") | ||
} else { | ||
let mut visitor = visitor.fork(); | ||
|
||
let indent = visitor.indent.to_string_with_newline(); | ||
visitor.indent.block_indent(visitor.config); | ||
let nested_indent = visitor.indent.to_string_with_newline(); | ||
|
||
let sub_expr = super::sub_expr(&visitor, shape, sub_expr); | ||
|
||
let mut result = String::new(); | ||
result.push('('); | ||
|
||
if !leading.is_empty() { | ||
result.push_str(&nested_indent); | ||
result.push_str(&leading); | ||
} | ||
|
||
result.push_str(&nested_indent); | ||
result.push_str(&sub_expr); | ||
|
||
if !trailing.is_empty() { | ||
result.push_str(&nested_indent); | ||
result.push_str(&trailing); | ||
} | ||
|
||
result.push_str(&indent); | ||
result.push(')'); | ||
|
||
result | ||
} | ||
} |
Oops, something went wrong.