Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct formatting for databus visibility types #4423

Merged
merged 4 commits into from
Feb 26, 2024
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
14 changes: 13 additions & 1 deletion tooling/nargo_fmt/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use crate::items::HasItem;
use crate::rewrite;
use crate::visitor::{FmtVisitor, Shape};
Expand Down Expand Up @@ -143,7 +145,7 @@
fn format(self, visitor: &FmtVisitor, shape: Shape) -> String {
let pattern = visitor.slice(self.pattern.span());
let visibility = match self.visibility {
Visibility::Public => "pub ",
Visibility::Public => "pub",
Visibility::Private => "",
Visibility::DataBus => "call_data",
};
Expand All @@ -152,6 +154,7 @@
pattern.to_string()
} else {
let ty = rewrite::typ(visitor, shape, self.typ);
let visibility = append_space_if_nonempty(visibility.into());
format!("{pattern}: {visibility}{ty}")
}
}
Expand All @@ -172,7 +175,7 @@
}

pub(crate) fn last_line_width(s: &str) -> usize {
s.rsplit('\n').next().unwrap_or("").chars().count()

Check warning on line 178 in tooling/nargo_fmt/src/utils.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (rsplit)
}

pub(crate) fn is_single_line(s: &str) -> bool {
Expand All @@ -183,6 +186,15 @@
s.lines().last().map_or(false, |line| line.contains("//"))
}

pub(crate) fn append_space_if_nonempty(mut string: Cow<str>) -> Cow<str> {
if !string.is_empty() {
let inner = string.to_mut();
inner.push(' ');
}

string
}

pub(crate) fn last_line_used_width(s: &str, offset: usize) -> usize {
if s.contains('\n') {
last_line_width(s)
Expand Down
14 changes: 10 additions & 4 deletions tooling/nargo_fmt/src/visitor/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use noirc_frontend::{

use crate::{
rewrite::{self, UseTree},
utils::{last_line_contains_single_line_comment, last_line_used_width, FindToken},
utils::{
append_space_if_nonempty, last_line_contains_single_line_comment, last_line_used_width,
FindToken,
},
visitor::expr::{format_seq, NewlineMode},
};

Expand Down Expand Up @@ -119,9 +122,12 @@ impl super::FmtVisitor<'_> {
result.push_str("distinct ");
}

if let Visibility::Public = func.def.return_visibility {
result.push_str("pub ");
}
let visibility = match func.def.return_visibility {
Visibility::Public => "pub",
Visibility::DataBus => "return_data",
Visibility::Private => "",
};
result.push_str(&append_space_if_nonempty(visibility.into()));

let typ = rewrite::typ(self, self.shape(), func.return_type());
result.push_str(&typ);
Expand Down
2 changes: 2 additions & 0 deletions tooling/nargo_fmt/tests/expected/databus.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fn main(x: pub u8, y: call_data u8) -> return_data u32 {}

2 changes: 2 additions & 0 deletions tooling/nargo_fmt/tests/input/databus.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fn main(x: pub u8, y: call_data u8) -> return_data u32 {}

Loading