Compare commits
3 Commits
d82389bc42
...
f347f50dbe
| Author | SHA1 | Date | |
|---|---|---|---|
|
f347f50dbe
|
|||
| f764c78c6b | |||
|
4f19c2f6c1
|
10
README.md
10
README.md
@@ -6,16 +6,14 @@ A TUI IDE.
|
||||
|
||||
# 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.
|
||||
- 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 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 search / replace along with search / virtual cursors are searched pos.
|
||||
- [ ] Add alt + click to set multiple cursors.
|
||||
- [ ] 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.
|
||||
@@ -24,3 +22,5 @@ A TUI IDE.
|
||||
- [ ] Normalize / validate unicode on file open.
|
||||
- [ ] Add git stuff.
|
||||
- [ ] 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.
|
||||
|
||||
@@ -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
|
||||
[
|
||||
"("
|
||||
|
||||
@@ -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
|
||||
[
|
||||
(identifier)
|
||||
|
||||
@@ -10,12 +10,13 @@
|
||||
#include <map>
|
||||
#include <shared_mutex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#define CHAR 0
|
||||
#define WORD 1
|
||||
#define LINE 2
|
||||
|
||||
#define EXTRA_META 3
|
||||
#define EXTRA_META 4
|
||||
|
||||
struct Highlight {
|
||||
uint32_t fg;
|
||||
@@ -39,6 +40,14 @@ struct Spans {
|
||||
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 {
|
||||
Spans &spans;
|
||||
size_t index = 0;
|
||||
@@ -105,14 +114,66 @@ struct Editor {
|
||||
const TSLanguage *language;
|
||||
Queue<TSInputEdit> edit_queue;
|
||||
std::vector<Highlight> query_map;
|
||||
std::vector<int8_t> folded;
|
||||
std::vector<Fold> folds;
|
||||
Spans spans;
|
||||
Spans def_spans;
|
||||
std::map<uint32_t, bool> folded_node;
|
||||
uint32_t hooks[94];
|
||||
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);
|
||||
Editor *new_editor(const char *filename, Coord position, Coord size);
|
||||
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);
|
||||
void cursor_left(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 ensure_cursor(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);
|
||||
void word_boundaries_exclusive(Editor *editor, Coord coord, uint32_t *prev_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
|
||||
|
||||
@@ -117,6 +117,14 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line);
|
||||
// freed by the caller
|
||||
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
|
||||
// root is the root of the rope
|
||||
// the caller must free the iterator after use
|
||||
|
||||
100
src/editor.cc
100
src/editor.cc
@@ -23,7 +23,6 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->root = load(str, len, optimal_chunk_size(len));
|
||||
free(str);
|
||||
editor->folded.resize(editor->root->line_count + 2);
|
||||
if (len <= (1024 * 128)) {
|
||||
editor->parser = ts_parser_new();
|
||||
Language language = language_for_file(filename);
|
||||
@@ -45,24 +44,6 @@ void free_editor(Editor *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) {
|
||||
uint32_t sel_start = 0, sel_end = 0;
|
||||
uint32_t numlen =
|
||||
@@ -74,6 +55,7 @@ void render_editor(Editor *editor) {
|
||||
if (editor->hooks[i] != 0)
|
||||
v.push_back({editor->hooks[i], '!' + i});
|
||||
std::sort(v.begin(), v.end());
|
||||
auto hook_it = v.begin();
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
if (editor->selection_active) {
|
||||
Coord start, end;
|
||||
@@ -135,12 +117,29 @@ void render_editor(Editor *editor) {
|
||||
span_cursor.sync(global_byte_offset);
|
||||
def_span_cursor.sync(global_byte_offset);
|
||||
while (rendered_rows < editor->size.row) {
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
update_render_fold_marker(rendered_rows, render_width);
|
||||
rendered_rows++;
|
||||
}
|
||||
do {
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
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++;
|
||||
|
||||
uint32_t skip_until = fold->end;
|
||||
while (line_index <= skip_until) {
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
@@ -151,8 +150,7 @@ void render_editor(Editor *editor) {
|
||||
global_byte_offset++;
|
||||
free(line);
|
||||
line_index++;
|
||||
} while (line_index < editor->size.row &&
|
||||
editor->folded[line_index] == 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
@@ -167,21 +165,27 @@ void render_editor(Editor *editor) {
|
||||
while (current_byte_offset < line_len && rendered_rows < editor->size.row) {
|
||||
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
|
||||
if (current_byte_offset == 0 || rendered_rows == 0) {
|
||||
char buf[EXTRA_META + 16];
|
||||
char hook = ' ';
|
||||
for (auto &p : v) {
|
||||
if (p.first == line_index + 1) {
|
||||
hook = p.second;
|
||||
const char *hook = nullptr;
|
||||
char h[2] = {0, 0};
|
||||
auto it2 = hook_it;
|
||||
for (; it2 != v.end(); ++it2) {
|
||||
if (it2->first == line_index + 1) {
|
||||
h[0] = it2->second;
|
||||
hook = h;
|
||||
hook_it = it2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int len = snprintf(buf, sizeof(buf), "%c%*u", hook, numlen - 2,
|
||||
line_index + 1);
|
||||
update(editor->position.row + rendered_rows, editor->position.col, hook,
|
||||
0xAAAAAA, 0, 0);
|
||||
char buf[16];
|
||||
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 3, line_index + 1);
|
||||
uint32_t num_color =
|
||||
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
|
||||
for (int i = 0; i < len; i++)
|
||||
update(editor->position.row + rendered_rows, editor->position.col + i,
|
||||
std::string(1, buf[i]).c_str(), num_color, 0, 0);
|
||||
update(editor->position.row + rendered_rows,
|
||||
editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color,
|
||||
0, 0);
|
||||
} else {
|
||||
for (uint32_t i = 0; i < numlen; i++)
|
||||
update(editor->position.row + rendered_rows, editor->position.col + i,
|
||||
@@ -252,21 +256,27 @@ void render_editor(Editor *editor) {
|
||||
if (line_len == 0 ||
|
||||
(current_byte_offset >= line_len && rendered_rows == 0)) {
|
||||
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
|
||||
char buf[EXTRA_META + 16];
|
||||
char hook = ' ';
|
||||
for (auto &p : v) {
|
||||
if (p.first == line_index + 1) {
|
||||
hook = p.second;
|
||||
const char *hook = nullptr;
|
||||
char h[2] = {0, 0};
|
||||
auto it2 = hook_it;
|
||||
for (; it2 != v.end(); ++it2) {
|
||||
if (it2->first == line_index + 1) {
|
||||
h[0] = it2->second;
|
||||
hook = h;
|
||||
hook_it = it2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int len =
|
||||
snprintf(buf, sizeof(buf), "%c%*u", hook, numlen - 2, line_index + 1);
|
||||
update(editor->position.row + rendered_rows, editor->position.col, hook,
|
||||
0xAAAAAA, 0, 0);
|
||||
char buf[16];
|
||||
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 3, line_index + 1);
|
||||
uint32_t num_color =
|
||||
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
|
||||
for (int i = 0; i < len; i++)
|
||||
update(editor->position.row + rendered_rows, editor->position.col + i,
|
||||
std::string(1, buf[i]).c_str(), num_color, 0, 0);
|
||||
update(editor->position.row + rendered_rows,
|
||||
editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color, 0,
|
||||
0);
|
||||
if (editor->cursor.row == line_index) {
|
||||
cursor.row = editor->position.row + rendered_rows;
|
||||
cursor.col = render_x;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
520
src/editor_events.cc
Normal file
520
src/editor_events.cc
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
#include <cstdint>
|
||||
extern "C" {
|
||||
#include "../libs/libgrapheme/grapheme.h"
|
||||
}
|
||||
@@ -6,72 +5,129 @@ extern "C" {
|
||||
#include "../include/utils.h"
|
||||
#include <cmath>
|
||||
|
||||
void scroll_up(Editor *editor, uint32_t number) {
|
||||
number++;
|
||||
void scroll_up(Editor *editor, int32_t number) {
|
||||
if (!editor || number == 0)
|
||||
return;
|
||||
uint32_t numlen =
|
||||
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * number);
|
||||
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;
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it) {
|
||||
free(scroll_queue);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
if (!line) {
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = line_index; i <= editor->scroll.row; i++) {
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line[line_len - 1] == '\n')
|
||||
--line_len;
|
||||
uint32_t offset = 0;
|
||||
uint32_t col = 0;
|
||||
if (q_size < number) {
|
||||
scroll_queue[(q_head + q_size) % number] = {i, 0};
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = {i, 0};
|
||||
q_head = (q_head + 1) % number;
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
len--;
|
||||
uint32_t current_byte_offset = 0;
|
||||
uint32_t col = 0;
|
||||
std::vector<uint32_t> segment_starts;
|
||||
segment_starts.reserve(16);
|
||||
if (current_byte_offset < editor->scroll.col)
|
||||
segment_starts.push_back(0);
|
||||
while (current_byte_offset < editor->scroll.col &&
|
||||
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;
|
||||
}
|
||||
if (i == editor->scroll.row && 0 == editor->scroll.col) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
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);
|
||||
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 (q_size < number) {
|
||||
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(line);
|
||||
line = prev_line(it, &len);
|
||||
if (!line) {
|
||||
editor->scroll = {0, 0};
|
||||
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);
|
||||
free(scroll_queue);
|
||||
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;
|
||||
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);
|
||||
}
|
||||
editor->scroll = {0, 0};
|
||||
free(scroll_queue);
|
||||
} while (number > 0);
|
||||
free(it);
|
||||
}
|
||||
|
||||
@@ -92,23 +148,23 @@ void scroll_down(Editor *editor, uint32_t number) {
|
||||
uint32_t visual_seen = 0;
|
||||
bool first_visual_line = true;
|
||||
while (true) {
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
Coord fold_coord = {line_index, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
visual_seen++;
|
||||
if (visual_seen >= number + max_visual_lines) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
Coord fold_coord = {fold->start, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
do {
|
||||
visual_seen++;
|
||||
if (visual_seen >= number + max_visual_lines) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
uint32_t skip_until = fold->end;
|
||||
while (line_index <= skip_until) {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line) {
|
||||
free(scroll_queue);
|
||||
@@ -117,7 +173,7 @@ void scroll_down(Editor *editor, uint32_t number) {
|
||||
}
|
||||
free(line);
|
||||
line_index++;
|
||||
} while (editor->folded[line_index] == 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
@@ -181,7 +237,11 @@ void scroll_down(Editor *editor, uint32_t number) {
|
||||
void ensure_cursor(Editor *editor) {
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
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;
|
||||
}
|
||||
uint32_t numlen =
|
||||
@@ -197,23 +257,19 @@ void ensure_cursor(Editor *editor) {
|
||||
while (true) {
|
||||
if (visual_rows >= editor->size.row)
|
||||
break;
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
Coord c = {line_index, 0};
|
||||
last_visible = c;
|
||||
visual_rows++;
|
||||
if (line_index == editor->cursor.row) {
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
do {
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
Coord c = {fold->start, 0};
|
||||
last_visible = c;
|
||||
visual_rows++;
|
||||
uint32_t skip_until = fold->end;
|
||||
while (line_index <= skip_until) {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line)
|
||||
break;
|
||||
free(line);
|
||||
line_index++;
|
||||
} while (editor->folded[line_index] == 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
@@ -260,7 +316,15 @@ void ensure_cursor(Editor *editor) {
|
||||
free(line);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -319,29 +383,29 @@ void ensure_scroll(Editor *editor) {
|
||||
uint32_t q_size = 0;
|
||||
bool first_visual_line = true;
|
||||
while (true) {
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
Coord fold_coord = {line_index, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
Coord fold_coord = {fold->start, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
do {
|
||||
if (fold->start <= editor->cursor.row &&
|
||||
editor->cursor.row <= fold->end) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
uint32_t skip_until = fold->end;
|
||||
while (line_index <= skip_until) {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line)
|
||||
break;
|
||||
free(line);
|
||||
line_index++;
|
||||
} while (line_index < editor->size.row &&
|
||||
editor->folded[line_index] == 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
|
||||
82
src/knot.cc
82
src/knot.cc
@@ -520,6 +520,86 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line) {
|
||||
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) {
|
||||
if (it->top == 0) {
|
||||
it->node = nullptr;
|
||||
@@ -533,6 +613,8 @@ static inline void iter_advance_leaf(LineIterator *it) {
|
||||
while (curr) {
|
||||
it->stack[it->top++] = curr;
|
||||
if (!curr->left && !curr->right) {
|
||||
if (curr->char_count == 0)
|
||||
break;
|
||||
it->node = curr;
|
||||
it->offset = 0;
|
||||
return;
|
||||
|
||||
@@ -17,6 +17,15 @@ extern "C" {
|
||||
#include <unistd.h>
|
||||
#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) {
|
||||
FILE *pipe = popen("xclip -selection clipboard -o", "r");
|
||||
if (!pipe) {
|
||||
|
||||
Reference in New Issue
Block a user