Folding update test
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#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
|
||||||
@@ -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,13 +114,67 @@ 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<std::pair<int8_t, uint32_t>> folded;
|
std::vector<Fold> folds;
|
||||||
Spans spans;
|
Spans spans;
|
||||||
Spans def_spans;
|
Spans def_spans;
|
||||||
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);
|
||||||
|
|||||||
@@ -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);
|
||||||
@@ -118,14 +117,15 @@ 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].first) {
|
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||||
if (editor->folded[line_index].first == 2) {
|
if (fold) {
|
||||||
update(editor->position.row + rendered_rows, editor->position.col,
|
update(editor->position.row + rendered_rows, editor->position.col, "",
|
||||||
"", 0xAAAAAA, 0, 0);
|
0xAAAAAA, 0, 0);
|
||||||
char buf[16];
|
char buf[16];
|
||||||
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 3, line_index + 1);
|
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 3,
|
||||||
|
fold->start + 1);
|
||||||
uint32_t num_color =
|
uint32_t num_color =
|
||||||
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
|
editor->cursor.row == fold->start ? 0xFFFFFF : 0x555555;
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
update(editor->position.row + rendered_rows,
|
update(editor->position.row + rendered_rows,
|
||||||
editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color,
|
editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color,
|
||||||
@@ -138,8 +138,9 @@ void render_editor(Editor *editor) {
|
|||||||
for (; i < render_width; i++)
|
for (; i < render_width; i++)
|
||||||
update(rendered_rows, i + render_x, " ", 0xc6c6c6, 0, 0);
|
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)
|
||||||
@@ -150,11 +151,9 @@ 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].first == 1);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
char *line = next_line(it, &line_len);
|
||||||
if (!line)
|
if (!line)
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -9,6 +9,107 @@ extern "C" {
|
|||||||
|
|
||||||
static Highlight HL_UNDERLINE = {0, 0, 1 << 2, 100};
|
static Highlight HL_UNDERLINE = {0, 0, 1 << 2, 100};
|
||||||
|
|
||||||
|
static 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
static 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void apply_line_insertion(Editor *editor, uint32_t line,
|
||||||
|
uint32_t rows) {
|
||||||
|
if (rows == 0)
|
||||||
|
return;
|
||||||
|
for (auto &fold : editor->folds) {
|
||||||
|
if (line <= fold.start) {
|
||||||
|
fold.start += rows;
|
||||||
|
fold.end += rows;
|
||||||
|
} else if (line <= fold.end) {
|
||||||
|
fold.end += rows;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static 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 (fold.end < removal_start) {
|
||||||
|
updated.push_back(fold);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (fold.start > removal_end) {
|
||||||
|
fold.start -= rows_removed;
|
||||||
|
fold.end -= rows_removed;
|
||||||
|
updated.push_back(fold);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (fold.start >= removal_start)
|
||||||
|
continue;
|
||||||
|
if (fold.end > removal_end)
|
||||||
|
fold.end -= rows_removed;
|
||||||
|
else
|
||||||
|
fold.end = removal_start - 1;
|
||||||
|
if (fold.end >= fold.start)
|
||||||
|
updated.push_back(fold);
|
||||||
|
}
|
||||||
|
editor->folds.swap(updated);
|
||||||
|
}
|
||||||
|
|
||||||
void handle_editor_event(Editor *editor, KeyEvent event) {
|
void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||||
static std::chrono::steady_clock::time_point last_click_time =
|
static std::chrono::steady_clock::time_point last_click_time =
|
||||||
std::chrono::steady_clock::now();
|
std::chrono::steady_clock::now();
|
||||||
@@ -359,10 +460,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
|||||||
if (editor->cursor.row != editor->selection.row) {
|
if (editor->cursor.row != editor->selection.row) {
|
||||||
uint32_t start = MIN(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);
|
uint32_t end = MAX(editor->cursor.row, editor->selection.row);
|
||||||
for (uint32_t row = start + 1; row <= end; row++)
|
add_fold(editor, start, end);
|
||||||
editor->folded[row].first = 1;
|
|
||||||
editor->folded[start].first = 2;
|
|
||||||
editor->folded[start].second = end - start;
|
|
||||||
}
|
}
|
||||||
cursor_left(editor, 1);
|
cursor_left(editor, 1);
|
||||||
cursor_down(editor, 1);
|
cursor_down(editor, 1);
|
||||||
@@ -428,7 +526,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
|||||||
} else {
|
} else {
|
||||||
uint32_t line = editor->hooks[event.c[0] - '!'];
|
uint32_t line = editor->hooks[event.c[0] - '!'];
|
||||||
if (line > 0) {
|
if (line > 0) {
|
||||||
if (editor->folded[--line].first)
|
if (line_is_folded(editor->folds, --line))
|
||||||
break;
|
break;
|
||||||
editor->cursor = {line, 0};
|
editor->cursor = {line, 0};
|
||||||
editor->cursor_preffered = UINT32_MAX;
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
@@ -619,28 +717,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].first) {
|
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||||
if (editor->folded[line_index].first == 2) {
|
if (fold) {
|
||||||
if (visual_row == target_visual_row) {
|
if (visual_row == target_visual_row) {
|
||||||
free(it);
|
free(it);
|
||||||
if (is_gutter_click) {
|
if (is_gutter_click) {
|
||||||
editor->folded[line_index].first = 0;
|
remove_fold(editor, fold->start);
|
||||||
for (uint32_t row = line_index + 1; editor->folded[row].first == 1;
|
|
||||||
row++)
|
|
||||||
editor->folded[row].first = 0;
|
|
||||||
return {UINT32_MAX, UINT32_MAX};
|
return {UINT32_MAX, UINT32_MAX};
|
||||||
}
|
}
|
||||||
return {line_index - 1, 0};
|
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].first == 1);
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
uint32_t line_len;
|
uint32_t line_len;
|
||||||
@@ -825,10 +919,7 @@ Coord move_right(Editor *editor, Coord cursor, uint32_t number) {
|
|||||||
if (col >= line_len) {
|
if (col >= line_len) {
|
||||||
free(line);
|
free(line);
|
||||||
line = nullptr;
|
line = nullptr;
|
||||||
uint32_t next_row = row + 1;
|
uint32_t next_row = next_unfolded_row(editor, row + 1);
|
||||||
while (next_row < editor->root->line_count &&
|
|
||||||
editor->folded[next_row].first != 0)
|
|
||||||
next_row++;
|
|
||||||
if (next_row >= editor->root->line_count) {
|
if (next_row >= editor->root->line_count) {
|
||||||
col = line_len;
|
col = line_len;
|
||||||
break;
|
break;
|
||||||
@@ -889,9 +980,17 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
|
|||||||
line = prev_line(it, &len);
|
line = prev_line(it, &len);
|
||||||
if (!line)
|
if (!line)
|
||||||
break;
|
break;
|
||||||
if (editor->folded[row].first != 0) {
|
const Fold *fold = fold_for_line(editor->folds, row);
|
||||||
|
if (fold) {
|
||||||
|
while (line && row > fold->start) {
|
||||||
|
free(line);
|
||||||
|
line = prev_line(it, &len);
|
||||||
|
row--;
|
||||||
|
}
|
||||||
|
if (line) {
|
||||||
free(line);
|
free(line);
|
||||||
line = nullptr;
|
line = nullptr;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -925,29 +1024,36 @@ 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);
|
|
||||||
editor->cursor.row += 1;
|
uint32_t target_row = editor->cursor.row;
|
||||||
if (editor->cursor.row >= editor->root->line_count) {
|
while (number > 0 && target_row < editor->root->line_count - 1) {
|
||||||
editor->cursor.row = editor->root->line_count - 1;
|
target_row = next_unfolded_row(editor, target_row + 1);
|
||||||
|
if (target_row >= editor->root->line_count) {
|
||||||
|
target_row = editor->root->line_count - 1;
|
||||||
break;
|
break;
|
||||||
};
|
}
|
||||||
if (editor->folded[editor->cursor.row].first != 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);
|
||||||
}
|
}
|
||||||
@@ -955,36 +1061,40 @@ void cursor_down(Editor *editor, uint32_t number) {
|
|||||||
void cursor_up(Editor *editor, uint32_t number) {
|
void cursor_up(Editor *editor, uint32_t number) {
|
||||||
if (!editor || !editor->root || number == 0 || editor->cursor.row == 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);
|
||||||
line_content = prev_line(it, &len);
|
|
||||||
do {
|
uint32_t target_row = editor->cursor.row;
|
||||||
free(line_content);
|
while (number > 0 && target_row > 0) {
|
||||||
line_content = prev_line(it, &len);
|
target_row = prev_unfolded_row(editor, target_row - 1);
|
||||||
if (!line_content) {
|
if (target_row == 0) {
|
||||||
editor->cursor.row = 0;
|
number--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
editor->cursor.row--;
|
number--;
|
||||||
if (editor->folded[editor->cursor.row].first != 0)
|
}
|
||||||
number++;
|
|
||||||
} while (--number > 0 && editor->cursor.row > 0);
|
it = begin_l_iter(editor->root, target_row);
|
||||||
|
line_content = next_line(it, &len);
|
||||||
free(it);
|
free(it);
|
||||||
if (line_content) {
|
if (line_content) {
|
||||||
|
if (len > 0 && line_content[len - 1] == '\n')
|
||||||
|
--len;
|
||||||
|
editor->cursor.row = target_row;
|
||||||
editor->cursor.col =
|
editor->cursor.col =
|
||||||
get_bytes_from_visual_col(line_content, len, visual_col);
|
get_bytes_from_visual_col(line_content, len, visual_col);
|
||||||
free(line_content);
|
free(line_content);
|
||||||
} else {
|
} else {
|
||||||
|
editor->cursor.row = 0;
|
||||||
editor->cursor.col = 0;
|
editor->cursor.col = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1019,12 +1129,8 @@ void move_line_up(Editor *editor) {
|
|||||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||||
line_len--;
|
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 up_by = 1;
|
uint32_t target_row = prev_unfolded_row(editor, editor->cursor.row - 1);
|
||||||
while (editor->cursor.row >= up_by &&
|
uint32_t up_by = editor->cursor.row - target_row;
|
||||||
editor->folded[editor->cursor.row - up_by].first)
|
|
||||||
up_by++;
|
|
||||||
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);
|
||||||
@@ -1072,11 +1178,14 @@ void move_line_down(Editor *editor) {
|
|||||||
if (line_len && line[line_len - 1] == '\n')
|
if (line_len && line[line_len - 1] == '\n')
|
||||||
line_len--;
|
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 down_by = 1;
|
uint32_t target_row =
|
||||||
while (editor->folded[editor->cursor.row + down_by].first)
|
next_unfolded_row(editor, editor->cursor.row + 1);
|
||||||
down_by++;
|
if (target_row >= editor->root->line_count) {
|
||||||
if (down_by > 1)
|
free(line);
|
||||||
down_by--;
|
lock.unlock();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uint32_t down_by = target_row - editor->cursor.row;
|
||||||
uint32_t ln;
|
uint32_t ln;
|
||||||
line_to_byte(editor->root, editor->cursor.row + down_by - 1, &ln);
|
line_to_byte(editor->root, editor->cursor.row + down_by - 1, &ln);
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
@@ -1114,7 +1223,6 @@ void move_line_down(Editor *editor) {
|
|||||||
void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
return;
|
return;
|
||||||
uint32_t erased_rows, erase_start_row;
|
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
std::shared_lock lock_1(editor->knot_mtx);
|
std::shared_lock lock_1(editor->knot_mtx);
|
||||||
uint32_t cursor_original =
|
uint32_t cursor_original =
|
||||||
@@ -1135,8 +1243,10 @@ 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();
|
||||||
erased_rows = point.row - pos.row;
|
uint32_t start_row = point.row;
|
||||||
erase_start_row = pos.row;
|
uint32_t end_row = pos.row;
|
||||||
|
if (end_row > start_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();
|
||||||
@@ -1177,26 +1287,10 @@ 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();
|
||||||
int32_t fold_start = -1;
|
uint32_t start_row = pos.row;
|
||||||
int32_t fold_end = -1;
|
uint32_t end_row = point.row;
|
||||||
if (editor->folded[point.row].first > 0) {
|
if (end_row > start_row)
|
||||||
fold_start = point.row;
|
apply_line_deletion(editor, start_row + 1, end_row);
|
||||||
while (fold_start > 0 && editor->folded[fold_start].first == 1)
|
|
||||||
fold_start--;
|
|
||||||
fold_end = point.row;
|
|
||||||
while (fold_end + 1 < editor->folded.size() &&
|
|
||||||
editor->folded[fold_end + 1].first == 1)
|
|
||||||
fold_end++;
|
|
||||||
}
|
|
||||||
if (fold_start == point.row)
|
|
||||||
editor->folded.erase(editor->folded.begin() + point.row,
|
|
||||||
editor->folded.begin() + pos.row);
|
|
||||||
else if (fold_end == point.row)
|
|
||||||
editor->folded.erase(editor->folded.begin() + point.row,
|
|
||||||
editor->folded.begin());
|
|
||||||
else if (fold_start != -1 && fold_start == fold_end)
|
|
||||||
editor->folded.erase(editor->folded.begin() + point.row,
|
|
||||||
editor->folded.begin());
|
|
||||||
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();
|
||||||
@@ -1247,21 +1341,8 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
|
|||||||
cols++;
|
cols++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int32_t fold_start = -1;
|
if (rows > 0)
|
||||||
int32_t fold_end = -1;
|
apply_line_insertion(editor, pos.row, rows);
|
||||||
if (editor->folded[pos.row].first > 0) {
|
|
||||||
fold_start = pos.row;
|
|
||||||
while (fold_start > 0 && editor->folded[fold_start].first == 1)
|
|
||||||
fold_start--;
|
|
||||||
fold_end = pos.row;
|
|
||||||
while (fold_end + 1 < editor->folded.size() &&
|
|
||||||
editor->folded[fold_end + 1].first == 1)
|
|
||||||
fold_end++;
|
|
||||||
}
|
|
||||||
if (fold_start == pos.row)
|
|
||||||
editor->folded.insert(editor->folded.begin() + pos.row, rows, {0, 0});
|
|
||||||
else if (fold_end == pos.row)
|
|
||||||
editor->folded.insert(editor->folded.begin() + pos.row + 1, rows, {0, 0});
|
|
||||||
if (editor->tree) {
|
if (editor->tree) {
|
||||||
TSInputEdit edit = {
|
TSInputEdit edit = {
|
||||||
.start_byte = byte_pos,
|
.start_byte = byte_pos,
|
||||||
|
|||||||
@@ -66,16 +66,37 @@ void scroll_up(Editor *editor, int32_t number) {
|
|||||||
free(it);
|
free(it);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (editor->folded[line_index].first) {
|
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||||
if (editor->folded[line_index].first == 2) {
|
if (fold) {
|
||||||
if (--number == 0) {
|
while (line && line_index > fold->start) {
|
||||||
editor->scroll = {line_index, 0};
|
|
||||||
free(line);
|
free(line);
|
||||||
|
line = prev_line(it, &len);
|
||||||
|
line_index--;
|
||||||
|
if (!line) {
|
||||||
|
editor->scroll = {0, 0};
|
||||||
free(it);
|
free(it);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (--number == 0) {
|
||||||
|
editor->scroll = {fold->start, 0};
|
||||||
free(line);
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
if (len > 0 && line[len - 1] == '\n')
|
if (len > 0 && line[len - 1] == '\n')
|
||||||
@@ -127,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].first) {
|
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||||
if (editor->folded[line_index].first == 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++;
|
||||||
@@ -142,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);
|
||||||
@@ -152,7 +173,7 @@ void scroll_down(Editor *editor, uint32_t number) {
|
|||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
line_index++;
|
line_index++;
|
||||||
} while (editor->folded[line_index].first == 1);
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
uint32_t line_len;
|
uint32_t line_len;
|
||||||
@@ -216,10 +237,7 @@ 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) {
|
||||||
uint32_t line_idx = editor->scroll.row;
|
uint32_t line_idx = next_unfolded_row(editor, editor->scroll.row);
|
||||||
while (line_idx < editor->root->line_count &&
|
|
||||||
editor->folded[line_idx].first)
|
|
||||||
line_idx++;
|
|
||||||
editor->cursor.row = line_idx;
|
editor->cursor.row = line_idx;
|
||||||
editor->cursor.col =
|
editor->cursor.col =
|
||||||
line_idx == editor->scroll.row ? editor->scroll.col : 0;
|
line_idx == editor->scroll.row ? editor->scroll.col : 0;
|
||||||
@@ -239,19 +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].first) {
|
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||||
if (editor->folded[line_index].first == 2) {
|
if (fold) {
|
||||||
Coord c = {line_index, 0};
|
Coord c = {fold->start, 0};
|
||||||
last_visible = c;
|
last_visible = c;
|
||||||
visual_rows++;
|
visual_rows++;
|
||||||
}
|
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 (editor->folded[line_index].first == 1);
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
uint32_t line_len;
|
uint32_t line_len;
|
||||||
@@ -299,8 +317,11 @@ void ensure_cursor(Editor *editor) {
|
|||||||
line_index++;
|
line_index++;
|
||||||
}
|
}
|
||||||
uint32_t last_real_row = last_visible.row;
|
uint32_t last_real_row = last_visible.row;
|
||||||
while (last_visible.row > 0 && editor->folded[last_visible.row].first)
|
const Fold *last_fold = fold_for_line(editor->folds, last_visible.row);
|
||||||
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.row = last_visible.row;
|
||||||
editor->cursor.col = last_visible.row == last_real_row ? last_visible.col : 0;
|
editor->cursor.col = last_visible.row == last_real_row ? last_visible.col : 0;
|
||||||
editor->cursor_preffered = UINT32_MAX;
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
@@ -362,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].first) {
|
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||||
if (editor->folded[line_index].first == 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++;
|
||||||
@@ -372,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].first == 1);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
uint32_t line_len;
|
uint32_t line_len;
|
||||||
|
|||||||
Reference in New Issue
Block a user