Compare commits
4 Commits
fd70a40a49
...
ce06ca8057
| Author | SHA1 | Date | |
|---|---|---|---|
|
ce06ca8057
|
|||
|
dad7d844ca
|
|||
|
e6ce95a1d4
|
|||
|
06c7b7dfaa
|
@@ -6,10 +6,9 @@ A TUI IDE.
|
|||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
- [ ] Make function to get selected text. (selection itself is done)
|
|
||||||
- [ ] Add modes for editing - insert, select, normal, etc.
|
|
||||||
- [ ] Add line numbers.
|
- [ ] Add line numbers.
|
||||||
- [ ] Add bg highlight for current line.
|
- [ ] Add bg highlight for current line.
|
||||||
|
- [ ] Make function to get selected text. (selection itself is done)
|
||||||
- [ ] Add support for copy/cut/paste.
|
- [ ] Add support for copy/cut/paste.
|
||||||
- [ ] Add support for ctrl + arrow key for moving words.
|
- [ ] Add support for ctrl + arrow key for moving words.
|
||||||
- [ ] Add support for ctrl + backspace / delete for deleting words.
|
- [ ] Add support for ctrl + backspace / delete for deleting words.
|
||||||
@@ -26,3 +25,4 @@ A TUI IDE.
|
|||||||
- [ ] Add `.scm` files for all the supported languages. (2/14) Done.
|
- [ ] Add `.scm` files for all the supported languages. (2/14) Done.
|
||||||
- [ ] Add support for LSP & autocomplete / snippets.
|
- [ ] Add support for LSP & autocomplete / snippets.
|
||||||
- [ ] Add codeium/copilot support.
|
- [ ] Add codeium/copilot support.
|
||||||
|
- [ ] Add git stuff.
|
||||||
|
|||||||
14
include/main.h
Normal file
14
include/main.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef MAIN_H
|
||||||
|
#define MAIN_H
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#define NORMAL 0
|
||||||
|
#define INSERT 1
|
||||||
|
#define SELECT 2
|
||||||
|
#define RUNNER 3
|
||||||
|
|
||||||
|
extern std::atomic<bool> running;
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -54,7 +54,7 @@ enum CellFlags : uint8_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct ScreenCell {
|
struct ScreenCell {
|
||||||
std::string utf8 = std::string(""); // empty => no content
|
std::string utf8 = std::string("");
|
||||||
uint32_t fg = 0;
|
uint32_t fg = 0;
|
||||||
uint32_t bg = 0;
|
uint32_t bg = 0;
|
||||||
uint8_t flags = CF_NONE;
|
uint8_t flags = CF_NONE;
|
||||||
@@ -63,7 +63,8 @@ struct ScreenCell {
|
|||||||
struct KeyEvent {
|
struct KeyEvent {
|
||||||
uint8_t key_type;
|
uint8_t key_type;
|
||||||
|
|
||||||
char c;
|
char *c;
|
||||||
|
uint32_t len;
|
||||||
|
|
||||||
uint8_t special_key;
|
uint8_t special_key;
|
||||||
uint8_t special_modifier;
|
uint8_t special_modifier;
|
||||||
@@ -77,10 +78,9 @@ struct KeyEvent {
|
|||||||
};
|
};
|
||||||
|
|
||||||
extern uint32_t rows, cols;
|
extern uint32_t rows, cols;
|
||||||
extern std::vector<ScreenCell> screen; // size rows*cols
|
extern std::vector<ScreenCell> screen;
|
||||||
extern std::vector<ScreenCell> old_screen;
|
extern std::vector<ScreenCell> old_screen;
|
||||||
extern std::mutex screen_mutex;
|
extern std::mutex screen_mutex;
|
||||||
extern std::atomic<bool> running;
|
|
||||||
|
|
||||||
Coord start_screen();
|
Coord start_screen();
|
||||||
void end_screen();
|
void end_screen();
|
||||||
@@ -90,7 +90,6 @@ void set_cursor(int row, int col, int show_cursor_param);
|
|||||||
void render();
|
void render();
|
||||||
Coord get_size();
|
Coord get_size();
|
||||||
|
|
||||||
int read_input(char *buf, size_t buflen);
|
|
||||||
KeyEvent read_key();
|
KeyEvent read_key();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
462
src/editor.cc
462
src/editor.cc
@@ -4,7 +4,6 @@ extern "C" {
|
|||||||
#include "../include/editor.h"
|
#include "../include/editor.h"
|
||||||
#include "../include/ts.h"
|
#include "../include/ts.h"
|
||||||
#include "../include/utils.h"
|
#include "../include/utils.h"
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
Editor *new_editor(const char *filename, Coord position, Coord size) {
|
Editor *new_editor(const char *filename, Coord position, Coord size) {
|
||||||
Editor *editor = new Editor();
|
Editor *editor = new Editor();
|
||||||
@@ -28,7 +27,7 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
|
|||||||
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);
|
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);
|
||||||
editor->language = language.fn();
|
editor->language = language.fn();
|
||||||
@@ -49,296 +48,6 @@ void free_editor(Editor *editor) {
|
|||||||
delete editor;
|
delete editor;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cursor_down(Editor *editor, uint32_t number) {
|
|
||||||
if (!editor || !editor->root || number == 0)
|
|
||||||
return;
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t len;
|
|
||||||
char *line_content = next_line(it, &len);
|
|
||||||
if (line_content == nullptr)
|
|
||||||
return;
|
|
||||||
if (editor->cursor_preffered == UINT32_MAX)
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
|
|
||||||
uint32_t visual_col = editor->cursor_preffered;
|
|
||||||
do {
|
|
||||||
free(line_content);
|
|
||||||
line_content = next_line(it, &len);
|
|
||||||
editor->cursor.row += 1;
|
|
||||||
if (editor->cursor.row >= editor->root->line_count) {
|
|
||||||
editor->cursor.row = editor->root->line_count - 1;
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
if (editor->folded[editor->cursor.row] != 0)
|
|
||||||
number++;
|
|
||||||
} while (--number > 0);
|
|
||||||
free(it);
|
|
||||||
if (line_content == nullptr)
|
|
||||||
return;
|
|
||||||
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
|
|
||||||
free(line_content);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cursor_up(Editor *editor, uint32_t number) {
|
|
||||||
if (!editor || !editor->root || number == 0)
|
|
||||||
return;
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t len;
|
|
||||||
char *line_content = next_line(it, &len);
|
|
||||||
if (!line_content) {
|
|
||||||
free(it);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (editor->cursor_preffered == UINT32_MAX)
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
|
|
||||||
uint32_t visual_col = editor->cursor_preffered;
|
|
||||||
free(line_content);
|
|
||||||
while (number > 0 && editor->cursor.row > 0) {
|
|
||||||
editor->cursor.row--;
|
|
||||||
if (editor->folded[editor->cursor.row] != 0)
|
|
||||||
continue;
|
|
||||||
number--;
|
|
||||||
}
|
|
||||||
free(it);
|
|
||||||
it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
line_content = next_line(it, &len);
|
|
||||||
if (!line_content) {
|
|
||||||
free(it);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
|
|
||||||
free(line_content);
|
|
||||||
free(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cursor_right(Editor *editor, uint32_t number) {
|
|
||||||
if (!editor || !editor->root || number == 0)
|
|
||||||
return;
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
if (line[line_len - 1] == '\n')
|
|
||||||
--line_len;
|
|
||||||
while (number > 0) {
|
|
||||||
if (editor->cursor.col >= line_len) {
|
|
||||||
free(line);
|
|
||||||
line = nullptr;
|
|
||||||
uint32_t next_row = editor->cursor.row + 1;
|
|
||||||
while (next_row < editor->root->line_count &&
|
|
||||||
editor->folded[next_row] != 0)
|
|
||||||
next_row++;
|
|
||||||
if (next_row >= editor->root->line_count) {
|
|
||||||
editor->cursor.col = line_len;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
editor->cursor.row = next_row;
|
|
||||||
editor->cursor.col = 0;
|
|
||||||
it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
break;
|
|
||||||
if (line[line_len - 1] == '\n')
|
|
||||||
--line_len;
|
|
||||||
} else {
|
|
||||||
uint32_t inc = grapheme_next_character_break_utf8(
|
|
||||||
line + editor->cursor.col, line_len - editor->cursor.col);
|
|
||||||
if (inc == 0)
|
|
||||||
break;
|
|
||||||
editor->cursor.col += inc;
|
|
||||||
}
|
|
||||||
number--;
|
|
||||||
}
|
|
||||||
LineIterator *it2 = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t len2;
|
|
||||||
char *cur_line = next_line(it2, &len2);
|
|
||||||
free(it2);
|
|
||||||
if (cur_line) {
|
|
||||||
if (len2 > 0 && cur_line[len2 - 1] == '\n')
|
|
||||||
--len2;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(cur_line, len2, editor->cursor.col);
|
|
||||||
free(cur_line);
|
|
||||||
} else {
|
|
||||||
editor->cursor_preffered = UINT32_MAX;
|
|
||||||
}
|
|
||||||
if (line)
|
|
||||||
free(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cursor_left(Editor *editor, uint32_t number) {
|
|
||||||
if (!editor || !editor->root || number == 0)
|
|
||||||
return;
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t len;
|
|
||||||
char *line = next_line(it, &len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
if (line[len - 1] == '\n')
|
|
||||||
line[--len] = '\0';
|
|
||||||
while (number > 0) {
|
|
||||||
if (editor->cursor.col == 0) {
|
|
||||||
free(line);
|
|
||||||
line = nullptr;
|
|
||||||
if (editor->cursor.row == 0)
|
|
||||||
break;
|
|
||||||
int32_t prev_row = editor->cursor.row - 1;
|
|
||||||
while (prev_row >= 0 && editor->folded[prev_row] != 0)
|
|
||||||
prev_row--;
|
|
||||||
if (prev_row < 0)
|
|
||||||
break;
|
|
||||||
editor->cursor.row = prev_row;
|
|
||||||
it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
line = next_line(it, &len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
break;
|
|
||||||
if (line[len - 1] == '\n')
|
|
||||||
--len;
|
|
||||||
editor->cursor.col = len;
|
|
||||||
} else {
|
|
||||||
uint32_t col = editor->cursor.col;
|
|
||||||
uint32_t new_col = 0;
|
|
||||||
uint32_t visual_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;
|
|
||||||
visual_col++;
|
|
||||||
}
|
|
||||||
editor->cursor.col = new_col;
|
|
||||||
}
|
|
||||||
number--;
|
|
||||||
}
|
|
||||||
LineIterator *it2 = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t len2;
|
|
||||||
char *cur_line = next_line(it2, &len2);
|
|
||||||
free(it2);
|
|
||||||
if (cur_line) {
|
|
||||||
if (len2 > 0 && cur_line[len2 - 1] == '\n')
|
|
||||||
--len2;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(cur_line, len2, editor->cursor.col);
|
|
||||||
free(cur_line);
|
|
||||||
} else {
|
|
||||||
editor->cursor_preffered = UINT32_MAX;
|
|
||||||
}
|
|
||||||
if (line)
|
|
||||||
free(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
Coord simulate_cursor_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 = row + 1;
|
|
||||||
while (next_row < editor->root->line_count &&
|
|
||||||
editor->folded[next_row] != 0)
|
|
||||||
next_row++;
|
|
||||||
if (next_row >= editor->root->line_count) {
|
|
||||||
col = line_len;
|
|
||||||
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 simulate_cursor_left(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);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return result;
|
|
||||||
if (len > 0 && line[len - 1] == '\n')
|
|
||||||
--len;
|
|
||||||
while (number > 0) {
|
|
||||||
if (col == 0) {
|
|
||||||
free(line);
|
|
||||||
line = nullptr;
|
|
||||||
if (row == 0)
|
|
||||||
break;
|
|
||||||
int32_t prev_row = row - 1;
|
|
||||||
while (prev_row >= 0 && editor->folded[prev_row] != 0)
|
|
||||||
prev_row--;
|
|
||||||
if (prev_row < 0)
|
|
||||||
break;
|
|
||||||
row = prev_row;
|
|
||||||
it = begin_l_iter(editor->root, row);
|
|
||||||
line = next_line(it, &len);
|
|
||||||
free(it);
|
|
||||||
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);
|
|
||||||
result.row = row;
|
|
||||||
result.col = col;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ensure_scroll(Editor *editor) {
|
void ensure_scroll(Editor *editor) {
|
||||||
std::shared_lock knot_lock(editor->knot_mtx);
|
std::shared_lock knot_lock(editor->knot_mtx);
|
||||||
if (editor->cursor.row < editor->scroll.row ||
|
if (editor->cursor.row < editor->scroll.row ||
|
||||||
@@ -522,175 +231,6 @@ void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
|
||||||
if (len == 0)
|
|
||||||
return;
|
|
||||||
if (len < 0) {
|
|
||||||
std::shared_lock lock_1(editor->knot_mtx);
|
|
||||||
uint32_t cursor_original =
|
|
||||||
line_to_byte(editor->root, editor->cursor.row, nullptr) +
|
|
||||||
editor->cursor.col;
|
|
||||||
TSPoint old_point = {pos.row, pos.col};
|
|
||||||
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
|
|
||||||
Coord point = simulate_cursor_left(editor, pos, -len);
|
|
||||||
uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
|
||||||
if (cursor_original > start && cursor_original <= byte_pos) {
|
|
||||||
editor->cursor = point;
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, point.row);
|
|
||||||
if (!it)
|
|
||||||
return;
|
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line, line_len, point.col);
|
|
||||||
free(line);
|
|
||||||
} else if (cursor_original > byte_pos) {
|
|
||||||
uint32_t cursor_new = cursor_original - (byte_pos - start);
|
|
||||||
uint32_t new_col;
|
|
||||||
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
|
||||||
editor->cursor = {new_row, new_col};
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, new_row);
|
|
||||||
if (!it)
|
|
||||||
return;
|
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line, line_len, new_col);
|
|
||||||
free(line);
|
|
||||||
}
|
|
||||||
lock_1.unlock();
|
|
||||||
std::unique_lock lock_2(editor->knot_mtx);
|
|
||||||
editor->root = erase(editor->root, start, byte_pos - start);
|
|
||||||
lock_2.unlock();
|
|
||||||
if (editor->tree) {
|
|
||||||
TSInputEdit edit = {
|
|
||||||
.start_byte = start,
|
|
||||||
.old_end_byte = byte_pos,
|
|
||||||
.new_end_byte = start,
|
|
||||||
.start_point = {point.row, point.col},
|
|
||||||
.old_end_point = old_point,
|
|
||||||
.new_end_point = {point.row, point.col},
|
|
||||||
};
|
|
||||||
editor->edit_queue.push(edit);
|
|
||||||
}
|
|
||||||
std::unique_lock lock_3(editor->spans.mtx);
|
|
||||||
apply_edit(editor->spans.spans, start, start - byte_pos);
|
|
||||||
if (editor->spans.mid_parse)
|
|
||||||
editor->spans.edits.push({start, start - byte_pos});
|
|
||||||
} else {
|
|
||||||
std::shared_lock lock_1(editor->knot_mtx);
|
|
||||||
uint32_t cursor_original =
|
|
||||||
line_to_byte(editor->root, editor->cursor.row, nullptr) +
|
|
||||||
editor->cursor.col;
|
|
||||||
TSPoint old_point = {pos.row, pos.col};
|
|
||||||
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
|
|
||||||
Coord point = simulate_cursor_right(editor, pos, len);
|
|
||||||
uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
|
||||||
if (cursor_original > byte_pos && cursor_original <= end) {
|
|
||||||
editor->cursor = pos;
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, pos.row);
|
|
||||||
if (!it)
|
|
||||||
return;
|
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line, line_len, pos.col);
|
|
||||||
free(line);
|
|
||||||
} else if (cursor_original > end) {
|
|
||||||
uint32_t cursor_new = cursor_original - (end - byte_pos);
|
|
||||||
uint32_t new_col;
|
|
||||||
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
|
||||||
editor->cursor = {new_row, new_col};
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, new_row);
|
|
||||||
if (!it)
|
|
||||||
return;
|
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line, line_len, new_col);
|
|
||||||
free(line);
|
|
||||||
}
|
|
||||||
lock_1.unlock();
|
|
||||||
std::unique_lock lock_2(editor->knot_mtx);
|
|
||||||
editor->root = erase(editor->root, byte_pos, end - byte_pos);
|
|
||||||
lock_2.unlock();
|
|
||||||
if (editor->tree) {
|
|
||||||
TSInputEdit edit = {
|
|
||||||
.start_byte = byte_pos,
|
|
||||||
.old_end_byte = end,
|
|
||||||
.new_end_byte = byte_pos,
|
|
||||||
.start_point = old_point,
|
|
||||||
.old_end_point = {point.row, point.col},
|
|
||||||
.new_end_point = old_point,
|
|
||||||
};
|
|
||||||
editor->edit_queue.push(edit);
|
|
||||||
}
|
|
||||||
std::unique_lock lock_3(editor->spans.mtx);
|
|
||||||
apply_edit(editor->spans.spans, byte_pos, byte_pos - end);
|
|
||||||
if (editor->spans.mid_parse)
|
|
||||||
editor->spans.edits.push({byte_pos, byte_pos - end});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
|
|
||||||
std::shared_lock lock_1(editor->knot_mtx);
|
|
||||||
uint32_t cursor_original =
|
|
||||||
line_to_byte(editor->root, editor->cursor.row, nullptr) +
|
|
||||||
editor->cursor.col;
|
|
||||||
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
|
|
||||||
TSPoint start_point = {pos.row, pos.col};
|
|
||||||
if (cursor_original > byte_pos) {
|
|
||||||
uint32_t cursor_new = cursor_original + len;
|
|
||||||
uint32_t new_col;
|
|
||||||
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
|
||||||
editor->cursor = {new_row, new_col};
|
|
||||||
}
|
|
||||||
lock_1.unlock();
|
|
||||||
std::unique_lock lock_2(editor->knot_mtx);
|
|
||||||
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();
|
|
||||||
uint32_t cols = 0;
|
|
||||||
uint32_t rows = 0;
|
|
||||||
for (uint32_t i = 0; i < len; i++) {
|
|
||||||
if (data[i] == '\n') {
|
|
||||||
rows++;
|
|
||||||
cols = 0;
|
|
||||||
} else {
|
|
||||||
cols++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (editor->tree) {
|
|
||||||
TSInputEdit edit = {
|
|
||||||
.start_byte = byte_pos,
|
|
||||||
.old_end_byte = byte_pos,
|
|
||||||
.new_end_byte = byte_pos + len,
|
|
||||||
.start_point = start_point,
|
|
||||||
.old_end_point = start_point,
|
|
||||||
.new_end_point = {start_point.row + rows,
|
|
||||||
(rows == 0) ? (start_point.column + cols) : cols},
|
|
||||||
};
|
|
||||||
editor->edit_queue.push(edit);
|
|
||||||
}
|
|
||||||
std::unique_lock lock_3(editor->spans.mtx);
|
|
||||||
apply_edit(editor->spans.spans, byte_pos, len);
|
|
||||||
if (editor->spans.mid_parse)
|
|
||||||
editor->spans.edits.push({byte_pos, len});
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
if (editor->selection_active) {
|
if (editor->selection_active) {
|
||||||
|
|||||||
464
src/editor_ctrl.cc
Normal file
464
src/editor_ctrl.cc
Normal file
@@ -0,0 +1,464 @@
|
|||||||
|
extern "C" {
|
||||||
|
#include "../libs/libgrapheme/grapheme.h"
|
||||||
|
}
|
||||||
|
#include "../include/editor.h"
|
||||||
|
#include "../include/utils.h"
|
||||||
|
|
||||||
|
void cursor_down(Editor *editor, uint32_t number) {
|
||||||
|
if (!editor || !editor->root || number == 0)
|
||||||
|
return;
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
uint32_t len;
|
||||||
|
char *line_content = next_line(it, &len);
|
||||||
|
if (line_content == nullptr)
|
||||||
|
return;
|
||||||
|
if (editor->cursor_preffered == UINT32_MAX)
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
|
||||||
|
uint32_t visual_col = editor->cursor_preffered;
|
||||||
|
do {
|
||||||
|
free(line_content);
|
||||||
|
line_content = next_line(it, &len);
|
||||||
|
editor->cursor.row += 1;
|
||||||
|
if (editor->cursor.row >= editor->root->line_count) {
|
||||||
|
editor->cursor.row = editor->root->line_count - 1;
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
if (editor->folded[editor->cursor.row] != 0)
|
||||||
|
number++;
|
||||||
|
} while (--number > 0);
|
||||||
|
free(it);
|
||||||
|
if (line_content == nullptr)
|
||||||
|
return;
|
||||||
|
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
|
||||||
|
free(line_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cursor_up(Editor *editor, uint32_t number) {
|
||||||
|
if (!editor || !editor->root || number == 0)
|
||||||
|
return;
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
uint32_t len;
|
||||||
|
char *line_content = next_line(it, &len);
|
||||||
|
if (!line_content) {
|
||||||
|
free(it);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (editor->cursor_preffered == UINT32_MAX)
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
|
||||||
|
uint32_t visual_col = editor->cursor_preffered;
|
||||||
|
free(line_content);
|
||||||
|
while (number > 0 && editor->cursor.row > 0) {
|
||||||
|
editor->cursor.row--;
|
||||||
|
if (editor->folded[editor->cursor.row] != 0)
|
||||||
|
continue;
|
||||||
|
number--;
|
||||||
|
}
|
||||||
|
free(it);
|
||||||
|
it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
line_content = next_line(it, &len);
|
||||||
|
if (!line_content) {
|
||||||
|
free(it);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
|
||||||
|
free(line_content);
|
||||||
|
free(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cursor_right(Editor *editor, uint32_t number) {
|
||||||
|
if (!editor || !editor->root || number == 0)
|
||||||
|
return;
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
uint32_t line_len;
|
||||||
|
char *line = next_line(it, &line_len);
|
||||||
|
free(it);
|
||||||
|
if (!line)
|
||||||
|
return;
|
||||||
|
if (line[line_len - 1] == '\n')
|
||||||
|
--line_len;
|
||||||
|
while (number > 0) {
|
||||||
|
if (editor->cursor.col >= line_len) {
|
||||||
|
free(line);
|
||||||
|
line = nullptr;
|
||||||
|
uint32_t next_row = editor->cursor.row + 1;
|
||||||
|
while (next_row < editor->root->line_count &&
|
||||||
|
editor->folded[next_row] != 0)
|
||||||
|
next_row++;
|
||||||
|
if (next_row >= editor->root->line_count) {
|
||||||
|
editor->cursor.col = line_len;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
editor->cursor.row = next_row;
|
||||||
|
editor->cursor.col = 0;
|
||||||
|
it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
line = next_line(it, &line_len);
|
||||||
|
free(it);
|
||||||
|
if (!line)
|
||||||
|
break;
|
||||||
|
if (line[line_len - 1] == '\n')
|
||||||
|
--line_len;
|
||||||
|
} else {
|
||||||
|
uint32_t inc = grapheme_next_character_break_utf8(
|
||||||
|
line + editor->cursor.col, line_len - editor->cursor.col);
|
||||||
|
if (inc == 0)
|
||||||
|
break;
|
||||||
|
editor->cursor.col += inc;
|
||||||
|
}
|
||||||
|
number--;
|
||||||
|
}
|
||||||
|
LineIterator *it2 = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
uint32_t len2;
|
||||||
|
char *cur_line = next_line(it2, &len2);
|
||||||
|
free(it2);
|
||||||
|
if (cur_line) {
|
||||||
|
if (len2 > 0 && cur_line[len2 - 1] == '\n')
|
||||||
|
--len2;
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(cur_line, len2, editor->cursor.col);
|
||||||
|
free(cur_line);
|
||||||
|
} else {
|
||||||
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
|
}
|
||||||
|
if (line)
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cursor_left(Editor *editor, uint32_t number) {
|
||||||
|
if (!editor || !editor->root || number == 0)
|
||||||
|
return;
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
uint32_t len;
|
||||||
|
char *line = next_line(it, &len);
|
||||||
|
free(it);
|
||||||
|
if (!line)
|
||||||
|
return;
|
||||||
|
if (line[len - 1] == '\n')
|
||||||
|
line[--len] = '\0';
|
||||||
|
while (number > 0) {
|
||||||
|
if (editor->cursor.col == 0) {
|
||||||
|
free(line);
|
||||||
|
line = nullptr;
|
||||||
|
if (editor->cursor.row == 0)
|
||||||
|
break;
|
||||||
|
int32_t prev_row = editor->cursor.row - 1;
|
||||||
|
while (prev_row >= 0 && editor->folded[prev_row] != 0)
|
||||||
|
prev_row--;
|
||||||
|
if (prev_row < 0)
|
||||||
|
break;
|
||||||
|
editor->cursor.row = prev_row;
|
||||||
|
it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
line = next_line(it, &len);
|
||||||
|
free(it);
|
||||||
|
if (!line)
|
||||||
|
break;
|
||||||
|
if (line[len - 1] == '\n')
|
||||||
|
--len;
|
||||||
|
editor->cursor.col = len;
|
||||||
|
} else {
|
||||||
|
uint32_t col = editor->cursor.col;
|
||||||
|
uint32_t new_col = 0;
|
||||||
|
uint32_t visual_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;
|
||||||
|
visual_col++;
|
||||||
|
}
|
||||||
|
editor->cursor.col = new_col;
|
||||||
|
}
|
||||||
|
number--;
|
||||||
|
}
|
||||||
|
LineIterator *it2 = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
uint32_t len2;
|
||||||
|
char *cur_line = next_line(it2, &len2);
|
||||||
|
free(it2);
|
||||||
|
if (cur_line) {
|
||||||
|
if (len2 > 0 && cur_line[len2 - 1] == '\n')
|
||||||
|
--len2;
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(cur_line, len2, editor->cursor.col);
|
||||||
|
free(cur_line);
|
||||||
|
} else {
|
||||||
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
|
}
|
||||||
|
if (line)
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = row + 1;
|
||||||
|
while (next_row < editor->root->line_count &&
|
||||||
|
editor->folded[next_row] != 0)
|
||||||
|
next_row++;
|
||||||
|
if (next_row >= editor->root->line_count) {
|
||||||
|
col = line_len;
|
||||||
|
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(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);
|
||||||
|
free(it);
|
||||||
|
if (!line)
|
||||||
|
return result;
|
||||||
|
if (len > 0 && line[len - 1] == '\n')
|
||||||
|
--len;
|
||||||
|
while (number > 0) {
|
||||||
|
if (col == 0) {
|
||||||
|
free(line);
|
||||||
|
line = nullptr;
|
||||||
|
if (row == 0)
|
||||||
|
break;
|
||||||
|
int32_t prev_row = row - 1;
|
||||||
|
while (prev_row >= 0 && editor->folded[prev_row] != 0)
|
||||||
|
prev_row--;
|
||||||
|
if (prev_row < 0)
|
||||||
|
break;
|
||||||
|
row = prev_row;
|
||||||
|
it = begin_l_iter(editor->root, row);
|
||||||
|
line = next_line(it, &len);
|
||||||
|
free(it);
|
||||||
|
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);
|
||||||
|
result.row = row;
|
||||||
|
result.col = col;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||||
|
if (len == 0)
|
||||||
|
return;
|
||||||
|
if (len < 0) {
|
||||||
|
std::shared_lock lock_1(editor->knot_mtx);
|
||||||
|
uint32_t cursor_original =
|
||||||
|
line_to_byte(editor->root, editor->cursor.row, nullptr) +
|
||||||
|
editor->cursor.col;
|
||||||
|
TSPoint old_point = {pos.row, pos.col};
|
||||||
|
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
|
||||||
|
Coord point = move_left(editor, pos, -len);
|
||||||
|
uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
||||||
|
if (cursor_original > start && cursor_original <= byte_pos) {
|
||||||
|
editor->cursor = point;
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, point.row);
|
||||||
|
if (!it)
|
||||||
|
return;
|
||||||
|
uint32_t line_len;
|
||||||
|
char *line = next_line(it, &line_len);
|
||||||
|
free(it);
|
||||||
|
if (!line)
|
||||||
|
return;
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(line, line_len, point.col);
|
||||||
|
free(line);
|
||||||
|
} else if (cursor_original > byte_pos) {
|
||||||
|
uint32_t cursor_new = cursor_original - (byte_pos - start);
|
||||||
|
uint32_t new_col;
|
||||||
|
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
||||||
|
editor->cursor = {new_row, new_col};
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, new_row);
|
||||||
|
if (!it)
|
||||||
|
return;
|
||||||
|
uint32_t line_len;
|
||||||
|
char *line = next_line(it, &line_len);
|
||||||
|
free(it);
|
||||||
|
if (!line)
|
||||||
|
return;
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(line, line_len, new_col);
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
lock_1.unlock();
|
||||||
|
std::unique_lock lock_2(editor->knot_mtx);
|
||||||
|
editor->root = erase(editor->root, start, byte_pos - start);
|
||||||
|
lock_2.unlock();
|
||||||
|
if (editor->tree) {
|
||||||
|
TSInputEdit edit = {
|
||||||
|
.start_byte = start,
|
||||||
|
.old_end_byte = byte_pos,
|
||||||
|
.new_end_byte = start,
|
||||||
|
.start_point = {point.row, point.col},
|
||||||
|
.old_end_point = old_point,
|
||||||
|
.new_end_point = {point.row, point.col},
|
||||||
|
};
|
||||||
|
editor->edit_queue.push(edit);
|
||||||
|
}
|
||||||
|
std::unique_lock lock_3(editor->spans.mtx);
|
||||||
|
apply_edit(editor->spans.spans, start, start - byte_pos);
|
||||||
|
if (editor->spans.mid_parse)
|
||||||
|
editor->spans.edits.push({start, start - byte_pos});
|
||||||
|
} else {
|
||||||
|
std::shared_lock lock_1(editor->knot_mtx);
|
||||||
|
uint32_t cursor_original =
|
||||||
|
line_to_byte(editor->root, editor->cursor.row, nullptr) +
|
||||||
|
editor->cursor.col;
|
||||||
|
TSPoint old_point = {pos.row, pos.col};
|
||||||
|
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
|
||||||
|
Coord point = move_right(editor, pos, len);
|
||||||
|
uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
||||||
|
if (cursor_original > byte_pos && cursor_original <= end) {
|
||||||
|
editor->cursor = pos;
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, pos.row);
|
||||||
|
if (!it)
|
||||||
|
return;
|
||||||
|
uint32_t line_len;
|
||||||
|
char *line = next_line(it, &line_len);
|
||||||
|
free(it);
|
||||||
|
if (!line)
|
||||||
|
return;
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(line, line_len, pos.col);
|
||||||
|
free(line);
|
||||||
|
} else if (cursor_original > end) {
|
||||||
|
uint32_t cursor_new = cursor_original - (end - byte_pos);
|
||||||
|
uint32_t new_col;
|
||||||
|
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
||||||
|
editor->cursor = {new_row, new_col};
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, new_row);
|
||||||
|
if (!it)
|
||||||
|
return;
|
||||||
|
uint32_t line_len;
|
||||||
|
char *line = next_line(it, &line_len);
|
||||||
|
free(it);
|
||||||
|
if (!line)
|
||||||
|
return;
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(line, line_len, new_col);
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
lock_1.unlock();
|
||||||
|
std::unique_lock lock_2(editor->knot_mtx);
|
||||||
|
editor->root = erase(editor->root, byte_pos, end - byte_pos);
|
||||||
|
lock_2.unlock();
|
||||||
|
if (editor->tree) {
|
||||||
|
TSInputEdit edit = {
|
||||||
|
.start_byte = byte_pos,
|
||||||
|
.old_end_byte = end,
|
||||||
|
.new_end_byte = byte_pos,
|
||||||
|
.start_point = old_point,
|
||||||
|
.old_end_point = {point.row, point.col},
|
||||||
|
.new_end_point = old_point,
|
||||||
|
};
|
||||||
|
editor->edit_queue.push(edit);
|
||||||
|
}
|
||||||
|
std::unique_lock lock_3(editor->spans.mtx);
|
||||||
|
apply_edit(editor->spans.spans, byte_pos, byte_pos - end);
|
||||||
|
if (editor->spans.mid_parse)
|
||||||
|
editor->spans.edits.push({byte_pos, byte_pos - end});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
|
||||||
|
std::shared_lock lock_1(editor->knot_mtx);
|
||||||
|
uint32_t cursor_original =
|
||||||
|
line_to_byte(editor->root, editor->cursor.row, nullptr) +
|
||||||
|
editor->cursor.col;
|
||||||
|
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
|
||||||
|
TSPoint start_point = {pos.row, pos.col};
|
||||||
|
if (cursor_original > byte_pos) {
|
||||||
|
uint32_t cursor_new = cursor_original + len;
|
||||||
|
uint32_t new_col;
|
||||||
|
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
||||||
|
editor->cursor = {new_row, new_col};
|
||||||
|
}
|
||||||
|
lock_1.unlock();
|
||||||
|
std::unique_lock lock_2(editor->knot_mtx);
|
||||||
|
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();
|
||||||
|
uint32_t cols = 0;
|
||||||
|
uint32_t rows = 0;
|
||||||
|
for (uint32_t i = 0; i < len; i++) {
|
||||||
|
if (data[i] == '\n') {
|
||||||
|
rows++;
|
||||||
|
cols = 0;
|
||||||
|
} else {
|
||||||
|
cols++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (editor->tree) {
|
||||||
|
TSInputEdit edit = {
|
||||||
|
.start_byte = byte_pos,
|
||||||
|
.old_end_byte = byte_pos,
|
||||||
|
.new_end_byte = byte_pos + len,
|
||||||
|
.start_point = start_point,
|
||||||
|
.old_end_point = start_point,
|
||||||
|
.new_end_point = {start_point.row + rows,
|
||||||
|
(rows == 0) ? (start_point.column + cols) : cols},
|
||||||
|
};
|
||||||
|
editor->edit_queue.push(edit);
|
||||||
|
}
|
||||||
|
std::unique_lock lock_3(editor->spans.mtx);
|
||||||
|
apply_edit(editor->spans.spans, byte_pos, len);
|
||||||
|
if (editor->spans.mid_parse)
|
||||||
|
editor->spans.edits.push({byte_pos, len});
|
||||||
|
}
|
||||||
140
src/input.cc
140
src/input.cc
@@ -1,22 +1,113 @@
|
|||||||
|
extern "C" {
|
||||||
|
#include "../libs/libgrapheme/grapheme.h"
|
||||||
|
}
|
||||||
#include "../include/ui.h"
|
#include "../include/ui.h"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
int read_input(char *buf, size_t buflen) {
|
static Queue<char> input_queue;
|
||||||
size_t i = 0;
|
|
||||||
int n;
|
int get_utf8_seq_len(uint8_t byte) {
|
||||||
n = read(STDIN_FILENO, &buf[i], 1);
|
if ((byte & 0x80) == 0x00)
|
||||||
if (n <= 0)
|
return 1;
|
||||||
return -1;
|
if ((byte & 0xE0) == 0xC0)
|
||||||
i++;
|
return 2;
|
||||||
if (buf[0] == '\x1b') {
|
if ((byte & 0xF0) == 0xE0)
|
||||||
while (i < buflen - 1) {
|
return 3;
|
||||||
n = read(STDIN_FILENO, &buf[i], 1);
|
if ((byte & 0xF8) == 0xF0)
|
||||||
if (n <= 0)
|
return 4;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_next_byte(char *out) {
|
||||||
|
if (!input_queue.empty()) {
|
||||||
|
input_queue.pop(*out);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int n = read(STDIN_FILENO, out, 1);
|
||||||
|
return (n > 0) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void enqueue_bytes(const char *bytes, int len) {
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
input_queue.push(bytes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_input(char *&buf) {
|
||||||
|
size_t cap = 16;
|
||||||
|
buf = (char *)calloc(cap, sizeof(char));
|
||||||
|
size_t len = 0;
|
||||||
|
char header;
|
||||||
|
if (!get_next_byte(&header)) {
|
||||||
|
free(buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (header == '\x1b') {
|
||||||
|
buf[len++] = header;
|
||||||
|
while (len < 6) {
|
||||||
|
char next_c;
|
||||||
|
if (!get_next_byte(&next_c))
|
||||||
break;
|
break;
|
||||||
i++;
|
buf[len++] = next_c;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
int seq_len = get_utf8_seq_len((uint8_t)header);
|
||||||
|
buf[len++] = header;
|
||||||
|
if (seq_len == 1)
|
||||||
|
return len;
|
||||||
|
for (int i = 1; i < seq_len; i++) {
|
||||||
|
char next_c;
|
||||||
|
if (!get_next_byte(&next_c)) {
|
||||||
|
enqueue_bytes(buf, len);
|
||||||
|
free(buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
buf[len++] = next_c;
|
||||||
|
}
|
||||||
|
uint_least32_t current_cp, prev_cp;
|
||||||
|
grapheme_decode_utf8(buf, len, &prev_cp);
|
||||||
|
uint_least16_t state = 0;
|
||||||
|
while (true) {
|
||||||
|
char next_header;
|
||||||
|
if (!get_next_byte(&next_header))
|
||||||
|
break;
|
||||||
|
int next_seq_len = get_utf8_seq_len((uint8_t)next_header);
|
||||||
|
char temp_seq[5];
|
||||||
|
temp_seq[0] = next_header;
|
||||||
|
int temp_len = 1;
|
||||||
|
bool complete_seq = true;
|
||||||
|
for (int i = 1; i < next_seq_len; i++) {
|
||||||
|
char c;
|
||||||
|
if (!get_next_byte(&c)) {
|
||||||
|
complete_seq = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
temp_seq[temp_len++] = c;
|
||||||
|
}
|
||||||
|
if (!complete_seq) {
|
||||||
|
enqueue_bytes(temp_seq, temp_len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
grapheme_decode_utf8(temp_seq, temp_len, ¤t_cp);
|
||||||
|
if (grapheme_is_character_break(prev_cp, current_cp, &state)) {
|
||||||
|
enqueue_bytes(temp_seq, temp_len);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
if (len + temp_len + 1 >= cap) {
|
||||||
|
cap *= 2;
|
||||||
|
buf = (char *)realloc(buf, cap);
|
||||||
|
}
|
||||||
|
memcpy(buf + len, temp_seq, temp_len);
|
||||||
|
len += temp_len;
|
||||||
|
prev_cp = current_cp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf[i] = '\0';
|
buf[len] = '\0';
|
||||||
return i;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
void capture_mouse(char *buf, KeyEvent *ret) {
|
void capture_mouse(char *buf, KeyEvent *ret) {
|
||||||
@@ -48,22 +139,17 @@ void capture_mouse(char *buf, KeyEvent *ret) {
|
|||||||
|
|
||||||
KeyEvent read_key() {
|
KeyEvent read_key() {
|
||||||
KeyEvent ret;
|
KeyEvent ret;
|
||||||
char buf[7];
|
char *buf;
|
||||||
int n = read_input(buf, sizeof(buf));
|
int n = read_input(buf);
|
||||||
if (n <= 0) {
|
if (n <= 0) {
|
||||||
ret.key_type = KEY_NONE;
|
ret.key_type = KEY_NONE;
|
||||||
ret.c = '\0';
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
if (n == 1) {
|
if (buf[0] == '\x1b' && buf[1] == '[' && buf[2] == 'M') {
|
||||||
ret.key_type = KEY_CHAR;
|
|
||||||
ret.c = buf[0];
|
|
||||||
} else if (buf[0] == '\x1b' && buf[1] == '[' && buf[2] == 'M') {
|
|
||||||
ret.key_type = KEY_MOUSE;
|
ret.key_type = KEY_MOUSE;
|
||||||
capture_mouse(buf, &ret);
|
capture_mouse(buf, &ret);
|
||||||
} else {
|
} else if (buf[0] == '\x1b' && buf[1] == '[') {
|
||||||
ret.key_type = KEY_SPECIAL;
|
ret.key_type = KEY_SPECIAL;
|
||||||
if (buf[0] == '\x1b' && buf[1] == '[') {
|
|
||||||
int using_modifiers = buf[3] == ';';
|
int using_modifiers = buf[3] == ';';
|
||||||
int pos;
|
int pos;
|
||||||
if (!using_modifiers) {
|
if (!using_modifiers) {
|
||||||
@@ -109,7 +195,13 @@ KeyEvent read_key() {
|
|||||||
ret.special_key = 99;
|
ret.special_key = 99;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else if (n > 0) {
|
||||||
|
ret.key_type = KEY_CHAR;
|
||||||
|
ret.c = buf;
|
||||||
|
ret.len = n;
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
if (n > 0)
|
||||||
|
free(buf);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
114
src/main.cc
114
src/main.cc
@@ -1,3 +1,4 @@
|
|||||||
|
#include "../include/main.h"
|
||||||
#include "../include/editor.h"
|
#include "../include/editor.h"
|
||||||
#include "../include/ts.h"
|
#include "../include/ts.h"
|
||||||
#include "../include/ui.h"
|
#include "../include/ui.h"
|
||||||
@@ -9,10 +10,11 @@
|
|||||||
std::atomic<bool> running{true};
|
std::atomic<bool> running{true};
|
||||||
Queue<KeyEvent> event_queue;
|
Queue<KeyEvent> event_queue;
|
||||||
|
|
||||||
|
uint8_t mode = NORMAL;
|
||||||
|
|
||||||
void background_worker(Editor *editor) {
|
void background_worker(Editor *editor) {
|
||||||
while (running) {
|
while (running) {
|
||||||
ts_collect_spans(editor);
|
ts_collect_spans(editor);
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(16));
|
std::this_thread::sleep_for(std::chrono::milliseconds(16));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -22,49 +24,107 @@ void input_listener() {
|
|||||||
KeyEvent event = read_key();
|
KeyEvent event = read_key();
|
||||||
if (event.key_type == KEY_NONE)
|
if (event.key_type == KEY_NONE)
|
||||||
continue;
|
continue;
|
||||||
if (event.key_type == KEY_CHAR && event.c == CTRL('q'))
|
if (event.key_type == KEY_CHAR && *event.c == CTRL('q'))
|
||||||
running = false;
|
running = false;
|
||||||
event_queue.push(event);
|
event_queue.push(event);
|
||||||
|
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_editor_event(Editor *editor, KeyEvent event) {
|
void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||||
if (event.key_type == KEY_SPECIAL && event.special_key == KEY_DOWN)
|
if (event.key_type == KEY_SPECIAL) {
|
||||||
|
switch (event.special_key) {
|
||||||
|
case KEY_DOWN:
|
||||||
cursor_down(editor, 1);
|
cursor_down(editor, 1);
|
||||||
if (event.key_type == KEY_SPECIAL && event.special_key == KEY_UP)
|
break;
|
||||||
|
case KEY_UP:
|
||||||
cursor_up(editor, 1);
|
cursor_up(editor, 1);
|
||||||
if (event.key_type == KEY_SPECIAL && event.special_key == KEY_LEFT)
|
break;
|
||||||
|
case KEY_LEFT:
|
||||||
cursor_left(editor, 1);
|
cursor_left(editor, 1);
|
||||||
if (event.key_type == KEY_SPECIAL && event.special_key == KEY_RIGHT)
|
break;
|
||||||
cursor_right(editor, 1);
|
case KEY_RIGHT:
|
||||||
if (event.key_type == KEY_CHAR &&
|
|
||||||
((event.c >= 'a' && event.c <= 'z') ||
|
|
||||||
(event.c >= 'A' && event.c <= 'Z') ||
|
|
||||||
(event.c >= '0' && event.c <= '9') || event.c == ' ' || event.c == '!' ||
|
|
||||||
event.c == '@' || event.c == '#' || event.c == '$' || event.c == '%' ||
|
|
||||||
event.c == '^' || event.c == '&' || event.c == '*' || event.c == '(' ||
|
|
||||||
event.c == ')' || event.c == '-' || event.c == '_' || event.c == '=' ||
|
|
||||||
event.c == '+' || event.c == '[' || event.c == ']' || event.c == '{' ||
|
|
||||||
event.c == '}' || event.c == '\\' || event.c == '|' || event.c == ';' ||
|
|
||||||
event.c == ':' || event.c == '\'' || event.c == '"' || event.c == ',' ||
|
|
||||||
event.c == '.' || event.c == '<' || event.c == '>' || event.c == '/' ||
|
|
||||||
event.c == '?' || event.c == '`' || event.c == '~')) {
|
|
||||||
edit_insert(editor, editor->cursor, &event.c, 1);
|
|
||||||
cursor_right(editor, 1);
|
cursor_right(editor, 1);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (event.key_type == KEY_CHAR && event.c == '\t') {
|
}
|
||||||
edit_insert(editor, editor->cursor, (char *)"\t", 1);
|
switch (mode) {
|
||||||
|
case NORMAL:
|
||||||
|
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||||
|
switch (event.c[0]) {
|
||||||
|
case 'a':
|
||||||
|
case 'i':
|
||||||
|
mode = INSERT;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
cursor_right(editor, 2);
|
||||||
}
|
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
|
||||||
if (event.key_type == KEY_CHAR && (event.c == '\n' || event.c == '\r')) {
|
|
||||||
edit_insert(editor, editor->cursor, (char *)"\n", 1);
|
edit_insert(editor, editor->cursor, (char *)"\n", 1);
|
||||||
cursor_right(editor, 1);
|
cursor_right(editor, 1);
|
||||||
}
|
} else if (event.c[0] == 0x7F) {
|
||||||
if (event.key_type == KEY_CHAR && event.c == 0x7F)
|
|
||||||
edit_erase(editor, editor->cursor, -1);
|
edit_erase(editor, editor->cursor, -1);
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
|
} else if (event.len > 1) {
|
||||||
|
edit_insert(editor, editor->cursor, event.c, event.len);
|
||||||
|
cursor_right(editor, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (event.key_type == KEY_SPECIAL && event.special_key == KEY_DELETE)
|
if (event.key_type == KEY_SPECIAL && event.special_key == KEY_DELETE)
|
||||||
edit_erase(editor, editor->cursor, 1);
|
edit_erase(editor, editor->cursor, 1);
|
||||||
|
break;
|
||||||
|
case SELECT:
|
||||||
|
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||||
|
switch (event.c[0]) {
|
||||||
|
case 0x1B:
|
||||||
|
case 's':
|
||||||
|
case 'v':
|
||||||
|
editor->selection_active = false;
|
||||||
|
mode = NORMAL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
ensure_scroll(editor);
|
||||||
|
if (event.key_type == KEY_CHAR && event.c)
|
||||||
|
free(event.c);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
@@ -89,7 +149,7 @@ int main(int argc, char *argv[]) {
|
|||||||
render_editor(editor);
|
render_editor(editor);
|
||||||
render();
|
render();
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(16));
|
std::this_thread::sleep_for(std::chrono::milliseconds(8));
|
||||||
}
|
}
|
||||||
|
|
||||||
input_thread.detach();
|
input_thread.detach();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "../include/ts.h"
|
#include "../include/ts.h"
|
||||||
#include "../include/editor.h"
|
#include "../include/editor.h"
|
||||||
#include "../include/knot.h"
|
#include "../include/knot.h"
|
||||||
|
#include "../include/main.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@@ -126,6 +127,8 @@ static inline bool ts_predicate(TSQuery *query, const TSQueryMatch &match,
|
|||||||
ts_query_predicates_for_pattern(query, match.pattern_index, &step_count);
|
ts_query_predicates_for_pattern(query, match.pattern_index, &step_count);
|
||||||
if (!steps || step_count != 4)
|
if (!steps || step_count != 4)
|
||||||
return true;
|
return true;
|
||||||
|
if (source->char_count >= (1024 * 64))
|
||||||
|
return false;
|
||||||
std::string command;
|
std::string command;
|
||||||
std::string regex_txt;
|
std::string regex_txt;
|
||||||
uint32_t subject_id = 0;
|
uint32_t subject_id = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user