Skip to content

Commit

Permalink
fix: correct formatting for databus visibility types (#4423)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR fixes an issue uncovered by #4422 where we're not properly
formatting databus visibility modifiers. I've fixed this and added a new
test case for regressions.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Feb 26, 2024
1 parent ab25b5e commit cd796de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
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 @@ impl HasItem for Param {
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 @@ impl HasItem for Param {
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 Down Expand Up @@ -183,6 +186,15 @@ pub(crate) fn last_line_contains_single_line_comment(s: &str) -> bool {
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 {}

0 comments on commit cd796de

Please sign in to comment.