Skip to content

Commit dca540c

Browse files
committed
fix some table align
1 parent 831296c commit dca540c

2 files changed

Lines changed: 58 additions & 18 deletions

File tree

crates/emmylua_formatter/src/formatter/expr.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,29 +1990,35 @@ fn field_requests_alignment(field: &LuaTableField) -> bool {
19901990
return false;
19911991
}
19921992

1993-
let Some(value) = field.get_value_expr() else {
1994-
return false;
1995-
};
19961993
let Some(assign_token) = field.syntax().children_with_tokens().find_map(|element| {
19971994
let token = element.into_token()?;
19981995
(token.kind() == LuaTokenKind::TkAssign.into()).then_some(token)
19991996
}) else {
20001997
return false;
20011998
};
20021999

2003-
let field_start = field.syntax().text_range().start();
2004-
let gap_start = usize::from(assign_token.text_range().end() - field_start);
2005-
let gap_end = usize::from(value.syntax().text_range().start() - field_start);
2006-
if gap_end <= gap_start {
2007-
return false;
2008-
}
2000+
let mut gap_width = 0usize;
2001+
let mut previous = assign_token.prev_token();
20092002

2010-
let text = field.syntax().text().to_string();
2011-
let Some(gap) = text.get(gap_start..gap_end) else {
2012-
return false;
2013-
};
2003+
while let Some(token) = previous {
2004+
match token.kind().to_token() {
2005+
LuaTokenKind::TkWhitespace => {
2006+
for ch in token.text().chars().rev() {
2007+
if matches!(ch, '\n' | '\r') {
2008+
return false;
2009+
}
2010+
if matches!(ch, ' ' | '\t') {
2011+
gap_width += 1;
2012+
}
2013+
}
2014+
previous = token.prev_token();
2015+
}
2016+
LuaTokenKind::TkEndOfLine => return false,
2017+
_ => break,
2018+
}
2019+
}
20142020

2015-
!gap.contains(['\n', '\r']) && gap.chars().filter(|ch| matches!(ch, ' ' | '\t')).count() > 1
2021+
gap_width > 1
20162022
}
20172023

20182024
fn table_group_requests_alignment(entries: &[TableEntry]) -> bool {

crates/emmylua_formatter/src/test/comment_tests.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ local zzz = 3
649649
r#"
650650
local t = {
651651
x = 1,
652-
long_name = 2,
652+
long_name = 2,
653653
yy = 3,
654654
}
655655
"#,
@@ -681,7 +681,7 @@ local t = {
681681
};
682682

683683
assert_format_with_config!(
684-
r#"local t = { x = 1, long_name = 2, yy = 3 }
684+
r#"local t = { x = 1, long_name = 2, yy = 3 }
685685
"#,
686686
r#"
687687
local t = {
@@ -694,6 +694,40 @@ local t = {
694694
);
695695
}
696696

697+
#[test]
698+
fn test_table_field_extra_space_after_equal_does_not_trigger_alignment() {
699+
use crate::{
700+
assert_format_with_config,
701+
config::{LayoutConfig, LuaFormatConfig},
702+
};
703+
704+
let config = LuaFormatConfig {
705+
layout: LayoutConfig {
706+
table_expand: crate::config::ExpandStrategy::Always,
707+
..Default::default()
708+
},
709+
..Default::default()
710+
};
711+
712+
assert_format_with_config!(
713+
r#"
714+
local t = {
715+
x = 1,
716+
long_name = 2,
717+
yy = 3,
718+
}
719+
"#,
720+
r#"
721+
local t = {
722+
x = 1,
723+
long_name = 2,
724+
yy = 3
725+
}
726+
"#,
727+
config
728+
);
729+
}
730+
697731
#[test]
698732
fn test_alignment_disabled() {
699733
use crate::{assert_format_with_config, config::LuaFormatConfig};
@@ -808,7 +842,7 @@ end
808842
r#"
809843
local t = {
810844
x = 100, -- first
811-
long_name = 2, -- second
845+
long_name = 2, -- second
812846
}
813847
"#,
814848
r#"
@@ -840,7 +874,7 @@ local t = {
840874
r#"
841875
local t = {
842876
a = "very very long", -- first
843-
b = 2, -- second
877+
b = 2, -- second
844878
c = 3,
845879
d = 4, -- third
846880
e = 5 -- fourth

0 commit comments

Comments
 (0)