예제 #1
0
        public void LineBreaksTest()
        {
            string sample = "Hello World";

            foreach (var nl in new[] { "\r", "\r\n", "\n", "\u0085", "\u2028", "\u2029" })
            {
                for (int breakscount = 0; breakscount < 512; breakscount += 17)
                {
                    // construct sample text with {linecount} lines
                    string text = string.Empty;
                    for (var line = 0; line < breakscount; line++)
                    {
                        text += sample + nl;
                    }
                    text += sample;

                    // test LineBreaks
                    var linebreaks = LineBreaks.Create(text);
                    Assert.AreEqual(linebreaks.LinesCount, breakscount + 1);
                    for (int i = 0; i <= text.Length; i += 7)
                    {
                        Assert.AreEqual(linebreaks.GetLineFromPosition(i), (i / (sample.Length + nl.Length)));
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Converts the given Markdown text to HTML format.
        ///
        /// This method enables a number of Markdig extensions.
        /// </summary>
        /// <param name="markdown">The Markdown content</param>
        /// <param name="softLineBreaks">The LineBreaks setting.</param>
        /// <returns>An HTML representation of the given content.</returns>
        public static string ToHtml(string markdown, LineBreaks softLineBreaks)
        {
            MarkdownPipelineBuilder pipelineBuilder = new MarkdownPipelineBuilder().UseAdvancedExtensions();

            if (softLineBreaks == LineBreaks.Hard)
            {
                pipelineBuilder.UseSoftlineBreakAsHardlineBreak();
            }

            MarkdownPipeline pipeline = pipelineBuilder.Build();

            return(Markdown.ToHtml(markdown, pipeline));
        }
예제 #3
0
 public static string StandardizeLineBreaks(string text, LineBreaks lineBreak)
 {
     switch (lineBreak)
     {
         case LineBreaks.Mac:
             return StandardizeLineBreaks(text, "\r");
         case LineBreaks.Unix:
             return StandardizeLineBreaks(text, "\n");
         case LineBreaks.Windows:
             return StandardizeLineBreaks(text, "\r\n");
         default:
             throw new NotImplementedException("This LineBreak hasn't been coded yet.");
     }
 }
예제 #4
0
        public static string StandardizeLineBreaks(string text, LineBreaks lineBreak)
        {
            switch (lineBreak)
            {
            case LineBreaks.Mac:
                return(StandardizeLineBreaks(text, "\r"));

            case LineBreaks.Unix:
                return(StandardizeLineBreaks(text, "\n"));

            case LineBreaks.Windows:
                return(StandardizeLineBreaks(text, "\r\n"));

            default:
                throw new NotImplementedException("This LineBreak hasn't been coded yet.");
            }
        }
예제 #5
0
        public static PhpArray /*!*/ token_get_all(string source, int flags = 0)
        {
            var tokens = new PhpArray();

            if (string.IsNullOrEmpty(source))
            {
                return(tokens);
            }

            Tokens t;
            var    lines = LineBreaks.Create(source);

            using (var tokenizer = new Lexer(new StringReader(source), Encoding.UTF8))
            {
                while ((t = tokenizer.GetNextToken()) != Tokens.EOF)
                {
                    if (tokenizer.TokenSpan.Length == 1 && (int)t == tokenizer.TokenText[0])
                    {
                        // single char token
                        tokens.Add(tokenizer.TokenText);
                    }
                    else
                    {
                        // other
                        tokens.Add(new PhpArray(3)
                        {
                            (int)t,
                            tokenizer.TokenText,
                            lines.GetLineFromPosition(tokenizer.TokenSpan.Start) + 1,
                        });
                    }

                    //

                    if (t == Tokens.T_ERROR)
                    {
                        break;
                    }
                }
            }

            return(tokens);
        }
 /// <inheritdoc />
 public void WriteXml(XmlWriter writer)
 {
     writer.WriteAttributeString(nameof(LineBreaks), LineBreaks.ToString());
 }
예제 #7
0
        public void InitializeLayout(int ResizeIteration)
        {
            if (ResizeIteration > 1)
            {
                this.SlotSize = Math.Min(OriginalSlotSize, Math.Max(24, OriginalSlotSize - (ResizeIteration - 1) * 8));
            }

            HoveredSlot = null;
            SecondaryActionButtonPressedTime     = null;
            SecondaryActionButtonPressedLocation = null;

            List <Rectangle> SlotBounds       = new List <Rectangle>();
            List <Rectangle> LockedSlotBounds = new List <Rectangle>();

            int CurrentIndex         = 0;
            int CurrentRow           = 0;
            int CurrentColumn        = 0;
            int CurrentLine          = 0;
            int TotalLineBreakHeight = 0;

            List <AllowedObject> UngroupedObjects = BoundedBag.AllowedObjects.Where(x => !Menu.GroupByQuality || !x.HasQualities).ToList();

            foreach (AllowedObject Obj in UngroupedObjects)
            {
                foreach (int Quality in Enumerable.Range(0, Obj.HasQualities ? Obj.Qualities.Count : 1))
                {
                    if (CurrentColumn == ColumnCount)
                    {
                        CurrentRow++;
                        CurrentColumn = 0;
                    }

                    int X = CurrentColumn * SlotSize;
                    int Y = CurrentRow * SlotSize + TotalLineBreakHeight;
                    SlotBounds.Add(new Rectangle(X + Padding, Y + Padding, SlotSize, SlotSize));

                    if (LineBreaks.Contains(CurrentIndex))
                    {
                        if (CurrentLine < LineBreakHeights.Count)
                        {
                            TotalLineBreakHeight += LineBreakHeights[CurrentLine % LineBreakHeights.Count];
                        }
                        CurrentLine++;
                        CurrentRow++;

                        if (ShowLockedSlots)
                        {
                            //  Fill in the rest of this row with locked slots
                            int LockedSlotColumn = CurrentColumn + 1;
                            while (LockedSlotColumn < ColumnCount)
                            {
                                LockedSlotBounds.Add(new Rectangle(LockedSlotColumn * SlotSize + Padding, Y + Padding, SlotSize, SlotSize));
                                LockedSlotColumn++;
                            }
                        }

                        CurrentColumn = 0;
                    }
                    else
                    {
                        CurrentColumn++;
                    }

                    CurrentIndex++;
                }
            }

            if (ShowLockedSlots)
            {
                //  Fill in the rest of this row with locked slots
                int Y = CurrentRow * SlotSize + TotalLineBreakHeight;
                int LockedSlotColumn = CurrentColumn;
                while (LockedSlotColumn < ColumnCount)
                {
                    LockedSlotBounds.Add(new Rectangle(LockedSlotColumn * SlotSize + Padding, Y + Padding, SlotSize, SlotSize));
                    LockedSlotColumn++;
                }
            }

            RelativeSlotBounds       = new ReadOnlyCollection <Rectangle>(SlotBounds);
            RelativeLockedSlotBounds = new ReadOnlyCollection <Rectangle>(LockedSlotBounds);

            this.RelativeBounds = new Rectangle(0, 0, ColumnCount * SlotSize + Padding * 2, (CurrentRow + 1) * SlotSize + TotalLineBreakHeight + Padding * 2);
        }
예제 #8
0
        static PHPDocBlock NewPHPDoc(string phpdoc)
        {
            var linebreaks = LineBreaks.Create(phpdoc);

            return(new PHPDocBlock(phpdoc, new TextSpan(linebreaks, 0, phpdoc.Length)));
        }
예제 #9
0
            private void FormatNode(StatementGrammarNode startNode, StringBuilder stringBuilder, ref bool skipSpaceBeforeToken, ref string indentation, ref TextSegment textSegment)
            {
                var childIndex = 0;
                var childNodes = ((IEnumerable <StatementNode>)startNode.ChildNodes)
                                 .Concat(startNode.Comments)
                                 .OrderBy(n => n.SourcePosition.IndexStart);

                foreach (var childNode in childNodes)
                {
                    if (_formatSelectionOnly)
                    {
                        if (childNode.SourcePosition.IndexEnd <= ExecutionContext.SelectionStart)
                        {
                            continue;
                        }

                        if (childNode.SourcePosition.IndexStart >= ExecutionContext.SelectionEnd)
                        {
                            break;
                        }
                    }

                    if (childNode.Token != null)
                    {
                        if (textSegment.IndextStart == -1)
                        {
                            textSegment.IndextStart = childNode.SourcePosition.IndexStart;
                        }

                        textSegment.Length = childNode.SourcePosition.IndexEnd - textSegment.IndextStart + 1;
                    }

                    var grammarNode = childNode as StatementGrammarNode;
                    if (grammarNode == null)
                    {
                        stringBuilder.Append(childNode.Token.Value);
                        continue;
                    }

                    var lineBreakSettings = LineBreaks.SingleOrDefault(s => String.Equals(s.NonTerminalId, startNode.Id) && String.Equals(s.ChildNodeId, grammarNode.Id));
                    if (String.IsNullOrEmpty(lineBreakSettings.NonTerminalId))
                    {
                        lineBreakSettings = LineBreaks.SingleOrDefault(s => String.Equals(s.NonTerminalId, startNode.Id) && String.IsNullOrEmpty(s.ChildNodeId));
                    }

                    var breakSettingsFound = !String.IsNullOrEmpty(lineBreakSettings.NonTerminalId);

                    var breakBefore = false;
                    var breakBeforeSettingsFound = breakSettingsFound && (!String.IsNullOrEmpty(lineBreakSettings.ChildNodeId) || childIndex == 0);
                    if (breakBeforeSettingsFound)
                    {
                        var indentationBefore = lineBreakSettings.GetIndentationBefore?.Invoke(grammarNode) ?? 0;
                        if (indentationBefore > 0)
                        {
                            indentation += new String('\t', indentationBefore);
                        }
                        else if (indentationBefore < 0)
                        {
                            indentation = indentation.Remove(0, Math.Min(-indentationBefore, indentation.Length));
                        }

                        breakBefore = stringBuilder.Length > 0 && lineBreakSettings.BreakPosition != null && lineBreakSettings.BreakPosition(grammarNode).HasFlag(LineBreakPosition.BeforeNode);
                        if (breakBefore)
                        {
                            stringBuilder.Append(Environment.NewLine);
                            stringBuilder.Append(indentation);

                            if (grammarNode.Type == NodeType.NonTerminal)
                            {
                                skipSpaceBeforeToken = true;
                            }
                        }
                    }

                    if (grammarNode.Type == NodeType.Terminal)
                    {
                        if ((!breakSettingsFound || !breakBefore) &&
                            (!SingleCharacterTerminals.Contains(grammarNode.Id) ||
                             OracleGrammarDescription.MathTerminals.Contains(grammarNode.Id) ||
                             grammarNode.Id.In(Terminals.Colon)) &&
                            !skipSpaceBeforeToken && stringBuilder.Length > 0)
                        {
                            stringBuilder.Append(' ');
                        }

                        stringBuilder.Append(FormatTerminal(grammarNode));

                        skipSpaceBeforeToken = SkipSpaceTerminalIds.Contains(grammarNode.Id) || grammarNode.Id.In(Terminals.Dot, Terminals.Colon, Terminals.AtCharacter);

                        if (_formatSelectionOnly && grammarNode.SourcePosition.IndexEnd >= ExecutionContext.SelectionEnd)
                        {
                            break;
                        }
                    }
                    else
                    {
                        FormatNode(grammarNode, stringBuilder, ref skipSpaceBeforeToken, ref indentation, ref textSegment);
                    }

                    var breakAfterSettingsFound = breakSettingsFound && (!String.IsNullOrEmpty(lineBreakSettings.ChildNodeId) || childIndex + 1 == startNode.ChildNodes.Count);
                    if (breakAfterSettingsFound)
                    {
                        var indentationAfter = lineBreakSettings.GetIndentationAfter?.Invoke(grammarNode) ?? 0;
                        if (indentationAfter > 0)
                        {
                            indentation += new String('\t', indentationAfter);
                        }
                        else if (indentationAfter < 0)
                        {
                            indentation = indentation.Remove(0, Math.Min(-indentationAfter, indentation.Length));
                        }

                        if (lineBreakSettings.BreakPosition != null && lineBreakSettings.BreakPosition(grammarNode).HasFlag(LineBreakPosition.AfterNode))
                        {
                            stringBuilder.Append(Environment.NewLine);
                            skipSpaceBeforeToken = true;
                            stringBuilder.Append(indentation);
                        }
                    }

                    childIndex++;
                }
            }