Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/TextViewPosition.cs @ 11937

Last change on this file since 11937 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 6.0 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Globalization;
21using ICSharpCode.NRefactory;
22using ICSharpCode.AvalonEdit.Document;
23
24namespace ICSharpCode.AvalonEdit
25{
26  /// <summary>
27  /// Represents a text location with a visual column.
28  /// </summary>
29  public struct TextViewPosition : IEquatable<TextViewPosition>, IComparable<TextViewPosition>
30  {
31    int line, column, visualColumn;
32    bool isAtEndOfLine;
33   
34    /// <summary>
35    /// Gets/Sets Location.
36    /// </summary>
37    public TextLocation Location {
38      get {
39        return new TextLocation(line, column);
40      }
41      set {
42        line = value.Line;
43        column = value.Column;
44      }
45    }
46   
47    /// <summary>
48    /// Gets/Sets the line number.
49    /// </summary>
50    public int Line {
51      get { return line; }
52      set { line = value; }
53    }
54   
55    /// <summary>
56    /// Gets/Sets the (text) column number.
57    /// </summary>
58    public int Column {
59      get { return column; }
60      set { column = value; }
61    }
62   
63    /// <summary>
64    /// Gets/Sets the visual column number.
65    /// Can be -1 (meaning unknown visual column).
66    /// </summary>
67    public int VisualColumn {
68      get { return visualColumn; }
69      set { visualColumn = value; }
70    }
71   
72    /// <summary>
73    /// When word-wrap is enabled and a line is wrapped at a position where there is no space character;
74    /// then both the end of the first TextLine and the beginning of the second TextLine
75    /// refer to the same position in the document, and also have the same visual column.
76    /// In this case, the IsAtEndOfLine property is used to distinguish between the two cases:
77    /// the value <c>true</c> indicates that the position refers to the end of the previous TextLine;
78    /// the value <c>false</c> indicates that the position refers to the beginning of the next TextLine.
79    ///
80    /// If this position is not at such a wrapping position, the value of this property has no effect.
81    /// </summary>
82    public bool IsAtEndOfLine {
83      get { return isAtEndOfLine; }
84      set { isAtEndOfLine = value; }
85    }
86   
87    /// <summary>
88    /// Creates a new TextViewPosition instance.
89    /// </summary>
90    public TextViewPosition(int line, int column, int visualColumn)
91    {
92      this.line = line;
93      this.column = column;
94      this.visualColumn = visualColumn;
95      this.isAtEndOfLine = false;
96    }
97   
98    /// <summary>
99    /// Creates a new TextViewPosition instance.
100    /// </summary>
101    public TextViewPosition(int line, int column)
102      : this(line, column, -1)
103    {
104    }
105   
106    /// <summary>
107    /// Creates a new TextViewPosition instance.
108    /// </summary>
109    public TextViewPosition(TextLocation location, int visualColumn)
110    {
111      this.line = location.Line;
112      this.column = location.Column;
113      this.visualColumn = visualColumn;
114      this.isAtEndOfLine = false;
115    }
116   
117    /// <summary>
118    /// Creates a new TextViewPosition instance.
119    /// </summary>
120    public TextViewPosition(TextLocation location)
121      : this(location, -1)
122    {
123    }
124   
125    /// <inheritdoc/>
126    public override string ToString()
127    {
128      return string.Format(CultureInfo.InvariantCulture,
129                           "[TextViewPosition Line={0} Column={1} VisualColumn={2} IsAtEndOfLine={3}]",
130                           this.line, this.column, this.visualColumn, this.isAtEndOfLine);
131    }
132   
133    #region Equals and GetHashCode implementation
134    // The code in this region is useful if you want to use this structure in collections.
135    // If you don't need it, you can just remove the region and the ": IEquatable<Struct1>" declaration.
136   
137    /// <inheritdoc/>
138    public override bool Equals(object obj)
139    {
140      if (obj is TextViewPosition)
141        return Equals((TextViewPosition)obj); // use Equals method below
142      else
143        return false;
144    }
145   
146    /// <inheritdoc/>
147    public override int GetHashCode()
148    {
149      int hashCode = isAtEndOfLine ? 115817 : 0;
150      unchecked {
151        hashCode += 1000000007 * Line.GetHashCode();
152        hashCode += 1000000009 * Column.GetHashCode();
153        hashCode += 1000000021 * VisualColumn.GetHashCode();
154      }
155      return hashCode;
156    }
157   
158    /// <summary>
159    /// Equality test.
160    /// </summary>
161    public bool Equals(TextViewPosition other)
162    {
163      return this.Line == other.Line && this.Column == other.Column && this.VisualColumn == other.VisualColumn && this.IsAtEndOfLine == other.IsAtEndOfLine;
164    }
165   
166    /// <summary>
167    /// Equality test.
168    /// </summary>
169    public static bool operator ==(TextViewPosition left, TextViewPosition right)
170    {
171      return left.Equals(right);
172    }
173   
174    /// <summary>
175    /// Inequality test.
176    /// </summary>
177    public static bool operator !=(TextViewPosition left, TextViewPosition right)
178    {
179      return !(left.Equals(right)); // use operator == and negate result
180    }
181    #endregion
182   
183    /// <inheritdoc/>
184    public int CompareTo(TextViewPosition other)
185    {
186      int r = this.Location.CompareTo(other.Location);
187      if (r != 0)
188        return r;
189      r = this.visualColumn.CompareTo(other.visualColumn);
190      if (r != 0)
191        return r;
192      if (isAtEndOfLine && !other.isAtEndOfLine)
193        return -1;
194      else if (!isAtEndOfLine && other.isAtEndOfLine)
195        return 1;
196      return 0;
197    }
198  }
199}
Note: See TracBrowser for help on using the repository browser.