Improve formatting
This commit is contained in:
parent
8737175df0
commit
14d967b659
@ -143,17 +143,9 @@ impl AbstractTree for Assignment {
|
|||||||
|
|
||||||
impl Format for Assignment {
|
impl Format for Assignment {
|
||||||
fn format(&self, output: &mut String, indent_level: u8) {
|
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);
|
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);
|
type_definition.format(output, indent_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,6 +153,6 @@ impl Format for Assignment {
|
|||||||
self.operator.format(output, indent_level);
|
self.operator.format(output, indent_level);
|
||||||
output.push_str(" ");
|
output.push_str(" ");
|
||||||
|
|
||||||
self.statement.format(output, indent_level);
|
self.statement.format(output, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ impl AbstractTree for AssignmentOperator {
|
|||||||
fn from_syntax_node(
|
fn from_syntax_node(
|
||||||
source: &str,
|
source: &str,
|
||||||
node: tree_sitter::Node,
|
node: tree_sitter::Node,
|
||||||
context: &crate::Map,
|
_context: &crate::Map,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
Error::expect_syntax_node(source, "assignment_operator", node)?;
|
Error::expect_syntax_node(source, "assignment_operator", node)?;
|
||||||
|
|
||||||
@ -35,17 +35,17 @@ impl AbstractTree for AssignmentOperator {
|
|||||||
Ok(operator)
|
Ok(operator)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, source: &str, context: &Map) -> Result<Value> {
|
fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
|
||||||
Ok(Value::none())
|
Ok(Value::none())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expected_type(&self, context: &Map) -> Result<Type> {
|
fn expected_type(&self, _context: &Map) -> Result<Type> {
|
||||||
Ok(Type::None)
|
Ok(Type::None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Format for AssignmentOperator {
|
impl Format for AssignmentOperator {
|
||||||
fn format(&self, output: &mut String, indent_level: u8) {
|
fn format(&self, output: &mut String, _indent_level: u8) {
|
||||||
match self {
|
match self {
|
||||||
AssignmentOperator::Equal => output.push('='),
|
AssignmentOperator::Equal => output.push('='),
|
||||||
AssignmentOperator::PlusEqual => output.push_str("+="),
|
AssignmentOperator::PlusEqual => output.push_str("+="),
|
||||||
|
@ -131,19 +131,17 @@ impl AbstractTree for Block {
|
|||||||
impl Format for Block {
|
impl Format for Block {
|
||||||
fn format(&self, output: &mut String, indent_level: u8) {
|
fn format(&self, output: &mut String, indent_level: u8) {
|
||||||
if self.is_async {
|
if self.is_async {
|
||||||
output.push_str("async {");
|
output.push_str("async {\n");
|
||||||
} else {
|
} else {
|
||||||
output.push('{');
|
output.push_str("{\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
for statement in &self.statements {
|
for statement in &self.statements {
|
||||||
for _ in 0..=indent_level {
|
statement.format(output, indent_level + 1);
|
||||||
output.push_str(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
statement.format(output, indent_level);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.push('\n');
|
||||||
|
Block::indent(output, indent_level);
|
||||||
output.push('}');
|
output.push('}');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,7 +163,7 @@ impl AbstractTree for BuiltInValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Format 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());
|
output.push_str(&self.get().to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
use std::fmt::Write;
|
|
||||||
|
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
use tree_sitter::Node;
|
||||||
@ -84,9 +82,9 @@ impl AbstractTree for For {
|
|||||||
impl Format for For {
|
impl Format for For {
|
||||||
fn format(&self, output: &mut String, indent_level: u8) {
|
fn format(&self, output: &mut String, indent_level: u8) {
|
||||||
if self.is_async {
|
if self.is_async {
|
||||||
output.write_str("async for ");
|
output.push_str("async for ");
|
||||||
} else {
|
} else {
|
||||||
output.write_str("for ");
|
output.push_str("for ");
|
||||||
}
|
}
|
||||||
|
|
||||||
self.item_id.format(output, indent_level);
|
self.item_id.format(output, indent_level);
|
||||||
|
@ -162,7 +162,7 @@ impl Format for FunctionNode {
|
|||||||
|
|
||||||
for (identifier, r#type) in self.parameters.iter().zip(parameter_types.iter()) {
|
for (identifier, r#type) in self.parameters.iter().zip(parameter_types.iter()) {
|
||||||
identifier.format(output, indent_level);
|
identifier.format(output, indent_level);
|
||||||
output.push('<');
|
output.push_str(" <");
|
||||||
r#type.format(output, indent_level);
|
r#type.format(output, indent_level);
|
||||||
output.push('>');
|
output.push('>');
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ impl AbstractTree for Identifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Format 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);
|
output.push_str(&self.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ pub enum MathOperator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AbstractTree for MathOperator {
|
impl AbstractTree for MathOperator {
|
||||||
fn from_syntax_node(source: &str, node: tree_sitter::Node, context: &Map) -> Result<Self> {
|
fn from_syntax_node(source: &str, node: tree_sitter::Node, _context: &Map) -> Result<Self> {
|
||||||
let operator_node = node.child(0).unwrap();
|
let operator_node = node.child(0).unwrap();
|
||||||
let operator = match operator_node.kind() {
|
let operator = match operator_node.kind() {
|
||||||
"+" => MathOperator::Add,
|
"+" => MathOperator::Add,
|
||||||
@ -43,7 +43,7 @@ impl AbstractTree for MathOperator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Format 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 {
|
let char = match self {
|
||||||
MathOperator::Add => '+',
|
MathOperator::Add => '+',
|
||||||
MathOperator::Subtract => '-',
|
MathOperator::Subtract => '-',
|
||||||
|
@ -156,4 +156,10 @@ pub trait AbstractTree: Sized + Format {
|
|||||||
|
|
||||||
pub trait Format {
|
pub trait Format {
|
||||||
fn format(&self, output: &mut String, indent_level: u8);
|
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(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,6 +113,8 @@ impl AbstractTree for Statement {
|
|||||||
|
|
||||||
impl Format for Statement {
|
impl Format for Statement {
|
||||||
fn format(&self, output: &mut String, indent_level: u8) {
|
fn format(&self, output: &mut String, indent_level: u8) {
|
||||||
|
Statement::indent(output, indent_level);
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
Statement::Assignment(assignment) => assignment.format(output, indent_level),
|
Statement::Assignment(assignment) => assignment.format(output, indent_level),
|
||||||
Statement::Expression(expression) => expression.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),
|
Statement::Return(statement) => statement.format(output, indent_level),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.push('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -241,7 +241,7 @@ impl Format for Type {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Type::None => output.push_str("none"),
|
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::String => output.push_str("str"),
|
||||||
Type::Option(optional_type) => {
|
Type::Option(optional_type) => {
|
||||||
output.push_str("option(");
|
output.push_str("option(");
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use std::{collections::BTreeMap, fmt::Write};
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
use tree_sitter::Node;
|
||||||
@ -304,10 +304,14 @@ impl AbstractTree for ValueNode {
|
|||||||
impl Format for ValueNode {
|
impl Format for ValueNode {
|
||||||
fn format(&self, output: &mut String, indent_level: u8) {
|
fn format(&self, output: &mut String, indent_level: u8) {
|
||||||
match self {
|
match self {
|
||||||
ValueNode::Boolean(source)
|
ValueNode::Boolean(source) | ValueNode::Float(source) | ValueNode::Integer(source) => {
|
||||||
| ValueNode::Float(source)
|
output.push_str(source)
|
||||||
| ValueNode::Integer(source)
|
}
|
||||||
| ValueNode::String(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::Function(function) => function.format(output, indent_level),
|
||||||
ValueNode::List(expressions) => {
|
ValueNode::List(expressions) => {
|
||||||
output.push('[');
|
output.push('[');
|
||||||
|
@ -43,6 +43,6 @@ impl Format for While {
|
|||||||
output.push_str("while ");
|
output.push_str("while ");
|
||||||
self.expression.format(output, indent_level);
|
self.expression.format(output, indent_level);
|
||||||
output.push(' ');
|
output.push(' ');
|
||||||
self.expression.format(output, indent_level);
|
self.block.format(output, indent_level);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@ impl BuiltInFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Format for 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());
|
output.push_str(self.name());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -527,7 +527,7 @@ impl Serialize for Value {
|
|||||||
impl Display for Value {
|
impl Display for Value {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
match self {
|
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::Float(float) => write!(f, "{float}"),
|
||||||
Value::Integer(int) => write!(f, "{int}"),
|
Value::Integer(int) => write!(f, "{int}"),
|
||||||
Value::Boolean(boolean) => write!(f, "{boolean}"),
|
Value::Boolean(boolean) => write!(f, "{boolean}"),
|
||||||
|
@ -13,8 +13,7 @@ const FORMATTED_BLOCK: &str = "{
|
|||||||
1
|
1
|
||||||
2
|
2
|
||||||
3
|
3
|
||||||
}
|
}";
|
||||||
";
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn format_block() {
|
fn format_block() {
|
||||||
@ -25,10 +24,24 @@ fn format_block() {
|
|||||||
assert_eq!(FORMATTED_BLOCK, interpreter.format());
|
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 <int>) <num> {
|
const FORMATTED_FUNCTION: &str = "(x <int>) <num> {
|
||||||
x / 2
|
x / 2
|
||||||
}
|
}";
|
||||||
";
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn format_function() {
|
fn format_function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user