diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ac6dc18..6e5e8747 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/formatters/assignment.rs b/src/formatters/assignment.rs index 3983e7c4..f3f8989a 100644 --- a/src/formatters/assignment.rs +++ b/src/formatters/assignment.rs @@ -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); @@ -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()) }); } @@ -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 @@ -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); @@ -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() { @@ -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( @@ -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); diff --git a/src/formatters/block.rs b/src/formatters/block.rs index 12d0ef5d..c3bac5c9 100644 --- a/src/formatters/block.rs +++ b/src/formatters/block.rs @@ -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) } }; diff --git a/src/formatters/compound_assignment.rs b/src/formatters/compound_assignment.rs index fdeb0710..cd791e96 100644 --- a/src/formatters/compound_assignment.rs +++ b/src/formatters/compound_assignment.rs @@ -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)); diff --git a/src/formatters/expression.rs b/src/formatters/expression.rs index 6291eedd..2e8fe83c 100644 --- a/src/formatters/expression.rs +++ b/src/formatters/expression.rs @@ -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, @@ -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, @@ -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)); @@ -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); @@ -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(), } } @@ -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 => ( @@ -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 { @@ -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, @@ -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, @@ -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, diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index ab3244e1..acdb8be7 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -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) => { @@ -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? @@ -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()); } } @@ -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) { @@ -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 @@ -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) @@ -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( @@ -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))]); @@ -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 { @@ -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)); @@ -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)); @@ -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)); @@ -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 }); diff --git a/src/formatters/luau.rs b/src/formatters/luau.rs index d6bc3328..6e8fca85 100644 --- a/src/formatters/luau.rs +++ b/src/formatters/luau.rs @@ -305,9 +305,9 @@ fn format_type_info_internal( .map(|token_reference| format_token_reference(ctx, token_reference, access_shape)); // Count the modifier and its required separator without trivia. - let access_shape_increment = access - .as_ref() - .map_or(0, |token| strip_trivia(token).to_string().len() + 1); + let access_shape_increment = access.as_ref().map_or(0, |token| { + strip_trivia(token).to_string().chars().count() + 1 + }); let (table_type, new_type_info) = if contains_comments { (TableType::MultiLine, None) @@ -423,8 +423,11 @@ fn format_type_info_internal( .add_width( PAREN_LEN * 2 + ARROW_LEN - + arguments.to_string().len() - + strip_trailing_trivia(&**return_type).to_string().len(), + + arguments.to_string().chars().count() + + strip_trailing_trivia(&**return_type) + .to_string() + .chars() + .count(), ) .over_budget(); @@ -442,7 +445,7 @@ fn format_type_info_internal( } else { let parentheses = format_contained_span(ctx, parentheses, shape); let arguments = format_punctuated(ctx, arguments, shape + 1, format_type_argument); - let shape = shape + (PAREN_LEN * 2 + arguments.to_string().len()); + let shape = shape + (PAREN_LEN * 2 + arguments.to_string().chars().count()); (parentheses, arguments, shape) }; @@ -535,7 +538,7 @@ fn format_type_info_internal( let type_info = Box::new(format_indexed_type_info( ctx, type_info, - shape + (strip_trivia(&module).to_string().len() + 1), // 1 = "." + shape + (strip_trivia(&module).to_string().chars().count() + 1), // 1 = "." )); TypeInfo::Module { module, @@ -963,7 +966,7 @@ fn format_type_argument(ctx: &Context, type_argument: &TypeArgument, shape: Shap let shape = shape + name.as_ref().map_or(0, |(name, _)| { - strip_trivia(name).to_string().len() + COLON_LEN + strip_trivia(name).to_string().chars().count() + COLON_LEN }); let type_info = format_hangable_type_info(ctx, type_argument.type_info(), shape, 1); @@ -995,9 +998,9 @@ pub fn format_type_field( TokenType::spaces(1), )])) }); - let access_shape_increment = access - .as_ref() - .map_or(0, |token| strip_leading_trivia(token).to_string().len() + 1); + let access_shape_increment = access.as_ref().map_or(0, |token| { + strip_leading_trivia(token).to_string().chars().count() + 1 + }); let key = format_type_field_key( ctx, @@ -1010,7 +1013,9 @@ pub fn format_type_field( shape, ); let colon_token = fmt_symbol!(ctx, type_field.colon_token(), ": ", shape); - let shape = shape + access_shape_increment + (strip_leading_trivia(&key).to_string().len() + 2); + let shape = shape + + access_shape_increment + + (strip_leading_trivia(&key).to_string().chars().count() + 2); let mut value = format_type_info(ctx, type_field.value(), shape); @@ -1248,7 +1253,7 @@ fn format_type_declaration( let shape = shape + TYPE_TOKEN_LENGTH; let type_name = format_token_reference(ctx, type_declaration.type_name(), shape); - let shape = shape + type_name.to_string().len(); + let shape = shape + type_name.to_string().chars().count(); let generics = type_declaration .generics() @@ -1399,7 +1404,7 @@ fn format_type_function( let shape = shape + (TYPE_TOKEN_LENGTH + FUNCTION_TOKEN_LENGTH - + strip_trivia(&function_name).to_string().len()); + + strip_trivia(&function_name).to_string().chars().count()); let function_body = format_function_body(ctx, type_function.function_body(), shape) .update_trailing_trivia(FormatTriviaType::Append(trailing_trivia)); diff --git a/src/formatters/stmt.rs b/src/formatters/stmt.rs index 1f2b2a6d..0887e87c 100644 --- a/src/formatters/stmt.rs +++ b/src/formatters/stmt.rs @@ -172,9 +172,9 @@ pub fn format_generic_for(ctx: &Context, generic_for: &GenericFor, shape: Shape) #[cfg(feature = "luau")] let singleline_shape = singleline_shape + type_specifiers.iter().fold(0, |acc, x| { - acc + x - .as_ref() - .map_or(0, |type_specifier| type_specifier.to_string().len()) + acc + x.as_ref().map_or(0, |type_specifier| { + type_specifier.to_string().chars().count() + }) }); let require_names_multiline = trivia_util::contains_comments(generic_for.names()) @@ -415,8 +415,13 @@ fn format_else_if(ctx: &Context, else_if_node: &ElseIf, shape: Shape) -> ElseIf let singleline_then_token = fmt_symbol!(ctx, else_if_node.then_token(), " then", shape); // Determine if we need to hang the condition - let singleline_shape = - shape + (7 + 5 + binding_width + strip_trivia(&singleline_condition).to_string().len()); // 7 = "elseif ", 5 = " then" + let singleline_shape = shape + + (7 + 5 + + binding_width + + strip_trivia(&singleline_condition) + .to_string() + .chars() + .count()); // 7 = "elseif ", 5 = " then" let require_multiline_expression = singleline_shape.over_budget() || else_if_node .else_if_token() @@ -568,7 +573,10 @@ pub fn format_if(ctx: &Context, if_node: &If, shape: Shape) -> If { + (IF_LEN + THEN_LEN + binding_width - + strip_trivia(&singleline_condition).to_string().len()); + + strip_trivia(&singleline_condition) + .to_string() + .chars() + .count()); let require_multiline_expression = singleline_shape.over_budget() || if_node.if_token().has_trailing_comments(CommentSearch::All) || if_node @@ -629,7 +637,7 @@ pub fn format_if(ctx: &Context, if_node: &If, shape: Shape) -> If { // See if it fits under the column width. If it does, bail early and return this singleline if if !shape - .add_width(strip_trivia(&singleline_if).to_string().len()) + .add_width(strip_trivia(&singleline_if).to_string().chars().count()) .over_budget() { return singleline_if; @@ -838,7 +846,7 @@ pub fn format_repeat_block(ctx: &Context, repeat_block: &Repeat, shape: Shape) - let condition = remove_condition_parentheses(repeat_block.until().to_owned()); // Determine if we need to hang the condition - let singleline_shape = shape + (6 + strip_trivia(&condition).to_string().len()); // 6 = "until " + let singleline_shape = shape + (6 + strip_trivia(&condition).to_string().chars().count()); // 6 = "until " let require_multiline_expression = singleline_shape.over_budget() || condition.has_inline_comments(); @@ -874,7 +882,12 @@ pub fn format_while_block(ctx: &Context, while_block: &While, shape: Shape) -> W let singleline_do_token = fmt_symbol!(ctx, while_block.do_token(), " do", shape); // Determine if we need to hang the condition - let singleline_shape = shape + (6 + 3 + strip_trivia(&singleline_condition).to_string().len()); // 6 = "while ", 3 = " do" + let singleline_shape = shape + + (6 + 3 + + strip_trivia(&singleline_condition) + .to_string() + .chars() + .count()); // 6 = "while ", 3 = " do" let require_multiline_expression = singleline_shape.over_budget() || while_block .while_token() diff --git a/src/formatters/table.rs b/src/formatters/table.rs index da949bee..09e0f733 100644 --- a/src/formatters/table.rs +++ b/src/formatters/table.rs @@ -226,7 +226,7 @@ fn format_field( let (equal, value) = if value.has_leading_comments(CommentSearch::Single) { hang_field_value_at_equals_due_to_comments(ctx, &equal, value, shape) } else { - let shape = shape + (strip_trivia(&key).to_string().len() + 3); // 3 = " = " + let shape = shape + (strip_trivia(&key).to_string().chars().count() + 3); // 3 = " = " let value = format_field_expression_value(ctx, value, shape); (equal, value) }; @@ -352,7 +352,7 @@ where let formatted_punctuation = match current_fields.peek() { Some(_) => { // Have more elements still to go - shape = shape + (formatted_field.to_string().len() + 2); // 2 = ", " + shape = shape + (formatted_field.to_string().chars().count() + 2); // 2 = ", " match punctuation { Some(punctuation) => Some(fmt_symbol!(ctx, punctuation, ", ", shape)), None => Some(TokenReference::symbol(", ").unwrap()), diff --git a/src/shape.rs b/src/shape.rs index 7ac6164c..b5406444 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -214,7 +214,7 @@ impl Shape { pub fn take_first_line(&self, item: &T) -> Shape { let string = format!("{item}"); let mut lines = string.lines(); - let width = lines.next().unwrap_or("").len(); + let width = lines.next().unwrap_or("").chars().count(); self.add_width(width) } @@ -230,10 +230,10 @@ impl Shape { // Check if we have any more lines remaining if lines.count() > 0 { // Reset the shape and add the last line - self.reset().add_width(last_item.len()) + self.reset().add_width(last_item.chars().count()) } else { // Continue adding to the current shape - self.add_width(last_item.len()) + self.add_width(last_item.chars().count()) } } @@ -272,7 +272,7 @@ impl Shape { lines.enumerate().any(|(idx, line)| { let shape = if idx == 0 { *self } else { self.reset() }; - shape.add_width(line.len()).over_budget() + shape.add_width(line.chars().count()).over_budget() }) } } diff --git a/tests/inputs/multibyte-line-width.lua b/tests/inputs/multibyte-line-width.lua new file mode 100644 index 00000000..06d0b32c --- /dev/null +++ b/tests/inputs/multibyte-line-width.lua @@ -0,0 +1,4 @@ +-- https://github.com/JohnnyMorganz/StyLua/issues/577 +string.rep("ыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыы", 10) +local message = "ыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыы" .. suffix +local words = { "ыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыы", "ыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыы" } diff --git a/tests/snapshots/tests__standard@multibyte-line-width.lua.snap b/tests/snapshots/tests__standard@multibyte-line-width.lua.snap new file mode 100644 index 00000000..9ad01fc9 --- /dev/null +++ b/tests/snapshots/tests__standard@multibyte-line-width.lua.snap @@ -0,0 +1,10 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Lua51)" +input_file: tests/inputs/multibyte-line-width.lua +--- +-- https://github.com/JohnnyMorganz/StyLua/issues/577 +string.rep("ыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыы", 10) +local message = "ыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыы" .. suffix +local words = { "ыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыы", "ыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыы" } +