Port Seek word, add ctrl+left

This commit is contained in:
Krzosa Karol
2024-06-23 09:17:54 +02:00
parent c0d69521fe
commit 10afbd3e4f
2 changed files with 86 additions and 16 deletions

View File

@@ -369,6 +369,55 @@ int64_t FindPos(Buffer &buffer, int64_t line_number, int64_t column) {
return result;
}
int64_t Seek(Buffer &buffer, int64_t pos, int64_t direction = ITERATE_FORWARD) {
Assert(direction == ITERATE_FORWARD || direction == ITERATE_BACKWARD);
// ( - inclusive
// < - non-inclusive
int64_t min = 0;
int64_t max = 0;
char c = 0;
if (direction == ITERATE_FORWARD) {
// (pos + 1, end>
min = AdjustUTF8Pos(buffer, pos + 1, ITERATE_FORWARD);
max = buffer.len;
c = GetChar(buffer, min);
} else {
// (0, pos>
max = pos;
min = 0;
int64_t next = AdjustUTF8Pos(buffer, max - 1, ITERATE_BACKWARD);
c = GetChar(buffer, next);
}
bool standing_on_whitespace = IsWhitespace(c);
bool seek_whitespace = standing_on_whitespace == false;
bool seek_word = standing_on_whitespace;
int64_t result = max;
BufferIter iter = Iterate(buffer, {min, max}, direction);
int64_t prev_pos = iter.pos;
for (; IsValid(iter); Advance(&iter)) {
bool char_is_whitespace = iter.item < 255 && IsWhitespace(iter.item);
if (seek_word && char_is_whitespace == false) {
result = prev_pos;
break;
}
if (seek_whitespace && char_is_whitespace) {
if (direction == ITERATE_FORWARD) {
result = iter.pos;
} else {
result = prev_pos;
}
break;
}
prev_pos = iter.pos;
}
return result;
}
void RunBufferTests() {
Scratch scratch;
{