Beispiel #1
0
        protected override void Format()
        {
            var firstChild = target.GetChild(0);

            if (firstChild is LuaParser.TableconstructorContext || firstChild is LuaParser.StringContext)
            {
                base.Format();
                return;
            }

            var firstAsTerminal = firstChild as ITerminalNode;

            if (firstAsTerminal != null)
            {
                if (firstAsTerminal.Symbol.Text == "(")
                {
                    var symbol = new FormatSymbol(firstAsTerminal, true, true); // remove space before '('
                    ctx.WriteSymbol(symbol);
                    Format(1);
                    return;
                }
            }

            base.Format();
        }
Beispiel #2
0
 private void Write(FormatSymbol symbol)
 {
     if (symbol.line <= 0)
     {
         symbol.SetLine(line);
     }
     prevSymbol = symbol;
     writer.Append(symbol);
 }
Beispiel #3
0
        protected override void Format()
        {
            var hasChild = target.ChildCount > 2;

            ctx.WriteSymbol(new FormatSymbol(target.GetChild(0) as ITerminalNode), WriteSymbolFlags.PushIndent);
            if (hasChild)
            {
                Format(1, target.ChildCount - 2);
            }
            var lastChild = target.GetChild(target.ChildCount - 1) as ITerminalNode;

            ctx.WriteSymbol(new FormatSymbol(lastChild, !hasChild, FormatSymbol.TrimSpaceFollow(lastChild.Symbol.Text)), WriteSymbolFlags.PopIndentBefore);
        }
Beispiel #4
0
 protected override void Format()
 {
     for (int i = 0, len = target.ChildCount; i < len; i++)
     {
         var child = target.GetChild(i);
         if (child.EqualTo("...") && (FormatSymbol.IsLineBreak(ctx.prevSymbol) || ctx.prevSymbol.line < (child as ITerminalNode).Symbol.Line))
         {
             ctx.PushIndent();
             ctx.WriteSymbol(new FormatSymbol(child as ITerminalNode));
             ctx.PopIndent();
         }
         else
         {
             FormatNode(child);
         }
     }
 }
Beispiel #5
0
        protected override void Format()
        {
            if (parent.ruleType.ruleIndex == LuaParser.RULE_explist)
            {
                var typeName = target.GetType().Name;
                var match    = Regex.Match(typeName, "Exp(?<form>\\d+)Context$");
                var form     = int.Parse(match.Groups["form"].Value);

                if ((FormatSymbol.IsLineBreak(ctx.prevSymbol) || ctx.prevSymbol.line < (target as LuaParser.ExpContext).Start.Line) &&
                    (form != 8))
                {
                    ctx.PushIndent();
                    base.Format();
                    ctx.PopIndent();
                    return;
                }
            }
            base.Format();
        }
Beispiel #6
0
 public static bool IsSameLine(FormatSymbol s1, FormatSymbol s2)
 {
     return(s1.line == s2.line);
 }
Beispiel #7
0
 public static bool IsLineBreak(FormatSymbol s)
 {
     return(s == "\n");
 }
Beispiel #8
0
 public static bool TrimSpaceFollow(FormatSymbol s)
 {
     return(IsWhiteSpace(s) || s.trimSpaceFollow);
 }
Beispiel #9
0
 public static bool TrimSpaceAhead(FormatSymbol s)
 {
     return(IsWhiteSpace(s) || s.trimSpaceAhead);
 }
Beispiel #10
0
        public void WriteSymbol(FormatSymbol symbol, WriteSymbolFlags flags = WriteSymbolFlags.None)
        {
            var text = symbol.text;

            var newline     = false;
            var insertSpace = false;

            if (!symbol.comment)
            {
                WriteComments(symbol.tokenIndex);
            }

            if (options.keepWraps)
            {
                while (symbol.line > line)
                {
                    WriteLineBreak();
                }
            }

            var prev = prevSymbol;

            if ((flags & WriteSymbolFlags.PopIndentBefore) != WriteSymbolFlags.None)
            {
                PopIndent();
            }

            newline = FormatSymbol.IsLineBreak(prev);
            if (newline)
            {
                if (!symbol.noIndent)
                {
                    WriteIndent();
                }
                insertSpace = false;
            }
            else if ((FormatSymbol.TrimSpaceFollow(prev) || FormatSymbol.TrimSpaceAhead(symbol)) && !prev.forceSpaceFollow && !symbol.forceSpaceAhead)
            {
                insertSpace = false;
            }
            else
            {
                insertSpace = true;
            }

            if (insertSpace)
            {
                Write(FormatSymbol.SPACE);
            }

            var l = validIndentLevel;

            Write(symbol);

            if (!symbol.comment && scopes.Count > 0)
            {
                scopes.Peek().OnWriteSymbol();
            }

            if ((flags & WriteSymbolFlags.PushIndent) != WriteSymbolFlags.None)
            {
                PushIndent();
            }
            if ((flags & WriteSymbolFlags.PopIndentBefore) == WriteSymbolFlags.None && (flags & WriteSymbolFlags.PopIndent) != WriteSymbolFlags.None)
            {
                PopIndent();
            }

            line += symbol.numLine - 1;
        }
Beispiel #11
0
        public void WriteComments(int tokenIndex)
        {
            if (comments.Count == 0)
            {
                return;
            }

            var next = comments.Peek();

            if (next.TokenIndex > tokenIndex)
            {
                return;
            }

            comments.Dequeue();

            if (options.keepWraps)
            {
                while (next.Line > line)
                {
                    WriteLineBreak();
                }
            }

            var commentLines = next.Text.TrimEnd(COMMENT_SUFFIX).Split(COMMENT_SEPARATOR, StringSplitOptions.None);

            if (commentLines.Length == 1)
            {
                var symbol = new FormatSymbol(commentLines[0].Trim());
                if (!FormatSymbol.IsWhiteSpace(prevSymbol))
                {
                    symbol.ForceSpaceAhead();
                }
                symbol.SetComment();
                WriteSymbol(symbol);
            }
            else
            {
                if (options.formatComments)
                {
                    var symbol = new FormatSymbol(commentLines[0].Trim());
                    if (!FormatSymbol.IsWhiteSpace(prevSymbol))
                    {
                        symbol.ForceSpaceAhead();
                    }
                    symbol.SetComment();
                    WriteSymbol(symbol);
                    WriteLineBreak();

                    bool unindented = false;
                    PushIndent();

                    for (int i = 1, len = commentLines.Length; i < len; i++)
                    {
                        var item = commentLines[i].Trim();
                        if (i == len - 1)
                        {
                            if (Regex.Match(item, "^\\]=*\\]$").Success)
                            {
                                unindented = true;
                                PopIndent();
                            }
                        }
                        symbol = new FormatSymbol(item.Trim());
                        symbol.SetComment();
                        WriteSymbol(symbol);
                        if (i < len - 1)
                        {
                            WriteLineBreak();
                        }
                    }

                    if (!unindented)
                    {
                        PopIndent();
                    }
                }
                else
                {
                    if (FormatSymbol.IsLineBreak(prevSymbol) || prevSymbol.line < next.Line)
                    {
                        while (next.Line > line)
                        {
                            WriteLineBreak();
                        }

                        while (spaces.Count > 0)
                        {
                            var item = spaces.Peek();
                            if (item.Line < line)
                            {
                                spaces.Dequeue();
                                continue;
                            }
                            if (item.Line > line)
                            {
                                break;
                            }
                            if (item != null && item.Line == line)
                            {
                                spaces.Dequeue();
                                Write(item.Text);
                            }
                        }
                    }

                    for (int i = 0, len = commentLines.Length; i < len; i++)
                    {
                        var item   = commentLines[i];
                        var symbol = new FormatSymbol(item);

                        symbol.SetComment();
                        symbol.SetNoIndent();

                        WriteSymbol(symbol);

                        if (i < len - 1)
                        {
                            WriteLineBreak();
                        }
                    }
                }
            }

            WriteComments(tokenIndex);
        }