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
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ jobs:
- name: Test (LuaJIT)
run: cargo test --features luajit

test_cfxlua:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test (CfxLua)
run: cargo test --features cfxlua

test_all_features:
runs-on: ubuntu-latest
steps:
Expand All @@ -70,7 +77,7 @@ jobs:
- name: Test Build (wasm)
run: |
rustup target add wasm32-unknown-unknown
cargo check --target wasm32-unknown-unknown --features luau,lua52,lua53,lua54,luajit
cargo check --target wasm32-unknown-unknown --features luau,lua52,lua53,lua54,luajit,cfxlua

test_wasm_build:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
sudo apt install ${{ matrix.linker }}

- name: Build Binary (All features)
run: cargo build --verbose --locked --release --features lua52,lua53,lua54,luau,luajit --target ${{ matrix.cargo-target }}
run: cargo build --verbose --locked --release --features lua52,lua53,lua54,luau,luajit,cfxlua --target ${{ matrix.cargo-target }}
env:
CARGO_TARGET_DIR: output

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-cases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
CI=false cargo insta test --features lua53 --accept
CI=false cargo insta test --features lua54 --accept
CI=false cargo insta test --features luau --accept
CI=false cargo insta test --features cfxlua --accept

- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Luau: Added support for parsing user-defined type functions ([#938](https://github.com/JohnnyMorganz/StyLua/issues/938))
- Luau: Added support for parsing attributes (`@native` / `@deprecated`) on functions
- Added support for CfxLua (FiveM) syntax formatting. This is available with `syntax = "cfxlua"` ([#855](https://github.com/JohnnyMorganz/StyLua/issues/855))

### Fixed

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ lua52 = ["full_moon/lua52"]
lua53 = ["lua52", "full_moon/lua53"]
lua54 = ["lua53", "full_moon/lua54"]
luajit = ["full_moon/luajit"]
cfxlua = ["lua54", "full_moon/cfxlua"]
editorconfig = ["ec4rs"]

[dependencies]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</h1>
</div>

A deterministic code formatter for Lua 5.1, 5.2, 5.3, 5.4, LuaJIT and [Luau](https://luau.org/), built using [full-moon](https://github.com/Kampfkarren/full-moon).
A deterministic code formatter for Lua 5.1, 5.2, 5.3, 5.4, LuaJIT, [Luau](https://luau.org/) and [CfxLua/FiveM Lua](https://docs.fivem.net/docs/scripting-manual/runtimes/lua/), 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 Down Expand Up @@ -285,7 +285,7 @@ StyLua only offers the following options:

| Option | Default | Description |
| ---------------------------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `syntax` | `All` | Specify a disambiguation for the style of Lua syntax being formatted. Possible options: `All` (default), `Lua51`, `Lua52`, `Lua53`, `Lua54`, `LuaJIT`, `Luau` |
| `syntax` | `All` | Specify a disambiguation for the style of Lua syntax being formatted. Possible options: `All` (default), `Lua51`, `Lua52`, `Lua53`, `Lua54`, `LuaJIT`, `Luau`, `CfxLua` |
| `column_width` | `120` | Approximate line length for printing. Used as a guide for line wrapping - this is not a hard requirement: lines may fall under or over the limit. |
| `line_endings` | `Unix` | Line endings type. Possible options: `Unix` (LF) or `Windows` (CRLF) |
| `indent_type` | `Tabs` | Indent type. Possible options: `Tabs` or `Spaces` |
Expand Down
1 change: 1 addition & 0 deletions src/cli/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ convert_enum!(LuaVersion, ArgLuaVersion, {
#[cfg(feature = "lua54")] Lua54,
#[cfg(feature = "luau")] Luau,
#[cfg(feature = "luajit")] LuaJIT,
#[cfg(feature = "cfxlua")] CfxLua,
});

convert_enum!(LineEndings, ArgLineEndings, {
Expand Down
11 changes: 11 additions & 0 deletions src/formatters/assignment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[cfg(feature = "luau")]
use full_moon::ast::luau::TypeSpecifier;
#[cfg(feature = "cfxlua")]
use full_moon::tokenizer::Symbol;
use full_moon::tokenizer::{Token, TokenReference};
use full_moon::{
ast::{
Expand Down Expand Up @@ -451,6 +453,15 @@ pub fn format_local_assignment_no_trivia(
Some(1),
);
let mut equal_token = fmt_symbol!(ctx, assignment.equal_token().unwrap(), " = ", shape);

// In CfxLua, the equal token could actually be an "in" token for table unpacking
#[cfg(feature = "cfxlua")]
if let TokenType::Symbol { symbol: Symbol::In } =
assignment.equal_token().unwrap().token_type()
{
equal_token = fmt_symbol!(ctx, assignment.equal_token().unwrap(), " in ", shape);
}

let mut expr_list = format_punctuated(
ctx,
assignment.expressions(),
Expand Down
60 changes: 60 additions & 0 deletions src/formatters/compound_assignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::{
context::{create_indent_trivia, create_newline_trivia, Context},
fmt_op, fmt_symbol,
formatters::{
expression::{format_expression, format_var},
trivia::{
strip_leading_trivia, FormatTriviaType, UpdateLeadingTrivia, UpdateTrailingTrivia,
},
},
shape::Shape,
};
use full_moon::{
ast::{CompoundAssignment, CompoundOp},
tokenizer::TokenReference,
};

pub fn format_compound_op(ctx: &Context, compound_op: &CompoundOp, shape: Shape) -> CompoundOp {
fmt_op!(ctx, CompoundOp, compound_op, shape, {
PlusEqual = " += ",
MinusEqual = " -= ",
StarEqual = " *= ",
SlashEqual = " /= ",
#[cfg(feature = "luau")]
PercentEqual = " %= ",
CaretEqual = " ^= ",
#[cfg(feature = "luau")]
TwoDotsEqual = " ..= ",
#[cfg(feature = "luau")]
DoubleSlashEqual = " //= ",
#[cfg(feature = "cfxlua")]
DoubleLessThanEqual = " <<= ",
#[cfg(feature = "cfxlua")]
DoubleGreaterThanEqual = " >>= ",
#[cfg(feature = "cfxlua")]
AmpersandEqual = " &= ",
#[cfg(feature = "cfxlua")]
PipeEqual = " |= ",
}, |other| panic!("unknown node {:?}", other))
}

pub fn format_compound_assignment(
ctx: &Context,
compound_assignment: &CompoundAssignment,
shape: Shape,
) -> CompoundAssignment {
// Calculate trivia
let leading_trivia = vec![create_indent_trivia(ctx, shape)];
let trailing_trivia = vec![create_newline_trivia(ctx)];

let lhs = format_var(ctx, compound_assignment.lhs(), shape)
.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());

let rhs = format_expression(ctx, compound_assignment.rhs(), shape)
.update_trailing_trivia(FormatTriviaType::Append(trailing_trivia));

CompoundAssignment::new(lhs, compound_operator, rhs)
}
2 changes: 2 additions & 0 deletions src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ fn table_constructor_contains_nested_function(table_constructor: &TableConstruct
Field::ExpressionKey { key, value, .. } => {
contains_nested_function(key) || contains_nested_function(value)
}
#[cfg(feature = "cfxlua")]
Field::SetConstructor { .. } => false,
Field::NameKey { value, .. } => contains_nested_function(value),
other => unreachable!("unknown node {:?}", other),
})
Expand Down
Loading