Adding commands, BeforeEdit AfterEdit

This commit is contained in:
Krzosa Karol
2024-06-25 07:29:28 +02:00
parent 84f0b65fa9
commit 7ef502f3b0
2 changed files with 95 additions and 27 deletions

View File

@@ -211,6 +211,8 @@ void MergeSort(int64_t Count, Edit *First, Edit *Temp) {
void ApplyEdits(Buffer *buffer, Array<Edit> edits) {
Scratch scratch((Arena *)buffer->allocator.object);
// Figure out how much we insert and how much we delete so
// we can resize buffers properly if necessary
Assert(edits.len);
int64_t size_to_delete = 0;
int64_t size_to_insert = 0;
@@ -241,6 +243,7 @@ void ApplyEdits(Buffer *buffer, Array<Edit> edits) {
Array<Edit> edits_copy = edits.copy(scratch);
MergeSort(edits.len, edits_copy.data, edits.data);
// Try resizing the buffers
int64_t len_offset = size_to_insert - size_to_delete;
int64_t allocated_size_required = Max((int64_t)0, len_offset);
if (buffer->len + allocated_size_required > buffer->cap) {
@@ -257,6 +260,7 @@ void ApplyEdits(Buffer *buffer, Array<Edit> edits) {
buffer->cap = new_cap;
}
// Figure out what we need to write to the second buffer
int srci = buffer->bi;
int dsti = (buffer->bi + 1) % 2;
@@ -293,12 +297,14 @@ void ApplyEdits(Buffer *buffer, Array<Edit> edits) {
writes.add({dest_range, source_string});
}
// Make sure there are no gaps between ranges
#if DEBUG_BUILD
for (int64_t i = 0; i < writes.len - 1; i += 1) {
Assert(writes[i].range.max == writes[i + 1].range.min);
}
#endif
// Write to the second buffer
int64_t new_buffer_len = 0;
For(writes) {
Assert(it.range.min >= 0);