From 14d967b6593e4d6ac0e6ea6f00b2c1fc87a9f33b Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 6 Jan 2024 08:53:31 -0500 Subject: [PATCH] Improve formatting --- src/abstract_tree/assignment.rs | 12 ++---------- src/abstract_tree/assignment_operator.rs | 8 ++++---- src/abstract_tree/block.rs | 12 +++++------- src/abstract_tree/built_in_value.rs | 2 +- src/abstract_tree/for.rs | 6 ++---- src/abstract_tree/function_node.rs | 2 +- src/abstract_tree/identifier.rs | 2 +- src/abstract_tree/math_operator.rs | 4 ++-- src/abstract_tree/mod.rs | 6 ++++++ src/abstract_tree/statement.rs | 4 ++++ src/abstract_tree/type.rs | 2 +- src/abstract_tree/value_node.rs | 14 +++++++++----- src/abstract_tree/while.rs | 2 +- src/built_in_functions/mod.rs | 2 +- src/value/mod.rs | 2 +- tests/format.rs | 21 +++++++++++++++++---- 16 files changed, 58 insertions(+), 43 deletions(-) diff --git a/src/abstract_tree/assignment.rs b/src/abstract_tree/assignment.rs index 7864ee5..27978b0 100644 --- a/src/abstract_tree/assignment.rs +++ b/src/abstract_tree/assignment.rs @@ -143,17 +143,9 @@ impl AbstractTree for Assignment { impl Format for Assignment { fn format(&self, output: &mut String, indent_level: u8) { - let Assignment { - identifier, - type_definition, - operator, - statement, - syntax_position: _, - } = self; - self.identifier.format(output, indent_level); - if let Some(type_definition) = type_definition { + if let Some(type_definition) = &self.type_definition { type_definition.format(output, indent_level); } @@ -161,6 +153,6 @@ impl Format for Assignment { self.operator.format(output, indent_level); output.push_str(" "); - self.statement.format(output, indent_level); + self.statement.format(output, 0); } } diff --git a/src/abstract_tree/assignment_operator.rs b/src/abstract_tree/assignment_operator.rs index 05db814..ca60427 100644 --- a/src/abstract_tree/assignment_operator.rs +++ b/src/abstract_tree/assignment_operator.rs @@ -13,7 +13,7 @@ impl AbstractTree for AssignmentOperator { fn from_syntax_node( source: &str, node: tree_sitter::Node, - context: &crate::Map, + _context: &crate::Map, ) -> Result { Error::expect_syntax_node(source, "assignment_operator", node)?; @@ -35,17 +35,17 @@ impl AbstractTree for AssignmentOperator { Ok(operator) } - fn run(&self, source: &str, context: &Map) -> Result { + fn run(&self, _source: &str, _context: &Map) -> Result { Ok(Value::none()) } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, _context: &Map) -> Result { Ok(Type::None) } } impl Format for AssignmentOperator { - fn format(&self, output: &mut String, indent_level: u8) { + fn format(&self, output: &mut String, _indent_level: u8) { match self { AssignmentOperator::Equal => output.push('='), AssignmentOperator::PlusEqual => output.push_str("+="), diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index ab35fed..c4d8441 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -131,19 +131,17 @@ impl AbstractTree for Block { impl Format for Block { fn format(&self, output: &mut String, indent_level: u8) { if self.is_async { - output.push_str("async {"); + output.push_str("async {\n"); } else { - output.push('{'); + output.push_str("{\n"); } for statement in &self.statements { - for _ in 0..=indent_level { - output.push_str(" "); - } - - statement.format(output, indent_level); + statement.format(output, indent_level + 1); } + output.push('\n'); + Block::indent(output, indent_level); output.push('}'); } } diff --git a/src/abstract_tree/built_in_value.rs b/src/abstract_tree/built_in_value.rs index e93a7c8..a9f34e8 100644 --- a/src/abstract_tree/built_in_value.rs +++ b/src/abstract_tree/built_in_value.rs @@ -163,7 +163,7 @@ impl AbstractTree for BuiltInValue { } impl Format for BuiltInValue { - fn format(&self, output: &mut String, indent_level: u8) { + fn format(&self, output: &mut String, _indent_level: u8) { output.push_str(&self.get().to_string()); } } diff --git a/src/abstract_tree/for.rs b/src/abstract_tree/for.rs index 8270a2d..0ec090d 100644 --- a/src/abstract_tree/for.rs +++ b/src/abstract_tree/for.rs @@ -1,5 +1,3 @@ -use std::fmt::Write; - use rayon::prelude::*; use serde::{Deserialize, Serialize}; use tree_sitter::Node; @@ -84,9 +82,9 @@ impl AbstractTree for For { impl Format for For { fn format(&self, output: &mut String, indent_level: u8) { if self.is_async { - output.write_str("async for "); + output.push_str("async for "); } else { - output.write_str("for "); + output.push_str("for "); } self.item_id.format(output, indent_level); diff --git a/src/abstract_tree/function_node.rs b/src/abstract_tree/function_node.rs index c97bcf5..57bb654 100644 --- a/src/abstract_tree/function_node.rs +++ b/src/abstract_tree/function_node.rs @@ -162,7 +162,7 @@ impl Format for FunctionNode { for (identifier, r#type) in self.parameters.iter().zip(parameter_types.iter()) { identifier.format(output, indent_level); - output.push('<'); + output.push_str(" <"); r#type.format(output, indent_level); output.push('>'); } diff --git a/src/abstract_tree/identifier.rs b/src/abstract_tree/identifier.rs index 9a0d18d..eec38f5 100644 --- a/src/abstract_tree/identifier.rs +++ b/src/abstract_tree/identifier.rs @@ -57,7 +57,7 @@ impl AbstractTree for Identifier { } impl Format for Identifier { - fn format(&self, output: &mut String, indent_level: u8) { + fn format(&self, output: &mut String, _indent_level: u8) { output.push_str(&self.0); } } diff --git a/src/abstract_tree/math_operator.rs b/src/abstract_tree/math_operator.rs index 0fe9291..0fa1d1b 100644 --- a/src/abstract_tree/math_operator.rs +++ b/src/abstract_tree/math_operator.rs @@ -12,7 +12,7 @@ pub enum MathOperator { } impl AbstractTree for MathOperator { - fn from_syntax_node(source: &str, node: tree_sitter::Node, context: &Map) -> Result { + fn from_syntax_node(source: &str, node: tree_sitter::Node, _context: &Map) -> Result { let operator_node = node.child(0).unwrap(); let operator = match operator_node.kind() { "+" => MathOperator::Add, @@ -43,7 +43,7 @@ impl AbstractTree for MathOperator { } impl Format for MathOperator { - fn format(&self, output: &mut String, indent_level: u8) { + fn format(&self, output: &mut String, _indent_level: u8) { let char = match self { MathOperator::Add => '+', MathOperator::Subtract => '-', diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index 9cce2c0..ab217b5 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -156,4 +156,10 @@ pub trait AbstractTree: Sized + Format { pub trait Format { fn format(&self, output: &mut String, indent_level: u8); + + fn indent(output: &mut String, indent_level: u8) { + for _ in 0..indent_level { + output.push_str(" "); + } + } } diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index f4ae71c..364f58f 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -113,6 +113,8 @@ impl AbstractTree for Statement { impl Format for Statement { fn format(&self, output: &mut String, indent_level: u8) { + Statement::indent(output, indent_level); + match self { Statement::Assignment(assignment) => assignment.format(output, indent_level), Statement::Expression(expression) => expression.format(output, indent_level), @@ -126,5 +128,7 @@ impl Format for Statement { } Statement::Return(statement) => statement.format(output, indent_level), } + + output.push('\n'); } } diff --git a/src/abstract_tree/type.rs b/src/abstract_tree/type.rs index 078b529..70da50f 100644 --- a/src/abstract_tree/type.rs +++ b/src/abstract_tree/type.rs @@ -241,7 +241,7 @@ impl Format for Type { } } Type::None => output.push_str("none"), - Type::Number => output.push_str("number"), + Type::Number => output.push_str("num"), Type::String => output.push_str("str"), Type::Option(optional_type) => { output.push_str("option("); diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index 6207083..3309751 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeMap, fmt::Write}; +use std::collections::BTreeMap; use serde::{Deserialize, Serialize}; use tree_sitter::Node; @@ -304,10 +304,14 @@ impl AbstractTree for ValueNode { impl Format for ValueNode { fn format(&self, output: &mut String, indent_level: u8) { match self { - ValueNode::Boolean(source) - | ValueNode::Float(source) - | ValueNode::Integer(source) - | ValueNode::String(source) => output.push_str(source), + ValueNode::Boolean(source) | ValueNode::Float(source) | ValueNode::Integer(source) => { + output.push_str(source) + } + ValueNode::String(source) => { + output.push('\''); + output.push_str(source); + output.push('\''); + } ValueNode::Function(function) => function.format(output, indent_level), ValueNode::List(expressions) => { output.push('['); diff --git a/src/abstract_tree/while.rs b/src/abstract_tree/while.rs index 480a6d1..24a9591 100644 --- a/src/abstract_tree/while.rs +++ b/src/abstract_tree/while.rs @@ -43,6 +43,6 @@ impl Format for While { output.push_str("while "); self.expression.format(output, indent_level); output.push(' '); - self.expression.format(output, indent_level); + self.block.format(output, indent_level); } } diff --git a/src/built_in_functions/mod.rs b/src/built_in_functions/mod.rs index d1fab6e..dc57cb9 100644 --- a/src/built_in_functions/mod.rs +++ b/src/built_in_functions/mod.rs @@ -153,7 +153,7 @@ impl BuiltInFunction { } impl Format for BuiltInFunction { - fn format(&self, output: &mut String, indent_level: u8) { + fn format(&self, output: &mut String, _indent_level: u8) { output.push_str(self.name()); } } diff --git a/src/value/mod.rs b/src/value/mod.rs index d77936f..5db9c85 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -527,7 +527,7 @@ impl Serialize for Value { impl Display for Value { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { - Value::String(string) => write!(f, "'{}'", string.read().unwrap()), + Value::String(string) => write!(f, "{}", string.read().unwrap()), Value::Float(float) => write!(f, "{float}"), Value::Integer(int) => write!(f, "{int}"), Value::Boolean(boolean) => write!(f, "{boolean}"), diff --git a/tests/format.rs b/tests/format.rs index f67f7f8..61a50e5 100644 --- a/tests/format.rs +++ b/tests/format.rs @@ -13,8 +13,7 @@ const FORMATTED_BLOCK: &str = "{ 1 2 3 -} -"; +}"; #[test] fn format_block() { @@ -25,10 +24,24 @@ fn format_block() { assert_eq!(FORMATTED_BLOCK, interpreter.format()); } +const FORMATTED_NESTED_BLOCK: &str = "{ + { + x = 1 + } +}"; + +#[test] +fn format_nested_block() { + let mut interpreter = Interpreter::new(Map::new()); + + interpreter.run("{{x=1}}").unwrap(); + + assert_eq!(FORMATTED_NESTED_BLOCK, interpreter.format()); +} + const FORMATTED_FUNCTION: &str = "(x ) { x / 2 -} -"; +}"; #[test] fn format_function() {