Skip to content
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
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ jobs:
- name: Test (Lua 5.2)
run: cargo test --features lua52

- name: Test (Lua 5.3)
run: cargo test --features lua53

- name: Test (Lua 5.4)
run: cargo test --features lua54

- name: Test Build (wasm)
run: |
rustup target add wasm32-unknown-unknown
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added support for Lua 5.3, gated behind the `lua53` feature flag ([#534](https://github.com/JohnnyMorganz/StyLua/issues/534))
- Added support for Lua 5.4, gated behind the `lua54` feature flag ([#533](https://github.com/JohnnyMorganz/StyLua/issues/533))
- Added `--allow-hidden` flag to allow entering and formatting hidden files/directories ([#562](https://github.com/JohnnyMorganz/StyLua/issues/562))
- Added `--output-format=summary` which can be used with `--check` to output a summary of the list of files not correctly formatted ([#573](https://github.com/JohnnyMorganz/StyLua/issues/573))

Expand Down
66 changes: 34 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ bench = false
default = []
luau = ["full_moon/roblox"]
lua52 = ["full_moon/lua52"]
lua53 = ["lua52", "full_moon/lua53"]
lua54 = ["lua53", "full_moon/lua54"]

[dependencies]
anyhow = "1.0.53"
Expand All @@ -34,7 +36,7 @@ clap = { version = "3.1.6", features = ["derive"] }
console = "0.15.0"
crossbeam-channel = "0.5.4"
env_logger = { version = "0.9.0", default-features = false }
full_moon = "0.15.1"
full_moon = "0.16.0"
globset = "0.4.8"
ignore = "0.4.18"
lazy_static = "1.4.0"
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</h1>
</div>

An opinionated code formatter for Lua 5.1, Lua 5.2 and [Luau](https://roblox.github.io/luau/), built using [full-moon](https://github.com/Kampfkarren/full-moon).
An opinionated code formatter for Lua 5.1, 5.2, 5.3, 5.4 and [Luau](https://roblox.github.io/luau/), built using [full-moon](https://github.com/Kampfkarren/full-moon).
StyLua is inspired by the likes of [prettier](https://github.com/prettier/prettier), it parses your Lua codebase, and prints it back out from scratch,
enforcing a consistent code style.

Expand All @@ -19,18 +19,20 @@ There are multiple ways to install StyLua:

Pre-built binaries are available on the [GitHub Releases Page](https://github.com/JohnnyMorganz/StyLua/releases).

By default, these are built with **both Luau and Lua 5.2 features enabled**, to cover all possible codebases.
By default, these are built with **all syntax variants enabled (Lua 5.2, 5.3, 5.4 and Luau)**, to cover all possible codebases.
If you would like to format a specific Lua version only, see [installing from crates.io](#from-cratesio).

### From Crates.io

If you have [Rust](https://www.rust-lang.org/) installed, you can install StyLua using cargo.
By default, this builds for just Lua 5.1.
You can pass the `--features <flag>` argument to build for Lua 5.2 (`lua52`) or Luau (`luau`)
You can pass the `--features <flag>` argument to build for Lua 5.2 (`lua52`), Lua 5.3 (`lua53`), Lua 5.4 (`lua54`) or Luau (`luau`)

```sh
cargo install stylua
cargo install stylua --features lua52
cargo install stylua --features lua53
cargo install stylua --features lua54
cargo install stylua --features luau
```

Expand Down
32 changes: 27 additions & 5 deletions src/formatters/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use full_moon::{
tokenizer::TokenType,
};

#[cfg(feature = "lua54")]
use crate::formatters::lua54::format_attribute;
#[cfg(feature = "luau")]
use crate::formatters::luau::format_type_specifier;
use crate::{
Expand Down Expand Up @@ -386,13 +388,21 @@ fn format_local_no_assignment(
Some(1),
);

#[cfg(feature = "lua54")]
let attributes = assignment
.attributes()
.map(|x| x.map(|attribute| format_attribute(ctx, attribute, shape)))
.collect();

#[cfg(feature = "luau")]
let type_specifiers: Vec<Option<TypeSpecifier>> = assignment
.type_specifiers()
.map(|x| x.map(|type_specifier| format_type_specifier(ctx, type_specifier, shape)))
.collect();

let local_assignment = LocalAssignment::new(name_list);
#[cfg(feature = "lua54")]
let local_assignment = local_assignment.with_attributes(attributes);
#[cfg(feature = "luau")]
let local_assignment = local_assignment.with_type_specifiers(type_specifiers);

Expand Down Expand Up @@ -439,21 +449,31 @@ pub fn format_local_assignment_no_trivia(
format_expression,
);

#[cfg(feature = "lua54")]
let attributes: Vec<Option<_>> = assignment
.attributes()
.map(|x| x.map(|attribute| format_attribute(ctx, attribute, shape)))
.collect();

#[cfg(feature = "luau")]
let type_specifiers: Vec<Option<TypeSpecifier>> = assignment
.type_specifiers()
.map(|x| x.map(|type_specifier| format_type_specifier(ctx, type_specifier, shape)))
.collect();
let type_specifier_len;
#[cfg(feature = "luau")]

#[allow(unused_mut)]
let mut type_specifier_len = 0;
#[cfg(feature = "lua54")]
{
type_specifier_len = type_specifiers.iter().fold(0, |acc, x| {
type_specifier_len += attributes.iter().fold(0, |acc, x| {
acc + x.as_ref().map_or(0, |y| y.to_string().len())
});
}
#[cfg(not(feature = "luau"))]
#[cfg(feature = "luau")]
{
type_specifier_len = 0;
type_specifier_len += type_specifiers.iter().fold(0, |acc, x| {
acc + x.as_ref().map_or(0, |y| y.to_string().len())
});
}

// Test the assignment to see if its over width
Expand Down Expand Up @@ -483,6 +503,8 @@ pub fn format_local_assignment_no_trivia(
}

let local_assignment = LocalAssignment::new(name_list);
#[cfg(feature = "lua54")]
let local_assignment = local_assignment.with_attributes(attributes);
#[cfg(feature = "luau")]
let local_assignment = local_assignment.with_type_specifiers(type_specifiers);
local_assignment
Expand Down
29 changes: 26 additions & 3 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ use crate::{

#[macro_export]
macro_rules! fmt_op {
($ctx:expr, $enum:ident, $value:ident, $shape:expr, { $($operator:ident = $output:expr,)+ }) => {
($ctx:expr, $enum:ident, $value:ident, $shape:expr, { $($(#[$inner:meta])* $operator:ident = $output:expr,)+ }, $other:expr) => {
match $value {
$(
$(#[$inner])*
$enum::$operator(token) => $enum::$operator(fmt_symbol!($ctx, token, $output, $shape)),
)+
other => panic!("unknown node {:?}", other),
other => $other(other),
}
};
}
Expand Down Expand Up @@ -79,6 +80,26 @@ pub fn format_binop(ctx: &Context, binop: &BinOp, shape: Shape) -> BinOp {
TildeEqual = " ~= ",
TwoDots = " .. ",
TwoEqual = " == ",
#[cfg(feature = "lua53")]
Ampersand = " & ",
#[cfg(feature = "lua53")]
DoubleSlash = " // ",
#[cfg(feature = "lua53")]
DoubleLessThan = " << ",
#[cfg(feature = "lua53")]
Pipe = " | ",
#[cfg(feature = "lua53")]
Tilde = " ~ ",
}, |other: &BinOp| match other {
#[cfg(feature = "lua53")]
BinOp::DoubleGreaterThan(token) => BinOp::DoubleGreaterThan(
format_token_reference(ctx, token, shape)
.update_trivia(
FormatTriviaType::Append(vec![Token::new(TokenType::spaces(1))]),
FormatTriviaType::Append(vec![Token::new(TokenType::spaces(1))])
)
),
other => panic!("unknown node {:?}", other)
})
}

Expand Down Expand Up @@ -793,7 +814,9 @@ pub fn format_unop(ctx: &Context, unop: &UnOp, shape: Shape) -> UnOp {
Minus = "-",
Not = "not ",
Hash = "#",
})
#[cfg(feature = "lua53")]
Tilde = "~",
}, |other| panic!("unknown node {:?}", other))
}

/// Pushes a [`BinOp`] onto a newline, and indent its depending on indent_level.
Expand Down
21 changes: 21 additions & 0 deletions src/formatters/lua54.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::{
context::Context,
formatters::{
general::{format_contained_span, format_token_reference},
trivia::{FormatTriviaType, UpdateLeadingTrivia},
},
shape::Shape,
};
use full_moon::{
ast::lua54::Attribute,
tokenizer::{Token, TokenType},
};

pub fn format_attribute(ctx: &Context, attribute: &Attribute, shape: Shape) -> Attribute {
let brackets = format_contained_span(ctx, attribute.brackets(), shape).update_leading_trivia(
FormatTriviaType::Append(vec![Token::new(TokenType::spaces(1))]),
);
let name = format_token_reference(ctx, attribute.name(), shape);

Attribute::new(name).with_brackets(brackets)
}
2 changes: 1 addition & 1 deletion src/formatters/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn format_compound_op(ctx: &Context, compound_op: &CompoundOp, shape: Shape)
PercentEqual = " %= ",
CaretEqual = " ^= ",
TwoDotsEqual = " ..= ",
})
}, |other| panic!("unknown node {:?}", other))
}

pub fn format_compound_assignment(
Expand Down
2 changes: 2 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub mod expression;
pub mod functions;
#[cfg(feature = "lua52")]
pub mod lua52;
#[cfg(feature = "lua54")]
pub mod lua54;
#[cfg(feature = "luau")]
pub mod luau;
pub mod stmt;
Expand Down
Loading