Port Seek word, add ctrl+left
This commit is contained in:
@@ -369,6 +369,55 @@ int64_t FindPos(Buffer &buffer, int64_t line_number, int64_t column) {
|
|||||||
return result;
|
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() {
|
void RunBufferTests() {
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -105,6 +105,21 @@ int64_t MoveUp(Buffer &buffer, int64_t pos) {
|
|||||||
return new_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() {
|
int main() {
|
||||||
InitScratch();
|
InitScratch();
|
||||||
RunBufferTests();
|
RunBufferTests();
|
||||||
@@ -168,9 +183,13 @@ int main() {
|
|||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) {
|
if (IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) {
|
||||||
For(focused_window->cursors) {
|
For(focused_window->cursors) {
|
||||||
|
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);
|
it.max = it.min = MoveRight(focused_window->buffer, it.min);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (IsKeyPressed(KEY_DOWN) || IsKeyPressedRepeat(KEY_DOWN)) {
|
if (IsKeyPressed(KEY_DOWN) || IsKeyPressedRepeat(KEY_DOWN)) {
|
||||||
For(focused_window->cursors) {
|
For(focused_window->cursors) {
|
||||||
it.max = it.min = MoveDown(focused_window->buffer, it.min);
|
it.max = it.min = MoveDown(focused_window->buffer, it.min);
|
||||||
@@ -196,8 +215,22 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle user input chars
|
if (IsKeyPressed(KEY_BACKSPACE) || IsKeyPressedRepeat(KEY_BACKSPACE)) {
|
||||||
for (int c = GetCharPressed(); c; c = GetCharPressed()) {
|
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 = "?";
|
String string = "?";
|
||||||
UTF8Result utf8 = UTF32ToUTF8((uint32_t)c);
|
UTF8Result utf8 = UTF32ToUTF8((uint32_t)c);
|
||||||
if (utf8.error == 0) {
|
if (utf8.error == 0) {
|
||||||
@@ -209,19 +242,7 @@ int main() {
|
|||||||
AddEdit(&edits, it, string);
|
AddEdit(&edits, it, string);
|
||||||
}
|
}
|
||||||
ApplyEdits(&focused_window->buffer, edits);
|
ApplyEdits(&focused_window->buffer, edits);
|
||||||
|
UpdateCursorsAfterEdit(focused_window, 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user