/// <summary> /// Calculates the 1-based 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> public BufferRange GetRangeBetweenOffsets(int startOffset, int endOffset) { bool foundStart = false; int currentOffset = 0; int searchedOffset = startOffset; BufferPosition startPosition = new BufferPosition(0, 0); BufferPosition endPosition = startPosition; int line = 0; while (line < this.FileLines.Count) { if (searchedOffset <= currentOffset + this.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 + 1, column + 1); break; } else { startPosition = new BufferPosition(line + 1, column + 1); // 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 += this.FileLines[line].Length + Environment.NewLine.Length; line++; } return(new BufferRange(startPosition, endPosition)); }
/// <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> /// 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) { throw new ArgumentException( 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)); } this.Start = start; this.End = end; }
/// <summary> /// Calculates a FilePosition relative to a starting BufferPosition /// using the given 1-based line and column offset. /// </summary> /// <param name="originalPosition">The original BufferPosition from which an new position should be calculated.</param> /// <param name="lineOffset">The 1-based line offset added to the original position in this file.</param> /// <param name="columnOffset">The 1-based column offset added to the original position in this file.</param> /// <returns>A new FilePosition instance with the resulting line and column number.</returns> public FilePosition CalculatePosition( BufferPosition originalPosition, int lineOffset, int columnOffset) { int newLine = originalPosition.Line + lineOffset, newColumn = originalPosition.Column + columnOffset; this.ValidatePosition(newLine, newColumn); string scriptLine = this.FileLines[newLine - 1]; newColumn = Math.Min(scriptLine.Length + 1, newColumn); return(new FilePosition(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) { this.ValidatePosition( bufferPosition.Line, bufferPosition.Column); }
internal FullScriptPosition(FileContext context, BufferPosition position, int offset) { fileContext = context; bufferPosition = position; Offset = offset; }
/// <summary> /// Calculates the 1-based 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> public BufferRange GetRangeBetweenOffsets(int startOffset, int endOffset) { bool foundStart = false; int currentOffset = 0; int searchedOffset = startOffset; BufferPosition startPosition = new BufferPosition(); BufferPosition endPosition = startPosition; int line = 0; while (line < this.FileLines.Count) { if (searchedOffset <= currentOffset + this.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 + 1, column + 1); break; } else { startPosition = new BufferPosition(line + 1, column + 1); // 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 += this.FileLines[line].Length + Environment.NewLine.Length; line++; } return new BufferRange(startPosition, endPosition); }