private void AddTokenRules()

in src/Kusto.Language/Editor/Kusto/KustoFormatter.cs [323:411]


        private void AddTokenRules(SyntaxToken token)
        {
            // don't adjust spacing if there are already line breaks in the trivia
            if (TextFacts.HasLineBreaks(token.Trivia))
                return;

            // if no previous token then leave as is
            var prev = token.GetPreviousToken();
            if (prev == null)
                return;

            if (IsIdentifierOrKeyword(token) && IsIdentifierOrKeyword(prev))
            {
                // always have space between two adjacent names
                if (token.Trivia != " ")
                {
                    AddRule(token, SpacingRule.From(SpacingKind.SingleSpaceIfOnSameLine));
                }
            }
            else if (token.Parent is BinaryExpression be && be.Operator == token
                || prev.Parent is BinaryExpression pbe && pbe.Operator == prev)
            {
                // space before and after binary operator
                if (token.Trivia != " ")
                {
                    AddRule(token, SpacingRule.From(SpacingKind.SingleSpaceIfOnSameLine));
                }
            }
            else if (prev.Parent is PrefixUnaryExpression pue && pue.Operator == prev)
            {
                // no space after prefix unary operator
                if (token.Trivia != "")
                {
                    AddRule(token, SpacingRule.From(SpacingKind.NoSpaceIfOnSameLine));
                }
            }
            else if (token.Kind != SyntaxKind.EndOfTextToken)
            {
                // spacing before token
                switch (token.Kind)
                {
                    case SyntaxKind.CloseBraceToken:
                    case SyntaxKind.CloseBracketToken:
                    case SyntaxKind.CloseParenToken:
                    case SyntaxKind.CommaToken:
                    case SyntaxKind.ColonToken:
                    case SyntaxKind.SemicolonToken:
                    case SyntaxKind.DotToken:
                        if (token.Trivia != "")
                        {
                            AddRule(token, SpacingRule.From(SpacingKind.NoSpaceIfOnSameLine));
                        }
                        break;

                    case SyntaxKind.DotDotToken:
                    case SyntaxKind.BarToken:
                        if (token.Trivia != " ")
                        {
                            AddRule(token, SpacingRule.From(SpacingKind.SingleSpaceIfOnSameLine));
                        }
                        break;
                }

                // spacing after previous token
                switch (prev.Kind)
                {
                    case SyntaxKind.OpenBraceToken:
                    case SyntaxKind.OpenBracketToken:
                    case SyntaxKind.OpenParenToken:
                    case SyntaxKind.DotToken:
                        if (token.Trivia != "")
                        {
                            AddRule(token, SpacingRule.From(SpacingKind.NoSpaceIfOnSameLine));
                        }
                        break;

                    case SyntaxKind.CommaToken:
                    case SyntaxKind.ColonToken:
                    case SyntaxKind.BarToken:
                    case SyntaxKind.SemicolonToken:
                    case SyntaxKind.DotDotToken:
                        if (token.Trivia != " ")
                        {
                            AddRule(token, SpacingRule.From(SpacingKind.SingleSpaceIfOnSameLine));
                        }
                        break;
                }
            }
        }