Skip to content

Commit

Permalink
use the ignored and helper mods
Browse files Browse the repository at this point in the history
  • Loading branch information
tippenein committed Jan 3, 2025
1 parent 3583f58 commit 6ccbe92
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 87 deletions.
68 changes: 33 additions & 35 deletions components/clarinet-format/src/formatter/ignored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
56 changes: 13 additions & 43 deletions components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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, ""));
Expand Down
9 changes: 0 additions & 9 deletions test.clar

This file was deleted.

0 comments on commit 6ccbe92

Please sign in to comment.