From 6ccbe9246cd3d2aa42b80b03903bdf500580cf4b Mon Sep 17 00:00:00 2001 From: "brady.ouren" Date: Tue, 31 Dec 2024 19:35:44 -0800 Subject: [PATCH] use the ignored and helper mods --- .../clarinet-format/src/formatter/ignored.rs | 68 +++++++++---------- .../clarinet-format/src/formatter/mod.rs | 56 ++++----------- test.clar | 9 --- 3 files changed, 46 insertions(+), 87 deletions(-) delete mode 100644 test.clar diff --git a/components/clarinet-format/src/formatter/ignored.rs b/components/clarinet-format/src/formatter/ignored.rs index e6a9e5707..773ff4347 100644 --- a/components/clarinet-format/src/formatter/ignored.rs +++ b/components/clarinet-format/src/formatter/ignored.rs @@ -2,50 +2,48 @@ use clarity::vm::representations::{PreSymbolicExpression, PreSymbolicExpressionT use crate::formatter::helpers::t; -pub fn ignored_exprs(expressions: &[PreSymbolicExpression]) -> String { +pub fn ignored_exprs(expr: &PreSymbolicExpression) -> String { let mut output = String::new(); let mut current_line = 1; - for expr in expressions { - let start_line = expr.span().start_line as usize; - let end_line = expr.span().end_line as usize; - let start_col = expr.span().start_column as usize; - let end_col = expr.span().end_column as usize; + let start_line = expr.span().start_line as usize; + let end_line = expr.span().end_line as usize; + let start_col = expr.span().start_column as usize; + let end_col = expr.span().end_column as usize; - // Add newlines if needed to reach the start line - while current_line < start_line { - output.push('\n'); - current_line += 1; - } + // Add newlines if needed to reach the start line + while current_line < start_line { + output.push('\n'); + current_line += 1; + } - // Handle single-line expressions - if start_line == end_line { - // Add padding spaces before the expression - output.extend(std::iter::repeat(' ').take(start_col - 1)); - output.push_str(&display_pse_unformatted(expr)); - } else { - // Handle multi-line expressions - let expr_str = display_pse_unformatted(&expr); - let lines: Vec<&str> = expr_str.lines().collect(); + // Handle single-line expressions + if start_line == end_line { + // Add padding spaces before the expression + output.extend(std::iter::repeat(' ').take(start_col - 1)); + output.push_str(&display_pse_unformatted(expr)); + } else { + // Handle multi-line expressions + let expr_str = display_pse_unformatted(expr); + let lines: Vec<&str> = expr_str.lines().collect(); - // Print first line with proper indentation - output.extend(std::iter::repeat(' ').take(start_col - 1)); - output.push_str(lines[0]); + // Print first line with proper indentation + output.extend(std::iter::repeat(' ').take(start_col - 1)); + output.push_str(lines[0]); + output.push('\n'); + current_line += 1; + + // Print middle lines + for line in &lines[1..lines.len() - 1] { + output.push_str(line); output.push('\n'); current_line += 1; + } - // Print middle lines - for line in &lines[1..lines.len() - 1] { - output.push_str(line); - output.push('\n'); - current_line += 1; - } - - // Print last line - if let Some(last_line) = lines.last() { - output.extend(std::iter::repeat(' ').take(end_col - last_line.len())); - output.push_str(last_line); - } + // Print last line + if let Some(last_line) = lines.last() { + output.extend(std::iter::repeat(' ').take(end_col - last_line.len())); + output.push_str(last_line); } } diff --git a/components/clarinet-format/src/formatter/mod.rs b/components/clarinet-format/src/formatter/mod.rs index e36c5e270..3f492d9fa 100644 --- a/components/clarinet-format/src/formatter/mod.rs +++ b/components/clarinet-format/src/formatter/mod.rs @@ -1,9 +1,10 @@ -use std::fmt::format; +pub mod helpers; +pub mod ignored; use clarity::vm::functions::{define::DefineFunctions, NativeFunctions}; use clarity::vm::representations::{PreSymbolicExpression, PreSymbolicExpressionType}; -use clarity::vm::types::{TupleTypeSignature, TypeSignature}; -use clarity::vm::ClarityName; +use helpers::{name_and_args, t}; +use ignored::ignored_exprs; pub enum Indentation { Space(usize), @@ -102,12 +103,13 @@ pub fn format_source_exprs( } _ => None, }; - let cur = display_pse(&Settings::default(), expr, previous_indentation); + let cur = display_pse(settings, expr, previous_indentation); if cur.contains(FORMAT_IGNORE_SYNTAX) { if let Some(next) = iter.peek() { // iter.next(); // we need PreSymbolicExpression back into orig Source - result.push_str(&format!("{:?}", next)); // TODO obviously wrong + // TODO probably very wrong + result.push_str(&ignored_exprs(next)); }; continue; } @@ -208,30 +210,6 @@ pub fn format_source_exprs( result } -// trim but leaves newlines preserved -fn t(input: &str) -> &str { - let start = input - .find(|c: char| !c.is_whitespace() || c == '\n') - .unwrap_or(0); - - let end = input - .rfind(|c: char| !c.is_whitespace() || c == '\n') - .map(|pos| pos + 1) - .unwrap_or(0); - - &input[start..end] -} - -fn name_and_args( - exprs: &[PreSymbolicExpression], -) -> Option<(&PreSymbolicExpression, &[PreSymbolicExpression])> { - if exprs.len() >= 2 { - Some((&exprs[1], &exprs[2..])) - } else { - None // Return None if there aren't enough items - } -} - fn format_constant(settings: &Settings, exprs: &[PreSymbolicExpression]) -> String { let func_type = display_pse(settings, exprs.first().unwrap(), ""); let indentation = &settings.indentation.to_string(); @@ -444,6 +422,7 @@ fn format_if( let mut index = 0; while let Some(expr) = iter.next() { + println!("{:?}", expr); let trailing = match iter.peek().cloned() { Some(next) => { if is_comment(next) && is_same_line(expr, next) { @@ -455,21 +434,12 @@ fn format_if( } _ => None, }; - if let Some(list) = expr.match_list() { - // expr args - acc.push_str(&format!( - "{}({})\n", - if index > 0 { - space.clone() - } else { - "".to_string() - }, - format_source_exprs(settings, list, &space, "") - )) - } else { - // atom args - acc.push_str(&format_source_exprs(settings, &[expr.clone()], &space, "")) + if index > 0 { + acc.push('\n'); + acc.push_str(&space); + acc.push_str(indentation); } + acc.push_str(&format_source_exprs(settings, &[expr.clone()], &space, "")); if let Some(comment) = trailing { acc.push(' '); acc.push_str(&display_pse(settings, comment, "")); diff --git a/test.clar b/test.clar deleted file mode 100644 index 955ad2847..000000000 --- a/test.clar +++ /dev/null @@ -1,9 +0,0 @@ -;; comment -(slice? "blockstack" u5 u10) ;; Returns (some "stack") -(slice? (list 1 2 3 4 5) u5 u9) ;; Returns none -(slice? (list 1 2 3 4 5) u3 u4) ;; Returns (some (4)) -(slice? "abcd" u1 u3) ;; Returns (some "bc") -(slice? "abcd" u2 u2) ;; Returns (some "") -(slice? "abcd" u3 u1) ;; Returns none -;; whatever -;;asdf asdf