Work on end of buffer

This commit is contained in:
Krzosa Karol
2024-06-28 17:24:58 +02:00
parent 2dacc969a3
commit d561883144
2 changed files with 19 additions and 9 deletions

View File

@@ -24,6 +24,8 @@ struct Range {
// <0,4> = 0,1,2,3 // <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 { struct Line {
int64_t number; int64_t number;
Range range; Range range;
@@ -453,7 +455,8 @@ BufferIter Iterate(Buffer &buffer, Range range, int64_t direction = ITERATE_FORW
range.min = Clamp(buffer, range.min); range.min = Clamp(buffer, range.min);
range.max = Clamp(buffer, range.max); 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) { if (direction == ITERATE_BACKWARD) {
result.end = range.min; result.end = range.min;
result.pos = range.max; 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) { Line GetLineByIndex(Buffer &buffer, int64_t line) {
Assert(buffer.lines.len); Assert(buffer.lines.len);
line = Clamp(line, (int64_t)0, buffer.lines.len - 1); line = Clamp(line, (int64_t)0, buffer.lines.len - 1);
Range range = buffer.lines[line]; Range range = buffer.lines[line];
Line result = {line, range, range.max}; 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; return result;
} }
Line FindLine(Buffer &buffer, int64_t pos) { Line FindLine(Buffer &buffer, int64_t pos) {
Line result = {}; Line result = {};
For(buffer.lines) { 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; bool found = pos >= it.min && pos < it.max;
if (found || found_last) { if (found || found_last) {
result = {buffer.lines.get_index(it), it, it.max}; 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; break;
} }
} }
@@ -489,8 +491,14 @@ Line FindLine(Buffer &buffer, int64_t pos) {
LineAndColumn FindLineAndColumn(Buffer &buffer, int64_t pos) { LineAndColumn FindLineAndColumn(Buffer &buffer, int64_t pos) {
LineAndColumn result = {}; LineAndColumn result = {};
result.column = 1; result.column = 0;
result.line = FindLine(buffer, pos); 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)) { 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 // @todo: make sure we handle when there is invalid unicode in the stream
if (iter.pos == pos) { if (iter.pos == pos) {

View File

@@ -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 // Add end of buffer as layout cell at the end
{ {
LayoutRow *row = layout.rows.alloc(); LayoutRow *row = NULL;
row->columns.allocator = *arena;
if (last_range->min == last_range->max) { 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 { } else {
text_offset_y -= line_spacing; text_offset_y -= line_spacing;
row = layout.rows.last();
} }
int index = GetGlyphIndex(font, ' '); int index = GetGlyphIndex(font, ' ');
GlyphInfo *glyph = font.glyphs + index; GlyphInfo *glyph = font.glyphs + index;