aboutsummaryrefslogtreecommitdiffstatsci
path: root/Drawing2D/src/CharLocation.cs
blob: 761274d13c6790624c79409024745470ad59b927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) 2013-2025  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using System.Diagnostics;

namespace Crow.Text
{
	[DebuggerDisplay ("{Line}, {Column}, {VisualCharXPosition}")]
	public struct CharLocation : IEquatable<CharLocation>
	{
		public readonly int Line;
		/// <summary>
		/// Character position in current line.
		/// If Column value is '-1', the visualX must contains the on screen position, and column will be computed from it.
		/// </summary>
		public int Column;
		/// <summary>
		/// The column as presented to the user, counting spaces in tabulations.
		/// Its value is set during computations.
		/// </summary>
		public int TabulatedColumn;
		public double VisualCharXPosition;
		public CharLocation (int line, int column, double visualX = -1) {
			Line = line;
			Column = column;
			TabulatedColumn = -1;
			VisualCharXPosition = visualX;
		}
		public bool HasVisualX => Column >= 0 && VisualCharXPosition >= 0;
		public void ResetVisualX () => VisualCharXPosition = -1;
		public static bool operator == (CharLocation a, CharLocation b)
			=> a.Equals (b);
		public static bool operator != (CharLocation a, CharLocation b)
			=> !a.Equals (b);
		public bool Equals (CharLocation other) {
			return Column < 0 ?
				Line == other.Line && VisualCharXPosition == other.VisualCharXPosition :
				Line == other.Line && Column == other.Column;
		}
		public override bool Equals (object obj) => obj is CharLocation loc ? Equals (loc) : false;
		public override int GetHashCode () {
			return Column < 0 ?
				HashCode.Combine (Line, VisualCharXPosition) :
				HashCode.Combine (Line, Column);
		}

		public override string ToString () => $"{Line}, {Column}";
	}
}