UNPKG

@phroun/paged-buffer

Version:

High-performance buffer system for editing massive files with intelligent memory management and undo/redo capabilities

237 lines 6.96 kB
/** * @fileoverview Enhanced Buffer Undo/Redo System with Line and Marks Integration * @author Jeffrey R. Day * @version 2.2.0 */ /// <reference types="node" /> /// <reference types="node" /> import { OperationType } from './types/common'; import { BufferOperation } from './buffer-operation'; import { type MarkTuple, type ILineAndMarksManager } from './types/common'; interface UndoConfig { maxUndoLevels?: number; mergeTimeWindow?: number; mergePositionWindow?: number; } interface TransactionOptions { [key: string]: any; } interface TransactionInfo { name: string; operationCount: number; startTime: number; duration: number; options: TransactionOptions; hasMarksSnapshot: boolean; hasLinesSnapshot: boolean; } interface UndoStats { undoGroups: number; redoGroups: number; totalUndoOperations: number; totalRedoOperations: number; currentGroupOperations: number; currentTransactionOperations: number; memoryUsage: number; maxUndoLevels: number; groupsWithMarksSnapshots: number; groupsWithLinesSnapshots: number; hasEnhancedTracking: boolean; } interface OperationDebugInfo { type: OperationType; position: number; dataLength: number; originalDataLength: number; } interface GroupDebugInfo { id: string; name: string | null; operationCount: number; isFromTransaction: boolean; hasMarksSnapshot: boolean; hasLinesSnapshot: boolean; marksCount: number; operations?: OperationDebugInfo[]; } interface UndoDebugInfo { undoStack: GroupDebugInfo[]; redoStack: GroupDebugInfo[]; activeTransaction: TransactionInfo | null; stats: UndoStats; } interface BufferInterface { virtualPageManager: { deleteRange(start: number, end: number): Promise<Buffer>; insertAt(position: number, data: Buffer): Promise<number>; }; lineAndMarksManager?: ILineAndMarksManager; markAsModified(): void; totalSize: number; getTotalSize(): number; } /** * Groups related operations together for undo/redo */ declare class OperationGroup { id: string; name: string | null; operations: BufferOperation[]; timestamp: number; isFromTransaction: boolean; marksSnapshot: MarkTuple[] | null; linesSnapshot: number | null; constructor(id: string, name?: string | null); /** * Calculate total memory usage of this group */ getMemoryUsage(): number; /** * Set marks snapshot for this group */ setMarksSnapshot(marks: MarkTuple[]): void; /** * Set lines snapshot for this group */ setLinesSnapshot(lineCount: number): void; } /** * Transaction for grouping operations */ declare class OperationTransaction { name: string; operations: BufferOperation[]; startTime: number; options: TransactionOptions; initialMarksSnapshot: MarkTuple[] | null; initialLinesSnapshot: number | null; constructor(name: string, options?: TransactionOptions); /** * Set initial state snapshots */ setInitialState(marks: MarkTuple[], lineCount: number): void; /** * Get info about this transaction */ getInfo(): TransactionInfo; } /** * Enhanced Buffer Undo/Redo System with Line and Marks Integration */ declare class BufferUndoSystem { private buffer; private maxUndoLevels; private undoStack; private redoStack; private activeTransaction; private mergeTimeWindow; private mergePositionWindow; private isUndoing; private groupIdCounter; private clockFunction; constructor(buffer: BufferInterface, maxUndoLevels?: number); /** * Configure the undo system */ configure(config: UndoConfig): void; /** * Set custom clock function (for testing) */ setClock(clockFn: () => number): void; /** * Get current time from clock function */ getClock(): number; /** * Generate unique group ID */ private _generateGroupId; /** * CRITICAL FIX: Capture current marks state for snapshot BEFORE any operation recording * This must be called by buffer operations BEFORE they execute */ captureCurrentMarksState(): MarkTuple[]; /** * Record an insert operation with enhanced tracking */ recordInsert(position: number, data: Buffer, timestamp?: number | null, preOpMarksSnapshot?: MarkTuple[] | null): BufferOperation; /** * Record a delete operation with enhanced tracking */ recordDelete(position: number, deletedData: Buffer, timestamp?: number | null, preOpMarksSnapshot?: MarkTuple[] | null): BufferOperation; /** * Record an overwrite operation with enhanced tracking */ recordOverwrite(position: number, newData: Buffer, originalData: Buffer, timestamp?: number | null, preOpMarksSnapshot?: MarkTuple[] | null): BufferOperation; /** * Enhanced operation recording with marks and lines tracking - FIXED SNAPSHOT TIMING */ private _recordOperation; /** * Helper method to get distance between operations */ private _getOperationDistance; /** * More conservative contiguous operation detection */ private _areContiguousOperations; /** * Begin a new transaction with state tracking */ beginUndoTransaction(name: string, options?: TransactionOptions): void; /** * Commit the current transaction with enhanced state tracking */ commitUndoTransaction(finalName?: string | null): boolean; /** * Enhanced rollback with marks and lines restoration */ rollbackUndoTransaction(): Promise<boolean>; /** * Check if currently in a transaction */ inTransaction(): boolean; /** * Get current transaction info */ getCurrentTransaction(): TransactionInfo | null; /** * Enhanced undo with marks and lines restoration */ undo(): Promise<boolean>; /** * Enhanced redo with marks and lines restoration */ redo(): Promise<boolean>; /** * CORRECTED: Restore marks state from snapshot using virtual addresses */ private _restoreMarksState; /** * Undo a single operation using Virtual Page Manager with enhanced tracking */ private _undoOperationVPM; private _redoOperationVPM; /** * Check if undo is available */ canUndo(): boolean; /** * Check if redo is available */ canRedo(): boolean; /** * Get enhanced undo/redo statistics */ getStats(): UndoStats; /** * Clear all undo/redo history */ clear(): void; /** * Get enhanced debug information */ getDebugInfo(): UndoDebugInfo; } export { BufferUndoSystem, OperationGroup, OperationTransaction, type UndoConfig, type TransactionOptions, type TransactionInfo, type UndoStats, type UndoDebugInfo, type BufferInterface }; //# sourceMappingURL=undo-system.d.ts.map