Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fixed line width being measured in bytes instead of characters, which split lines containing multibyte characters (e.g. Cyrillic or CJK text in strings and comments) before they reached the column width ([#577](https://github.com/JohnnyMorganz/StyLua/issues/577))

## [2.6.0] - 2026-10-10

### Added
Expand Down
33 changes: 21 additions & 12 deletions src/formatters/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,16 @@ pub fn format_assignment_no_trivia(

// Test the assignment to see if its over width
let singleline_shape = shape
+ (strip_leading_trivia(&var_list).to_string().len()
+ (strip_leading_trivia(&var_list).to_string().chars().count()
+ 3
+ strip_trailing_trivia(&expr_list).to_string().len());
+ strip_trailing_trivia(&expr_list)
.to_string()
.chars()
.count());
if contains_comments || singleline_shape.over_budget() {
// We won't attempt anything else with the var_list. Format it normally
var_list = try_format_punctuated(ctx, assignment.variables(), shape, format_var, Some(1));
let shape = shape + (strip_leading_trivia(&var_list).to_string().len() + 3);
let shape = shape + (strip_leading_trivia(&var_list).to_string().chars().count() + 3);

let (new_expr_list, new_equal_token) =
attempt_assignment_tactics(ctx, assignment.expressions(), shape, equal_token);
Expand Down Expand Up @@ -494,13 +497,13 @@ pub fn format_local_assignment_no_trivia(
#[cfg(feature = "lua54")]
{
type_specifier_len += attributes.iter().fold(0, |acc, x| {
acc + x.as_ref().map_or(0, |y| y.to_string().len())
acc + x.as_ref().map_or(0, |y| y.to_string().chars().count())
});
}
#[cfg(feature = "luau")]
{
type_specifier_len += type_specifiers.iter().fold(0, |acc, x| {
acc + x.as_ref().map_or(0, |y| y.to_string().len())
acc + x.as_ref().map_or(0, |y| y.to_string().chars().count())
});
}

Expand All @@ -527,11 +530,11 @@ pub fn format_local_assignment_no_trivia(

// Test the assignment to see if its over width
let singleline_shape = shape
+ (strip_leading_trivia(&name_list).to_string().len()
+ (strip_leading_trivia(&name_list).to_string().chars().count()
+ 6 // 6 = "local "
+ 3 // 3 = " = "
+ type_specifier_len
+ strip_trailing_trivia(&expr_list).to_string().len());
+ strip_trailing_trivia(&expr_list).to_string().chars().count());

if contains_comments || singleline_shape.over_budget() {
// We won't attempt anything else with the name_list. Format it normally
Expand All @@ -543,7 +546,10 @@ pub fn format_local_assignment_no_trivia(
Some(1),
);
let shape = shape
+ (strip_leading_trivia(&name_list).to_string().len() + 6 + 3 + type_specifier_len);
+ (strip_leading_trivia(&name_list).to_string().chars().count()
+ 6
+ 3
+ type_specifier_len);

let (new_expr_list, new_equal_token) =
attempt_assignment_tactics(ctx, assignment.expressions(), shape, equal_token);
Expand Down Expand Up @@ -622,7 +628,7 @@ pub fn format_const_assignment_no_trivia(
.collect();

let type_specifier_len = type_specifiers.iter().fold(0, |acc, x| {
acc + x.as_ref().map_or(0, |y| y.to_string().len())
acc + x.as_ref().map_or(0, |y| y.to_string().chars().count())
});

let var_list_ends_with_comments = match type_specifiers.last() {
Expand All @@ -642,11 +648,11 @@ pub fn format_const_assignment_no_trivia(
}

let singleline_shape = shape
+ (strip_leading_trivia(&name_list).to_string().len()
+ (strip_leading_trivia(&name_list).to_string().chars().count()
+ 6 // 6 = "const "
+ 3 // 3 = " = "
+ type_specifier_len
+ strip_trailing_trivia(&expr_list).to_string().len());
+ strip_trailing_trivia(&expr_list).to_string().chars().count());

if contains_comments || singleline_shape.over_budget() {
name_list = try_format_punctuated(
Expand All @@ -657,7 +663,10 @@ pub fn format_const_assignment_no_trivia(
Some(1),
);
let shape = shape
+ (strip_leading_trivia(&name_list).to_string().len() + 6 + 3 + type_specifier_len);
+ (strip_leading_trivia(&name_list).to_string().chars().count()
+ 6
+ 3
+ type_specifier_len);

let (new_expr_list, new_equal_token) =
attempt_assignment_tactics(ctx, assignment.expressions(), shape, equal_token);
Expand Down
7 changes: 5 additions & 2 deletions src/formatters/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ pub fn format_return(ctx: &Context, return_node: &Return, shape: Shape) -> Retur
format_punctuated(ctx, returns, shape.with_infinite_width(), format_expression);

// Test the return to see if its over width
let singleline_shape =
shape + strip_trailing_trivia(&singleline_returns).to_string().len();
let singleline_shape = shape
+ strip_trailing_trivia(&singleline_returns)
.to_string()
.chars()
.count();
(singleline_shape.over_budget(), singleline_returns)
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/compound_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pub fn format_compound_assignment(
.update_leading_trivia(FormatTriviaType::Append(leading_trivia));
let compound_operator = format_compound_op(ctx, compound_assignment.compound_operator(), shape);
let shape = shape
+ (strip_leading_trivia(&lhs).to_string().len() + compound_operator.to_string().len());
+ (strip_leading_trivia(&lhs).to_string().chars().count()
+ compound_operator.to_string().chars().count());

let rhs = format_expression(ctx, compound_assignment.rhs(), shape)
.update_trailing_trivia(FormatTriviaType::Append(trailing_trivia));
Expand Down
38 changes: 22 additions & 16 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn format_expression_internal(
}
Expression::UnaryOperator { unop, expression } => {
let unop = format_unop(ctx, unop, shape);
let shape = shape + strip_leading_trivia(&unop).to_string().len();
let shape = shape + strip_leading_trivia(&unop).to_string().chars().count();
let expression = format_expression_internal(
ctx,
expression,
Expand All @@ -295,7 +295,7 @@ fn format_expression_internal(
};
let lhs = format_expression_internal(ctx, lhs, context, shape);
let binop = format_binop(ctx, binop, shape);
let shape = shape.take_last_line(&lhs) + binop.to_string().len();
let shape = shape.take_last_line(&lhs) + binop.to_string().chars().count();
Expression::BinaryOperator {
lhs: Box::new(lhs),
binop,
Expand Down Expand Up @@ -547,7 +547,7 @@ fn format_token_expression_sequence(
) -> (TokenReference, Expression) {
const SPACE_LEN: usize = " ".len();
let formatted_token = format_token_reference(ctx, token, shape);
let token_width = strip_trivia(&formatted_token).to_string().len();
let token_width = strip_trivia(&formatted_token).to_string().chars().count();

let formatted_expression =
format_expression(ctx, expression, shape.add_width(token_width + SPACE_LEN));
Expand Down Expand Up @@ -841,7 +841,7 @@ fn format_interpolated_string(
let mut segments = Vec::new();
for segment in interpolated_string.segments() {
let literal = format_token_reference(ctx, &segment.literal, shape);
shape = shape + literal.to_string().len();
shape = shape + literal.to_string().chars().count();

let mut expression = format_expression(ctx, &segment.expression, shape);
shape = shape.take_last_line(&expression);
Expand Down Expand Up @@ -1006,18 +1006,18 @@ fn binop_expression_length(expression: &Expression, top_binop: &BinOp) -> usize
{
if binop.is_right_associative() {
binop_expression_length(rhs, top_binop)
+ strip_trivia(binop).to_string().len() + 2 // 2 = space before and after binop
+ strip_trivia(&**lhs).to_string().len()
+ strip_trivia(binop).to_string().chars().count() + 2 // 2 = space before and after binop
+ strip_trivia(&**lhs).to_string().chars().count()
} else {
binop_expression_length(lhs, top_binop)
+ strip_trivia(binop).to_string().len() + 2 // 2 = space before and after binop
+ strip_trivia(&**rhs).to_string().len()
+ strip_trivia(binop).to_string().chars().count() + 2 // 2 = space before and after binop
+ strip_trivia(&**rhs).to_string().chars().count()
}
} else {
0
}
}
_ => strip_trivia(expression).to_string().len(),
_ => strip_trivia(expression).to_string().chars().count(),
}
}

Expand Down Expand Up @@ -1214,8 +1214,9 @@ fn hang_binop_expression(
let (lhs, rhs) = match should_hang {
true => {
let lhs_shape = shape;
let rhs_shape =
shape.reset() + strip_trivia(&new_binop).to_string().len() + SPACE_LEN;
let rhs_shape = shape.reset()
+ strip_trivia(&new_binop).to_string().chars().count()
+ SPACE_LEN;

let (lhs, rhs) = match side_to_hang {
ExpressionSide::Left => (
Expand Down Expand Up @@ -1423,7 +1424,9 @@ fn format_hanging_expression_(

let expression_str = formatted_expression.to_string();
if !contains_comments(expression)
&& !lhs_shape.add_width(2 + expression_str.len()).over_budget()
&& !lhs_shape
.add_width(2 + expression_str.chars().count())
.over_budget()
{
// The expression inside the parentheses is small, we do not need to break it down further
return Expression::Parentheses {
Expand Down Expand Up @@ -1465,7 +1468,7 @@ fn format_hanging_expression_(
}
Expression::UnaryOperator { unop, expression } => {
let unop = format_unop(ctx, unop, shape);
let shape = shape + strip_leading_trivia(&unop).to_string().len();
let shape = shape + strip_leading_trivia(&unop).to_string().chars().count();
let expression = format_hanging_expression_(
ctx,
expression,
Expand Down Expand Up @@ -1494,7 +1497,8 @@ fn format_hanging_expression_(
let current_shape = shape.take_last_line(&lhs) + 1; // 1 = space before binop
let mut new_binop = format_binop(ctx, binop, current_shape);

let singleline_shape = current_shape + strip_trivia(binop).to_string().len() + 1; // 1 = space after binop
let singleline_shape =
current_shape + strip_trivia(binop).to_string().chars().count() + 1; // 1 = space after binop

let mut new_rhs = hang_binop_expression(
ctx,
Expand All @@ -1511,9 +1515,11 @@ fn format_hanging_expression_(
&& binop_precedence_level(&new_rhs) >= binop.precedence())
|| contains_comments(binop)
|| lhs.has_trailing_comments(CommentSearch::All)
|| (shape.take_last_line(&lhs) + format!("{binop}{rhs}").len()).over_budget()
|| (shape.take_last_line(&lhs) + format!("{binop}{rhs}").chars().count())
.over_budget()
{
let hanging_shape = shape.reset() + strip_trivia(binop).to_string().len() + 1;
let hanging_shape =
shape.reset() + strip_trivia(binop).to_string().chars().count() + 1;
new_binop = hang_binop(ctx, binop.to_owned(), shape, rhs);
new_rhs = hang_binop_expression(
ctx,
Expand Down
46 changes: 31 additions & 15 deletions src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ fn function_args_multiline_heuristic(
singleline_shape = singleline_shape.reset() + END_LEN;
} else {
// Update the width with the collapsed function (normally indicative of a noop function)
singleline_shape = singleline_shape + argument.to_string().len();
singleline_shape = singleline_shape + argument.to_string().chars().count();
}
}
Expression::TableConstructor(table) => {
Expand Down Expand Up @@ -314,7 +314,7 @@ fn function_args_multiline_heuristic(
singleline_shape = singleline_shape.reset() + BRACKET_LEN;
} else {
// Update the shape with the size of the collapsed table constructor
singleline_shape = singleline_shape + argument.to_string().len();
singleline_shape = singleline_shape + argument.to_string().chars().count();
}
}
// TODO: Parentheses/UnOp, do we need to do more checking?
Expand Down Expand Up @@ -362,7 +362,7 @@ fn function_args_multiline_heuristic(
+ punctuation
.trailing_trivia()
.filter(|x| trivia_util::trivia_is_comment(x))
.fold(0, |acc, trivia| acc + trivia.to_string().len());
.fold(0, |acc, trivia| acc + trivia.to_string().chars().count());
}
}

Expand All @@ -381,7 +381,12 @@ fn format_argument_multiline(ctx: &Context, argument: &Expression, shape: Shape)
// Also: if the argument contains comments, it should be multilined
if argument.has_inline_comments()
|| shape
.add_width(strip_trivia(&infinite_width_argument).to_string().len())
.add_width(
strip_trivia(&infinite_width_argument)
.to_string()
.chars()
.count(),
)
.over_budget()
{
if trivia_util::can_hang_expression(argument) {
Expand Down Expand Up @@ -628,7 +633,8 @@ fn should_parameters_format_multiline(
// Check the length of the parameters. We need to format them first onto a single line to check if required
let mut line_length = format_singleline_parameters(ctx, function_body, shape)
.to_string()
.len()
.chars()
.count()
+ PARENS_LEN;

// If we are in Luau mode, take into account the types
Expand All @@ -642,7 +648,7 @@ fn should_parameters_format_multiline(
.map(|x| {
x.map_or((0, false), |specifier| {
let formatted = format_type_specifier(ctx, specifier, shape).to_string();
let length = formatted.lines().next_back().unwrap_or("").len();
let length = formatted.lines().next_back().unwrap_or("").chars().count();
let contains_newline = formatted.lines().count() > 1;

(length, contains_newline)
Expand Down Expand Up @@ -832,7 +838,10 @@ pub fn format_function_body(
.generics()
.map(|generic_declaration| format_generic_declaration(ctx, generic_declaration, shape));
#[cfg(feature = "luau")]
let shape = shape + generics.as_ref().map_or(0, |x| x.to_string().len());
let shape = shape
+ generics
.as_ref()
.map_or(0, |x| x.to_string().chars().count());

let (parameters_parentheses, formatted_parameters) = match multiline_params {
true => format_contained_punctuated_multiline(
Expand Down Expand Up @@ -882,9 +891,11 @@ pub fn format_function_body(
#[cfg(feature = "luau")]
let block_shape = block_shape
+ type_specifiers.iter().fold(0, |acc, x| {
acc + x.as_ref().map_or(0, |x| x.to_string().len())
acc + x.as_ref().map_or(0, |x| x.to_string().chars().count())
})
+ return_type.as_ref().map_or(0, |x| x.to_string().len());
+ return_type
.as_ref()
.map_or(0, |x| x.to_string().chars().count());

let trailing_trivia = FormatTriviaType::Append(vec![Token::new(TokenType::spaces(1))]);

Expand Down Expand Up @@ -979,7 +990,7 @@ fn should_inline_prefix(ctx: &Context, prefix: &Prefix) -> bool {
let prefix = strip_trivia(prefix).to_string();

prefix.as_str().chars().next().unwrap().is_uppercase()
|| prefix.len() <= ctx.config().indent_width
|| prefix.chars().count() <= ctx.config().indent_width
}

fn suffix_contains_comments(suffix: &Suffix) -> bool {
Expand Down Expand Up @@ -1243,7 +1254,11 @@ pub fn format_function_declaration(
let formatted_function_name = format_function_name(ctx, function_declaration.name(), shape)
.update_trailing_trivia(FormatTriviaType::Append(function_definition_trivia));

let shape = shape + (9 + strip_trivia(&formatted_function_name).to_string().len()); // 9 = "function "
let shape = shape
+ (9 + strip_trivia(&formatted_function_name)
.to_string()
.chars()
.count()); // 9 = "function "
let function_body = format_function_body(ctx, function_declaration.body(), shape)
.update_trailing_trivia(FormatTriviaType::Append(trailing_trivia));

Expand Down Expand Up @@ -1283,7 +1298,7 @@ pub fn format_local_function(
let formatted_name = format_token_reference(ctx, local_function.name(), shape)
.update_trailing_trivia(FormatTriviaType::Append(function_definition_trivia));

let shape = shape + (6 + 9 + strip_trivia(&formatted_name).to_string().len()); // 6 = "local ", 9 = "function "
let shape = shape + (6 + 9 + strip_trivia(&formatted_name).to_string().chars().count()); // 6 = "local ", 9 = "function "
let function_body = format_function_body(ctx, local_function.body(), shape)
.update_trailing_trivia(FormatTriviaType::Append(trailing_trivia));

Expand Down Expand Up @@ -1327,7 +1342,7 @@ pub fn format_const_function(
let formatted_name = format_token_reference(ctx, const_function.name(), shape)
.update_trailing_trivia(FormatTriviaType::Append(function_definition_trivia));

let shape = shape + (6 + 9 + strip_trivia(&formatted_name).to_string().len()); // 6 = "const ", 9 = "function "
let shape = shape + (6 + 9 + strip_trivia(&formatted_name).to_string().chars().count()); // 6 = "const ", 9 = "function "
let function_body = format_function_body(ctx, const_function.body(), shape)
.update_trailing_trivia(FormatTriviaType::Append(trailing_trivia));

Expand All @@ -1350,12 +1365,13 @@ pub fn format_method_call(
let (colon_token, name) =
process_dot_name(ctx, method_call.colon_token(), method_call.name(), shape);
#[allow(unused_mut)]
let mut shape = shape + (colon_token.to_string().len() + name.to_string().len());
let mut shape =
shape + (colon_token.to_string().chars().count() + name.to_string().chars().count());

#[cfg(feature = "luau")]
let type_instantiation = method_call.type_instantiation().map(|ti| {
let formatted = format_type_instantiation(ctx, ti, shape);
shape = shape + strip_trivia(&formatted).to_string().len();
shape = shape + strip_trivia(&formatted).to_string().chars().count();
formatted
});

Expand Down
Loading
Loading