|
| 1 | +use emmylua_parser::{LuaAstNode, LuaSyntaxId, LuaSyntaxKind, LuaSyntaxToken, LuaTokenKind}; |
| 2 | +use rowan::NodeOrToken; |
| 3 | + |
| 4 | +use crate::format::{TokenExpected, XmakeFormatter}; |
| 5 | + |
| 6 | +use super::StyleRuler; |
| 7 | + |
| 8 | +pub struct BasicSpaceRuler; |
| 9 | + |
| 10 | +impl StyleRuler for BasicSpaceRuler { |
| 11 | + fn apply_style(f: &mut XmakeFormatter) { |
| 12 | + let root = f.get_root(); |
| 13 | + for node_or_token in root.syntax().descendants_with_tokens() { |
| 14 | + if let NodeOrToken::Token(token) = node_or_token { |
| 15 | + let syntax_id = LuaSyntaxId::from_token(&token); |
| 16 | + match token.kind().to_token() { |
| 17 | + LuaTokenKind::TkLeftParen | LuaTokenKind::TkLeftBracket => { |
| 18 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(0)); |
| 19 | + } |
| 20 | + LuaTokenKind::TkRightBracket | LuaTokenKind::TkRightParen => { |
| 21 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(0)); |
| 22 | + } |
| 23 | + LuaTokenKind::TkLeftBrace => { |
| 24 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(1)); |
| 25 | + } |
| 26 | + LuaTokenKind::TkRightBrace => { |
| 27 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(1)); |
| 28 | + } |
| 29 | + LuaTokenKind::TkComma => { |
| 30 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(0)); |
| 31 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(1)); |
| 32 | + } |
| 33 | + LuaTokenKind::TkPlus | LuaTokenKind::TkMinus => { |
| 34 | + if is_parent_syntax(&token, LuaSyntaxKind::UnaryExpr) { |
| 35 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(0)); |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(1)); |
| 40 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(1)); |
| 41 | + } |
| 42 | + LuaTokenKind::TkLt => { |
| 43 | + if is_parent_syntax(&token, LuaSyntaxKind::Attribute) { |
| 44 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(1)); |
| 45 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(0)); |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(1)); |
| 50 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(1)); |
| 51 | + } |
| 52 | + LuaTokenKind::TkGt => { |
| 53 | + if is_parent_syntax(&token, LuaSyntaxKind::Attribute) { |
| 54 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(0)); |
| 55 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(1)); |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(1)); |
| 60 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(1)); |
| 61 | + } |
| 62 | + LuaTokenKind::TkMul |
| 63 | + | LuaTokenKind::TkDiv |
| 64 | + | LuaTokenKind::TkIDiv |
| 65 | + | LuaTokenKind::TkMod |
| 66 | + | LuaTokenKind::TkPow |
| 67 | + | LuaTokenKind::TkConcat |
| 68 | + | LuaTokenKind::TkAssign |
| 69 | + | LuaTokenKind::TkBitAnd |
| 70 | + | LuaTokenKind::TkBitOr |
| 71 | + | LuaTokenKind::TkBitXor |
| 72 | + | LuaTokenKind::TkEq |
| 73 | + | LuaTokenKind::TkGe |
| 74 | + | LuaTokenKind::TkLe |
| 75 | + | LuaTokenKind::TkNe |
| 76 | + | LuaTokenKind::TkAnd |
| 77 | + | LuaTokenKind::TkOr |
| 78 | + | LuaTokenKind::TkShl |
| 79 | + | LuaTokenKind::TkShr => { |
| 80 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(1)); |
| 81 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(1)); |
| 82 | + } |
| 83 | + LuaTokenKind::TkColon | LuaTokenKind::TkDot => { |
| 84 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(0)); |
| 85 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(0)); |
| 86 | + } |
| 87 | + LuaTokenKind::TkLocal |
| 88 | + | LuaTokenKind::TkFunction |
| 89 | + | LuaTokenKind::TkIf |
| 90 | + | LuaTokenKind::TkWhile |
| 91 | + | LuaTokenKind::TkFor |
| 92 | + | LuaTokenKind::TkRepeat |
| 93 | + | LuaTokenKind::TkReturn |
| 94 | + | LuaTokenKind::TkDo |
| 95 | + | LuaTokenKind::TkElseIf |
| 96 | + | LuaTokenKind::TkElse |
| 97 | + | LuaTokenKind::TkThen |
| 98 | + | LuaTokenKind::TkUntil |
| 99 | + | LuaTokenKind::TkIn |
| 100 | + | LuaTokenKind::TkNot => { |
| 101 | + f.add_token_left_expected(syntax_id, TokenExpected::Space(1)); |
| 102 | + f.add_token_right_expected(syntax_id, TokenExpected::Space(1)); |
| 103 | + } |
| 104 | + _ => {} |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +fn is_parent_syntax(token: &LuaSyntaxToken, kind: LuaSyntaxKind) -> bool { |
| 112 | + if let Some(parent) = token.parent() { |
| 113 | + return parent.kind().to_syntax() == kind; |
| 114 | + } |
| 115 | + false |
| 116 | +} |
0 commit comments