Example #1
0
        /// <summary>
        /// Creates a new instance of the BufferRange class.
        /// </summary>
        /// <param name="start">The start position of the range.</param>
        /// <param name="end">The end position of the range.</param>
        public BufferRange(BufferPosition start, BufferPosition end)
        {
            if (start > end)
            {
                // TODO: localization
                string message = string.Format("Start position ({0}, {1}) must come before or be equal to the end position ({2}, {3})", start.Line, start.Column, end.Line, end.Column);
                throw new ArgumentException(message);
            }

            this.Start = start;
            this.End   = end;
        }
        /// <summary>
        /// Calculates the line and column number range based on
        /// the given start and end buffer offsets.
        /// </summary>
        /// <param name="startOffset">The start offset of the range.</param>
        /// <param name="endOffset">The end offset of the range.</param>
        /// <returns>A new BufferRange containing the positions in the offset range.</returns>
        /// <seealso cref="BaseOffset"/>
        public BufferRange GetRangeBetweenOffsets(int startOffset, int endOffset)
        {
            bool foundStart     = false;
            int  currentOffset  = 0;
            int  searchedOffset = startOffset;

            BufferPosition startPosition = new BufferPosition(0, 0);    // TODO: is 0,0 correct or should it be -1,-1?
            BufferPosition endPosition   = startPosition;

            int line = 0;

            while (line < FileLines.Count)
            {
                if (searchedOffset <= currentOffset + FileLines[line].Length)
                {
                    int column = searchedOffset - currentOffset;

                    // Have we already found the start position?
                    if (foundStart)
                    {
                        // Assign the end position and end the search
                        endPosition = new BufferPosition(line + BaseOffset, column + BaseOffset);
                        break;
                    }
                    else
                    {
                        startPosition = new BufferPosition(line + BaseOffset, column + BaseOffset);

                        // Do we only need to find the start position?
                        if (startOffset == endOffset)
                        {
                            endPosition = startPosition;
                            break;
                        }
                        else
                        {
                            // Since the end offset can be on the same line,
                            // skip the line increment and continue searching
                            // for the end position
                            foundStart     = true;
                            searchedOffset = endOffset;
                            continue;
                        }
                    }
                }

                // Increase the current offset and include newline length
                currentOffset += FileLines[line].Length + Environment.NewLine.Length;
                line++;
            }

            return(new BufferRange(startPosition, endPosition));
        }
Example #3
0
        /// <summary>
        /// Compares two instances of the BufferPosition class.
        /// </summary>
        /// <param name="obj">The object to which this instance will be compared.</param>
        /// <returns>True if the positions are equal, false otherwise.</returns>
        public override bool Equals(object obj)
        {
            if (!(obj is BufferPosition))
            {
                return(false);
            }

            BufferPosition other = (BufferPosition)obj;

            return
                (this.Line == other.Line &&
                 this.Column == other.Column);
        }
        /// <summary>
        /// Calculates a FilePosition relative to a starting BufferPosition
        /// using the given line and column offset.
        /// </summary>
        /// <param name="originalPosition">The original BufferPosition from which an new position should be calculated.</param>
        /// <param name="lineOffset">The line offset added to the original position in this file.</param>
        /// <param name="columnOffset">The column offset added to the original position in this file.</param>
        /// <returns>A new FilePosition instance with the resulting line and column number.</returns>
        /// <seealso cref="BaseOffset"/>
        public DocumentPosition CalculatePosition(
            BufferPosition originalPosition,
            int lineOffset,
            int columnOffset)
        {
            int newLine   = originalPosition.Line + lineOffset,
                newColumn = originalPosition.Column + columnOffset;

            ValidatePosition(newLine, newColumn);

            string scriptLine = FileLines[newLine - BaseOffset];

            newColumn = Math.Min(scriptLine.Length + BaseOffset, newColumn);

            return(new DocumentPosition(this, newLine, newColumn));
        }
 /// <summary>
 /// Throws ArgumentOutOfRangeException if the given position is outside
 /// of the file's buffer extents.
 /// </summary>
 /// <param name="bufferPosition">The position in the buffer to be validated.</param>
 public void ValidatePosition(BufferPosition bufferPosition)
 {
     ValidatePosition(bufferPosition.Line, bufferPosition.Column);
 }