using AppKit;
using Foundation;
namespace Cauldron.Macos.SourceWriter;
public class FormatDescriptor : NSObject
{
#region Computed Properties
/// Gets or sets the FormatDescriptorType for this format descriptor.
/// The FormatDescriptorType.
public FormatDescriptorType Type { get; set; } = FormatDescriptorType.Prefix;
///
/// Gets or sets the forground color that text matching this format will be set to.
///
/// The NSColor.
public NSColor Color { get; set; } = NSColor.Gray;
/// Gets or sets the character sequence that this format starts with.
/// The starting string sequence.
public string StartsWith { get; set; } = "";
///
/// Gets or sets the character sequence that text matching this format ends with.
///
/// The ending string sequence.
///
/// This value will be an empty string ("") if the Type is a Prefix format.
///
public string EndsWith { get; set; } = "";
///
/// Gets or sets the index of the last matching character within either the StartsWith or
/// EndsWith based on the state of the Active property.
///
/// The index of the char.
///
/// This value should ONLY be changed by the .
///
public int CharIndex { get; set; } = 0;
///
/// Gets or sets if this format has been "activated" (if the matching StartsWith character sequence
/// has been found).
///
/// true if the matching StartsWith character sequence
/// has been found; otherwise, false.
///
/// This value should ONLY be changed by the .
///
public bool Active { get; set; } = false;
///
/// Gets a value indicating whether this is "triggered"
/// (all of the StartsWith or EndsWith characters have been found based on the
/// Active property).
///
/// true if triggered; otherwise, false.
public bool Triggered
{
get
{
if (Active)
{
return CharIndex > (EndsWith.Length - 1);
}
else
{
return CharIndex > (StartsWith.Length - 1);
}
}
}
#endregion
#region Constructors
/// Initializes a new instance of the class.
/// The starting character sequence for this format.
/// The NSColor that text in this sequence will be set too.
/// The type will automatically be set to Prefix.
public FormatDescriptor(string startsWith, NSColor color)
{
this.Type = FormatDescriptorType.Prefix;
this.StartsWith = startsWith;
this.Color = color;
}
/// Initializes a new instance of the class.
/// The starting character sequence for this format.
/// The ending character sequence for this format.
/// The NSColor that text in this sequence will be set too.
/// The type will automatically be set to Enclosure.
public FormatDescriptor(string startsWith, string endsWith, NSColor color)
{
this.Type = FormatDescriptorType.Enclosure;
this.StartsWith = startsWith;
this.EndsWith = endsWith;
this.Color = color;
}
#endregion
#region Public Methods
///
/// Tests to see if the passed in character matches the character at CharIndex of
/// either the StartsWith or EndsWith character sequence based on the state
/// of the Active property.
///
/// true, if character was matched, false otherwise.
/// The character being tested.
public bool MatchesCharacter(char c)
{
bool matches;
// Is this format currently active?
if (Active)
{
matches = c == EndsWith[CharIndex];
}
else
{
matches = (c == StartsWith[CharIndex]);
}
// Increment
if (matches)
{
++CharIndex;
}
return matches;
}
#endregion
}