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

View File

@@ -105,6 +105,21 @@ int64_t MoveUp(Buffer &buffer, int64_t pos) {
return new_pos;
}
void UpdateCursorsAfterEdit(Window *window, Array<Edit> edits) {
ForItem(edit, edits) {
int64_t remove_size = GetRangeSize(edit.range);
int64_t insert_size = edit.string.len;
int64_t offset = insert_size - remove_size;
ForItem(cursor, window->cursors) {
if (edit.range.min <= cursor.min) {
cursor.min += offset;
cursor.max = cursor.min;
}
}
}
}
int main() {
InitScratch();
RunBufferTests();
@@ -168,7 +183,11 @@ int main() {
}
if (IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) {
For(focused_window->cursors) {
it.max = it.min = MoveRight(focused_window->buffer, it.min);
if (IsKeyDown(KEY_LEFT_CONTROL)) {
it.max = it.min = Seek(focused_window->buffer, it.min, ITERATE_FORWARD);
} else {
it.max = it.min = MoveRight(focused_window->buffer, it.min);
}
}
}
if (IsKeyPressed(KEY_DOWN) || IsKeyPressedRepeat(KEY_DOWN)) {
@@ -196,8 +215,22 @@ int main() {
}
}
// Handle user input chars
for (int c = GetCharPressed(); c; c = GetCharPressed()) {
if (IsKeyPressed(KEY_BACKSPACE) || IsKeyPressedRepeat(KEY_BACKSPACE)) {
Array<Edit> edits = {FrameArena};
String string = {};
For(focused_window->cursors) {
int64_t pos = MoveLeft(focused_window->buffer, it.min);
AddEdit(&edits, {pos, it.min}, string);
}
ApplyEdits(&focused_window->buffer, edits);
UpdateCursorsAfterEdit(focused_window, edits);
}
// Handle user input
for (;;) {
int c = GetCharPressed();
if (!c) break;
String string = "?";
UTF8Result utf8 = UTF32ToUTF8((uint32_t)c);
if (utf8.error == 0) {
@@ -209,19 +242,7 @@ int main() {
AddEdit(&edits, it, string);
}
ApplyEdits(&focused_window->buffer, edits);
ForItem(edit, edits) {
int64_t remove_size = GetRangeSize(edit.range);
int64_t add_size = string.len;
int64_t offset = add_size - remove_size;
ForItem(cursor, focused_window->cursors) {
if (edit.range.min <= cursor.min) {
cursor.min += offset;
cursor.max = cursor.min;
}
}
}
UpdateCursorsAfterEdit(focused_window, edits);
}
}