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 spacing in generated code #23

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 36 additions & 3 deletions src/core/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ impl<'a> ToTokens for AccountAnnotationWithTyExpr<'a> {

let unchecked = if let &&AccountTyExpr::UncheckedAccount = ty_expr {
Some(quote! {
#[doc="CHECK: This account is unchecked."]
/// CHECK: This account is unchecked.
})
} else {
None
Expand Down Expand Up @@ -1644,7 +1644,7 @@ fn make_lib(

#[derive(Clone, Debug)]
pub struct CpiAccount<'info> {
#[doc="CHECK: CpiAccounts temporarily store AccountInfos."]
/// CHECK: CpiAccounts temporarily store AccountInfos.
pub account_info: AccountInfo<'info>,
pub is_writable: bool,
pub is_signer: bool,
Expand Down Expand Up @@ -1821,7 +1821,40 @@ fn beautify_impl(tokens: TokenStream) -> CResult<String> {
let re = Regex::new(r"\n\n\n+").unwrap();
source = re.replace_all(&source, "\n\n").to_string();

return Ok(source);
// Convert #[doc = "CHECK: ..."] back to /// CHECK: ...
let re = Regex::new(r#"#\[doc = r" CHECK: (.*?)"\]"#).unwrap();
source = re.replace_all(&source, "/// CHECK: $1").to_string();

// rustfmt misses some stuff around attributes, so we'll fix that here
// Remove spaces around double colons
let re = Regex::new(r"\s*::\s*").unwrap();
source = re.replace_all(&source, "::").to_string();

// Remove spaces in annotation name, like # [ account and # [ instruction
let re = Regex::new(r"#\s*\[\s*(\w+) \(").unwrap();
source = re.replace_all(&source, "#[$1(").to_string();

// Remove spaces before commas
let re = Regex::new(r"\s+,").unwrap();
source = re.replace_all(&source, ",").to_string();

// Remove space before colon (no ternary operator in Rust)
let re = Regex::new(r"\s+:\s").unwrap();
source = re.replace_all(&source, ": ").to_string();

// Remove spaces around the contents of angle brackets, so < foo > to <foo>
let re = Regex::new(r"<\s*(.*?)\s*>").unwrap();
source = re.replace_all(&source, "<$1>").to_string();

// Remove spaces around periods
let re = Regex::new(r"\s+\.\s+").unwrap();
source = re.replace_all(&source, ".").to_string();

// Remove spaces between a word and (), i.e. fn calls
let re = Regex::new(r"(.*)+\s*\(").unwrap();
source = re.replace_all(&source, "$1(").to_string();

Ok(source)
}

fn beautify(tokens: TokenStream) -> CResult<String> {
Expand Down
21 changes: 8 additions & 13 deletions tests/compiled-examples/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,16 @@ pub mod seahorse_util {

#[derive(Clone, Debug)]
pub struct CpiAccount<'info> {
#[doc = "CHECK: CpiAccounts temporarily store AccountInfos."]
/// CHECK: CpiAccounts temporarily store AccountInfos.
pub account_info: AccountInfo<'info>,
pub is_writable: bool,
pub is_signer: bool,
pub seeds: Option<Vec<Vec<u8>>>,
}

#[macro_export]
macro_rules! seahorse_const {
($ name : ident , $ value : expr) => {
macro_rules! $name {
() => {
macro_rules! seahorse_const {($ name: ident, $ value: expr) => {
macro_rules! $name {() => {
$value
};
}
Expand All @@ -322,26 +320,23 @@ pub mod seahorse_util {
fn store(loaded: Self::Loaded) -> Self;
}

macro_rules! Loaded {
($ name : ty) => {
macro_rules! Loaded {($ name: ty) => {
<$name as Loadable>::Loaded
};
}

pub(crate) use Loaded;

#[macro_export]
macro_rules! assign {
($ lval : expr , $ rval : expr) => {{
macro_rules! assign {($ lval: expr, $ rval: expr) => {{
let temp = $rval;

$lval = temp;
}};
}

#[macro_export]
macro_rules! index_assign {
($ lval : expr , $ idx : expr , $ rval : expr) => {
macro_rules! index_assign {($ lval: expr, $ idx: expr, $ rval: expr) => {
let temp_rval = $rval;
let temp_idx = $idx;

Expand All @@ -363,7 +358,7 @@ mod calculator {
use std::collections::HashMap;

#[derive(Accounts)]
# [instruction (op : Operation , num : i64)]
#[instruction(op: Operation, num: i64)]
pub struct DoOperation<'info> {
#[account(mut)]
pub owner: Signer<'info>,
Expand Down Expand Up @@ -393,7 +388,7 @@ mod calculator {
pub struct InitCalculator<'info> {
#[account(mut)]
pub owner: Signer<'info>,
# [account (init , space = std :: mem :: size_of :: < dot :: program :: Calculator > () + 8 , payer = owner , seeds = ["Calculator" . as_bytes () . as_ref () , owner . key () . as_ref ()] , bump)]
#[account(init, space = std::mem::size_of::<dot::program::Calculator> () + 8, payer = owner, seeds = ["Calculator".as_bytes ().as_ref (), owner.key ().as_ref ()], bump)]
pub calculator: Box<Account<'info, dot::program::Calculator>>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
Expand Down
25 changes: 10 additions & 15 deletions tests/compiled-examples/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use anchor_lang::{prelude::*, solana_program};
use anchor_spl::token::{self, Mint, Token, TokenAccount};
use std::{cell::RefCell, rc::Rc};

seahorse_const! { MAX , 7 }
seahorse_const! { MAX, 7 }

seahorse_const! { MESSAGE , "Hello constants" . to_string () }
seahorse_const! { MESSAGE, "Hello constants".to_string () }

seahorse_const! { MIN , 2 }
seahorse_const! { MIN, 2 }

seahorse_const! { RANGE , (MAX ! () - MIN ! ()) }
seahorse_const! { RANGE, (MAX ! () - MIN ! ()) }

pub fn use_constants_handler<'info>(mut signer: SeahorseSigner<'info, '_>) -> () {
solana_program::msg!("{}", MESSAGE!());
Expand Down Expand Up @@ -189,18 +189,16 @@ pub mod seahorse_util {

#[derive(Clone, Debug)]
pub struct CpiAccount<'info> {
#[doc = "CHECK: CpiAccounts temporarily store AccountInfos."]
/// CHECK: CpiAccounts temporarily store AccountInfos.
pub account_info: AccountInfo<'info>,
pub is_writable: bool,
pub is_signer: bool,
pub seeds: Option<Vec<Vec<u8>>>,
}

#[macro_export]
macro_rules! seahorse_const {
($ name : ident , $ value : expr) => {
macro_rules! $name {
() => {
macro_rules! seahorse_const {($ name: ident, $ value: expr) => {
macro_rules! $name {() => {
$value
};
}
Expand All @@ -217,26 +215,23 @@ pub mod seahorse_util {
fn store(loaded: Self::Loaded) -> Self;
}

macro_rules! Loaded {
($ name : ty) => {
macro_rules! Loaded {($ name: ty) => {
<$name as Loadable>::Loaded
};
}

pub(crate) use Loaded;

#[macro_export]
macro_rules! assign {
($ lval : expr , $ rval : expr) => {{
macro_rules! assign {($ lval: expr, $ rval: expr) => {{
let temp = $rval;

$lval = temp;
}};
}

#[macro_export]
macro_rules! index_assign {
($ lval : expr , $ idx : expr , $ rval : expr) => {
macro_rules! index_assign {($ lval: expr, $ idx: expr, $ rval: expr) => {
let temp_rval = $rval;
let temp_idx = $idx;

Expand Down
19 changes: 7 additions & 12 deletions tests/compiled-examples/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,16 @@ pub mod seahorse_util {

#[derive(Clone, Debug)]
pub struct CpiAccount<'info> {
#[doc = "CHECK: CpiAccounts temporarily store AccountInfos."]
/// CHECK: CpiAccounts temporarily store AccountInfos.
pub account_info: AccountInfo<'info>,
pub is_writable: bool,
pub is_signer: bool,
pub seeds: Option<Vec<Vec<u8>>>,
}

#[macro_export]
macro_rules! seahorse_const {
($ name : ident , $ value : expr) => {
macro_rules! $name {
() => {
macro_rules! seahorse_const {($ name: ident, $ value: expr) => {
macro_rules! $name {() => {
$value
};
}
Expand All @@ -263,26 +261,23 @@ pub mod seahorse_util {
fn store(loaded: Self::Loaded) -> Self;
}

macro_rules! Loaded {
($ name : ty) => {
macro_rules! Loaded {($ name: ty) => {
<$name as Loadable>::Loaded
};
}

pub(crate) use Loaded;

#[macro_export]
macro_rules! assign {
($ lval : expr , $ rval : expr) => {{
macro_rules! assign {($ lval: expr, $ rval: expr) => {{
let temp = $rval;

$lval = temp;
}};
}

#[macro_export]
macro_rules! index_assign {
($ lval : expr , $ idx : expr , $ rval : expr) => {
macro_rules! index_assign {($ lval: expr, $ idx: expr, $ rval: expr) => {
let temp_rval = $rval;
let temp_idx = $idx;

Expand All @@ -304,7 +299,7 @@ mod event {
use std::collections::HashMap;

#[derive(Accounts)]
# [instruction (data : u8 , title : String)]
#[instruction(data: u8, title: String)]
pub struct SendEvent<'info> {
#[account(mut)]
pub sender: Signer<'info>,
Expand Down
21 changes: 8 additions & 13 deletions tests/compiled-examples/fizzbuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,16 @@ pub mod seahorse_util {

#[derive(Clone, Debug)]
pub struct CpiAccount<'info> {
#[doc = "CHECK: CpiAccounts temporarily store AccountInfos."]
/// CHECK: CpiAccounts temporarily store AccountInfos.
pub account_info: AccountInfo<'info>,
pub is_writable: bool,
pub is_signer: bool,
pub seeds: Option<Vec<Vec<u8>>>,
}

#[macro_export]
macro_rules! seahorse_const {
($ name : ident , $ value : expr) => {
macro_rules! $name {
() => {
macro_rules! seahorse_const {($ name: ident, $ value: expr) => {
macro_rules! $name {() => {
$value
};
}
Expand All @@ -272,26 +270,23 @@ pub mod seahorse_util {
fn store(loaded: Self::Loaded) -> Self;
}

macro_rules! Loaded {
($ name : ty) => {
macro_rules! Loaded {($ name: ty) => {
<$name as Loadable>::Loaded
};
}

pub(crate) use Loaded;

#[macro_export]
macro_rules! assign {
($ lval : expr , $ rval : expr) => {{
macro_rules! assign {($ lval: expr, $ rval: expr) => {{
let temp = $rval;

$lval = temp;
}};
}

#[macro_export]
macro_rules! index_assign {
($ lval : expr , $ idx : expr , $ rval : expr) => {
macro_rules! index_assign {($ lval: expr, $ idx: expr, $ rval: expr) => {
let temp_rval = $rval;
let temp_idx = $idx;

Expand All @@ -313,7 +308,7 @@ mod fizzbuzz {
use std::collections::HashMap;

#[derive(Accounts)]
# [instruction (n : u64)]
#[instruction(n: u64)]
pub struct DoFizzbuzz<'info> {
#[account(mut)]
pub fizzbuzz: Box<Account<'info, dot::program::FizzBuzz>>,
Expand All @@ -335,7 +330,7 @@ mod fizzbuzz {
pub struct Init<'info> {
#[account(mut)]
pub owner: Signer<'info>,
# [account (init , space = std :: mem :: size_of :: < dot :: program :: FizzBuzz > () + 8 , payer = owner , seeds = ["fizzbuzz" . as_bytes () . as_ref () , owner . key () . as_ref ()] , bump)]
#[account(init, space = std::mem::size_of::<dot::program::FizzBuzz> () + 8, payer = owner, seeds = ["fizzbuzz".as_bytes ().as_ref (), owner.key ().as_ref ()], bump)]
pub fizzbuzz: Box<Account<'info, dot::program::FizzBuzz>>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
Expand Down
21 changes: 8 additions & 13 deletions tests/compiled-examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,16 @@ pub mod seahorse_util {

#[derive(Clone, Debug)]
pub struct CpiAccount<'info> {
#[doc = "CHECK: CpiAccounts temporarily store AccountInfos."]
/// CHECK: CpiAccounts temporarily store AccountInfos.
pub account_info: AccountInfo<'info>,
pub is_writable: bool,
pub is_signer: bool,
pub seeds: Option<Vec<Vec<u8>>>,
}

#[macro_export]
macro_rules! seahorse_const {
($ name : ident , $ value : expr) => {
macro_rules! $name {
() => {
macro_rules! seahorse_const {($ name: ident, $ value: expr) => {
macro_rules! $name {() => {
$value
};
}
Expand All @@ -276,26 +274,23 @@ pub mod seahorse_util {
fn store(loaded: Self::Loaded) -> Self;
}

macro_rules! Loaded {
($ name : ty) => {
macro_rules! Loaded {($ name: ty) => {
<$name as Loadable>::Loaded
};
}

pub(crate) use Loaded;

#[macro_export]
macro_rules! assign {
($ lval : expr , $ rval : expr) => {{
macro_rules! assign {($ lval: expr, $ rval: expr) => {{
let temp = $rval;

$lval = temp;
}};
}

#[macro_export]
macro_rules! index_assign {
($ lval : expr , $ idx : expr , $ rval : expr) => {
macro_rules! index_assign {($ lval: expr, $ idx: expr, $ rval: expr) => {
let temp_rval = $rval;
let temp_idx = $idx;

Expand All @@ -320,9 +315,9 @@ mod hello {
pub struct Init<'info> {
#[account(mut)]
pub owner: Signer<'info>,
# [account (init , space = std :: mem :: size_of :: < dot :: program :: Hello > () + 8 , payer = owner , seeds = ["hello" . as_bytes () . as_ref ()] , bump)]
#[account(init, space = std::mem::size_of::<dot::program::Hello> () + 8, payer = owner, seeds = ["hello".as_bytes ().as_ref ()], bump)]
pub hello: Box<Account<'info, dot::program::Hello>>,
# [account (init , payer = owner , seeds = ["hello-mint" . as_bytes () . as_ref ()] , bump , mint :: decimals = 0 , mint :: authority = hello)]
#[account(init, payer = owner, seeds = ["hello-mint".as_bytes ().as_ref ()], bump, mint::decimals = 0, mint::authority = hello)]
pub mint: Box<Account<'info, Mint>>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
Expand Down
Loading