Skip to content

Commit 934393d

Browse files
Keep small prefix inlined in call chain (#550)
* Add test case * Ensure small prefix is kept inlined * Update changelog * Update snapshots
1 parent 096bbeb commit 934393d

File tree

7 files changed

+84
-16
lines changed

7 files changed

+84
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Fixed unnecessary break on `else` in an if-expression when the expression contains a comment ([#520](https://github.com/JohnnyMorganz/StyLua/issues/520))
1616
- Take into account the extra line created when hanging at equals token in an assignment. This should prevent unnecessary hanging ([#542](https://github.com/JohnnyMorganz/StyLua/issues/542))
1717
- Fixed comments added to a newly created trailing comment not being formatted ([#547](https://github.com/JohnnyMorganz/StyLua/issues/547))
18+
- Fixed call chain with a small prefix not being kept inlined causing unstable formatting ([#514](https://github.com/JohnnyMorganz/StyLua/issues/514))
1819

1920
## [0.14.2] - 2022-07-27
2021

src/formatters/functions.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,13 @@ pub fn format_function_body(
906906
.with_end_token(end_token)
907907
}
908908

909+
fn should_inline_prefix(ctx: &Context, prefix: &Prefix) -> bool {
910+
let prefix = strip_trivia(prefix).to_string();
911+
912+
prefix.as_str().chars().next().unwrap().is_uppercase()
913+
|| prefix.len() <= ctx.config().indent_width
914+
}
915+
909916
/// Formats a FunctionCall node
910917
pub fn format_function_call(
911918
ctx: &Context,
@@ -977,14 +984,7 @@ pub fn format_function_call(
977984

978985
// If the prefix starts with an uppercase character, or is smaller than the indent width
979986
// we can inline the first call. BUT, inlining overall should still be under the column width
980-
keep_first_call_inlined = (strip_leading_trivia(function_call.prefix())
981-
.to_string()
982-
.as_str()
983-
.chars()
984-
.next()
985-
.unwrap()
986-
.is_uppercase()
987-
|| function_call.prefix().to_string().len() <= ctx.config().indent_width)
987+
keep_first_call_inlined = should_inline_prefix(ctx, function_call.prefix())
988988
&& !shape
989989
.take_last_line(&strip_leading_trivia(&formatted_prefix))
990990
.test_over_budget(&formatted_suffixes.into_iter().next().unwrap());

src/formatters/trivia.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,18 @@ define_update_leading_trivia!(Prefix, |this, leading| {
410410
}
411411
});
412412

413+
define_update_trailing_trivia!(Prefix, |this, trailing| {
414+
match this {
415+
Prefix::Name(token_reference) => {
416+
Prefix::Name(token_reference.update_trailing_trivia(trailing))
417+
}
418+
Prefix::Expression(expression) => {
419+
Prefix::Expression(expression.update_trailing_trivia(trailing))
420+
}
421+
other => panic!("unknown node {:?}", other),
422+
}
423+
});
424+
413425
impl<T> UpdateLeadingTrivia for Punctuated<T>
414426
where
415427
T: UpdateLeadingTrivia + Clone,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/514
2+
local function escape(str)
3+
return (str:gsub("\\", "\\\\"):gsub("(%c)%f[0-9]", longControlCharEscapesssssssssssssssssssss):gsub("%c", shortControlCharEscapes))
4+
end
5+
6+
do
7+
function dec(data)
8+
data = string.gsub(data, '[^' .. chars .. '=]', '')
9+
return (data:gsub('.', function(x)
10+
if (x == '=') then return '' end
11+
local r, f = '', (chars:find(x) - 1)
12+
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
13+
return r;
14+
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
15+
if (#x ~= 8) then return '' end
16+
local c=0
17+
for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
18+
return string.char(c)
19+
end))
20+
end
21+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
source: tests/tests.rs
3+
expression: format(&contents)
4+
---
5+
-- https://github.com/JohnnyMorganz/StyLua/issues/514
6+
local function escape(str)
7+
return (
8+
str:gsub("\\", "\\\\")
9+
:gsub("(%c)%f[0-9]", longControlCharEscapesssssssssssssssssssss)
10+
:gsub("%c", shortControlCharEscapes)
11+
)
12+
end
13+
14+
do
15+
function dec(data)
16+
data = string.gsub(data, "[^" .. chars .. "=]", "")
17+
return (
18+
data:gsub(".", function(x)
19+
if x == "=" then
20+
return ""
21+
end
22+
local r, f = "", (chars:find(x) - 1)
23+
for i = 6, 1, -1 do
24+
r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and "1" or "0")
25+
end
26+
return r
27+
end):gsub("%d%d%d?%d?%d?%d?%d?%d?", function(x)
28+
if #x ~= 8 then
29+
return ""
30+
end
31+
local c = 0
32+
for i = 1, 8 do
33+
c = c + (x:sub(i, i) == "1" and 2 ^ (8 - i) or 0)
34+
end
35+
return string.char(c)
36+
end)
37+
)
38+
end
39+
end
40+

tests/snapshots/tests__standard@hang-call-chains-static.lua.snap

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
---
22
source: tests/tests.rs
3-
assertion_line: 12
43
expression: format(&contents)
5-
64
---
75
-- https://github.com/JohnnyMorganz/StyLua/issues/368
8-
foo
9-
.bar("-------------------------------------------------------------------------------------------------------------")
6+
foo.bar("-------------------------------------------------------------------------------------------------------------")
107
.returns()
118
foo.bar("--------------------------------------------------------------------------------------------------")
129
.returns()

tests/snapshots/tests__standard@hang-call-chains.lua.snap

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
source: tests/tests.rs
3-
assertion_line: 12
43
expression: format(&contents)
5-
64
---
75
Promise.new()
86
:andThen(callThis)
@@ -29,8 +27,7 @@ local f = this:andThen(callThis):andThen({
2927
true,
3028
}).X.Y.Z
3129

32-
this
33-
:andThen(callThis)
30+
this:andThen(callThis)
3431
:andThen({
3532
true,
3633
}).X.Y.Z

0 commit comments

Comments
 (0)