From d5618831447f0af99834c1126c38e06e4212b4a2 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 28 Jun 2024 17:24:58 +0200 Subject: [PATCH] Work on end of buffer --- src/text_editor/buffer.cpp | 20 ++++++++++++++------ src/text_editor/layout.cpp | 8 +++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 2e42def..c626317 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -24,6 +24,8 @@ struct Range { // <0,4> = 0,1,2,3 }; +// @end_of_buffer - need to make sure that we can actually lookup the end of buffer line. +// This end of buffer is also incorporated into the layout. struct Line { int64_t number; Range range; @@ -453,7 +455,8 @@ BufferIter Iterate(Buffer &buffer, Range range, int64_t direction = ITERATE_FORW range.min = Clamp(buffer, range.min); range.max = Clamp(buffer, range.max); - BufferIter result = {&buffer, range.min, range.max, direction}; + BufferIter result = {&buffer, range.min, range.max, direction}; + result.codepoint_index = -1; if (direction == ITERATE_BACKWARD) { result.end = range.min; result.pos = range.max; @@ -465,22 +468,21 @@ BufferIter Iterate(Buffer &buffer, Range range, int64_t direction = ITERATE_FORW Line GetLineByIndex(Buffer &buffer, int64_t line) { Assert(buffer.lines.len); - line = Clamp(line, (int64_t)0, buffer.lines.len - 1); Range range = buffer.lines[line]; Line result = {line, range, range.max}; - if (GetChar(buffer, range.max - 1) == '\n') result.max_without_new_line -= 1; + if (range.max > range.min && GetChar(buffer, range.max - 1) == '\n') result.max_without_new_line -= 1; return result; } Line FindLine(Buffer &buffer, int64_t pos) { Line result = {}; For(buffer.lines) { - bool found_last = (pos == it.min && pos == it.max); + bool found_last = (pos == it.min && pos == it.max); // @end_of_buffer bool found = pos >= it.min && pos < it.max; if (found || found_last) { result = {buffer.lines.get_index(it), it, it.max}; - if (GetChar(buffer, it.max - 1) == '\n') result.max_without_new_line -= 1; + if (it.max > it.min && GetChar(buffer, it.max - 1) == '\n') result.max_without_new_line -= 1; break; } } @@ -489,8 +491,14 @@ Line FindLine(Buffer &buffer, int64_t pos) { LineAndColumn FindLineAndColumn(Buffer &buffer, int64_t pos) { LineAndColumn result = {}; - result.column = 1; + result.column = 0; result.line = FindLine(buffer, pos); + + // @end_of_buffer + if (pos == result.line.range.min && pos == result.line.range.max) { + return result; + } + for (BufferIter iter = Iterate(buffer, result.line.range); IsValid(iter); Advance(&iter)) { // @todo: make sure we handle when there is invalid unicode in the stream if (iter.pos == pos) { diff --git a/src/text_editor/layout.cpp b/src/text_editor/layout.cpp index 4e8d320..a055496 100644 --- a/src/text_editor/layout.cpp +++ b/src/text_editor/layout.cpp @@ -75,12 +75,14 @@ Layout CalculateLayout(Arena *arena, Buffer &buffer, Font font, float font_size, // Add end of buffer as layout cell at the end { - LayoutRow *row = layout.rows.alloc(); - row->columns.allocator = *arena; + LayoutRow *row = NULL; if (last_range->min == last_range->max) { - text_offset_x = 0; + LayoutRow *row = layout.rows.alloc(); + row->columns.allocator = *arena; + text_offset_x = 0; } else { text_offset_y -= line_spacing; + row = layout.rows.last(); } int index = GetGlyphIndex(font, ' '); GlyphInfo *glyph = font.glyphs + index;