MoveOnWhitespaceBoundaryVertical
This commit is contained in:
@@ -7,6 +7,7 @@ const bool CTRL_PRESSED = true;
|
|||||||
|
|
||||||
Int MoveOnWhitespaceBoundary(Buffer &buffer, Int pos, int direction) {
|
Int MoveOnWhitespaceBoundary(Buffer &buffer, Int pos, int direction) {
|
||||||
Assert(direction == DIR_RIGHT || direction == DIR_LEFT);
|
Assert(direction == DIR_RIGHT || direction == DIR_LEFT);
|
||||||
|
bool right = direction == DIR_RIGHT;
|
||||||
|
|
||||||
pos = Clamp(pos, (Int)0, buffer.len);
|
pos = Clamp(pos, (Int)0, buffer.len);
|
||||||
bool standing_on_whitespace = IsWhitespace(buffer.str[pos]);
|
bool standing_on_whitespace = IsWhitespace(buffer.str[pos]);
|
||||||
@@ -16,24 +17,24 @@ Int MoveOnWhitespaceBoundary(Buffer &buffer, Int pos, int direction) {
|
|||||||
bool seek_word = standing_on_whitespace || standing_on_symbol;
|
bool seek_word = standing_on_whitespace || standing_on_symbol;
|
||||||
bool seek_symbol = standing_on_whitespace || standing_on_word;
|
bool seek_symbol = standing_on_whitespace || standing_on_word;
|
||||||
|
|
||||||
Int result = direction == DIR_RIGHT ? buffer.len : 0;
|
Int result = right ? buffer.len : 0;
|
||||||
Int delta = direction == DIR_RIGHT ? 1 : -1;
|
Int delta = right ? 1 : -1;
|
||||||
Int prev_pos = pos;
|
Int prev_pos = pos;
|
||||||
for (Int i = pos; direction == DIR_RIGHT ? i < buffer.len : i >= 0; i += delta) {
|
for (Int i = pos; right ? i < buffer.len : i >= 0; i += delta) {
|
||||||
bool is_whitespace = IsWhitespace(buffer.str[i]);
|
bool is_whitespace = IsWhitespace(buffer.str[i]);
|
||||||
bool is_symbol = IsSymbol(buffer.str[i]);
|
bool is_symbol = IsSymbol(buffer.str[i]);
|
||||||
bool is_word = !is_whitespace && !is_symbol;
|
bool is_word = !is_whitespace && !is_symbol;
|
||||||
Int r = direction == DIR_RIGHT ? i : prev_pos;
|
Int res = right ? i : prev_pos;
|
||||||
if (seek_word && is_word) {
|
if (seek_word && is_word) {
|
||||||
result = r;
|
result = res;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (seek_whitespace && is_whitespace) {
|
if (seek_whitespace && is_whitespace) {
|
||||||
result = r;
|
result = res;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (seek_symbol && is_symbol) {
|
if (seek_symbol && is_symbol) {
|
||||||
result = r;
|
result = res;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
prev_pos = i;
|
prev_pos = i;
|
||||||
@@ -41,29 +42,15 @@ Int MoveOnWhitespaceBoundary(Buffer &buffer, Int pos, int direction) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Int MoveOnWhitespaceBoundaryDown(Buffer &buffer, Int pos) {
|
Int MoveOnWhitespaceBoundaryVertical(Buffer &buffer, Int pos, int direction) {
|
||||||
Int result = pos;
|
Assert(direction == DIR_UP || direction == DIR_DOWN);
|
||||||
Int next_line = PosToLine(buffer, pos) + 1;
|
|
||||||
for (Int line = next_line; line < buffer.line_starts.len; line += 1) {
|
|
||||||
Range line_range = GetLineRange(buffer, line);
|
|
||||||
result = line_range.min;
|
|
||||||
|
|
||||||
bool whitespace_line = true;
|
bool up = direction == DIR_UP;
|
||||||
for (Int i = line_range.min; i < line_range.max; i += 1) {
|
Int delta = up ? -1 : 1;
|
||||||
if (!IsWhitespace(buffer.str[i])) {
|
|
||||||
whitespace_line = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (whitespace_line) break;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int MoveOnWhitespaceBoundaryUp(Buffer &buffer, Int pos) {
|
|
||||||
Int result = pos;
|
Int result = pos;
|
||||||
Int next_line = PosToLine(buffer, pos) - 1;
|
Int next_line = PosToLine(buffer, pos) + delta;
|
||||||
for (Int line = next_line; line >= 0; line -= 1) {
|
for (Int line = next_line; up ? line >= 0 : line < buffer.line_starts.len; line += delta) {
|
||||||
Range line_range = GetLineRange(buffer, line);
|
Range line_range = GetLineRange(buffer, line);
|
||||||
result = line_range.min;
|
result = line_range.min;
|
||||||
|
|
||||||
@@ -92,8 +79,8 @@ Int MovePos(Buffer &buffer, Int pos, int direction, bool ctrl_pressed = false) {
|
|||||||
switch (direction) {
|
switch (direction) {
|
||||||
case DIR_RIGHT: return MoveOnWhitespaceBoundary(buffer, pos, direction);
|
case DIR_RIGHT: return MoveOnWhitespaceBoundary(buffer, pos, direction);
|
||||||
case DIR_LEFT: return MoveOnWhitespaceBoundary(buffer, pos - 1, direction);
|
case DIR_LEFT: return MoveOnWhitespaceBoundary(buffer, pos - 1, direction);
|
||||||
case DIR_DOWN: return MoveOnWhitespaceBoundaryDown(buffer, pos);
|
case DIR_DOWN: return MoveOnWhitespaceBoundaryVertical(buffer, pos, direction);
|
||||||
case DIR_UP: return MoveOnWhitespaceBoundaryUp(buffer, pos);
|
case DIR_UP: return MoveOnWhitespaceBoundaryVertical(buffer, pos, direction);
|
||||||
default: return pos;
|
default: return pos;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user