Skip to content

Commit fb9119b

Browse files
Support Cfx Lua Syntax (#972)
* start with cfxlua support * fix: enhance CFXLua support for backtick string literals in format_token function * CFXLua -> CfxLua * Add tests * Add CfxLua to CI * Move compound op formatting to generic location * Handle set constructor field * Extract out string literal handling * Ensure cfxlua is enabled in "All" mode, and fix symbol parsing * Update snapshots * Handle in unpacking * Update snapshot * Formatting * Update changelog and readme * Fix compilation * Enable compound op for cfxlua --------- Co-authored-by: JohnnyMorganz <[email protected]>
1 parent 0aa2b48 commit fb9119b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+422
-124
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ jobs:
5656
- name: Test (LuaJIT)
5757
run: cargo test --features luajit
5858

59+
test_cfxlua:
60+
runs-on: ubuntu-latest
61+
steps:
62+
- uses: actions/checkout@v4
63+
- name: Test (CfxLua)
64+
run: cargo test --features cfxlua
65+
5966
test_all_features:
6067
runs-on: ubuntu-latest
6168
steps:
@@ -70,7 +77,7 @@ jobs:
7077
- name: Test Build (wasm)
7178
run: |
7279
rustup target add wasm32-unknown-unknown
73-
cargo check --target wasm32-unknown-unknown --features luau,lua52,lua53,lua54,luajit
80+
cargo check --target wasm32-unknown-unknown --features luau,lua52,lua53,lua54,luajit,cfxlua
7481
7582
test_wasm_build:
7683
runs-on: ubuntu-latest

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
sudo apt install ${{ matrix.linker }}
6565
6666
- name: Build Binary (All features)
67-
run: cargo build --verbose --locked --release --features lua52,lua53,lua54,luau,luajit --target ${{ matrix.cargo-target }}
67+
run: cargo build --verbose --locked --release --features lua52,lua53,lua54,luau,luajit,cfxlua --target ${{ matrix.cargo-target }}
6868
env:
6969
CARGO_TARGET_DIR: output
7070

.github/workflows/test-cases.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
CI=false cargo insta test --features lua53 --accept
2222
CI=false cargo insta test --features lua54 --accept
2323
CI=false cargo insta test --features luau --accept
24+
CI=false cargo insta test --features cfxlua --accept
2425
2526
- name: Create Pull Request
2627
uses: peter-evans/create-pull-request@v5

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

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

1516
### Fixed
1617

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ lua52 = ["full_moon/lua52"]
3131
lua53 = ["lua52", "full_moon/lua53"]
3232
lua54 = ["lua53", "full_moon/lua54"]
3333
luajit = ["full_moon/luajit"]
34+
cfxlua = ["lua54", "full_moon/cfxlua"]
3435
editorconfig = ["ec4rs"]
3536

3637
[dependencies]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</h1>
88
</div>
99

10-
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).
10+
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).
1111
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,
1212
enforcing a consistent code style.
1313

@@ -285,7 +285,7 @@ StyLua only offers the following options:
285285

286286
| Option | Default | Description |
287287
| ---------------------------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
288-
| `syntax` | `All` | Specify a disambiguation for the style of Lua syntax being formatted. Possible options: `All` (default), `Lua51`, `Lua52`, `Lua53`, `Lua54`, `LuaJIT`, `Luau` |
288+
| `syntax` | `All` | Specify a disambiguation for the style of Lua syntax being formatted. Possible options: `All` (default), `Lua51`, `Lua52`, `Lua53`, `Lua54`, `LuaJIT`, `Luau`, `CfxLua` |
289289
| `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. |
290290
| `line_endings` | `Unix` | Line endings type. Possible options: `Unix` (LF) or `Windows` (CRLF) |
291291
| `indent_type` | `Tabs` | Indent type. Possible options: `Tabs` or `Spaces` |

src/cli/opt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ convert_enum!(LuaVersion, ArgLuaVersion, {
237237
#[cfg(feature = "lua54")] Lua54,
238238
#[cfg(feature = "luau")] Luau,
239239
#[cfg(feature = "luajit")] LuaJIT,
240+
#[cfg(feature = "cfxlua")] CfxLua,
240241
});
241242

242243
convert_enum!(LineEndings, ArgLineEndings, {

src/formatters/assignment.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[cfg(feature = "luau")]
22
use full_moon::ast::luau::TypeSpecifier;
3+
#[cfg(feature = "cfxlua")]
4+
use full_moon::tokenizer::Symbol;
35
use full_moon::tokenizer::{Token, TokenReference};
46
use full_moon::{
57
ast::{
@@ -451,6 +453,15 @@ pub fn format_local_assignment_no_trivia(
451453
Some(1),
452454
);
453455
let mut equal_token = fmt_symbol!(ctx, assignment.equal_token().unwrap(), " = ", shape);
456+
457+
// In CfxLua, the equal token could actually be an "in" token for table unpacking
458+
#[cfg(feature = "cfxlua")]
459+
if let TokenType::Symbol { symbol: Symbol::In } =
460+
assignment.equal_token().unwrap().token_type()
461+
{
462+
equal_token = fmt_symbol!(ctx, assignment.equal_token().unwrap(), " in ", shape);
463+
}
464+
454465
let mut expr_list = format_punctuated(
455466
ctx,
456467
assignment.expressions(),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::{
2+
context::{create_indent_trivia, create_newline_trivia, Context},
3+
fmt_op, fmt_symbol,
4+
formatters::{
5+
expression::{format_expression, format_var},
6+
trivia::{
7+
strip_leading_trivia, FormatTriviaType, UpdateLeadingTrivia, UpdateTrailingTrivia,
8+
},
9+
},
10+
shape::Shape,
11+
};
12+
use full_moon::{
13+
ast::{CompoundAssignment, CompoundOp},
14+
tokenizer::TokenReference,
15+
};
16+
17+
pub fn format_compound_op(ctx: &Context, compound_op: &CompoundOp, shape: Shape) -> CompoundOp {
18+
fmt_op!(ctx, CompoundOp, compound_op, shape, {
19+
PlusEqual = " += ",
20+
MinusEqual = " -= ",
21+
StarEqual = " *= ",
22+
SlashEqual = " /= ",
23+
#[cfg(feature = "luau")]
24+
PercentEqual = " %= ",
25+
CaretEqual = " ^= ",
26+
#[cfg(feature = "luau")]
27+
TwoDotsEqual = " ..= ",
28+
#[cfg(feature = "luau")]
29+
DoubleSlashEqual = " //= ",
30+
#[cfg(feature = "cfxlua")]
31+
DoubleLessThanEqual = " <<= ",
32+
#[cfg(feature = "cfxlua")]
33+
DoubleGreaterThanEqual = " >>= ",
34+
#[cfg(feature = "cfxlua")]
35+
AmpersandEqual = " &= ",
36+
#[cfg(feature = "cfxlua")]
37+
PipeEqual = " |= ",
38+
}, |other| panic!("unknown node {:?}", other))
39+
}
40+
41+
pub fn format_compound_assignment(
42+
ctx: &Context,
43+
compound_assignment: &CompoundAssignment,
44+
shape: Shape,
45+
) -> CompoundAssignment {
46+
// Calculate trivia
47+
let leading_trivia = vec![create_indent_trivia(ctx, shape)];
48+
let trailing_trivia = vec![create_newline_trivia(ctx)];
49+
50+
let lhs = format_var(ctx, compound_assignment.lhs(), shape)
51+
.update_leading_trivia(FormatTriviaType::Append(leading_trivia));
52+
let compound_operator = format_compound_op(ctx, compound_assignment.compound_operator(), shape);
53+
let shape = shape
54+
+ (strip_leading_trivia(&lhs).to_string().len() + compound_operator.to_string().len());
55+
56+
let rhs = format_expression(ctx, compound_assignment.rhs(), shape)
57+
.update_trailing_trivia(FormatTriviaType::Append(trailing_trivia));
58+
59+
CompoundAssignment::new(lhs, compound_operator, rhs)
60+
}

src/formatters/functions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,8 @@ fn table_constructor_contains_nested_function(table_constructor: &TableConstruct
665665
Field::ExpressionKey { key, value, .. } => {
666666
contains_nested_function(key) || contains_nested_function(value)
667667
}
668+
#[cfg(feature = "cfxlua")]
669+
Field::SetConstructor { .. } => false,
668670
Field::NameKey { value, .. } => contains_nested_function(value),
669671
other => unreachable!("unknown node {:?}", other),
670672
})

0 commit comments

Comments
 (0)