Add proper selection modes and word jumping
This commit is contained in:
@@ -6,8 +6,6 @@ A TUI IDE.
|
||||
|
||||
# TODO
|
||||
|
||||
- [ ] Add support for ctrl + arrow key for moving words.
|
||||
- [ ] Add support for ctrl + backspace / delete for deleting words.
|
||||
- [ ] Add underline highlight for current word and all occurences.
|
||||
- [ ] Add `hooks` in files that can be set/unset/jumped to.
|
||||
- [ ] Add folding support at tree-sitter level (basic folding is done).
|
||||
@@ -18,7 +16,8 @@ A TUI IDE.
|
||||
- [ ] Add search / replace along with search / virtual cursors are searched pos.
|
||||
- [ ] Add support for undo/redo.
|
||||
- [ ] Add `.scm` files for all the supported languages. (2/14) Done.
|
||||
- [ ] Add splash screen / minigame jumping.
|
||||
- [ ] Add support for LSP & autocomplete / snippets.
|
||||
- [ ] Add codeium/copilot support.
|
||||
- [ ] Normalize / validate unicode on file open.
|
||||
- [ ] Add git stuff.
|
||||
- [ ] Add splash screen / minigame jumping.
|
||||
|
||||
@@ -11,6 +11,10 @@
|
||||
#include <shared_mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#define CHAR 0
|
||||
#define WORD 1
|
||||
#define LINE 2
|
||||
|
||||
struct Highlight {
|
||||
uint32_t fg;
|
||||
uint32_t bg;
|
||||
@@ -89,6 +93,7 @@ struct Editor {
|
||||
uint32_t cursor_preffered;
|
||||
Coord selection;
|
||||
bool selection_active;
|
||||
int selection_type;
|
||||
Coord position;
|
||||
Coord size;
|
||||
Coord scroll;
|
||||
@@ -123,5 +128,8 @@ void edit_erase(Editor *editor, Coord pos, int64_t len);
|
||||
void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len);
|
||||
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y);
|
||||
char *get_selection(Editor *editor, uint32_t *out_len);
|
||||
void word_boundaries(Editor *editor, Coord coord, uint32_t *prev_col,
|
||||
uint32_t *next_col, uint32_t *prev_clusters,
|
||||
uint32_t *next_clusters);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -61,5 +61,6 @@ char *detect_file_type(const char *filename);
|
||||
Language language_for_file(const char *filename);
|
||||
void copy_to_clipboard(const char *text, size_t len);
|
||||
char *get_from_clipboard(uint32_t *out_len);
|
||||
uint32_t count_clusters(const char *line, size_t len, size_t from, size_t to);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -74,14 +74,50 @@ void render_editor(Editor *editor) {
|
||||
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
uint32_t render_x = editor->position.col + numlen;
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
if (editor->selection_active) {
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
uint32_t prev_col, next_col;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, editor->selection, &prev_col, &next_col,
|
||||
nullptr, nullptr);
|
||||
start = {editor->selection.row, prev_col};
|
||||
end = editor->cursor;
|
||||
break;
|
||||
case LINE:
|
||||
start = {editor->selection.row, 0};
|
||||
end = editor->cursor;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
start = editor->cursor;
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
uint32_t prev_col, next_col, line_len;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, editor->selection, &prev_col, &next_col,
|
||||
nullptr, nullptr);
|
||||
end = {editor->selection.row, next_col};
|
||||
break;
|
||||
case LINE:
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->selection.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
end = {editor->selection.row, line_len};
|
||||
break;
|
||||
}
|
||||
}
|
||||
sel_start = line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
sel_end = line_to_byte(editor->root, end.row, nullptr) + end.col;
|
||||
@@ -89,7 +125,6 @@ void render_editor(Editor *editor) {
|
||||
Coord cursor = {UINT32_MAX, UINT32_MAX};
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
SpanCursor span_cursor(editor->spans);
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include <cstdint>
|
||||
extern "C" {
|
||||
#include "../libs/libgrapheme/grapheme.h"
|
||||
}
|
||||
@@ -10,23 +9,8 @@ extern "C" {
|
||||
void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
static std::chrono::steady_clock::time_point last_click_time =
|
||||
std::chrono::steady_clock::now();
|
||||
static uint32_t click_count = 0;
|
||||
static Coord last_click_pos = {UINT32_MAX, UINT32_MAX};
|
||||
if (event.key_type == KEY_SPECIAL) {
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
cursor_right(editor, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (event.key_type == KEY_MOUSE) {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
@@ -53,31 +37,92 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
break;
|
||||
case PRESS:
|
||||
if (event.mouse_button == LEFT_BTN) {
|
||||
if (duration < 250 &&
|
||||
last_click_pos == (Coord){event.mouse_x, event.mouse_y}) {
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
} else {
|
||||
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||
Coord cur_pos = {event.mouse_x, event.mouse_y};
|
||||
if (duration < 250 && last_click_pos == cur_pos)
|
||||
click_count++;
|
||||
else
|
||||
click_count = 1;
|
||||
last_click_time = now;
|
||||
last_click_pos = cur_pos;
|
||||
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (click_count == 1) {
|
||||
editor->cursor = p;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->selection = p;
|
||||
if (mode == SELECT) {
|
||||
mode = NORMAL;
|
||||
editor->selection_active = false;
|
||||
}
|
||||
last_click_pos = (Coord){event.mouse_x, event.mouse_y};
|
||||
last_click_time = now;
|
||||
} else if (click_count == 2) {
|
||||
uint32_t prev_col, next_col;
|
||||
word_boundaries(editor, editor->cursor, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
if (editor->cursor < editor->selection)
|
||||
editor->cursor = {editor->cursor.row, prev_col};
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, next_col};
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->selection_type = WORD;
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
} else if (click_count >= 3) {
|
||||
if (editor->cursor < editor->selection) {
|
||||
editor->cursor = {p.row, 0};
|
||||
} else {
|
||||
uint32_t line_len;
|
||||
LineIterator *it = begin_l_iter(editor->root, p.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
editor->cursor = {p.row, line_len};
|
||||
}
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->selection_type = LINE;
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
click_count = 3;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DRAG:
|
||||
if (event.mouse_button == LEFT_BTN) {
|
||||
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||
editor->cursor = p;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
if (!editor->selection_active) {
|
||||
editor->selection_active = true;
|
||||
editor->selection_type = CHAR;
|
||||
}
|
||||
uint32_t prev_col, next_col, line_len;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
editor->cursor = p;
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, p, &prev_col, &next_col, nullptr, nullptr);
|
||||
if (editor->cursor < editor->selection)
|
||||
editor->cursor = {p.row, prev_col};
|
||||
else
|
||||
editor->cursor = {p.row, next_col};
|
||||
break;
|
||||
case LINE:
|
||||
if (editor->cursor < editor->selection) {
|
||||
editor->cursor = {p.row, 0};
|
||||
} else {
|
||||
LineIterator *it = begin_l_iter(editor->root, p.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
editor->cursor = {p.row, line_len};
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case RELEASE:
|
||||
@@ -90,6 +135,57 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (event.key_type == KEY_SPECIAL) {
|
||||
switch (event.special_modifier) {
|
||||
case 0:
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
cursor_right(editor, 1);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CNTRL:
|
||||
uint32_t prev_col, next_col;
|
||||
word_boundaries(editor, editor->cursor, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (prev_col == editor->cursor.col)
|
||||
cursor_left(editor, 1);
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, prev_col};
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (next_col == editor->cursor.col)
|
||||
cursor_right(editor, 1);
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, next_col};
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ALT:
|
||||
// TODO: For up/down in insert/normal move line and in select move lines
|
||||
// overlapping selection up/down. right/left are normal
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch (mode) {
|
||||
case NORMAL:
|
||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||
@@ -133,6 +229,16 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
scroll_up(editor, 1);
|
||||
ensure_cursor(editor);
|
||||
break;
|
||||
case 'p':
|
||||
uint32_t len;
|
||||
char *text = get_from_clipboard(&len);
|
||||
if (text) {
|
||||
edit_insert(editor, editor->cursor, text, len);
|
||||
uint32_t grapheme_len = count_clusters(text, len, 0, len);
|
||||
cursor_right(editor, grapheme_len);
|
||||
free(text);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -147,6 +253,14 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
cursor_right(editor, 1);
|
||||
} else if (event.c[0] == 0x7F) {
|
||||
edit_erase(editor, editor->cursor, -1);
|
||||
} else if (event.c[0] == CTRL('W')) {
|
||||
uint32_t prev_col_byte, prev_col_cluster;
|
||||
word_boundaries(editor, editor->cursor, &prev_col_byte, nullptr,
|
||||
&prev_col_cluster, nullptr);
|
||||
if (prev_col_byte == editor->cursor.col)
|
||||
edit_erase(editor, editor->cursor, -1);
|
||||
else
|
||||
edit_erase(editor, editor->cursor, -(int64_t)prev_col_cluster);
|
||||
} else if (isprint((unsigned char)(event.c[0]))) {
|
||||
edit_insert(editor, editor->cursor, event.c, 1);
|
||||
cursor_right(editor, 1);
|
||||
@@ -160,7 +274,20 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
}
|
||||
} else if (event.key_type == KEY_SPECIAL &&
|
||||
event.special_key == KEY_DELETE) {
|
||||
edit_erase(editor, editor->cursor, 1);
|
||||
switch (event.special_modifier) {
|
||||
case 0:
|
||||
edit_erase(editor, editor->cursor, 1);
|
||||
break;
|
||||
case CNTRL:
|
||||
uint32_t next_col_byte, next_col_cluster;
|
||||
word_boundaries(editor, editor->cursor, nullptr, &next_col_byte,
|
||||
nullptr, &next_col_cluster);
|
||||
if (next_col_byte == editor->cursor.col)
|
||||
edit_erase(editor, editor->cursor, 1);
|
||||
else
|
||||
edit_erase(editor, editor->cursor, next_col_cluster);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SELECT:
|
||||
@@ -229,6 +356,61 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
free(event.c);
|
||||
}
|
||||
|
||||
uint32_t word_jump_right(const char *line, size_t len, uint32_t pos) {
|
||||
if (pos >= len)
|
||||
return len;
|
||||
size_t next = grapheme_next_word_break_utf8(line + pos, len - pos);
|
||||
return static_cast<uint32_t>(pos + next);
|
||||
}
|
||||
|
||||
uint32_t word_jump_left(const char *line, size_t len, uint32_t col) {
|
||||
if (col == 0)
|
||||
return 0;
|
||||
size_t pos = 0;
|
||||
size_t last = 0;
|
||||
size_t cursor = col;
|
||||
while (pos < len) {
|
||||
size_t next = pos + grapheme_next_word_break_utf8(line + pos, len - pos);
|
||||
if (next >= cursor)
|
||||
break;
|
||||
last = next;
|
||||
pos = next;
|
||||
}
|
||||
return static_cast<uint32_t>(last);
|
||||
}
|
||||
|
||||
void word_boundaries(Editor *editor, Coord coord, uint32_t *prev_col,
|
||||
uint32_t *next_col, uint32_t *prev_clusters,
|
||||
uint32_t *next_clusters) {
|
||||
if (!editor)
|
||||
return;
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
LineIterator *it = begin_l_iter(editor->root, coord.row);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
size_t col = coord.col;
|
||||
if (col > line_len)
|
||||
col = line_len;
|
||||
size_t left = word_jump_left(line, line_len, col);
|
||||
size_t right = word_jump_right(line, line_len, col);
|
||||
if (prev_col)
|
||||
*prev_col = static_cast<uint32_t>(left);
|
||||
if (next_col)
|
||||
*next_col = static_cast<uint32_t>(right);
|
||||
if (prev_clusters)
|
||||
*prev_clusters = count_clusters(line, line_len, left, col);
|
||||
if (next_clusters)
|
||||
*next_clusters = count_clusters(line, line_len, col, right);
|
||||
free(line);
|
||||
}
|
||||
|
||||
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
|
||||
if (mode == INSERT)
|
||||
x++;
|
||||
@@ -627,11 +809,46 @@ char *get_selection(Editor *editor, uint32_t *out_len) {
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
uint32_t prev_col, next_col;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, editor->selection, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
start = {editor->selection.row, prev_col};
|
||||
end = editor->cursor;
|
||||
break;
|
||||
case LINE:
|
||||
start = {editor->selection.row, 0};
|
||||
end = editor->cursor;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
start = editor->cursor;
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
uint32_t prev_col, next_col, line_len;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, editor->selection, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
end = {editor->selection.row, next_col};
|
||||
break;
|
||||
case LINE:
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->selection.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return nullptr;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
end = {editor->selection.row, line_len};
|
||||
break;
|
||||
}
|
||||
}
|
||||
uint32_t start_byte =
|
||||
line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
|
||||
@@ -156,8 +156,8 @@ KeyEvent read_key() {
|
||||
pos = 2;
|
||||
ret.special_modifier = 0;
|
||||
} else {
|
||||
pos = 4;
|
||||
switch (buf[3]) {
|
||||
pos = 5;
|
||||
switch (buf[4]) {
|
||||
case '2':
|
||||
ret.special_modifier = SHIFT;
|
||||
break;
|
||||
|
||||
@@ -21,19 +21,15 @@ void enable_raw_mode() {
|
||||
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1)
|
||||
exit(EXIT_FAILURE);
|
||||
atexit(disable_raw_mode);
|
||||
|
||||
struct termios raw = orig_termios;
|
||||
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
||||
raw.c_oflag &= ~(OPOST);
|
||||
raw.c_cflag |= (CS8);
|
||||
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
||||
raw.c_lflag |= ISIG;
|
||||
raw.c_cc[VMIN] = 0;
|
||||
raw.c_cc[VTIME] = 0;
|
||||
|
||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
std::string os = "\x1b[?1049h\x1b[2 q\x1b[?1002h\x1b[?25l";
|
||||
write(STDOUT_FILENO, os.c_str(), os.size());
|
||||
}
|
||||
@@ -44,8 +40,8 @@ Coord start_screen() {
|
||||
ioctl(0, TIOCGWINSZ, &w);
|
||||
rows = w.ws_row;
|
||||
cols = w.ws_col;
|
||||
screen.assign(rows * cols, {}); // allocate & zero-init
|
||||
old_screen.assign(rows * cols, {}); // allocate & zero-init
|
||||
screen.assign(rows * cols, {});
|
||||
old_screen.assign(rows * cols, {});
|
||||
return {rows, cols};
|
||||
}
|
||||
|
||||
|
||||
14
src/utils.cc
14
src/utils.cc
@@ -148,6 +148,20 @@ uint32_t get_bytes_from_visual_col(const char *line, uint32_t len,
|
||||
return current_byte;
|
||||
}
|
||||
|
||||
uint32_t count_clusters(const char *line, size_t len, size_t from, size_t to) {
|
||||
uint32_t count = 0;
|
||||
size_t pos = from;
|
||||
while (pos < to && pos < len) {
|
||||
size_t next =
|
||||
pos + grapheme_next_character_break_utf8(line + pos, len - pos);
|
||||
if (next > to)
|
||||
break;
|
||||
pos = next;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void log(const char *fmt, ...) {
|
||||
FILE *fp = fopen("/tmp/log.txt", "a");
|
||||
if (!fp)
|
||||
|
||||
Reference in New Issue
Block a user