Full width line highlight and commands.cpp
This commit is contained in:
@@ -1 +1,156 @@
|
|||||||
|
inline bool Press(int key) {
|
||||||
|
bool result = IsKeyPressed(key) || IsKeyPressedRepeat(key);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool Shift() {
|
||||||
|
bool result = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool Ctrl() {
|
||||||
|
bool result = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool Alt() {
|
||||||
|
bool result = IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool CtrlPress(int key) {
|
||||||
|
bool result = Press(key) && Ctrl();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool ShiftPress(int key) {
|
||||||
|
bool result = Press(key) && Shift();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool CtrlShiftPress(int key) {
|
||||||
|
bool result = Press(key) && Shift() && Ctrl();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool CtrlAltPress(int key) {
|
||||||
|
bool result = Press(key) && Ctrl() && Alt();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Int MoveOnWhitespaceBoundaryForward(Buffer &buffer, Int pos) {
|
||||||
|
pos = Clamp(pos, (Int)0, buffer.len);
|
||||||
|
bool standing_on_whitespace = IsWhitespace(buffer.str[pos]);
|
||||||
|
bool seek_whitespace = standing_on_whitespace == false;
|
||||||
|
bool seek_word = standing_on_whitespace;
|
||||||
|
|
||||||
|
Int result = buffer.len;
|
||||||
|
Int prev_pos = pos;
|
||||||
|
for (Int i = pos; i < buffer.len; i += 1) {
|
||||||
|
bool whitespace = IsWhitespace(buffer.str[i]);
|
||||||
|
if (seek_word && !whitespace) {
|
||||||
|
result = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (seek_whitespace && whitespace) {
|
||||||
|
result = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
prev_pos = i;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Int MoveOnWhitespaceBoundaryBackward(Buffer &buffer, Int pos) {
|
||||||
|
pos = Clamp(pos - 1, (Int)0, buffer.len);
|
||||||
|
bool standing_on_whitespace = IsWhitespace(buffer.str[pos]);
|
||||||
|
bool seek_whitespace = standing_on_whitespace == false;
|
||||||
|
bool seek_word = standing_on_whitespace;
|
||||||
|
|
||||||
|
Int result = 0;
|
||||||
|
Int prev_pos = pos;
|
||||||
|
for (Int i = pos; i >= 0; i -= 1) {
|
||||||
|
bool whitespace = IsWhitespace(buffer.str[i]);
|
||||||
|
if (seek_word && !whitespace) {
|
||||||
|
result = prev_pos;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (seek_whitespace && whitespace) {
|
||||||
|
result = prev_pos;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
prev_pos = i;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Int MoveOnWhitespaceBoundaryDown(Buffer &buffer, Int pos) {
|
||||||
|
Int result = pos;
|
||||||
|
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;
|
||||||
|
for (Int i = line_range.min; i < line_range.max; i += 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 next_line = PosToLine(buffer, pos) - 1;
|
||||||
|
for (Int line = next_line; line >= 0; line -= 1) {
|
||||||
|
Range line_range = GetLineRange(buffer, line);
|
||||||
|
result = line_range.min;
|
||||||
|
|
||||||
|
bool whitespace_line = true;
|
||||||
|
for (Int i = line_range.min; i < line_range.max; i += 1) {
|
||||||
|
if (!IsWhitespace(buffer.str[i])) {
|
||||||
|
whitespace_line = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (whitespace_line) break;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Int MovePosByXY(Buffer &buffer, Int pos, XY offset) {
|
||||||
|
XY xy = PosToXY(buffer, pos);
|
||||||
|
Int result = XYToPosWithoutNL(buffer, {xy.col + offset.col, xy.line + offset.line});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int DIR_RIGHT = 0;
|
||||||
|
const int DIR_LEFT = 1;
|
||||||
|
const int DIR_DOWN = 2;
|
||||||
|
const int DIR_UP = 3;
|
||||||
|
|
||||||
|
Int MovePos(Buffer &buffer, Int pos, int direction, bool ctrl_pressed) {
|
||||||
|
ProfileFunction();
|
||||||
|
Assert(direction >= 0 && direction <= 3);
|
||||||
|
if (ctrl_pressed) {
|
||||||
|
switch (direction) {
|
||||||
|
case DIR_RIGHT: return MoveOnWhitespaceBoundaryForward(buffer, pos);
|
||||||
|
case DIR_LEFT: return MoveOnWhitespaceBoundaryBackward(buffer, pos);
|
||||||
|
case DIR_DOWN: return MoveOnWhitespaceBoundaryDown(buffer, pos);
|
||||||
|
case DIR_UP: return MoveOnWhitespaceBoundaryUp(buffer, pos);
|
||||||
|
default: return pos;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (direction) {
|
||||||
|
case DIR_RIGHT: return Clamp(pos + 1, (Int)0, buffer.len);
|
||||||
|
case DIR_LEFT: return Clamp(pos - 1, (Int)0, buffer.len);
|
||||||
|
case DIR_DOWN: return MovePosByXY(buffer, pos, {0, 1});
|
||||||
|
case DIR_UP: return MovePosByXY(buffer, pos, {0, -1});
|
||||||
|
default: return pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,9 +18,11 @@
|
|||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "raylib_utils.cpp"
|
#include "raylib_utils.cpp"
|
||||||
|
|
||||||
|
#include "commands.cpp"
|
||||||
|
#include "colors.cpp"
|
||||||
|
|
||||||
#include "view.h"
|
#include "view.h"
|
||||||
#include "view_commands.cpp"
|
#include "view_commands.cpp"
|
||||||
#include "colors.cpp"
|
|
||||||
#include "view_draw.cpp"
|
#include "view_draw.cpp"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -1,120 +1,3 @@
|
|||||||
Int MoveOnWhitespaceBoundaryForward(Buffer &buffer, Int pos) {
|
|
||||||
pos = Clamp(pos, (Int)0, buffer.len);
|
|
||||||
bool standing_on_whitespace = IsWhitespace(buffer.str[pos]);
|
|
||||||
bool seek_whitespace = standing_on_whitespace == false;
|
|
||||||
bool seek_word = standing_on_whitespace;
|
|
||||||
|
|
||||||
Int result = buffer.len;
|
|
||||||
Int prev_pos = pos;
|
|
||||||
for (Int i = pos; i < buffer.len; i += 1) {
|
|
||||||
bool whitespace = IsWhitespace(buffer.str[i]);
|
|
||||||
if (seek_word && !whitespace) {
|
|
||||||
result = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (seek_whitespace && whitespace) {
|
|
||||||
result = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
prev_pos = i;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int MoveOnWhitespaceBoundaryBackward(Buffer &buffer, Int pos) {
|
|
||||||
pos = Clamp(pos - 1, (Int)0, buffer.len);
|
|
||||||
bool standing_on_whitespace = IsWhitespace(buffer.str[pos]);
|
|
||||||
bool seek_whitespace = standing_on_whitespace == false;
|
|
||||||
bool seek_word = standing_on_whitespace;
|
|
||||||
|
|
||||||
Int result = 0;
|
|
||||||
Int prev_pos = pos;
|
|
||||||
for (Int i = pos; i >= 0; i -= 1) {
|
|
||||||
bool whitespace = IsWhitespace(buffer.str[i]);
|
|
||||||
if (seek_word && !whitespace) {
|
|
||||||
result = prev_pos;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (seek_whitespace && whitespace) {
|
|
||||||
result = prev_pos;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
prev_pos = i;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int MoveOnWhitespaceBoundaryDown(Buffer &buffer, Int pos) {
|
|
||||||
Int result = pos;
|
|
||||||
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;
|
|
||||||
for (Int i = line_range.min; i < line_range.max; i += 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 next_line = PosToLine(buffer, pos) - 1;
|
|
||||||
for (Int line = next_line; line >= 0; line -= 1) {
|
|
||||||
Range line_range = GetLineRange(buffer, line);
|
|
||||||
result = line_range.min;
|
|
||||||
|
|
||||||
bool whitespace_line = true;
|
|
||||||
for (Int i = line_range.min; i < line_range.max; i += 1) {
|
|
||||||
if (!IsWhitespace(buffer.str[i])) {
|
|
||||||
whitespace_line = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (whitespace_line) break;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int MovePosByXY(Buffer &buffer, Int pos, XY offset) {
|
|
||||||
XY xy = PosToXY(buffer, pos);
|
|
||||||
Int result = XYToPosWithoutNL(buffer, {xy.col + offset.col, xy.line + offset.line});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int DIR_RIGHT = 0;
|
|
||||||
const int DIR_LEFT = 1;
|
|
||||||
const int DIR_DOWN = 2;
|
|
||||||
const int DIR_UP = 3;
|
|
||||||
|
|
||||||
Int MovePos(Buffer &buffer, Int pos, int direction, bool ctrl_pressed) {
|
|
||||||
ProfileFunction();
|
|
||||||
Assert(direction >= 0 && direction <= 3);
|
|
||||||
if (ctrl_pressed) {
|
|
||||||
switch (direction) {
|
|
||||||
case DIR_RIGHT: return MoveOnWhitespaceBoundaryForward(buffer, pos);
|
|
||||||
case DIR_LEFT: return MoveOnWhitespaceBoundaryBackward(buffer, pos);
|
|
||||||
case DIR_DOWN: return MoveOnWhitespaceBoundaryDown(buffer, pos);
|
|
||||||
case DIR_UP: return MoveOnWhitespaceBoundaryUp(buffer, pos);
|
|
||||||
default: return pos;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
switch (direction) {
|
|
||||||
case DIR_RIGHT: return Clamp(pos + 1, (Int)0, buffer.len);
|
|
||||||
case DIR_LEFT: return Clamp(pos - 1, (Int)0, buffer.len);
|
|
||||||
case DIR_DOWN: return MovePosByXY(buffer, pos, {0, 1});
|
|
||||||
case DIR_UP: return MovePosByXY(buffer, pos, {0, -1});
|
|
||||||
default: return pos;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void AfterEdit(View *view, Array<Edit> edits) {
|
void AfterEdit(View *view, Array<Edit> edits) {
|
||||||
//
|
//
|
||||||
// Offset all cursors by edits
|
// Offset all cursors by edits
|
||||||
@@ -231,46 +114,6 @@ void Command_Delete(View *_view, int direction, bool ctrl = false) {
|
|||||||
Command_Replace(&view, {});
|
Command_Replace(&view, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Press(int key) {
|
|
||||||
bool result = IsKeyPressed(key) || IsKeyPressedRepeat(key);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Shift() {
|
|
||||||
bool result = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Ctrl() {
|
|
||||||
bool result = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Alt() {
|
|
||||||
bool result = IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool CtrlPress(int key) {
|
|
||||||
bool result = Press(key) && Ctrl();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool ShiftPress(int key) {
|
|
||||||
bool result = Press(key) && Shift();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool CtrlShiftPress(int key) {
|
|
||||||
bool result = Press(key) && Shift() && Ctrl();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool CtrlAltPress(int key) {
|
|
||||||
bool result = Press(key) && Ctrl() && Alt();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void HandleKeybindings(View *_view) {
|
void HandleKeybindings(View *_view) {
|
||||||
ProfileFunction();
|
ProfileFunction();
|
||||||
View &view = *_view;
|
View &view = *_view;
|
||||||
|
|||||||
@@ -71,9 +71,7 @@ void DrawCaret(const View &view, XY xy, float size, Color color) {
|
|||||||
void DrawLineHighlight(const View &view, XY fxy, Color color) {
|
void DrawLineHighlight(const View &view, XY fxy, Color color) {
|
||||||
Vec2I w = XYToWorldPos(view, XYLine(fxy.line));
|
Vec2I w = XYToWorldPos(view, XYLine(fxy.line));
|
||||||
w -= view.scroll;
|
w -= view.scroll;
|
||||||
Range range = GetLineRange(*view.buffer, fxy.line);
|
Rect2I rect = Rect2IFromSize(w, {GetRenderWidth(), view.line_spacing});
|
||||||
Int xsize = (Int)view.char_spacing * GetSize(range);
|
|
||||||
Rect2I rect = Rect2IFromSize(w, {xsize, view.line_spacing});
|
|
||||||
DrawRectangleRec(ToRectangle(rect), color);
|
DrawRectangleRec(ToRectangle(rect), color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user