Example #1
0
        /// <summary>
        /// Sets the total line length and checks the delimiter.
        /// This method can cause line to be deleted when it contains a single '\n' character
        /// and the previous line ends with '\r'.
        /// </summary>
        /// <returns>Usually returns <paramref name="line"/>, but if line was deleted due to
        /// the "\r\n" merge, returns the previous line.</returns>
        private DocumentLine SetLineLength(DocumentLine line, int newTotalLength)
        {
            var delta = newTotalLength - line.TotalLength;

            if (delta != 0)
            {
                foreach (var lt in _lineTrackers)
                {
                    lt.SetLineLength(line, newTotalLength);
                }
                line.TotalLength = newTotalLength;
                DocumentLineTree.UpdateAfterChildrenChange(line);
            }
            // determine new DelimiterLength
            if (newTotalLength == 0)
            {
                line.DelimiterLength = 0;
            }
            else
            {
                var lineOffset = line.Offset;
                var lastChar   = _document.GetCharAt(lineOffset + newTotalLength - 1);
                if (lastChar == '\r')
                {
                    line.DelimiterLength = 1;
                }
                else if (lastChar == '\n')
                {
                    if (newTotalLength >= 2 && _document.GetCharAt(lineOffset + newTotalLength - 2) == '\r')
                    {
                        line.DelimiterLength = 2;
                    }
                    else if (newTotalLength == 1 && lineOffset > 0 && _document.GetCharAt(lineOffset - 1) == '\r')
                    {
                        // we need to join this line with the previous line
                        var previousLine = line.PreviousLine;
                        RemoveLine(line);
                        return(SetLineLength(previousLine, previousLine.TotalLength + 1));
                    }
                    else
                    {
                        line.DelimiterLength = 1;
                    }
                }
                else
                {
                    line.DelimiterLength = 0;
                }
            }
            return(line);
        }
Example #2
0
        public int FindMatchingBracketBackward(AvaloniaEdit.Document.TextDocument document, int startOffset, char close, char open)
        {
            var result = startOffset;

            var currentChar = document.GetCharAt(startOffset--);

            if (currentChar == close)
            {
                var numOpen = 0;

                while (true)
                {
                    if (startOffset < 0)
                    {
                        break;
                    }

                    currentChar = document.GetCharAt(startOffset--);

                    if (currentChar == open && numOpen == 0)
                    {
                        result = startOffset + 1;
                        break;
                    }
                    if (currentChar == close)
                    {
                        numOpen++;
                    }
                    else if (currentChar == open)
                    {
                        numOpen--;
                    }

                    if (startOffset >= document.TextLength)
                    {
                        break;
                    }
                }
            }

            return(result);
        }
Example #3
0
 public void GetCharAt0EmptyDocument()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => document.GetCharAt(0));
 }