apply_edits


Description:

public abstract async void apply_edits (ArrayList<FileChange> changes) throws Error

Efficiently apply multiple edits to the buffer.

Applies edits in reverse order (from end to start) to preserve line numbers. For GTK buffers: Uses GTK TextBuffer operations for efficient chunk editing. For dummy buffers: Works with in-memory lines array manipulation.

Process

  1. Ensure buffer is loaded

    2. Apply edits in reverse order (from end to start) to preserve line numbers

    3. For GTK buffers: Uses GTK TextBuffer operations

    4. For dummy buffers: Uses array manipulation

    5. Syncs to file (creates backup, writes, updates metadata)

FileChange Format

  • Line numbers are 1-based (inclusive start, exclusive end)
  • start == end indicates insertion
  • start != end indicates replacement

Important

Changes must be sorted descending by start line before calling.

Usage:

var changes = new Gee.ArrayList<FileChange>();
changes.add(new FileChange(10, 12, "new line 10\nnew line 11\n"));
changes.add(new FileChange(5, 6, "replacement for line 5\n"));

// Sort descending by start line (required)
changes.sort((a, b) => {
if (a.start > b.start) return -1;
if (a.start < b.start) return 1;
return 0;
});

try {
yield file.buffer.apply_edits(changes);
} catch (Error e) {
// Handle error
}

Parameters:

changes

List of FileChange objects to apply (must be sorted descending by start)

Exceptions:

Error

if edits cannot be applied