Skip to content

Hex1bDocument

Namespace: Hex1b.Documents

Assembly: Hex1b.dll

Default IHex1bDocument implementation backed by a piece table. Internally byte-oriented: the piece table operates on UTF-8 byte sequences. Character-level API (GetText, Length, OffsetToPosition) is derived from bytes.

csharp
public sealed class Hex1bDocument : IHex1bDocument

Inheritance

ObjectHex1bDocument

Implements

Constructors

Hex1bDocument(byte[])

Creates a document from raw bytes (not necessarily valid UTF-8).

Parameters:

csharp
public Hex1bDocument(byte[] initialBytes)

Hex1bDocument(string)

Parameters:

csharp
public Hex1bDocument(string initialText = "")

Properties

ByteCount

Total byte count in the document's underlying byte buffer.

Returns: Int32

csharp
public int ByteCount { get; }

FilePath

The file path this document is associated with, or null for in-memory documents.

Returns: String

csharp
public string? FilePath { get; }

IsDirty

Whether the document has unsaved changes since the last save or load. Always false for documents without a .

Returns: Boolean

csharp
public bool IsDirty { get; }

Length

Total character count in the document.

Returns: Int32

csharp
public int Length { get; }

LineCount

Number of lines (at least 1, even for empty documents).

Returns: Int32

csharp
public int LineCount { get; }

Version

Monotonic version counter, incremented on every edit.

Returns: Int64

csharp
public long Version { get; }

Methods

Apply(EditOperation, string?)

Apply a single character-level edit operation.

Parameters:

Returns: EditResult

csharp
public EditResult Apply(EditOperation operation, string? source = null)

Apply(IReadOnlyList<EditOperation>, string?)

Apply multiple character-level edit operations atomically.

Parameters:

Returns: EditResult

csharp
public EditResult Apply(IReadOnlyList<EditOperation> operations, string? source = null)

ApplyBytes(ByteEditOperation, string?)

Apply a byte-level edit operation directly on the byte buffer.

Parameters:

Returns: EditResult

csharp
public EditResult ApplyBytes(ByteEditOperation operation, string? source = null)

BeginBatch()

Begins a batch of edits. While a batch is open, and events are deferred until is called. Batches may be nested; only the outermost triggers the rebuild.

This is critical for multi-cursor edits: without batching, each cursor's edit triggers a full O(n) cache rebuild (assemble bytes + UTF-8 decode + line scan). With batching, all piece-tree mutations happen first, then a single O(n) rebuild.

csharp
public void BeginBatch()

EndBatch()

Ends a batch of edits. When the outermost batch ends, rebuilds all caches once and fires a single event.

csharp
public void EndBatch()

GetByteMap()

Returns a cached byte↔char mapping. The map is lazily rebuilt when the document changes and reused across calls within the same document version.

Returns: Utf8ByteMap

csharp
public Utf8ByteMap GetByteMap()

GetBytes()

Get the full document content as raw bytes.

Returns: ReadOnlyMemory<Byte>

csharp
public ReadOnlyMemory<byte> GetBytes()

GetBytes(int, int)

Get a slice of the document's byte content.

Parameters:

Returns: ReadOnlyMemory<Byte>

csharp
public ReadOnlyMemory<byte> GetBytes(int byteOffset, int count)

GetDiagnosticInfo()

Returns a diagnostic snapshot of the document's internal structure. Returns null if the implementation does not support diagnostics.

Returns: DocumentDiagnosticInfo

csharp
public DocumentDiagnosticInfo GetDiagnosticInfo()

GetLineLength(int)

Get the length of a single line (1-based), excluding line ending.

Parameters:

Returns: Int32

csharp
public int GetLineLength(int line)

GetLineText(int)

Get the text of a single line (1-based).

Parameters:

Returns: String

csharp
public string GetLineText(int line)

GetText()

Get the full document text (UTF-8 decoded, with U+FFFD for invalid sequences).

Returns: String

csharp
public string GetText()

GetText(DocumentRange)

Get text within a character range.

Parameters:

Returns: String

csharp
public string GetText(DocumentRange range)

OffsetToPosition(DocumentOffset)

Convert an absolute character offset to a line/column position.

Parameters:

Returns: DocumentPosition

csharp
public DocumentPosition OffsetToPosition(DocumentOffset offset)

PositionToOffset(DocumentPosition)

Convert a line/column position to an absolute character offset.

Parameters:

Returns: DocumentOffset

csharp
public DocumentOffset PositionToOffset(DocumentPosition position)

SaveAsync(CancellationToken)

Saves the document content to its .

Parameters:

Returns: Task

csharp
public Task SaveAsync(CancellationToken ct = default)

Events

Changed

Fired after any edit is applied (character or byte level).

Returns: EventHandler<DocumentChangedEventArgs>

csharp
public event EventHandler<DocumentChangedEventArgs>? Changed

Released under the MIT License.