Skip to content

Commit

Permalink
remove dep on clarity-repl
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Nov 15, 2024
1 parent 07bd0ec commit 6d33c36
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions components/clarinet-format/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@ version = "0.1.0"
edition = "2021"

[dependencies]
clarinet-files = { path = "../clarinet-files", default-features = false, optional = true }
clarity-repl = { path = "../clarity-repl" }
# clarity-repl = { path = "../clarity-repl" }
clarity = { workspace = true}

[features]
default = ["cli"]
cli = [
"clarity/canonical",
"clarity/developer-mode",
"clarity/devtools",
"clarity/log",
]
wasm = [
"clarity/wasm",
"clarity/developer-mode",
"clarity/devtools",
]


[lib]
name = "clarinet_format"
Expand Down
63 changes: 48 additions & 15 deletions components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
use clarity_repl::clarity::ast::build_ast_with_rules;
use clarity_repl::clarity::vm::functions::{define::DefineFunctions, NativeFunctions};
use clarity_repl::clarity::vm::types::QualifiedContractIdentifier;
use clarity_repl::clarity::ClarityVersion;
use clarity_repl::clarity::StacksEpochId;
use clarity_repl::clarity::SymbolicExpression;
use clarity::types::StacksEpochId;
use clarity::vm::ast::{build_ast_with_rules, ASTRules};
use clarity::vm::functions::{define::DefineFunctions, NativeFunctions};
use clarity::vm::types::QualifiedContractIdentifier;
use clarity::vm::{ClarityVersion, SymbolicExpression};

pub enum Indentation {
Space(u8),
Space(usize),
Tab,
}

pub struct Settings {
pub indentation: Indentation,
pub max_line_length: u8,
pub max_line_length: usize,
}

impl Settings {
pub fn default() -> Settings {
pub fn new(indentation: Indentation, max_line_length: usize) -> Self {
Settings {
indentation,
max_line_length,
}
}
}
impl Default for Settings {
fn default() -> Settings {
Settings {
indentation: Indentation::Space(2),
max_line_length: 80,
Expand All @@ -37,7 +45,7 @@ impl ClarityFormatter {
&mut (),
ClarityVersion::Clarity3,
StacksEpochId::Epoch30,
clarity_repl::clarity::ast::ASTRules::Typical,
ASTRules::Typical,
)
.unwrap();
let output = format_source_exprs(&self.settings, &ast.expressions, "");
Expand Down Expand Up @@ -96,12 +104,21 @@ pub fn format_source_exprs(
acc.to_owned()
}

fn indentation_to_string(indentation: &Indentation) -> String {
match indentation {
Indentation::Space(i) => " ".repeat(*i),
Indentation::Tab => "\t".to_string(),
}
}

fn format_begin(settings: &Settings, exprs: &[SymbolicExpression]) -> String {
let mut begin_acc = "(begin\n".to_string();
let mut begin_acc = "(begin".to_string();
let indentation = indentation_to_string(&settings.indentation);
for arg in exprs.get(1..).unwrap_or_default() {
if let Some(list) = arg.match_list() {
begin_acc.push_str(&format!(
"\n ({})",
"\n{}({})",
indentation,
format_source_exprs(settings, list, "")
))
}
Expand All @@ -111,11 +128,13 @@ fn format_begin(settings: &Settings, exprs: &[SymbolicExpression]) -> String {
}

fn format_let(settings: &Settings, exprs: &[SymbolicExpression]) -> String {
let mut begin_acc = "(let (\n".to_string();
let mut begin_acc = "(let (".to_string();
let indentation = indentation_to_string(&settings.indentation);
for arg in exprs.get(1..).unwrap_or_default() {
if let Some(list) = arg.match_list() {
begin_acc.push_str(&format!(
"\n ({})",
"\n{}({})",
indentation,
format_source_exprs(settings, list, "")
))
}
Expand Down Expand Up @@ -149,6 +168,7 @@ fn format_tuple(settings: &Settings, exprs: &[SymbolicExpression]) -> String {

fn format_function(settings: &Settings, exprs: &[SymbolicExpression]) -> String {
let func_type = exprs.first().unwrap();
let indentation = indentation_to_string(&settings.indentation);
let name_and_args = exprs.get(1).and_then(|f| f.match_list()).unwrap();
let mut func_acc = format!(
"({func_type} ({})",
Expand All @@ -157,7 +177,8 @@ fn format_function(settings: &Settings, exprs: &[SymbolicExpression]) -> String
for arg in exprs.get(2..).unwrap_or_default() {
if let Some(list) = arg.match_list() {
func_acc.push_str(&format!(
"\n ({})",
"\n{}({})",
indentation,
format_source_exprs(settings, list, "")
))
}
Expand All @@ -168,10 +189,15 @@ fn format_function(settings: &Settings, exprs: &[SymbolicExpression]) -> String
#[cfg(test)]
mod tests_formatter {
use super::{ClarityFormatter, Settings};
use crate::formatter::Indentation;
fn format_with_default(source: &str) -> String {
let mut formatter = ClarityFormatter::new(Settings::default());
formatter.format(source)
}
fn format_with(source: &str, settings: Settings) -> String {
let mut formatter = ClarityFormatter::new(settings);
formatter.format(source)
}
#[test]
fn test_simplest_formatter() {
let result = format_with_default(&String::from("( ok true )"));
Expand Down Expand Up @@ -217,4 +243,11 @@ mod tests_formatter {
let result = format_with_default(&String::from(src));
assert_eq!(result, "(begin\n (ok true)\n)");
}

#[test]
fn test_custom_tab_setting() {
let src = "(begin (ok true))";
let result = format_with(&String::from(src), Settings::new(Indentation::Space(4), 80));
assert_eq!(result, "(begin\n (ok true)\n)");
}
}

0 comments on commit 6d33c36

Please sign in to comment.