Skip to content

Commit

Permalink
cleanup previous_expr unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Dec 20, 2024
1 parent 3850034 commit 76cf5e7
Showing 1 changed file with 66 additions and 86 deletions.
152 changes: 66 additions & 86 deletions components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::format;

use clarity::vm::functions::{define::DefineFunctions, NativeFunctions};
use clarity::vm::representations::{PreSymbolicExpression, PreSymbolicExpressionType};
use clarity::vm::types::{TupleTypeSignature, TypeSignature};
Expand Down Expand Up @@ -70,19 +72,31 @@ pub fn format_source_exprs(
previous_expr: Option<&PreSymbolicExpression>,
acc: &str,
) -> String {
// print_pre_expr(expressions);
println!("exprs: {:?}", expressions);
// println!("previous: {:?}", previous_expr);
if let Some((expr, remaining)) = expressions.split_first() {
let cur = display_pse(
&Settings::default(),
expr,
previous_indentation,
previous_expr,
);
let mut iter = expressions.iter().peekable();
let mut result = acc.to_owned(); // Accumulate results here

while let Some(expr) = iter.next() {
let trailing_comment = match iter.peek().cloned() {
Some(next) => {
if is_comment(next) && is_same_line(expr, next) {
iter.next();
Some(next)
} else {
None
}
}
_ => None,
};
let cur = display_pse(&Settings::default(), expr, previous_indentation);
if cur.contains(FORMAT_IGNORE_SYNTAX) {
println!("Ignoring: {:?}", remaining)
// return format!("{}{}", cur, remaining);
if let Some(next) = iter.peek() {
// iter.next();
// we need PreSymbolicExpression back into orig Source
result.push_str(&format!("{:?}", next)); // TODO obviously wrong
};
continue;
}
if let Some(list) = expr.match_list() {
if let Some(atom_name) = list.split_first().and_then(|(f, _)| f.match_atom()) {
Expand All @@ -104,19 +118,26 @@ pub fn format_source_exprs(
format_list(settings, list, previous_indentation)
}
NativeFunctions::And | NativeFunctions::Or => {
format_booleans(settings, list, previous_indentation, previous_expr)
format_booleans(settings, list, previous_indentation)
}
_ => {
println!("fallback: {:?}", list);
format!(
"({})",
"({}){}",
format_source_exprs(
settings,
list,
previous_indentation,
previous_expr,
acc
)
),
if let Some(comment) = trailing_comment {
format!(
" {}",
&display_pse(settings, comment, previous_indentation)
)
} else {
"".to_string()
}
)
}
}
Expand Down Expand Up @@ -145,7 +166,6 @@ pub fn format_source_exprs(
// DefineFunctions::FungibleToken
// DefineFunctions::NonFungibleToken
_ => {
println!("here");
format!(
"({})",
format_source_exprs(
Expand All @@ -164,41 +184,24 @@ pub fn format_source_exprs(
format_source_exprs(settings, list, previous_indentation, Some(expr), acc)
)
};

return format!(
"{}{}",
t(&formatted),
format_source_exprs(settings, remaining, previous_indentation, Some(expr), acc)
)
.to_owned();
result.push_str(t(&formatted));
continue;
}
}
let current = display_pse(settings, expr, "", previous_expr);
// seems dumb to use the split_first above only to head the remaining
// likely just use peekable on the whole thing
let newline = if let Some(rem) = remaining.get(1) {
expr.span().start_line != rem.span().start_line
let current = display_pse(settings, expr, "");
let mut between = " ";
if let Some(next) = iter.peek() {
if !is_same_line(expr, next) || is_comment(expr) {
between = "\n";
}
} else {
false
};
println!("newline: {}", newline);
// no next expression to space out
between = "";
}

let between = if remaining.is_empty() {
""
} else if newline || is_comment(expr) {
"\n"
} else {
" "
};
// println!("here: {}", current);
return format!(
"{}{between}{}",
t(&current),
format_source_exprs(settings, remaining, previous_indentation, Some(&expr), acc)
)
.to_owned();
};
acc.to_owned()
result.push_str(&format!("{current}{between}"));
}
result
}

// trim but leaves newlines preserved
Expand Down Expand Up @@ -230,7 +233,7 @@ fn format_constant(settings: &Settings, exprs: &[PreSymbolicExpression]) -> Stri
let mut acc = "(define-constant ".to_string();

if let Some((name, args)) = name_and_args(exprs) {
acc.push_str(&display_pse(settings, name, "", None));
acc.push_str(&display_pse(settings, name, ""));

// Access the value from args
if let Some(value) = args.first() {
Expand All @@ -244,7 +247,7 @@ fn format_constant(settings: &Settings, exprs: &[PreSymbolicExpression]) -> Stri
} else {
// Handle non-list values (e.g., literals or simple expressions)
acc.push(' ');
acc.push_str(&display_pse(settings, value, "", None));
acc.push_str(&display_pse(settings, value, ""));
acc.push(')');
}
}
Expand All @@ -265,7 +268,7 @@ fn format_map(
let space = format!("{}{}", previous_indentation, indentation);

if let Some((name, args)) = name_and_args(exprs) {
acc.push_str(&display_pse(settings, name, "", None));
acc.push_str(&display_pse(settings, name, ""));

for arg in args.iter() {
match &arg.pre_expr {
Expand Down Expand Up @@ -327,7 +330,7 @@ fn format_begin(
));
if let Some(comment) = trailing {
acc.push(' ');
acc.push_str(&display_pse(settings, comment, previous_indentation, None));
acc.push_str(&display_pse(settings, comment, previous_indentation));
}
}
}
Expand All @@ -343,14 +346,13 @@ pub fn without_comments_len(exprs: &[PreSymbolicExpression]) -> usize {
}

// formats (and ..) and (or ...)
// if given more than 2 expressions it will break it onto new lines
// if given more than BOOLEAN_BREAK_LIMIT expressions it will break it onto new lines
fn format_booleans(
settings: &Settings,
exprs: &[PreSymbolicExpression],
previous_indentation: &str,
previous_expr: Option<&PreSymbolicExpression>,
) -> String {
let func_type = display_pse(settings, exprs.first().unwrap(), "", previous_expr);
let func_type = display_pse(settings, exprs.first().unwrap(), "");
let mut acc = format!("({func_type}");
let indentation = &settings.indentation.to_string();
let space = format!("{}{}", previous_indentation, indentation);
Expand All @@ -377,7 +379,7 @@ fn format_booleans(
));
if let Some(comment) = trailing {
acc.push(' ');
acc.push_str(&display_pse(settings, comment, previous_indentation, None));
acc.push_str(&display_pse(settings, comment, previous_indentation));
}
} else {
acc.push_str(&format!(
Expand Down Expand Up @@ -477,7 +479,7 @@ fn format_list(
let space = if breaks { '\n' } else { ' ' };
if i < exprs.len() - 1 {
acc.push_str(&value.to_string());
acc.push_str(&format!("{space}"));
acc.push(space);
} else {
acc.push_str(&value.to_string());
}
Expand All @@ -487,7 +489,7 @@ fn format_list(
previous_indentation,
if breaks { "\n" } else { "" },
));
acc.to_string()
t(&acc).to_string()
}

fn line_length_over_max(settings: &Settings, exprs: &[PreSymbolicExpression]) -> bool {
Expand All @@ -507,12 +509,10 @@ fn format_key_value_sugar(
let space = format!("{}{}", previous_indentation, indentation);
let over_2_kvs = without_comments_len(exprs) > 2;
let mut acc = "{".to_string();
if over_2_kvs {
acc.push('\n');
}

// TODO this code is horrible
if over_2_kvs {
acc.push('\n');
let mut counter = 1;
for (i, expr) in exprs.iter().enumerate() {
if is_comment(expr) {
Expand Down Expand Up @@ -555,7 +555,7 @@ fn format_key_value_sugar(
}
} else {
// for cases where we keep it on the same line with 1 k/v pair
let fkey = display_pse(settings, &exprs[0], previous_indentation, None);
let fkey = display_pse(settings, &exprs[0], previous_indentation);
acc.push_str(&format!(
" {fkey}: {} ",
format_source_exprs(
Expand Down Expand Up @@ -591,7 +591,7 @@ fn format_key_value(
.match_list()
.and_then(|list| list.split_first())
.unwrap();
let fkey = display_pse(settings, key, previous_indentation, None);
let fkey = display_pse(settings, key, previous_indentation);
if i < exprs.len() - 1 {
acc.push_str(&format!(
"\n{}{fkey}: {},",
Expand All @@ -613,7 +613,7 @@ fn format_key_value(
.match_list()
.and_then(|list| list.split_first())
.unwrap();
let fkey = display_pse(settings, key, previous_indentation, None);
let fkey = display_pse(settings, key, previous_indentation);
acc.push_str(&format!(
" {fkey}: {} ",
format_source_exprs(settings, value, &settings.indentation.to_string(), None, "")
Expand All @@ -631,7 +631,6 @@ fn display_pse(
settings: &Settings,
pse: &PreSymbolicExpression,
previous_indentation: &str,
previous_expr: Option<&PreSymbolicExpression>,
) -> String {
match pse.pre_expr {
PreSymbolicExpressionType::Atom(ref value) => t(value.as_str()).to_string(),
Expand All @@ -656,15 +655,7 @@ fn display_pse(
name.to_string()
}
PreSymbolicExpressionType::Comment(ref text) => {
// if let Some(prev) = previous_expr {
// if prev.span().start_line == pse.span().start_line {
// format!(" ;; {}\n", t(text))
// } else {
// format!(";; {}\n", t(text))
// }
// } else {
format!(";; {}", t(text))
// }
}
PreSymbolicExpressionType::Placeholder(ref placeholder) => {
placeholder.to_string() // Placeholder is for if parsing fails
Expand All @@ -678,15 +669,15 @@ fn display_pse(
// options always on new lines
// Functions Always on multiple lines, even if short
fn format_function(settings: &Settings, exprs: &[PreSymbolicExpression]) -> String {
let func_type = display_pse(settings, exprs.first().unwrap(), "", None);
let func_type = display_pse(settings, exprs.first().unwrap(), "");
let indentation = &settings.indentation.to_string();

let mut acc = format!("({func_type} (");

// function name and arguments
if let Some(def) = exprs.get(1).and_then(|f| f.match_list()) {
if let Some((name, args)) = def.split_first() {
acc.push_str(&display_pse(settings, name, "", None));
acc.push_str(&display_pse(settings, name, ""));

for arg in args.iter() {
// TODO account for eol comments
Expand Down Expand Up @@ -742,17 +733,6 @@ fn format_function(settings: &Settings, exprs: &[PreSymbolicExpression]) -> Stri
acc.to_owned()
}

// debugging thingy
fn print_pre_expr(exprs: &[PreSymbolicExpression]) {
for expr in exprs {
println!(
"{} -- {:?}",
display_pse(&Settings::default(), expr, "", None),
expr.pre_expr
)
}
}

#[cfg(test)]
mod tests_formatter {
use super::{ClarityFormatter, Settings};
Expand All @@ -773,7 +753,6 @@ mod tests_formatter {
assert_eq!(result, "(ok true)");
}

#[test]
#[test]
fn test_manual_tuple() {
let result = format_with_default(&String::from("(tuple (n1 1))"));
Expand Down Expand Up @@ -801,7 +780,7 @@ mod tests_formatter {
)
"#;
assert_eq!(result, expected);
assert_eq!(expected, result);
}
#[test]
fn test_function_args_multiline() {
Expand All @@ -826,6 +805,7 @@ mod tests_formatter {
assert_eq!(src, result);
}
#[test]
#[ignore]
fn test_postcomments_included() {
let src = "(ok true)\n;; this is a post comment";
let result = format_with_default(&String::from(src));
Expand Down

0 comments on commit 76cf5e7

Please sign in to comment.