Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hang level to hanging if expression #599

Merged
merged 8 commits into from
Oct 15, 2022
Merged
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

- Fix incorrect indentation level used for hanging expressions in if expression syntax ([#596](https://github.com/JohnnyMorganz/StyLua/issues/596))

## [0.15.1] - 2022-09-22

### Fixed
Expand Down
5 changes: 3 additions & 2 deletions src/formatters/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ use crate::{
};

/// Calculates the hanging level to use when hanging an expression.
/// By default, we indent one further, but we DO NOT want to do this if the expression is just parentheses
/// By default, we indent one further, but we DO NOT want to do this if the expression is just parentheses (or a unary operation on them)
/// https://github.com/JohnnyMorganz/StyLua/issues/274
fn calculate_hang_level(expression: &Expression) -> Option<usize> {
pub fn calculate_hang_level(expression: &Expression) -> Option<usize> {
match expression {
Expression::Value { value, .. } => match **value {
Value::ParenthesesExpression(_) => None,
_ => Some(1),
},
Expression::UnaryOperator { expression, .. } => calculate_hang_level(expression),
_ => Some(1),
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use full_moon::{
use std::boxed::Box;

#[cfg(feature = "luau")]
use crate::formatters::{luau::format_type_assertion, stmt::remove_condition_parentheses};
use crate::formatters::{
assignment::calculate_hang_level, luau::format_type_assertion,
stmt::remove_condition_parentheses,
};
use crate::{
context::{create_indent_trivia, create_newline_trivia, Context},
fmt_symbol,
Expand Down Expand Up @@ -513,15 +516,16 @@ fn format_token_expression_sequence(
true => match newline_after_token {
true => {
let shape = shape.reset().increment_additional_indent();
hang_expression(ctx, expression, shape, None).update_leading_trivia(
FormatTriviaType::Append(vec![create_indent_trivia(ctx, shape)]),
)
hang_expression(ctx, expression, shape, calculate_hang_level(expression))
.update_leading_trivia(FormatTriviaType::Append(vec![create_indent_trivia(
ctx, shape,
)]))
}
false => hang_expression(
ctx,
expression,
shape.add_width(token_width + SPACE_LEN),
None,
calculate_hang_level(expression),
),
},
false => formatted_expression,
Expand Down
40 changes: 40 additions & 0 deletions tests/inputs-luau/if-expression-6.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/596rr
local function xyzzy()
return hagCoding.open
.. "<"
.. type_
.. (if id10t(hintedProps)
then hagCoding.close .. hintedProps .. config.flinchingOuter .. indentation .. hagCoding.open
else hintedProps)
.. (if id10t(hintedChildren)
then ">"
.. hagCoding.close
.. hintedChildren
.. config.flinchingOuter
.. indentation
.. hagCoding.open
.. "</"
.. type_
else (if id10t(hintedProps) and not id10t(config.min) then "" else " ") .. "/")
.. ">"
.. hagCoding.close
end

-- https://github.com/JohnnyMorganz/StyLua/issues/596#issuecomment-1275547227
local function het(xyzzy: Sirius_InscribeBlock): boolean
local ref = getState()
local hasFeaturedTeats, teatNamePattern = ref.hasFeaturedTeats, ref.teatNamePattern
return Array.some(inscribeBlock.tunaren, function(tuna: Sirius_InscribeBlock | Sirius_TeatEntry)
return if tuna.type == "inscribeBlock"
then hasEnabledTeat(tuna)
else
not (
tuna.mode == "soot"
or (hasFeaturedTeats and tuna.mode ~= "moot")
or (
teatNamePattern
and not teatNamePattern:teat(getTeatID(tuna :: Sirius_TeatEntry))
)
)
end)
end
14 changes: 14 additions & 0 deletions tests/inputs/hang-parens-assign-no-indent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,17 @@ local test, test2 = foo and bar or baz, (
+ long_variable_name
+ long_variable_name
)

-- Negated Assigns
local test = not (
long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
)
41 changes: 41 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: tests/tests.rs
expression: format(&contents)
---
-- https://github.com/JohnnyMorganz/StyLua/issues/596rr
local function xyzzy()
return hagCoding.open
.. "<"
.. type_
.. (if id10t(hintedProps)
then hagCoding.close .. hintedProps .. config.flinchingOuter .. indentation .. hagCoding.open
else hintedProps)
.. (if id10t(hintedChildren)
then ">"
.. hagCoding.close
.. hintedChildren
.. config.flinchingOuter
.. indentation
.. hagCoding.open
.. "</"
.. type_
else (if id10t(hintedProps) and not id10t(config.min) then "" else " ") .. "/")
.. ">"
.. hagCoding.close
end

-- https://github.com/JohnnyMorganz/StyLua/issues/596#issuecomment-1275547227
local function het(xyzzy: Sirius_InscribeBlock): boolean
local ref = getState()
local hasFeaturedTeats, teatNamePattern = ref.hasFeaturedTeats, ref.teatNamePattern
return Array.some(inscribeBlock.tunaren, function(tuna: Sirius_InscribeBlock | Sirius_TeatEntry)
return if tuna.type == "inscribeBlock"
then hasEnabledTeat(tuna)
else not (
tuna.mode == "soot"
or (hasFeaturedTeats and tuna.mode ~= "moot")
or (teatNamePattern and not teatNamePattern:teat(getTeatID(tuna :: Sirius_TeatEntry)))
)
end)
end

16 changes: 14 additions & 2 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: tests/tests.rs
assertion_line: 12
expression: format(&contents)

---
-- https://github.com/JohnnyMorganz/StyLua/issues/274
local tbl = {
Expand Down Expand Up @@ -80,3 +78,17 @@ local test, test2 =
+ long_variable_name
)

-- Negated Assigns
local test = not (
long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
+ long_variable_name
)