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
};
// @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) {

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
{
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;