// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System; using System.Diagnostics; namespace Microsoft.SqlTools.ServiceLayer.Workspace.Contracts { /// /// Provides details about a range between two positions in /// a file buffer. /// [DebuggerDisplay("Start = {Start.Line}:{Start.Column}, End = {End.Line}:{End.Column}")] public class BufferRange { #region Properties /// /// Provides an instance that represents a range that has not been set. /// public static readonly BufferRange None = new BufferRange(0, 0, 0, 0); /// /// Gets the start position of the range in the buffer. /// public BufferPosition Start { get; private set; } /// /// Gets the end position of the range in the buffer. /// public BufferPosition End { get; private set; } /// /// Returns true if the current range is non-zero, i.e. /// contains valid start and end positions. /// public bool HasRange { get { return this.Equals(BufferRange.None); } } #endregion #region Constructors /// /// Creates a new instance of the BufferRange class. /// /// The start position of the range. /// The end position of the range. public BufferRange(BufferPosition start, BufferPosition end) { if (start > end) { throw new ArgumentException(SR.WorkspaceServiceBufferPositionOutOfOrder(start.Line, start.Column, end.Line, end.Column)); } this.Start = start; this.End = end; } /// /// Creates a new instance of the BufferRange class. /// /// The 1-based starting line number of the range. /// The 1-based starting column number of the range. /// The 1-based ending line number of the range. /// The 1-based ending column number of the range. public BufferRange( int startLine, int startColumn, int endLine, int endColumn) { this.Start = new BufferPosition(startLine, startColumn); this.End = new BufferPosition(endLine, endColumn); } #endregion #region Public Methods /// /// Compares two instances of the BufferRange class. /// /// The object to which this instance will be compared. /// True if the ranges are equal, false otherwise. public override bool Equals(object obj) { if (!(obj is BufferRange)) { return false; } BufferRange other = (BufferRange)obj; return this.Start.Equals(other.Start) && this.End.Equals(other.End); } /// /// Calculates a unique hash code that represents this instance. /// /// A hash code representing this instance. public override int GetHashCode() { return this.Start.GetHashCode() ^ this.End.GetHashCode(); } #endregion } }