|
1 | 1 | use emmylua_parser::{ |
2 | 2 | BinaryOperator, LuaAssignStat, LuaAstNode, LuaAstToken, LuaBinaryExpr, LuaCallArgList, |
3 | 3 | LuaCallExpr, LuaClosureExpr, LuaComment, LuaExpr, LuaIndexExpr, LuaIndexKey, LuaKind, |
4 | | - LuaLiteralExpr, LuaLiteralToken, LuaLocalStat, LuaNameExpr, LuaParamList, LuaParenExpr, |
5 | | - LuaSingleArgExpr, LuaStat, LuaSyntaxId, LuaSyntaxKind, LuaSyntaxNode, LuaSyntaxToken, |
6 | | - LuaTableExpr, LuaTableField, LuaTokenKind, LuaUnaryExpr, |
| 4 | + LuaLiteralExpr, LuaLiteralToken, LuaLocalStat, LuaNameExpr, LuaNilCoalescingExpr, LuaParamList, |
| 5 | + LuaParenExpr, LuaSingleArgExpr, LuaStat, LuaSyntaxId, LuaSyntaxKind, LuaSyntaxNode, |
| 6 | + LuaSyntaxToken, LuaTableExpr, LuaTableField, LuaTernaryExpr, LuaTokenKind, LuaUnaryExpr, |
7 | 7 | }; |
8 | 8 | use rowan::TextRange; |
9 | 9 |
|
@@ -60,6 +60,8 @@ pub fn format_expr_with_options( |
60 | 60 | LuaExpr::CallExpr(expr) => format_call_expr(ctx, plan, expr), |
61 | 61 | LuaExpr::TableExpr(expr) => format_table_expr(ctx, plan, expr), |
62 | 62 | LuaExpr::ClosureExpr(expr) => format_closure_expr(ctx, plan, expr), |
| 63 | + LuaExpr::TernaryExpr(expr) => format_ternary_expr(ctx, plan, expr), |
| 64 | + LuaExpr::NilCoalescingExpr(expr) => format_nil_coalescing_expr(ctx, plan, expr), |
63 | 65 | } |
64 | 66 | } |
65 | 67 |
|
@@ -2341,6 +2343,45 @@ fn format_closure_expr( |
2341 | 2343 | render_closure_shell(ctx, plan, expr, shell_plan) |
2342 | 2344 | } |
2343 | 2345 |
|
| 2346 | +fn format_ternary_expr( |
| 2347 | + ctx: &FormatContext, |
| 2348 | + plan: &FormatPlan, |
| 2349 | + expr: &LuaTernaryExpr, |
| 2350 | +) -> Vec<DocIR> { |
| 2351 | + let Some(cond_expr) = expr.get_condition_expr() else { |
| 2352 | + return vec![ir::source_node(expr.syntax().clone())]; |
| 2353 | + }; |
| 2354 | + let Some((true_expr, false_expr)) = expr.get_true_false_exprs() else { |
| 2355 | + return vec![ir::source_node(expr.syntax().clone())]; |
| 2356 | + }; |
| 2357 | + let mut docs = format_expr(ctx, plan, &cond_expr); |
| 2358 | + docs.push(ir::space()); |
| 2359 | + docs.push(ir::syntax_token(LuaTokenKind::TkTernary)); |
| 2360 | + docs.push(ir::space()); |
| 2361 | + docs.extend(format_expr(ctx, plan, &true_expr)); |
| 2362 | + docs.push(ir::space()); |
| 2363 | + docs.push(ir::syntax_token(LuaTokenKind::TkColon)); |
| 2364 | + docs.push(ir::space()); |
| 2365 | + docs.extend(format_expr(ctx, plan, &false_expr)); |
| 2366 | + docs |
| 2367 | +} |
| 2368 | + |
| 2369 | +fn format_nil_coalescing_expr( |
| 2370 | + ctx: &FormatContext, |
| 2371 | + plan: &FormatPlan, |
| 2372 | + expr: &LuaNilCoalescingExpr, |
| 2373 | +) -> Vec<DocIR> { |
| 2374 | + let Some((left_expr, right_expr)) = expr.get_left_right_exprs() else { |
| 2375 | + return vec![ir::source_node(expr.syntax().clone())]; |
| 2376 | + }; |
| 2377 | + let mut docs = format_expr(ctx, plan, &left_expr); |
| 2378 | + docs.push(ir::space()); |
| 2379 | + docs.push(ir::syntax_token(LuaTokenKind::TkNilCoalescing)); |
| 2380 | + docs.push(ir::space()); |
| 2381 | + docs.extend(format_expr(ctx, plan, &right_expr)); |
| 2382 | + docs |
| 2383 | +} |
| 2384 | + |
2344 | 2385 | fn try_format_simple_inline_closure_expr( |
2345 | 2386 | ctx: &FormatContext, |
2346 | 2387 | plan: &FormatPlan, |
|
0 commit comments