Compare commits

...

3 Commits

Author SHA1 Message Date
f347f50dbe Stuff 2025-12-18 17:06:47 +00:00
f764c78c6b Folding update test 2025-12-18 15:40:25 +00:00
4f19c2f6c1 Stuff 2025-12-18 15:21:25 +00:00
11 changed files with 1234 additions and 679 deletions

View File

@@ -6,16 +6,14 @@ A TUI IDE.
# TODO # TODO
- [ ] Add folding support at tree-sitter level (basic folding is done).
- [ ] Add feature where doing enter uses tree-sitter to add newline with indentation. - [ ] Add feature where doing enter uses tree-sitter to add newline with indentation.
- it should also put stuff like `}` on the next line. - it should also put stuff like `}` on the next line.
- [ ] Add this thing where selection double click on a bracket selects whole block.
- (only on the first time) and sets mode to WORD.
- [ ] Add the highlight of block edges when cursor is on a bracket (or in). - [ ] Add the highlight of block edges when cursor is on a bracket (or in).
- [ ] Add support for brackets/quotes to auto-close. (also for backspace) - [ ] Add this thing where selection double click on a bracket selects whole block.
- (only on the first time) and sets mode to `WORD`.
- [ ] Add support for virtual cursor where edits apply at all the places. - [ ] Add support for virtual cursor where edits apply at all the places.
- [ ] Add search / replace along with search / virtual cursors are searched pos.
- [ ] Add alt + click to set multiple cursors. - [ ] Add alt + click to set multiple cursors.
- [ ] Add search / replace along with search / virtual cursors are searched pos.
- [ ] Add support for undo/redo. - [ ] Add support for undo/redo.
- [ ] Add `.scm` files for all the supported languages. (2/14) Done. - [ ] Add `.scm` files for all the supported languages. (2/14) Done.
- [ ] Add splash screen / minigame jumping. - [ ] Add splash screen / minigame jumping.
@@ -24,3 +22,5 @@ A TUI IDE.
- [ ] Normalize / validate unicode on file open. - [ ] Normalize / validate unicode on file open.
- [ ] Add git stuff. - [ ] Add git stuff.
- [ ] Fix bug where alt+up at eof adds extra line. - [ ] Fix bug where alt+up at eof adds extra line.
- [ ] Think about how i would keep fold states sensical if i added code prettying.
- [ ] Redo folding system and its relation to move_line_* functions.

View File

@@ -1,13 +1,3 @@
[
(function_definition)
(if_statement)
(case_statement)
(for_statement)
(while_statement)
(c_style_for_statement)
(heredoc_redirect)
] @fold
;; #bd9ae6 #000000 0 0 0 1 ;; #bd9ae6 #000000 0 0 0 1
[ [
"(" "("

View File

@@ -1,19 +1,3 @@
[
(method)
(singleton_method)
(class)
(module)
(if)
(else)
(case)
(when)
(in)
(do_block)
(singleton_class)
(heredoc_content)
(lambda)
] @fold
;; #ffffff #000000 0 0 0 1 ;; #ffffff #000000 0 0 0 1
[ [
(identifier) (identifier)

View File

@@ -10,12 +10,13 @@
#include <map> #include <map>
#include <shared_mutex> #include <shared_mutex>
#include <unordered_map> #include <unordered_map>
#include <vector>
#define CHAR 0 #define CHAR 0
#define WORD 1 #define WORD 1
#define LINE 2 #define LINE 2
#define EXTRA_META 3 #define EXTRA_META 4
struct Highlight { struct Highlight {
uint32_t fg; uint32_t fg;
@@ -39,6 +40,14 @@ struct Spans {
std::shared_mutex mtx; std::shared_mutex mtx;
}; };
struct Fold {
uint32_t start;
uint32_t end;
bool contains(uint32_t line) const { return line >= start && line <= end; }
bool operator<(const Fold &other) const { return start < other.start; }
};
struct SpanCursor { struct SpanCursor {
Spans &spans; Spans &spans;
size_t index = 0; size_t index = 0;
@@ -105,14 +114,66 @@ struct Editor {
const TSLanguage *language; const TSLanguage *language;
Queue<TSInputEdit> edit_queue; Queue<TSInputEdit> edit_queue;
std::vector<Highlight> query_map; std::vector<Highlight> query_map;
std::vector<int8_t> folded; std::vector<Fold> folds;
Spans spans; Spans spans;
Spans def_spans; Spans def_spans;
std::map<uint32_t, bool> folded_node;
uint32_t hooks[94]; uint32_t hooks[94];
bool jumper_set; bool jumper_set;
}; };
inline const Fold *fold_for_line(const std::vector<Fold> &folds,
uint32_t line) {
auto it = std::lower_bound(
folds.begin(), folds.end(), line,
[](const Fold &fold, uint32_t value) { return fold.start < value; });
if (it != folds.end() && it->start == line)
return &(*it);
if (it != folds.begin()) {
--it;
if (it->contains(line))
return &(*it);
}
return nullptr;
}
inline Fold *fold_for_line(std::vector<Fold> &folds, uint32_t line) {
const auto *fold =
fold_for_line(static_cast<const std::vector<Fold> &>(folds), line);
return const_cast<Fold *>(fold);
}
inline bool line_is_fold_start(const std::vector<Fold> &folds, uint32_t line) {
const Fold *fold = fold_for_line(folds, line);
return fold && fold->start == line;
}
inline bool line_is_folded(const std::vector<Fold> &folds, uint32_t line) {
return fold_for_line(folds, line) != nullptr;
}
inline uint32_t next_unfolded_row(const Editor *editor, uint32_t row) {
uint32_t limit = editor && editor->root ? editor->root->line_count : 0;
while (row < limit) {
const Fold *fold = fold_for_line(editor->folds, row);
if (!fold)
return row;
row = fold->end + 1;
}
return limit;
}
inline uint32_t prev_unfolded_row(const Editor *editor, uint32_t row) {
while (row > 0) {
const Fold *fold = fold_for_line(editor->folds, row);
if (!fold)
return row;
if (fold->start == 0)
return 0;
row = fold->start - 1;
}
return 0;
}
void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y); void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y);
Editor *new_editor(const char *filename, Coord position, Coord size); Editor *new_editor(const char *filename, Coord position, Coord size);
void free_editor(Editor *editor); void free_editor(Editor *editor);
@@ -124,7 +185,7 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number);
Coord move_right(Editor *editor, Coord cursor, uint32_t number); Coord move_right(Editor *editor, Coord cursor, uint32_t number);
void cursor_left(Editor *editor, uint32_t number); void cursor_left(Editor *editor, uint32_t number);
void cursor_right(Editor *editor, uint32_t number); void cursor_right(Editor *editor, uint32_t number);
void scroll_up(Editor *editor, uint32_t number); void scroll_up(Editor *editor, int32_t number);
void scroll_down(Editor *editor, uint32_t number); void scroll_down(Editor *editor, uint32_t number);
void ensure_cursor(Editor *editor); void ensure_cursor(Editor *editor);
void ensure_scroll(Editor *editor); void ensure_scroll(Editor *editor);
@@ -141,5 +202,11 @@ void word_boundaries(Editor *editor, Coord coord, uint32_t *prev_col,
uint32_t *next_clusters); uint32_t *next_clusters);
void word_boundaries_exclusive(Editor *editor, Coord coord, uint32_t *prev_col, void word_boundaries_exclusive(Editor *editor, Coord coord, uint32_t *prev_col,
uint32_t *next_col); uint32_t *next_col);
std::vector<Fold>::iterator find_fold_iter(Editor *editor, uint32_t line);
bool add_fold(Editor *editor, uint32_t start, uint32_t end);
bool remove_fold(Editor *editor, uint32_t line);
void apply_line_insertion(Editor *editor, uint32_t line, uint32_t rows);
void apply_line_deletion(Editor *editor, uint32_t removal_start,
uint32_t removal_end);
#endif #endif

View File

@@ -117,6 +117,14 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line);
// freed by the caller // freed by the caller
char *next_line(LineIterator *it, uint32_t *out_len); char *next_line(LineIterator *it, uint32_t *out_len);
// Returns the previous line as a null terminated string
// `it` is the iterator returned from begin_l_iter
// it can be used to iterate backwards
// and can be used along with next_line
// doing prev_line then next_line or vice versa will return the same line
// `out_len` is set to the length of the returned string
char *prev_line(LineIterator *it, uint32_t *out_len);
// Used to start an iterator over leaf data // Used to start an iterator over leaf data
// root is the root of the rope // root is the root of the rope
// the caller must free the iterator after use // the caller must free the iterator after use

View File

@@ -23,7 +23,6 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
editor->cursor_preffered = UINT32_MAX; editor->cursor_preffered = UINT32_MAX;
editor->root = load(str, len, optimal_chunk_size(len)); editor->root = load(str, len, optimal_chunk_size(len));
free(str); free(str);
editor->folded.resize(editor->root->line_count + 2);
if (len <= (1024 * 128)) { if (len <= (1024 * 128)) {
editor->parser = ts_parser_new(); editor->parser = ts_parser_new();
Language language = language_for_file(filename); Language language = language_for_file(filename);
@@ -45,24 +44,6 @@ void free_editor(Editor *editor) {
delete editor; delete editor;
} }
void fold(Editor *editor, uint32_t start_line, uint32_t end_line) {
if (!editor)
return;
for (uint32_t i = start_line; i <= end_line && i < editor->size.row; i++)
editor->folded[i] = 1;
editor->folded[start_line] = 2;
}
void update_render_fold_marker(uint32_t row, uint32_t cols) {
const char *marker = "... folded ...";
uint32_t len = strlen(marker);
uint32_t i = 0;
for (; i < len && i < cols; i++)
update(row, i, (char[2]){marker[i], 0}, 0xc6c6c6, 0, 0);
for (; i < cols; i++)
update(row, i, " ", 0xc6c6c6, 0, 0);
}
void render_editor(Editor *editor) { void render_editor(Editor *editor) {
uint32_t sel_start = 0, sel_end = 0; uint32_t sel_start = 0, sel_end = 0;
uint32_t numlen = uint32_t numlen =
@@ -74,6 +55,7 @@ void render_editor(Editor *editor) {
if (editor->hooks[i] != 0) if (editor->hooks[i] != 0)
v.push_back({editor->hooks[i], '!' + i}); v.push_back({editor->hooks[i], '!' + i});
std::sort(v.begin(), v.end()); std::sort(v.begin(), v.end());
auto hook_it = v.begin();
std::shared_lock knot_lock(editor->knot_mtx); std::shared_lock knot_lock(editor->knot_mtx);
if (editor->selection_active) { if (editor->selection_active) {
Coord start, end; Coord start, end;
@@ -135,12 +117,29 @@ void render_editor(Editor *editor) {
span_cursor.sync(global_byte_offset); span_cursor.sync(global_byte_offset);
def_span_cursor.sync(global_byte_offset); def_span_cursor.sync(global_byte_offset);
while (rendered_rows < editor->size.row) { while (rendered_rows < editor->size.row) {
if (editor->folded[line_index]) { const Fold *fold = fold_for_line(editor->folds, line_index);
if (editor->folded[line_index] == 2) { if (fold) {
update_render_fold_marker(rendered_rows, render_width); update(editor->position.row + rendered_rows, editor->position.col, "",
0xAAAAAA, 0, 0);
char buf[16];
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 3, fold->start + 1);
uint32_t num_color =
editor->cursor.row == fold->start ? 0xFFFFFF : 0x555555;
for (int i = 0; i < len; i++)
update(editor->position.row + rendered_rows,
editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color, 0,
0);
const char marker[15] = "... folded ...";
uint32_t i = 0;
for (; i < 14 && i < render_width; i++)
update(rendered_rows, i + render_x, (char[2]){marker[i], 0}, 0xc6c6c6,
0, 0);
for (; i < render_width; i++)
update(rendered_rows, i + render_x, " ", 0xc6c6c6, 0, 0);
rendered_rows++; rendered_rows++;
}
do { uint32_t skip_until = fold->end;
while (line_index <= skip_until) {
uint32_t line_len; uint32_t line_len;
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
if (!line) if (!line)
@@ -151,8 +150,7 @@ void render_editor(Editor *editor) {
global_byte_offset++; global_byte_offset++;
free(line); free(line);
line_index++; line_index++;
} while (line_index < editor->size.row && }
editor->folded[line_index] == 1);
continue; continue;
} }
uint32_t line_len; uint32_t line_len;
@@ -167,21 +165,27 @@ void render_editor(Editor *editor) {
while (current_byte_offset < line_len && rendered_rows < editor->size.row) { while (current_byte_offset < line_len && rendered_rows < editor->size.row) {
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0; uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
if (current_byte_offset == 0 || rendered_rows == 0) { if (current_byte_offset == 0 || rendered_rows == 0) {
char buf[EXTRA_META + 16]; const char *hook = nullptr;
char hook = ' '; char h[2] = {0, 0};
for (auto &p : v) { auto it2 = hook_it;
if (p.first == line_index + 1) { for (; it2 != v.end(); ++it2) {
hook = p.second; if (it2->first == line_index + 1) {
h[0] = it2->second;
hook = h;
hook_it = it2;
break; break;
} }
} }
int len = snprintf(buf, sizeof(buf), "%c%*u", hook, numlen - 2, update(editor->position.row + rendered_rows, editor->position.col, hook,
line_index + 1); 0xAAAAAA, 0, 0);
char buf[16];
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 3, line_index + 1);
uint32_t num_color = uint32_t num_color =
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555; editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
update(editor->position.row + rendered_rows, editor->position.col + i, update(editor->position.row + rendered_rows,
std::string(1, buf[i]).c_str(), num_color, 0, 0); editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color,
0, 0);
} else { } else {
for (uint32_t i = 0; i < numlen; i++) for (uint32_t i = 0; i < numlen; i++)
update(editor->position.row + rendered_rows, editor->position.col + i, update(editor->position.row + rendered_rows, editor->position.col + i,
@@ -252,21 +256,27 @@ void render_editor(Editor *editor) {
if (line_len == 0 || if (line_len == 0 ||
(current_byte_offset >= line_len && rendered_rows == 0)) { (current_byte_offset >= line_len && rendered_rows == 0)) {
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0; uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
char buf[EXTRA_META + 16]; const char *hook = nullptr;
char hook = ' '; char h[2] = {0, 0};
for (auto &p : v) { auto it2 = hook_it;
if (p.first == line_index + 1) { for (; it2 != v.end(); ++it2) {
hook = p.second; if (it2->first == line_index + 1) {
h[0] = it2->second;
hook = h;
hook_it = it2;
break; break;
} }
} }
int len = update(editor->position.row + rendered_rows, editor->position.col, hook,
snprintf(buf, sizeof(buf), "%c%*u", hook, numlen - 2, line_index + 1); 0xAAAAAA, 0, 0);
char buf[16];
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 3, line_index + 1);
uint32_t num_color = uint32_t num_color =
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555; editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
update(editor->position.row + rendered_rows, editor->position.col + i, update(editor->position.row + rendered_rows,
std::string(1, buf[i]).c_str(), num_color, 0, 0); editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color, 0,
0);
if (editor->cursor.row == line_index) { if (editor->cursor.row == line_index) {
cursor.row = editor->position.row + rendered_rows; cursor.row = editor->position.row + rendered_rows;
cursor.col = render_x; cursor.col = render_x;

View File

@@ -1,450 +1,11 @@
#include <cstdint>
extern "C" { extern "C" {
#include "../libs/libgrapheme/grapheme.h" #include "../libs/libgrapheme/grapheme.h"
} }
#include "../include/editor.h" #include "../include/editor.h"
#include "../include/main.h" #include "../include/main.h"
#include "../include/ts.h"
#include "../include/utils.h" #include "../include/utils.h"
#include <cmath> #include <cmath>
static Highlight HL_UNDERLINE = {0, 0, 1 << 2, 100};
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_MOUSE) {
auto now = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
now - last_click_time)
.count();
switch (event.mouse_state) {
case SCROLL:
switch (event.mouse_direction) {
case SCROLL_UP:
scroll_up(editor, 10);
ensure_cursor(editor);
break;
case SCROLL_DOWN:
scroll_down(editor, 10);
ensure_cursor(editor);
break;
case SCROLL_LEFT:
cursor_left(editor, 10);
break;
case SCROLL_RIGHT:
cursor_right(editor, 10);
break;
}
break;
case PRESS:
if (event.mouse_button == LEFT_BTN) {
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->selection = p;
if (mode == SELECT) {
mode = NORMAL;
editor->selection_active = false;
}
} 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--;
free(line);
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_preffered = UINT32_MAX;
mode = SELECT;
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--;
free(line);
editor->cursor = {p.row, line_len};
}
break;
}
}
break;
case RELEASE:
if (event.mouse_button == LEFT_BTN)
if (editor->cursor.row == editor->selection.row &&
editor->cursor.col == editor->selection.col) {
mode = NORMAL;
editor->selection_active = false;
}
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, 5);
break;
case KEY_UP:
cursor_up(editor, 5);
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:
switch (event.special_key) {
case KEY_DOWN:
move_line_down(editor);
break;
case KEY_UP:
move_line_up(editor);
break;
case KEY_LEFT:
cursor_left(editor, 8);
break;
case KEY_RIGHT:
cursor_right(editor, 8);
break;
}
break;
}
}
switch (mode) {
case NORMAL:
if (event.key_type == KEY_CHAR && event.len == 1) {
switch (event.c[0]) {
case 'a':
mode = INSERT;
cursor_right(editor, 1);
break;
case 'i':
mode = INSERT;
break;
case 'n':
mode = JUMPER;
editor->jumper_set = true;
break;
case 'm':
mode = JUMPER;
editor->jumper_set = false;
break;
case 'N':
for (uint8_t i = 0; i < 94; i++)
if (editor->hooks[i] == editor->cursor.row + 1) {
editor->hooks[i] = 0;
break;
}
break;
case 's':
case 'v':
mode = SELECT;
editor->selection_active = true;
editor->selection = editor->cursor;
break;
case ';':
case ':':
mode = RUNNER;
break;
case 0x7F:
cursor_left(editor, 1);
break;
case ' ':
cursor_right(editor, 1);
break;
case '\r':
case '\n':
cursor_down(editor, 1);
break;
case '\\':
case '|':
cursor_up(editor, 1);
break;
case CTRL('d'):
scroll_down(editor, 1);
ensure_cursor(editor);
break;
case CTRL('u'):
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;
case INSERT:
if (event.key_type == KEY_CHAR) {
if (event.len == 1) {
if (event.c[0] == '\t') {
edit_insert(editor, editor->cursor, (char *)" ", 1);
cursor_right(editor, 2);
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
edit_insert(editor, editor->cursor, (char *)"\n", 1);
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);
} else if (event.c[0] == 0x1B) {
mode = NORMAL;
cursor_left(editor, 1);
}
} else if (event.len > 1) {
edit_insert(editor, editor->cursor, event.c, event.len);
cursor_right(editor, 1);
}
} else if (event.key_type == KEY_SPECIAL &&
event.special_key == KEY_DELETE) {
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:
if (event.key_type == KEY_CHAR && event.len == 1) {
uint32_t len;
char *text;
switch (event.c[0]) {
case 0x1B:
case 's':
case 'v':
editor->selection_active = false;
mode = NORMAL;
break;
case 'y':
text = get_selection(editor, &len);
copy_to_clipboard(text, len);
free(text);
editor->selection_active = false;
mode = NORMAL;
break;
case 'x':
text = get_selection(editor, &len);
copy_to_clipboard(text, len);
edit_erase(editor, MIN(editor->cursor, editor->selection), len);
free(text);
editor->selection_active = false;
mode = NORMAL;
break;
case 'p':
text = get_from_clipboard(&len);
if (text) {
Coord start, end;
if (editor->cursor >= editor->selection) {
start = editor->selection;
end = move_right(editor, editor->cursor, 1);
} else {
start = editor->cursor;
end = move_right(editor, editor->selection, 1);
}
uint32_t start_byte =
line_to_byte(editor->root, start.row, nullptr) + start.col;
uint32_t end_byte =
line_to_byte(editor->root, end.row, nullptr) + end.col;
edit_erase(editor, start, end_byte - start_byte);
edit_insert(editor, editor->cursor, text, len);
free(text);
}
editor->selection_active = false;
mode = NORMAL;
break;
}
}
break;
case JUMPER:
if (event.key_type == KEY_CHAR && event.len == 1 &&
(event.c[0] > '!' && event.c[0] < '~')) {
if (editor->jumper_set) {
for (uint8_t i = 0; i < 94; i++)
if (editor->hooks[i] == editor->cursor.row + 1) {
editor->hooks[i] = 0;
break;
}
editor->hooks[event.c[0] - '!'] = editor->cursor.row + 1;
} else {
uint32_t line = editor->hooks[event.c[0] - '!'];
if (line > 0) {
editor->cursor = {line - 1, 0};
editor->cursor_preffered = UINT32_MAX;
}
}
}
mode = NORMAL;
break;
case RUNNER:
if (event.key_type == KEY_CHAR && event.len == 1) {
switch (event.c[0]) {
case 0x1B:
mode = NORMAL;
break;
}
}
break;
}
ensure_scroll(editor);
if (event.key_type == KEY_CHAR && event.c)
free(event.c);
}
void editor_worker(Editor *editor) {
if (!editor || !editor->root)
return;
if (editor->parser && editor->query)
ts_collect_spans(editor);
uint32_t prev_col, next_col;
word_boundaries_exclusive(editor, editor->cursor, &prev_col, &next_col);
if (next_col - prev_col > 0 && next_col - prev_col < 256 - 4) {
std::shared_lock lock(editor->knot_mtx);
uint32_t offset = line_to_byte(editor->root, editor->cursor.row, nullptr);
char *word = read(editor->root, offset + prev_col, next_col - prev_col);
if (word) {
char buf[256];
snprintf(buf, sizeof(buf), "\\b%s\\b", word);
std::vector<std::pair<size_t, size_t>> results =
search_rope(editor->root, buf);
std::unique_lock lock(editor->def_spans.mtx);
editor->def_spans.spans.clear();
for (const auto &match : results) {
Span s;
s.start = match.first;
s.end = match.first + match.second;
s.hl = &HL_UNDERLINE;
editor->def_spans.spans.push_back(s);
}
std::sort(editor->def_spans.spans.begin(), editor->def_spans.spans.end());
lock.unlock();
free(word);
}
} else {
std::unique_lock lock(editor->def_spans.mtx);
editor->def_spans.spans.clear();
lock.unlock();
}
}
uint32_t scan_left(const char *line, uint32_t len, uint32_t off) { uint32_t scan_left(const char *line, uint32_t len, uint32_t off) {
if (off > len) if (off > len)
off = len; off = len;
@@ -564,6 +125,7 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
x++; x++;
uint32_t numlen = uint32_t numlen =
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1)); EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
bool is_gutter_click = (x < numlen);
uint32_t render_width = editor->size.col - numlen; uint32_t render_width = editor->size.col - numlen;
x = MAX(x, numlen) - numlen; x = MAX(x, numlen) - numlen;
uint32_t target_visual_row = y; uint32_t target_visual_row = y;
@@ -575,21 +137,24 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
if (!it) if (!it)
return editor->scroll; return editor->scroll;
while (visual_row <= target_visual_row) { while (visual_row <= target_visual_row) {
if (editor->folded[line_index]) { const Fold *fold = fold_for_line(editor->folds, line_index);
if (editor->folded[line_index] == 2) { if (fold) {
if (visual_row == target_visual_row) { if (visual_row == target_visual_row) {
free(it); free(it);
return {line_index, 0}; if (is_gutter_click) {
remove_fold(editor, fold->start);
return {UINT32_MAX, UINT32_MAX};
}
return {fold->start > 0 ? fold->start - 1 : 0, 0};
} }
visual_row++; visual_row++;
} while (line_index <= fold->end) {
do {
char *l = next_line(it, nullptr); char *l = next_line(it, nullptr);
if (!l) if (!l)
break; break;
free(l); free(l);
line_index++; line_index++;
} while (editor->folded[line_index] == 1); }
continue; continue;
} }
uint32_t line_len; uint32_t line_len;
@@ -642,7 +207,7 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
return editor->scroll; return editor->scroll;
} }
Coord move_right(Editor *editor, Coord cursor, uint32_t number) { Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
Coord result = cursor; Coord result = cursor;
if (!editor || !editor->root || number == 0) if (!editor || !editor->root || number == 0)
return result; return result;
@@ -661,9 +226,108 @@ Coord move_right(Editor *editor, Coord cursor, uint32_t number) {
free(line); free(line);
line = nullptr; line = nullptr;
uint32_t next_row = row + 1; uint32_t next_row = row + 1;
while (next_row < editor->root->line_count && if (next_row >= editor->root->line_count) {
editor->folded[next_row] != 0) col = line_len;
next_row++; break;
}
row = next_row;
col = 0;
it = begin_l_iter(editor->root, row);
line = next_line(it, &line_len);
free(it);
if (!line)
break;
if (line_len > 0 && line[line_len - 1] == '\n')
--line_len;
} else {
uint32_t inc =
grapheme_next_character_break_utf8(line + col, line_len - col);
if (inc == 0)
break;
col += inc;
}
number--;
}
if (line)
free(line);
result.row = row;
result.col = col;
return result;
}
Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number) {
Coord result = cursor;
if (!editor || !editor->root || number == 0)
return result;
uint32_t row = result.row;
uint32_t col = result.col;
uint32_t len = 0;
LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &len);
if (!line) {
free(it);
return result;
}
if (len > 0 && line[len - 1] == '\n')
--len;
bool iterator_ahead = true;
while (number > 0) {
if (col == 0) {
if (row == 0)
break;
if (iterator_ahead) {
free(prev_line(it, nullptr));
iterator_ahead = false;
}
free(line);
line = nullptr;
row--;
line = prev_line(it, &len);
if (!line)
break;
if (len > 0 && line[len - 1] == '\n')
--len;
col = len;
} else {
uint32_t new_col = 0;
while (new_col < col) {
uint32_t inc =
grapheme_next_character_break_utf8(line + new_col, len - new_col);
if (new_col + inc >= col)
break;
new_col += inc;
}
col = new_col;
}
number--;
}
if (line)
free(line);
free(it);
result.row = row;
result.col = col;
return result;
}
Coord move_right(Editor *editor, Coord cursor, uint32_t number) {
Coord result = cursor;
if (!editor || !editor->root || number == 0)
return result;
uint32_t row = result.row;
uint32_t col = result.col;
uint32_t line_len = 0;
LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &line_len);
free(it);
if (!line)
return result;
if (line_len > 0 && line[line_len - 1] == '\n')
--line_len;
while (number > 0) {
if (col >= line_len) {
free(line);
line = nullptr;
uint32_t next_row = next_unfolded_row(editor, row + 1);
if (next_row >= editor->root->line_count) { if (next_row >= editor->root->line_count) {
col = line_len; col = line_len;
break; break;
@@ -702,26 +366,43 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
uint32_t len = 0; uint32_t len = 0;
LineIterator *it = begin_l_iter(editor->root, row); LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &len); char *line = next_line(it, &len);
if (!line) {
free(it); free(it);
if (!line)
return result; return result;
}
if (len > 0 && line[len - 1] == '\n') if (len > 0 && line[len - 1] == '\n')
--len; --len;
bool iterator_ahead = true;
while (number > 0) { while (number > 0) {
if (col == 0) { if (col == 0) {
free(line);
line = nullptr;
if (row == 0) if (row == 0)
break; break;
int32_t prev_row = row - 1; if (iterator_ahead) {
while (prev_row >= 0 && editor->folded[prev_row] != 0) free(prev_line(it, nullptr));
prev_row--; iterator_ahead = false;
if (prev_row < 0) }
free(line);
line = nullptr;
while (row > 0) {
row--;
line = prev_line(it, &len);
if (!line)
break; break;
row = prev_row; const Fold *fold = fold_for_line(editor->folds, row);
it = begin_l_iter(editor->root, row); if (fold) {
line = next_line(it, &len); while (line && row > fold->start) {
free(it); free(line);
line = prev_line(it, &len);
row--;
}
if (line) {
free(line);
line = nullptr;
}
continue;
}
break;
}
if (!line) if (!line)
break; break;
if (len > 0 && line[len - 1] == '\n') if (len > 0 && line[len - 1] == '\n')
@@ -742,6 +423,7 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
} }
if (line) if (line)
free(line); free(line);
free(it);
result.row = row; result.row = row;
result.col = col; result.col = col;
return result; return result;
@@ -750,64 +432,75 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
void cursor_down(Editor *editor, uint32_t number) { void cursor_down(Editor *editor, uint32_t number) {
if (!editor || !editor->root || number == 0) if (!editor || !editor->root || number == 0)
return; return;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
uint32_t len; uint32_t len;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line_content = next_line(it, &len); char *line_content = next_line(it, &len);
free(it);
if (line_content == nullptr) if (line_content == nullptr)
return; return;
if (editor->cursor_preffered == UINT32_MAX) if (editor->cursor_preffered == UINT32_MAX)
editor->cursor_preffered = editor->cursor_preffered =
get_visual_col_from_bytes(line_content, len, editor->cursor.col); get_visual_col_from_bytes(line_content, len, editor->cursor.col);
uint32_t visual_col = editor->cursor_preffered; uint32_t visual_col = editor->cursor_preffered;
do {
free(line_content); free(line_content);
line_content = next_line(it, &len); uint32_t target_row = editor->cursor.row;
editor->cursor.row += 1; while (number > 0 && target_row < editor->root->line_count - 1) {
if (editor->cursor.row >= editor->root->line_count) { target_row = next_unfolded_row(editor, target_row + 1);
editor->cursor.row = editor->root->line_count - 1; if (target_row >= editor->root->line_count) {
target_row = editor->root->line_count - 1;
break; break;
}; }
if (editor->folded[editor->cursor.row] != 0) number--;
number++; }
} while (--number > 0); it = begin_l_iter(editor->root, target_row);
line_content = next_line(it, &len);
free(it); free(it);
if (line_content == nullptr) if (!line_content)
return; return;
if (len > 0 && line_content[len - 1] == '\n')
--len;
editor->cursor.row = target_row;
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col); editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
free(line_content); free(line_content);
} }
void cursor_up(Editor *editor, uint32_t number) { void cursor_up(Editor *editor, uint32_t number) {
if (!editor || !editor->root || number == 0) if (!editor || !editor->root || number == 0 || editor->cursor.row == 0)
return; return;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
uint32_t len; uint32_t len;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line_content = next_line(it, &len); char *line_content = next_line(it, &len);
if (!line_content) {
free(it); free(it);
if (!line_content)
return; return;
}
if (editor->cursor_preffered == UINT32_MAX) if (editor->cursor_preffered == UINT32_MAX)
editor->cursor_preffered = editor->cursor_preffered =
get_visual_col_from_bytes(line_content, len, editor->cursor.col); get_visual_col_from_bytes(line_content, len, editor->cursor.col);
uint32_t visual_col = editor->cursor_preffered; uint32_t visual_col = editor->cursor_preffered;
free(line_content); free(line_content);
while (number > 0 && editor->cursor.row > 0) { uint32_t target_row = editor->cursor.row;
editor->cursor.row--; while (number > 0 && target_row > 0) {
if (editor->folded[editor->cursor.row] != 0) target_row = prev_unfolded_row(editor, target_row - 1);
continue; if (target_row == 0) {
number--;
break;
}
number--; number--;
} }
free(it); it = begin_l_iter(editor->root, target_row);
it = begin_l_iter(editor->root, editor->cursor.row);
line_content = next_line(it, &len); line_content = next_line(it, &len);
if (!line_content) {
free(it); free(it);
return; if (line_content) {
} if (len > 0 && line_content[len - 1] == '\n')
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col); --len;
editor->cursor.row = target_row;
editor->cursor.col =
get_bytes_from_visual_col(line_content, len, visual_col);
free(line_content); free(line_content);
free(it); } else {
editor->cursor.row = 0;
editor->cursor.col = 0;
}
} }
void cursor_right(Editor *editor, uint32_t number) { void cursor_right(Editor *editor, uint32_t number) {
@@ -837,13 +530,21 @@ void move_line_up(Editor *editor) {
lock.unlock(); lock.unlock();
return; return;
} }
if (line_len > 0 && line[line_len - 1] == '\n')
line_len--;
line_cluster_len = count_clusters(line, line_len, 0, line_len); line_cluster_len = count_clusters(line, line_len, 0, line_len);
uint32_t target_row = prev_unfolded_row(editor, editor->cursor.row - 1);
uint32_t up_by = editor->cursor.row - target_row;
if (up_by > 1)
up_by--;
lock.unlock(); lock.unlock();
Coord cursor = editor->cursor; Coord cursor = editor->cursor;
edit_erase(editor, {cursor.row, 0}, line_cluster_len); edit_erase(editor, {cursor.row, 0}, line_cluster_len);
edit_insert(editor, {cursor.row - 1, 0}, line, line_len); edit_erase(editor, {cursor.row, 0}, -1);
edit_insert(editor, {cursor.row - up_by, 0}, (char *)"\n", 1);
edit_insert(editor, {cursor.row - up_by, 0}, line, line_len);
free(line); free(line);
editor->cursor = {cursor.row - 1, cursor.col}; editor->cursor = {cursor.row - up_by, cursor.col};
} else if (mode == SELECT) { } else if (mode == SELECT) {
uint32_t start_row = MIN(editor->cursor.row, editor->selection.row); uint32_t start_row = MIN(editor->cursor.row, editor->selection.row);
uint32_t end_row = MAX(editor->cursor.row, editor->selection.row); uint32_t end_row = MAX(editor->cursor.row, editor->selection.row);
@@ -880,22 +581,45 @@ void move_line_down(Editor *editor) {
lock.unlock(); lock.unlock();
return; return;
} }
if (line_len && line[line_len - 1] == '\n')
line_len--;
line_cluster_len = count_clusters(line, line_len, 0, line_len); line_cluster_len = count_clusters(line, line_len, 0, line_len);
uint32_t target_row = next_unfolded_row(editor, editor->cursor.row + 1);
if (target_row >= editor->root->line_count) {
free(line);
lock.unlock();
return;
}
uint32_t down_by = target_row - editor->cursor.row;
if (down_by > 1)
down_by--;
uint32_t ln;
line_to_byte(editor->root, editor->cursor.row + down_by - 1, &ln);
lock.unlock(); lock.unlock();
Coord cursor = editor->cursor; Coord cursor = editor->cursor;
edit_erase(editor, {cursor.row, 0}, line_cluster_len); edit_erase(editor, {cursor.row, 0}, line_cluster_len);
edit_insert(editor, {cursor.row + 1, 0}, line, line_len); edit_erase(editor, {cursor.row, 0}, -1);
edit_insert(editor, {cursor.row + down_by, 0}, (char *)"\n", 1);
edit_insert(editor, {cursor.row + down_by, 0}, line, line_len);
free(line); free(line);
editor->cursor = {cursor.row + 1, cursor.col}; editor->cursor = {cursor.row + down_by, cursor.col};
} else if (mode == SELECT) { } else if (mode == SELECT) {
if (editor->cursor.row >= editor->root->line_count - 1 || if (editor->cursor.row >= editor->root->line_count - 1 ||
editor->selection.row >= editor->root->line_count - 1) editor->selection.row >= editor->root->line_count - 1)
return; return;
std::shared_lock lock(editor->knot_mtx);
uint32_t start_row = MIN(editor->cursor.row, editor->selection.row); uint32_t start_row = MIN(editor->cursor.row, editor->selection.row);
uint32_t end_row = MAX(editor->cursor.row, editor->selection.row); uint32_t end_row = MAX(editor->cursor.row, editor->selection.row);
uint32_t target_row = next_unfolded_row(editor, end_row + 1);
if (target_row >= editor->root->line_count)
return;
uint32_t down_by = target_row - end_row;
if (down_by > 1)
down_by--;
uint32_t start_byte = line_to_byte(editor->root, start_row, nullptr); uint32_t start_byte = line_to_byte(editor->root, start_row, nullptr);
uint32_t end_byte = line_to_byte(editor->root, end_row + 1, nullptr); uint32_t end_byte = line_to_byte(editor->root, end_row + 1, nullptr);
char *selected_text = read(editor->root, start_byte, end_byte - start_byte); char *selected_text = read(editor->root, start_byte, end_byte - start_byte);
lock.unlock();
if (!selected_text) if (!selected_text)
return; return;
uint32_t selected_len = count_clusters(selected_text, end_byte - start_byte, uint32_t selected_len = count_clusters(selected_text, end_byte - start_byte,
@@ -903,11 +627,11 @@ void move_line_down(Editor *editor) {
Coord cursor = editor->cursor; Coord cursor = editor->cursor;
Coord selection = editor->selection; Coord selection = editor->selection;
edit_erase(editor, {start_row, 0}, selected_len); edit_erase(editor, {start_row, 0}, selected_len);
edit_insert(editor, {start_row + 1, 0}, selected_text, edit_insert(editor, {start_row + down_by, 0}, selected_text,
end_byte - start_byte); end_byte - start_byte);
free(selected_text); free(selected_text);
editor->cursor = {cursor.row + 1, cursor.col}; editor->cursor = {cursor.row + down_by, cursor.col};
editor->selection = {selection.row + 1, selection.col}; editor->selection = {selection.row + down_by, selection.col};
} }
} }
@@ -921,7 +645,7 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
editor->cursor.col; editor->cursor.col;
TSPoint old_point = {pos.row, pos.col}; TSPoint old_point = {pos.row, pos.col};
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col; uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
Coord point = move_left(editor, pos, -len); Coord point = move_left_pure(editor, pos, -len);
uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col; uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col;
if (cursor_original > start && cursor_original <= byte_pos) { if (cursor_original > start && cursor_original <= byte_pos) {
editor->cursor = point; editor->cursor = point;
@@ -934,6 +658,9 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
editor->cursor_preffered = UINT32_MAX; editor->cursor_preffered = UINT32_MAX;
} }
lock_1.unlock(); lock_1.unlock();
uint32_t start_row = point.row;
uint32_t end_row = pos.row;
apply_line_deletion(editor, start_row + 1, end_row);
std::unique_lock lock_2(editor->knot_mtx); std::unique_lock lock_2(editor->knot_mtx);
editor->root = erase(editor->root, start, byte_pos - start); editor->root = erase(editor->root, start, byte_pos - start);
lock_2.unlock(); lock_2.unlock();
@@ -961,7 +688,7 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
editor->cursor.col; editor->cursor.col;
TSPoint old_point = {pos.row, pos.col}; TSPoint old_point = {pos.row, pos.col};
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col; uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
Coord point = move_right(editor, pos, len); Coord point = move_right_pure(editor, pos, len);
uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col; uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col;
if (cursor_original > byte_pos && cursor_original <= end) { if (cursor_original > byte_pos && cursor_original <= end) {
editor->cursor = pos; editor->cursor = pos;
@@ -974,6 +701,9 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
editor->cursor_preffered = UINT32_MAX; editor->cursor_preffered = UINT32_MAX;
} }
lock_1.unlock(); lock_1.unlock();
uint32_t start_row = pos.row;
uint32_t end_row = point.row;
apply_line_deletion(editor, start_row + 1, end_row);
std::unique_lock lock_2(editor->knot_mtx); std::unique_lock lock_2(editor->knot_mtx);
editor->root = erase(editor->root, byte_pos, end - byte_pos); editor->root = erase(editor->root, byte_pos, end - byte_pos);
lock_2.unlock(); lock_2.unlock();
@@ -1013,8 +743,6 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
lock_1.unlock(); lock_1.unlock();
std::unique_lock lock_2(editor->knot_mtx); std::unique_lock lock_2(editor->knot_mtx);
editor->root = insert(editor->root, byte_pos, data, len); editor->root = insert(editor->root, byte_pos, data, len);
if (memchr(data, '\n', len))
editor->folded.resize(editor->root->line_count + 2);
lock_2.unlock(); lock_2.unlock();
uint32_t cols = 0; uint32_t cols = 0;
uint32_t rows = 0; uint32_t rows = 0;
@@ -1026,6 +754,7 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
cols++; cols++;
} }
} }
apply_line_insertion(editor, pos.row, rows);
if (editor->tree) { if (editor->tree) {
TSInputEdit edit = { TSInputEdit edit = {
.start_byte = byte_pos, .start_byte = byte_pos,
@@ -1122,3 +851,95 @@ void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y) {
++i; ++i;
} }
} }
std::vector<Fold>::iterator find_fold_iter(Editor *editor, uint32_t line) {
auto &folds = editor->folds;
auto it = std::lower_bound(
folds.begin(), folds.end(), line,
[](const Fold &fold, uint32_t value) { return fold.start < value; });
if (it != folds.end() && it->start == line)
return it;
if (it != folds.begin()) {
--it;
if (it->contains(line))
return it;
}
return folds.end();
}
bool add_fold(Editor *editor, uint32_t start, uint32_t end) {
if (!editor || !editor->root)
return false;
if (start > end)
std::swap(start, end);
if (start >= editor->root->line_count)
return false;
end = std::min(end, editor->root->line_count - 1);
if (start == end)
return false;
Fold new_fold{start, end};
auto &folds = editor->folds;
auto it = std::lower_bound(
folds.begin(), folds.end(), new_fold.start,
[](const Fold &fold, uint32_t value) { return fold.start < value; });
if (it != folds.begin()) {
auto prev = std::prev(it);
if (prev->end + 1 >= new_fold.start) {
new_fold.start = std::min(new_fold.start, prev->start);
new_fold.end = std::max(new_fold.end, prev->end);
it = folds.erase(prev);
}
}
while (it != folds.end() && it->start <= new_fold.end + 1) {
new_fold.end = std::max(new_fold.end, it->end);
it = folds.erase(it);
}
folds.insert(it, new_fold);
return true;
}
bool remove_fold(Editor *editor, uint32_t line) {
auto it = find_fold_iter(editor, line);
if (it == editor->folds.end())
return false;
editor->folds.erase(it);
return true;
}
void apply_line_insertion(Editor *editor, uint32_t line, uint32_t rows) {
for (auto it = editor->folds.begin(); it != editor->folds.end();) {
if (line <= it->start) {
it->start += rows;
it->end += rows;
++it;
} else if (line <= it->end) {
it = editor->folds.erase(it);
} else {
++it;
}
}
}
void apply_line_deletion(Editor *editor, uint32_t removal_start,
uint32_t removal_end) {
if (removal_start > removal_end)
return;
uint32_t rows_removed = removal_end - removal_start + 1;
std::vector<Fold> updated;
updated.reserve(editor->folds.size());
for (auto fold : editor->folds) {
if (removal_end < fold.start) {
fold.start -= rows_removed;
fold.end -= rows_removed;
updated.push_back(fold);
continue;
}
if (removal_start > fold.end) {
updated.push_back(fold);
continue;
}
}
editor->folds.swap(updated);
}

520
src/editor_events.cc Normal file
View File

@@ -0,0 +1,520 @@
#include "../include/editor.h"
#include "../include/main.h"
#include "../include/ts.h"
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_MOUSE) {
auto now = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
now - last_click_time)
.count();
switch (event.mouse_state) {
case SCROLL:
switch (event.mouse_direction) {
case SCROLL_UP:
scroll_up(editor, 10);
ensure_cursor(editor);
break;
case SCROLL_DOWN:
scroll_down(editor, 10);
ensure_cursor(editor);
break;
case SCROLL_LEFT:
cursor_left(editor, 10);
break;
case SCROLL_RIGHT:
cursor_right(editor, 10);
break;
}
break;
case PRESS:
if (event.mouse_button == LEFT_BTN) {
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);
if (p.row == UINT32_MAX && p.col == UINT32_MAX)
return;
editor->cursor_preffered = UINT32_MAX;
if (click_count == 1) {
editor->cursor = p;
editor->selection = p;
if (mode == SELECT) {
mode = NORMAL;
editor->selection_active = false;
}
} 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--;
free(line);
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);
if (p.row == UINT32_MAX && p.col == UINT32_MAX)
return;
editor->cursor_preffered = UINT32_MAX;
mode = SELECT;
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--;
free(line);
editor->cursor = {p.row, line_len};
}
break;
}
}
break;
case RELEASE:
if (event.mouse_button == LEFT_BTN)
if (editor->cursor.row == editor->selection.row &&
editor->cursor.col == editor->selection.col) {
mode = NORMAL;
editor->selection_active = false;
}
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, 5);
break;
case KEY_UP:
cursor_up(editor, 5);
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:
switch (event.special_key) {
case KEY_DOWN:
move_line_down(editor);
break;
case KEY_UP:
move_line_up(editor);
break;
case KEY_LEFT:
cursor_left(editor, 8);
break;
case KEY_RIGHT:
cursor_right(editor, 8);
break;
}
break;
}
}
switch (mode) {
case NORMAL:
if (event.key_type == KEY_CHAR && event.len == 1) {
switch (event.c[0]) {
case 'u':
if (editor->root->line_count > 0) {
editor->cursor.row = editor->root->line_count - 1;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
if (!it)
break;
uint32_t line_len;
char *line = next_line(it, &line_len);
if (!line)
break;
if (line_len > 0 && line[line_len - 1] == '\n')
line_len--;
free(it);
line_len = count_clusters(line, line_len, 0, line_len);
free(line);
editor->cursor.col = line_len;
editor->cursor_preffered = UINT32_MAX;
mode = SELECT;
editor->selection_active = true;
editor->selection = {0, 0};
editor->selection_type = LINE;
}
break;
case 'a':
mode = INSERT;
cursor_right(editor, 1);
break;
case 'i':
mode = INSERT;
break;
case 'n':
mode = JUMPER;
editor->jumper_set = true;
break;
case 'm':
mode = JUMPER;
editor->jumper_set = false;
break;
case 'N':
for (uint8_t i = 0; i < 94; i++)
if (editor->hooks[i] == editor->cursor.row + 1) {
editor->hooks[i] = 0;
break;
}
break;
case 's':
case 'v':
mode = SELECT;
editor->selection_active = true;
editor->selection = editor->cursor;
editor->selection_type = CHAR;
break;
case ';':
case ':':
mode = RUNNER;
break;
case 0x7F:
cursor_left(editor, 1);
break;
case ' ':
cursor_right(editor, 1);
break;
case '\r':
case '\n':
cursor_down(editor, 1);
break;
case '\\':
case '|':
cursor_up(editor, 1);
break;
case CTRL('d'):
scroll_down(editor, 1);
ensure_cursor(editor);
break;
case CTRL('u'):
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;
case INSERT:
if (event.key_type == KEY_CHAR) {
if (event.len == 1) {
if (event.c[0] == '\t') {
edit_insert(editor, editor->cursor, (char *)" ", 2);
cursor_right(editor, 2);
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
edit_insert(editor, editor->cursor, (char *)"\n", 1);
cursor_right(editor, 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]))) {
char c = event.c[0];
char closing = 0;
if (c == '{')
closing = '}';
else if (c == '(')
closing = ')';
else if (c == '[')
closing = ']';
else if (c == '"')
closing = '"';
else if (c == '\'')
closing = '\'';
if (closing) {
char pair[2] = {c, closing};
edit_insert(editor, editor->cursor, pair, 2);
cursor_right(editor, 1);
} else {
edit_insert(editor, editor->cursor, event.c, 1);
cursor_right(editor, 1);
}
} else if (event.c[0] == 0x7F || event.c[0] == 0x08) {
Coord prev_pos = editor->cursor;
if (prev_pos.col > 0)
prev_pos.col--;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
if (!it)
return;
char *line = next_line(it, nullptr);
free(it);
char prev_char = line[prev_pos.col];
char next_char = line[editor->cursor.col];
free(line);
bool is_pair = (prev_char == '{' && next_char == '}') ||
(prev_char == '(' && next_char == ')') ||
(prev_char == '[' && next_char == ']') ||
(prev_char == '"' && next_char == '"') ||
(prev_char == '\'' && next_char == '\'');
if (is_pair) {
edit_erase(editor, editor->cursor, 1);
edit_erase(editor, prev_pos, 1);
} else {
edit_erase(editor, editor->cursor, -1);
}
} else if (event.c[0] == 0x1B) {
mode = NORMAL;
cursor_left(editor, 1);
}
} else if (event.len > 1) {
edit_insert(editor, editor->cursor, event.c, event.len);
cursor_right(editor, 1);
}
} else if (event.key_type == KEY_SPECIAL &&
event.special_key == KEY_DELETE) {
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:
if (event.key_type == KEY_CHAR && event.len == 1) {
uint32_t len;
char *text;
switch (event.c[0]) {
case 'f':
if (editor->cursor.row != editor->selection.row) {
uint32_t start = MIN(editor->cursor.row, editor->selection.row);
uint32_t end = MAX(editor->cursor.row, editor->selection.row);
add_fold(editor, start, end);
}
cursor_left(editor, 1);
cursor_down(editor, 1);
editor->selection_active = false;
mode = NORMAL;
break;
case 0x1B:
case 's':
case 'v':
editor->selection_active = false;
mode = NORMAL;
break;
case 'y':
text = get_selection(editor, &len);
copy_to_clipboard(text, len);
free(text);
editor->selection_active = false;
mode = NORMAL;
break;
case 'x':
text = get_selection(editor, &len);
copy_to_clipboard(text, len);
edit_erase(editor, MIN(editor->cursor, editor->selection), len);
free(text);
editor->selection_active = false;
mode = NORMAL;
break;
case 'p':
text = get_from_clipboard(&len);
if (text) {
Coord start, end;
if (editor->cursor >= editor->selection) {
start = editor->selection;
end = move_right(editor, editor->cursor, 1);
} else {
start = editor->cursor;
end = move_right(editor, editor->selection, 1);
}
uint32_t start_byte =
line_to_byte(editor->root, start.row, nullptr) + start.col;
uint32_t end_byte =
line_to_byte(editor->root, end.row, nullptr) + end.col;
edit_erase(editor, start, end_byte - start_byte);
edit_insert(editor, editor->cursor, text, len);
free(text);
}
editor->selection_active = false;
mode = NORMAL;
break;
}
}
break;
case JUMPER:
if (event.key_type == KEY_CHAR && event.len == 1 &&
(event.c[0] >= '!' && event.c[0] <= '~')) {
if (editor->jumper_set) {
for (uint8_t i = 0; i < 94; i++)
if (editor->hooks[i] == editor->cursor.row + 1) {
editor->hooks[i] = 0;
break;
}
editor->hooks[event.c[0] - '!'] = editor->cursor.row + 1;
} else {
uint32_t line = editor->hooks[event.c[0] - '!'];
if (line > 0) {
if (line_is_folded(editor->folds, --line))
break;
editor->cursor = {line, 0};
editor->cursor_preffered = UINT32_MAX;
}
}
}
mode = NORMAL;
break;
case RUNNER:
if (event.key_type == KEY_CHAR && event.len == 1) {
switch (event.c[0]) {
case 0x1B:
mode = NORMAL;
break;
}
}
break;
}
ensure_scroll(editor);
if (event.key_type == KEY_CHAR && event.c)
free(event.c);
}
static Highlight HL_UNDERLINE = {0, 0, 1 << 2, 100};
void editor_worker(Editor *editor) {
if (!editor || !editor->root)
return;
if (editor->parser && editor->query)
ts_collect_spans(editor);
uint32_t prev_col, next_col;
word_boundaries_exclusive(editor, editor->cursor, &prev_col, &next_col);
if (next_col - prev_col > 0 && next_col - prev_col < 256 - 4) {
std::shared_lock lock(editor->knot_mtx);
uint32_t offset = line_to_byte(editor->root, editor->cursor.row, nullptr);
char *word = read(editor->root, offset + prev_col, next_col - prev_col);
if (word) {
char buf[256];
snprintf(buf, sizeof(buf), "\\b%s\\b", word);
std::vector<std::pair<size_t, size_t>> results =
search_rope(editor->root, buf);
std::unique_lock lock(editor->def_spans.mtx);
editor->def_spans.spans.clear();
for (const auto &match : results) {
Span s;
s.start = match.first;
s.end = match.first + match.second;
s.hl = &HL_UNDERLINE;
editor->def_spans.spans.push_back(s);
}
std::sort(editor->def_spans.spans.begin(), editor->def_spans.spans.end());
lock.unlock();
free(word);
}
} else {
std::unique_lock lock(editor->def_spans.mtx);
editor->def_spans.spans.clear();
lock.unlock();
}
}

View File

@@ -1,4 +1,3 @@
#include <cstdint>
extern "C" { extern "C" {
#include "../libs/libgrapheme/grapheme.h" #include "../libs/libgrapheme/grapheme.h"
} }
@@ -6,72 +5,129 @@ extern "C" {
#include "../include/utils.h" #include "../include/utils.h"
#include <cmath> #include <cmath>
void scroll_up(Editor *editor, uint32_t number) { void scroll_up(Editor *editor, int32_t number) {
number++; if (!editor || number == 0)
return;
uint32_t numlen = uint32_t numlen =
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1)); EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen; uint32_t render_width = editor->size.col - numlen;
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * number); uint32_t line_index = editor->scroll.row;
uint32_t q_head = 0;
uint32_t q_size = 0;
int line_index = editor->scroll.row - number;
line_index = line_index < 0 ? 0 : line_index;
LineIterator *it = begin_l_iter(editor->root, line_index); LineIterator *it = begin_l_iter(editor->root, line_index);
if (!it) { if (!it)
free(scroll_queue); return;
uint32_t len;
char *line = next_line(it, &len);
if (!line) {
free(it);
return; return;
} }
for (uint32_t i = line_index; i <= editor->scroll.row; i++) { if (len > 0 && line[len - 1] == '\n')
uint32_t line_len; len--;
char *line = next_line(it, &line_len); uint32_t current_byte_offset = 0;
if (!line)
break;
if (line[line_len - 1] == '\n')
--line_len;
uint32_t offset = 0;
uint32_t col = 0; uint32_t col = 0;
if (q_size < number) { std::vector<uint32_t> segment_starts;
scroll_queue[(q_head + q_size) % number] = {i, 0}; segment_starts.reserve(16);
q_size++; if (current_byte_offset < editor->scroll.col)
} else { segment_starts.push_back(0);
scroll_queue[q_head] = {i, 0}; while (current_byte_offset < editor->scroll.col &&
q_head = (q_head + 1) % number; current_byte_offset < len) {
} uint32_t cluster_len = grapheme_next_character_break_utf8(
if (i == editor->scroll.row && 0 == editor->scroll.col) { line + current_byte_offset, len - current_byte_offset);
editor->scroll = scroll_queue[q_head]; int width = display_width(line + current_byte_offset, cluster_len);
free(line);
free(it);
free(scroll_queue);
return;
}
while (offset < line_len) {
uint32_t inc =
grapheme_next_character_break_utf8(line + offset, line_len - offset);
int width = display_width(line + offset, inc);
if (col + width > render_width) { if (col + width > render_width) {
if (q_size < number) { segment_starts.push_back(current_byte_offset);
scroll_queue[(q_head + q_size) % number] = {i, offset};
q_size++;
} else {
scroll_queue[q_head] = {i, offset};
q_head = (q_head + 1) % number;
}
if (i == editor->scroll.row && offset >= editor->scroll.col) {
editor->scroll = scroll_queue[q_head];
free(line);
free(it);
free(scroll_queue);
return;
}
col = 0; col = 0;
} }
current_byte_offset += cluster_len;
col += width; col += width;
offset += inc; }
for (auto it_seg = segment_starts.rbegin(); it_seg != segment_starts.rend();
++it_seg) {
if (--number == 0) {
editor->scroll = {line_index, *it_seg};
free(line);
free(it);
return;
}
} }
free(line); free(line);
} line = prev_line(it, &len);
if (!line) {
editor->scroll = {0, 0}; editor->scroll = {0, 0};
free(scroll_queue); free(it);
return;
}
free(line);
do {
line_index--;
line = prev_line(it, &len);
if (!line) {
editor->scroll = {0, 0};
free(it);
return;
}
const Fold *fold = fold_for_line(editor->folds, line_index);
if (fold) {
while (line && line_index > fold->start) {
free(line);
line = prev_line(it, &len);
line_index--;
if (!line) {
editor->scroll = {0, 0};
free(it);
return;
}
}
if (--number == 0) {
editor->scroll = {fold->start, 0};
free(line);
free(it);
return;
}
free(line);
if (fold->start == 0) {
editor->scroll = {0, 0};
free(it);
return;
}
line_index = fold->start - 1;
line = prev_line(it, &len);
if (!line) {
editor->scroll = {0, 0};
free(it);
return;
}
continue;
}
if (len > 0 && line[len - 1] == '\n')
len--;
current_byte_offset = 0;
col = 0;
std::vector<uint32_t> segment_starts;
segment_starts.reserve(16);
segment_starts.push_back(0);
while (current_byte_offset < len) {
uint32_t cluster_len = grapheme_next_character_break_utf8(
line + current_byte_offset, len - current_byte_offset);
int width = display_width(line + current_byte_offset, cluster_len);
if (col + width > render_width) {
segment_starts.push_back(current_byte_offset);
col = 0;
}
current_byte_offset += cluster_len;
col += width;
}
for (auto it_seg = segment_starts.rbegin(); it_seg != segment_starts.rend();
++it_seg) {
if (--number == 0) {
editor->scroll = {line_index, *it_seg};
free(line);
free(it);
return;
}
}
free(line);
} while (number > 0);
free(it); free(it);
} }
@@ -92,9 +148,9 @@ void scroll_down(Editor *editor, uint32_t number) {
uint32_t visual_seen = 0; uint32_t visual_seen = 0;
bool first_visual_line = true; bool first_visual_line = true;
while (true) { while (true) {
if (editor->folded[line_index]) { const Fold *fold = fold_for_line(editor->folds, line_index);
if (editor->folded[line_index] == 2) { if (fold) {
Coord fold_coord = {line_index, 0}; Coord fold_coord = {fold->start, 0};
if (q_size < max_visual_lines) { if (q_size < max_visual_lines) {
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord; scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
q_size++; q_size++;
@@ -107,8 +163,8 @@ void scroll_down(Editor *editor, uint32_t number) {
editor->scroll = scroll_queue[q_head]; editor->scroll = scroll_queue[q_head];
break; break;
} }
} uint32_t skip_until = fold->end;
do { while (line_index <= skip_until) {
char *line = next_line(it, nullptr); char *line = next_line(it, nullptr);
if (!line) { if (!line) {
free(scroll_queue); free(scroll_queue);
@@ -117,7 +173,7 @@ void scroll_down(Editor *editor, uint32_t number) {
} }
free(line); free(line);
line_index++; line_index++;
} while (editor->folded[line_index] == 1); }
continue; continue;
} }
uint32_t line_len; uint32_t line_len;
@@ -181,7 +237,11 @@ void scroll_down(Editor *editor, uint32_t number) {
void ensure_cursor(Editor *editor) { void ensure_cursor(Editor *editor) {
std::shared_lock knot_lock(editor->knot_mtx); std::shared_lock knot_lock(editor->knot_mtx);
if (editor->cursor < editor->scroll) { if (editor->cursor < editor->scroll) {
editor->cursor = editor->scroll; uint32_t line_idx = next_unfolded_row(editor, editor->scroll.row);
editor->cursor.row = line_idx;
editor->cursor.col =
line_idx == editor->scroll.row ? editor->scroll.col : 0;
editor->cursor_preffered = UINT32_MAX;
return; return;
} }
uint32_t numlen = uint32_t numlen =
@@ -197,23 +257,19 @@ void ensure_cursor(Editor *editor) {
while (true) { while (true) {
if (visual_rows >= editor->size.row) if (visual_rows >= editor->size.row)
break; break;
if (editor->folded[line_index]) { const Fold *fold = fold_for_line(editor->folds, line_index);
if (editor->folded[line_index] == 2) { if (fold) {
Coord c = {line_index, 0}; Coord c = {fold->start, 0};
last_visible = c; last_visible = c;
visual_rows++; visual_rows++;
if (line_index == editor->cursor.row) { uint32_t skip_until = fold->end;
free(it); while (line_index <= skip_until) {
return;
}
}
do {
char *line = next_line(it, nullptr); char *line = next_line(it, nullptr);
if (!line) if (!line)
break; break;
free(line); free(line);
line_index++; line_index++;
} while (editor->folded[line_index] == 1); }
continue; continue;
} }
uint32_t line_len; uint32_t line_len;
@@ -260,7 +316,15 @@ void ensure_cursor(Editor *editor) {
free(line); free(line);
line_index++; line_index++;
} }
editor->cursor = last_visible; uint32_t last_real_row = last_visible.row;
const Fold *last_fold = fold_for_line(editor->folds, last_visible.row);
if (last_fold) {
last_visible.row = last_fold->start == 0 ? 0 : last_fold->start - 1;
last_visible.col = 0;
}
editor->cursor.row = last_visible.row;
editor->cursor.col = last_visible.row == last_real_row ? last_visible.col : 0;
editor->cursor_preffered = UINT32_MAX;
free(it); free(it);
} }
@@ -319,9 +383,9 @@ void ensure_scroll(Editor *editor) {
uint32_t q_size = 0; uint32_t q_size = 0;
bool first_visual_line = true; bool first_visual_line = true;
while (true) { while (true) {
if (editor->folded[line_index]) { const Fold *fold = fold_for_line(editor->folds, line_index);
if (editor->folded[line_index] == 2) { if (fold) {
Coord fold_coord = {line_index, 0}; Coord fold_coord = {fold->start, 0};
if (q_size < max_visual_lines) { if (q_size < max_visual_lines) {
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord; scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
q_size++; q_size++;
@@ -329,19 +393,19 @@ void ensure_scroll(Editor *editor) {
scroll_queue[q_head] = fold_coord; scroll_queue[q_head] = fold_coord;
q_head = (q_head + 1) % max_visual_lines; q_head = (q_head + 1) % max_visual_lines;
} }
if (line_index == editor->cursor.row) { if (fold->start <= editor->cursor.row &&
editor->cursor.row <= fold->end) {
editor->scroll = scroll_queue[q_head]; editor->scroll = scroll_queue[q_head];
break; break;
} }
} uint32_t skip_until = fold->end;
do { while (line_index <= skip_until) {
char *line = next_line(it, nullptr); char *line = next_line(it, nullptr);
if (!line) if (!line)
break; break;
free(line); free(line);
line_index++; line_index++;
} while (line_index < editor->size.row && }
editor->folded[line_index] == 1);
continue; continue;
} }
uint32_t line_len; uint32_t line_len;

View File

@@ -520,6 +520,86 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line) {
return it; return it;
} }
static inline void iter_retreat_leaf(LineIterator *it) {
if (it->top == 0) {
it->node = nullptr;
return;
}
Knot *curr = it->stack[--it->top];
while (it->top > 0) {
Knot *parent = it->stack[it->top - 1];
if (parent->right == curr && parent->left) {
Knot *target = parent->left;
while (target) {
it->stack[it->top++] = target;
if (!target->left && !target->right) {
if (target->char_count == 0)
break;
it->node = target;
it->offset = target->char_count;
return;
}
target = (target->right) ? target->right : target->left;
}
}
curr = it->stack[--it->top];
}
it->node = nullptr;
}
static void str_reverse(char *begin, char *end) {
char temp;
while (begin < end) {
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
char *prev_line(LineIterator *it, uint32_t *out_len) {
if (!it || !it->node)
return nullptr;
size_t capacity = 128;
size_t len = 0;
char *buffer = (char *)malloc(capacity);
if (!buffer)
return nullptr;
while (it->node) {
if (it->offset == 0) {
iter_retreat_leaf(it);
if (!it->node)
break;
}
it->offset--;
char c = it->node->data[it->offset];
if (c == '\n') {
if (len > 0) {
it->offset++;
break;
}
}
if (len + 1 >= capacity) {
capacity *= 2;
char *new_buf = (char *)realloc(buffer, capacity);
if (!new_buf) {
free(buffer);
return nullptr;
}
buffer = new_buf;
}
buffer[len++] = c;
}
if (len > 0) {
buffer[len] = '\0';
str_reverse(buffer, buffer + len - 1);
if (out_len)
*out_len = len;
return buffer;
}
free(buffer);
return nullptr;
}
static inline void iter_advance_leaf(LineIterator *it) { static inline void iter_advance_leaf(LineIterator *it) {
if (it->top == 0) { if (it->top == 0) {
it->node = nullptr; it->node = nullptr;
@@ -533,6 +613,8 @@ static inline void iter_advance_leaf(LineIterator *it) {
while (curr) { while (curr) {
it->stack[it->top++] = curr; it->stack[it->top++] = curr;
if (!curr->left && !curr->right) { if (!curr->left && !curr->right) {
if (curr->char_count == 0)
break;
it->node = curr; it->node = curr;
it->offset = 0; it->offset = 0;
return; return;

View File

@@ -17,6 +17,15 @@ extern "C" {
#include <unistd.h> #include <unistd.h>
#include <unordered_map> #include <unordered_map>
uint64_t fnv1a_64(const char *s, size_t len) {
uint64_t hash = 1469598103934665603ull;
for (size_t i = 0; i < len; ++i) {
hash ^= (uint8_t)s[i];
hash *= 1099511628211ull;
}
return hash;
}
char *get_from_clipboard(uint32_t *out_len) { char *get_from_clipboard(uint32_t *out_len) {
FILE *pipe = popen("xclip -selection clipboard -o", "r"); FILE *pipe = popen("xclip -selection clipboard -o", "r");
if (!pipe) { if (!pipe) {