// // DocumentLocation.cs // // Author: // Mike Krüger // // Copyright (c) 2014 Xamarin Inc. (http://xamarin.com) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.ComponentModel; using System.Globalization; namespace MonoDevelop.Ide.Editor { /// /// A line/column position. /// Text editor lines/columns are counted started from one. /// [Serializable] [TypeConverter(typeof(DocumentLocationConverter))] public readonly struct DocumentLocation : IComparable, IEquatable { /// /// Represents no text location (0, 0). /// public static readonly DocumentLocation Empty = new DocumentLocation(0, 0); /// /// Constant of the minimum line. /// public const int MinLine = 1; /// /// Constant of the minimum column. /// public const int MinColumn = 1; /// /// Creates a TextLocation instance. /// public DocumentLocation(int line, int column) { this.Line = line; this.Column = column; } /// /// Gets the line number. /// public int Line { get; } /// /// Gets the column number. /// public int Column { get; } /// /// Gets whether the TextLocation instance is empty. /// public bool IsEmpty { get { return Column < MinLine && Line < MinColumn; } } /// /// Gets a string representation for debugging purposes. /// public override string ToString() { return string.Format(CultureInfo.InvariantCulture, "(Line {1}, Col {0})", this.Column, this.Line); } /// /// Gets a hash code. /// public override int GetHashCode() { return unchecked (Column << 20 ^ Line); } /// /// Equality test. /// public override bool Equals(object obj) { if (!(obj is DocumentLocation)) return false; return (DocumentLocation)obj == this; } /// /// Equality test. /// public bool Equals(DocumentLocation other) { return this == other; } /// /// Equality test. /// public static bool operator ==(DocumentLocation left, DocumentLocation right) { return left.Column == right.Column && left.Line == right.Line; } /// /// Inequality test. /// public static bool operator !=(DocumentLocation left, DocumentLocation right) { return left.Column != right.Column || left.Line != right.Line; } /// /// Compares two text locations. /// public static bool operator <(DocumentLocation left, DocumentLocation right) { if (left.Line < right.Line) return true; else if (left.Line == right.Line) return left.Column < right.Column; else return false; } /// /// Compares two text locations. /// public static bool operator >(DocumentLocation left, DocumentLocation right) { if (left.Line > right.Line) return true; else if (left.Line == right.Line) return left.Column > right.Column; else return false; } /// /// Compares two text locations. /// public static bool operator <=(DocumentLocation left, DocumentLocation right) { return !(left > right); } /// /// Compares two text locations. /// public static bool operator >=(DocumentLocation left, DocumentLocation right) { return !(left < right); } public static implicit operator Microsoft.CodeAnalysis.Text.LinePosition (DocumentLocation location) { return new Microsoft.CodeAnalysis.Text.LinePosition (location.Line - 1, location.Column - 1); } public static implicit operator DocumentLocation(Microsoft.CodeAnalysis.Text.LinePosition location) { return new DocumentLocation (location.Line + 1, location.Character + 1); } /// /// Compares two text locations. /// public int CompareTo(DocumentLocation other) { if (this == other) return 0; if (this < other) return -1; else return 1; } } public class DocumentLocationConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(DocumentLocation) || base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string[] parts = ((string)value).Split(';', ','); if (parts.Length == 2) { return new DocumentLocation(int.Parse(parts[0]), int.Parse(parts[1])); } } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value is DocumentLocation) { var loc = (DocumentLocation)value; return loc.Line + ";" + loc.Column; } return base.ConvertTo(context, culture, value, destinationType); } } public class DocumentLocationEventArgs : System.EventArgs { readonly DocumentLocation location; public DocumentLocation Location { get { return location; } } public DocumentLocationEventArgs (DocumentLocation location) { this.location = location; } } }