Refactor movement code
This commit is contained in:
@@ -95,20 +95,33 @@ Int MoveOnWhitespaceBoundaryUp(Buffer &buffer, Int pos) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Int MovePos(Buffer &buffer, Int pos, XY offset, bool ctrl_pressed) {
|
||||
Int result = pos;
|
||||
if (ctrl_pressed) {
|
||||
if (offset.col == 1 && offset.line == 0) return MoveOnWhitespaceBoundaryForward(buffer, pos);
|
||||
if (offset.col == -1 && offset.line == 0) return MoveOnWhitespaceBoundaryBackward(buffer, pos);
|
||||
if (offset.col == 0 && offset.line == 1) return MoveOnWhitespaceBoundaryDown(buffer, pos);
|
||||
if (offset.col == 0 && offset.line == -1) return MoveOnWhitespaceBoundaryUp(buffer, pos);
|
||||
} else {
|
||||
XY xy = PosToXY(buffer, pos);
|
||||
result = XYToPos(buffer, {xy.col + offset.col, xy.line + offset.line});
|
||||
}
|
||||
Int MovePosByXY(Buffer &buffer, Int pos, XY offset) {
|
||||
XY xy = PosToXY(buffer, pos);
|
||||
Int result = XYToPos(buffer, {xy.col + offset.col, xy.line + offset.line});
|
||||
return result;
|
||||
}
|
||||
|
||||
Int MoveCursor(Buffer &buffer, Int pos, int direction, bool ctrl_pressed) {
|
||||
Assert(direction >= 0 && direction <= 3);
|
||||
if (ctrl_pressed) {
|
||||
switch (direction) {
|
||||
case 0: return MoveOnWhitespaceBoundaryForward(buffer, pos);
|
||||
case 1: return MoveOnWhitespaceBoundaryBackward(buffer, pos);
|
||||
case 2: return MoveOnWhitespaceBoundaryDown(buffer, pos);
|
||||
case 3: return MoveOnWhitespaceBoundaryUp(buffer, pos);
|
||||
default: return pos;
|
||||
}
|
||||
} else {
|
||||
XY offsets[4] = {
|
||||
{ 1, 0},
|
||||
{-1, 0},
|
||||
{ 0, 1},
|
||||
{ 0, -1},
|
||||
};
|
||||
return MovePosByXY(buffer, pos, offsets[direction]);
|
||||
}
|
||||
}
|
||||
|
||||
void HandleKeybindings(View *_view) {
|
||||
View &view = *_view;
|
||||
Buffer &buf = *view.buffer;
|
||||
@@ -119,22 +132,16 @@ void HandleKeybindings(View *_view) {
|
||||
view.scroll.y -= GetMouseWheelMove() * 48;
|
||||
}
|
||||
|
||||
int keys[4] = {KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP};
|
||||
XY keys_offset[4] = {
|
||||
{ 1, 0},
|
||||
{-1, 0},
|
||||
{ 0, 1},
|
||||
{ 0, -1}
|
||||
};
|
||||
int keys[4] = {KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP};
|
||||
for (int i = 0; i < 4; i += 1) {
|
||||
if (IsKeyPressed(keys[i]) || IsKeyPressedRepeat(keys[i])) {
|
||||
For(view.cursors) {
|
||||
if (IsKeyDown(KEY_LEFT_SHIFT)) {
|
||||
Int front = GetFront(it);
|
||||
Int new_front = MovePos(buf, front, keys_offset[i], IsKeyDown(KEY_LEFT_CONTROL));
|
||||
Int new_front = MoveCursor(buf, front, i, IsKeyDown(KEY_LEFT_CONTROL));
|
||||
it = ChangeFront(it, new_front);
|
||||
} else {
|
||||
it.range.max = it.range.min = MovePos(buf, it.range.max, keys_offset[i], IsKeyDown(KEY_LEFT_CONTROL));
|
||||
it.range.max = it.range.min = MoveCursor(buf, it.range.max, i, IsKeyDown(KEY_LEFT_CONTROL));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user