Skip to content

Commit

Permalink
Fix extra spaces when generating field-like structures (#193)
Browse files Browse the repository at this point in the history
This removes the extra space when generating field expressions, function
statements (like `Table0.fun`) and types using namespaces when the 
identifier before the `.` symbol ends with a number.
  • Loading branch information
jeparlefrancais authored May 17, 2024
1 parent 1df4b13 commit a76494e
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* fix generators that creates spaces when writing field expressions, function statements and field-types ([#193](https://github.com/seaofvoices/darklua/pull/193))

## 0.13.0

* add `convert` command to convert data files (`json`, `json5`, `yaml` and `toml`) into Lua modules ([#178](https://github.com/seaofvoices/darklua/pull/178))
Expand Down
22 changes: 19 additions & 3 deletions src/generator/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ impl DenseLuaGenerator {
}
}

fn push_new_line_if_needed(&mut self, pushed_length: usize) {
if self.current_line_length >= self.column_span {
self.push_new_line();
} else {
let total_length = self.current_line_length + pushed_length;

if total_length > self.column_span {
self.push_new_line();
}
}
}

fn push_space_if_needed(&mut self, next_character: char, pushed_length: usize) {
if self.current_line_length >= self.column_span {
self.push_new_line();
Expand Down Expand Up @@ -391,7 +403,8 @@ impl LuaGenerator for DenseLuaGenerator {

self.push_str(name.get_name().get_name());
name.get_field_names().iter().for_each(|field| {
self.push_char('.');
self.push_new_line_if_needed(1);
self.raw_push_char('.');
self.push_str(field.get_name());
});

Expand Down Expand Up @@ -728,7 +741,9 @@ impl LuaGenerator for DenseLuaGenerator {
fn write_field(&mut self, field: &nodes::FieldExpression) {
self.write_prefix(field.get_prefix());

self.push_char('.');
self.push_new_line_if_needed(1);
self.raw_push_char('.');

self.push_str(field.get_field().get_name());
}

Expand Down Expand Up @@ -951,7 +966,8 @@ impl LuaGenerator for DenseLuaGenerator {

fn write_type_field(&mut self, type_field: &nodes::TypeField) {
self.write_identifier(type_field.get_namespace());
self.push_char('.');
self.push_new_line_if_needed(1);
self.raw_push_char('.');
self.write_type_name(type_field.get_type_name());
}

Expand Down
10 changes: 10 additions & 0 deletions src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,12 @@ mod $mod_name {
Vec::new(),
false
),
empty_with_name_ending_with_number_with_field => FunctionStatement::new(
FunctionName::from_name("fn1").with_fields(vec!["bar".into()]),
Block::default(),
Vec::new(),
false
),
empty_with_one_typed_parameter => FunctionStatement::from_name("fn", Block::default())
.with_parameter(Identifier::new("a").with_type(TypeName::new("string"))),
empty_with_two_typed_parameters => FunctionStatement::from_name("fn", Block::default())
Expand Down Expand Up @@ -741,6 +747,9 @@ mod $mod_name {

snapshot_node!($mod_name, $generator, type_declaration, write_type_declaration_statement => (
string_alias => TypeDeclarationStatement::new("Str", TypeName::new("string")),
type_field => TypeDeclarationStatement::new("Object", TypeField::new("module", TypeName::new("Object"))),
type_field_with_name_ending_with_number
=> TypeDeclarationStatement::new("Object", TypeField::new("module0", TypeName::new("Object"))),
exported_string_alias => TypeDeclarationStatement::new("Str", TypeName::new("string"))
.export(),
generic_array => TypeDeclarationStatement::new("Array", ArrayType::new(TypeName::new("T")))
Expand Down Expand Up @@ -958,6 +967,7 @@ mod $mod_name {

snapshot_node!($mod_name, $generator, field, write_expression => (
identifier_prefix => FieldExpression::new(Prefix::from_name("foo"), "bar"),
identifier_ending_with_number_prefix => FieldExpression::new(Prefix::from_name("oof0"), "field"),
));

snapshot_node!($mod_name, $generator, index, write_expression => (
Expand Down
24 changes: 22 additions & 2 deletions src/generator/readable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ impl ReadableLuaGenerator {
self.pop_indentation();
}

fn push_new_line_if_needed(&mut self, pushed_length: usize) {
if self.current_line_length == 0 && self.current_indentation != 0 {
self.write_indentation();
}

if self.can_add_new_line() {
if self.current_line_length >= self.column_span {
self.push_new_line();
} else {
let total_length = self.current_line_length + pushed_length;

if total_length > self.column_span {
self.push_new_line();
}
}
}
}

fn push_space_if_needed(&mut self, next_character: char, pushed_length: usize) {
if self.current_line_length == 0 && self.current_indentation != 0 {
self.write_indentation();
Expand Down Expand Up @@ -980,7 +998,8 @@ impl LuaGenerator for ReadableLuaGenerator {
self.write_prefix(field.get_prefix());
self.pop_can_add_new_line();

self.push_char('.');
self.push_new_line_if_needed(1);
self.raw_push_char('.');
self.raw_push_str(field.get_field().get_name());
}

Expand Down Expand Up @@ -1187,7 +1206,8 @@ impl LuaGenerator for ReadableLuaGenerator {

fn write_type_field(&mut self, type_field: &nodes::TypeField) {
self.write_identifier(type_field.get_namespace());
self.push_char('.');
self.push_new_line_if_needed(1);
self.raw_push_char('.');
self.write_type_name(type_field.get_type_name());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
oof0.field
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
function fn1.bar()end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
type Object=module.Object
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
type Object=module0.Object
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
oof0.field
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
function fn1.bar() end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
type Object = module.Object
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
type Object = module0.Object
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
oof0.field
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
function fn1.bar()end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
type Object=module.Object
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/generator/mod.rs
expression: generator.into_string()
---
type Object=module0.Object
13 changes: 10 additions & 3 deletions src/generator/token_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ impl<'a> TokenBasedLuaGenerator<'a> {
.enumerate()
.for_each(|(i, field)| {
if let Some(period) = tokens.periods.get(i) {
self.write_token(period);
self.write_token_options(period, false);
} else {
self.write_symbol(".");
self.write_symbol_without_space_check(".");
}
self.write_identifier(field);
});
Expand Down Expand Up @@ -773,7 +773,7 @@ impl<'a> TokenBasedLuaGenerator<'a> {

fn write_type_field_with_token(&mut self, type_field: &TypeField, token: &Token) {
self.write_identifier(type_field.get_namespace());
self.write_token(token);
self.write_token_options(token, false);
self.write_type_name(type_field.get_type_name());
}

Expand Down Expand Up @@ -1572,6 +1572,13 @@ impl<'a> TokenBasedLuaGenerator<'a> {
self.push_str(symbol);
}

fn write_symbol_without_space_check(&mut self, symbol: &str) {
if self.currently_commenting {
self.uncomment();
}
self.push_str(symbol);
}

fn write_typed_identifier(&mut self, typed_identifier: &TypedIdentifier) {
if let Some(token) = typed_identifier.get_token() {
let name_in_token = token.read(self.original_code);
Expand Down

0 comments on commit a76494e

Please sign in to comment.