namespace AIStudio.Tools.DocumentSegments; /// /// Puts a segment of a document which is text itself together, while a format walks its markup. /// public sealed class TextSegmentBuilder { private readonly TaggedTextBuilder text = new(); private readonly Dictionary openings = []; private readonly Dictionary closings = []; private readonly Dictionary placeholders = []; private readonly HashSet optionalPlaceholders = []; /// /// Adds words, which a model works on. /// /// The words, as the source holds them. public void Words(string words) => this.text.Words(words); /// /// Opens a pair of tags for markup which encloses words, e.g., the start of a link. /// /// The markup before the words. /// The number of the pair. public int Open(string markup) { var id = this.text.Open(); this.openings[id] = markup; return id; } /// /// Closes a pair of tags. /// /// The number Open returned. /// The markup after the words, e.g., the end of a link with its address. public void Close(int id, string markup) { this.text.Close(id); this.closings[id] = markup; } /// /// Adds a tag of its own for markup which the model keeps as it is, e.g., a code span. /// /// The markup. /// Whether the writer leaves the tag out when the model dropped it, see TextSegment.OptionalPlaceholders. /// The number of the tag. public int Placeholder(string markup, bool isOptional = false) { var id = this.text.Placeholder(); this.placeholders[id] = markup; if (isOptional) this.optionalPlaceholders.Add(id); return id; } /// /// Adds markup to a tag of its own, e.g., the indentation of the line after a line break. /// /// The number Placeholder returned. /// The markup which follows. public void ExtendPlaceholder(int id, string markup) => this.placeholders[id] += markup; /// /// Finishes the segment, when a model can work on it. /// /// /// A segment whose words read as a tag stays out, also when they come from several parts of the source, e.g., an /// entity, words, and another entity in Markdown: a model could not tell them apart from the tags which guard the /// markup, see TaggedText.HasTagLikeWords. So does a segment without letters, e.g., a number, which stays as it /// is anyway. /// /// Where the segment starts in the source. /// Where the segment ends in the source, exclusive. /// Makes the words of this segment fit the format in place of what the writer gets; null for the writer's. /// The segment, or null when it stays out. public TextSegment? TryBuild(int start, int end, Func? formatWords = null) { var segment = this.Build(start, end, formatWords); return segment.Segment is { HasWords: true, Source.HasTagLikeWords: false } ? segment : null; } /// /// Finishes the segment. /// /// Where the segment starts in the source. /// Where the segment ends in the source, exclusive. /// Makes the words of this segment fit the format in place of what the writer gets; null for the writer's. public TextSegment Build(int start, int end, Func? formatWords = null) => new(start, end, new DocumentSegment(this.text.Build()), this.openings, this.closings, this.placeholders, formatWords, this.optionalPlaceholders); }