Compare commits
2 Commits
5153770ee9
...
aa25bf0ac3
| Author | SHA1 | Date | |
|---|---|---|---|
|
aa25bf0ac3
|
|||
|
628000f851
|
8
Makefile
8
Makefile
@@ -11,12 +11,12 @@ CXX_RELEASE := $(CCACHE) clang++
|
||||
|
||||
CFLAGS_DEBUG := -std=c++20 -Wall -Wextra -O0 -fno-inline -gsplit-dwarf -g -fsanitize=address -fno-omit-frame-pointer
|
||||
CFLAGS_RELEASE := -std=c++20 -O3 -march=native -flto=thin \
|
||||
-fno-exceptions -fno-rtti -fstrict-aliasing -ffast-math -funroll-loops \
|
||||
-fno-exceptions -fno-rtti -fstrict-aliasing \
|
||||
-ffast-math -funroll-loops \
|
||||
-fvisibility=hidden \
|
||||
-fomit-frame-pointer -DNDEBUG -s \
|
||||
-mllvm -inline-threshold=10000 \
|
||||
-mllvm -vectorize-loops \
|
||||
-mllvm -force-vector-width=8 \
|
||||
-mllvm -unroll-threshold=500000
|
||||
-fno-unwind-tables -fno-asynchronous-unwind-tables
|
||||
|
||||
UNICODE_SRC := $(wildcard libs/unicode_width/*.c)
|
||||
|
||||
|
||||
13
README.md
13
README.md
@@ -6,19 +6,22 @@ A TUI IDE.
|
||||
|
||||
# TODO
|
||||
|
||||
- [ ] Add support for ctrl + arrow key for moving words.
|
||||
- [ ] Add support for ctrl + backspace / delete for deleting words.
|
||||
- [ ] Add underline highlight for current word and all occurences.
|
||||
- [ ] Add alt+arrows to move line/block up/down.
|
||||
- [ ] Add `hooks` in files that can be set/unset/jumped to.
|
||||
- [ ] 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 support for brackets/quotes to auto-close.
|
||||
- [ ] 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 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 support for undo/redo.
|
||||
- [ ] Add `.scm` files for all the supported languages. (2/14) Done.
|
||||
- [ ] Add splash screen / minigame jumping.
|
||||
- [ ] Add support for LSP & autocomplete / snippets.
|
||||
- [ ] Add codeium/copilot support.
|
||||
- [ ] Normalize / validate unicode on file open.
|
||||
- [ ] Add git stuff.
|
||||
- [ ] Add splash screen / minigame jumping.
|
||||
|
||||
@@ -11,6 +11,10 @@
|
||||
#include <shared_mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#define CHAR 0
|
||||
#define WORD 1
|
||||
#define LINE 2
|
||||
|
||||
struct Highlight {
|
||||
uint32_t fg;
|
||||
uint32_t bg;
|
||||
@@ -89,6 +93,7 @@ struct Editor {
|
||||
uint32_t cursor_preffered;
|
||||
Coord selection;
|
||||
bool selection_active;
|
||||
int selection_type;
|
||||
Coord position;
|
||||
Coord size;
|
||||
Coord scroll;
|
||||
@@ -100,6 +105,7 @@ struct Editor {
|
||||
std::vector<Highlight> query_map;
|
||||
std::vector<int8_t> folded;
|
||||
Spans spans;
|
||||
Spans def_spans;
|
||||
std::map<uint32_t, bool> folded_node;
|
||||
};
|
||||
|
||||
@@ -123,5 +129,11 @@ void edit_erase(Editor *editor, Coord pos, int64_t len);
|
||||
void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len);
|
||||
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y);
|
||||
char *get_selection(Editor *editor, uint32_t *out_len);
|
||||
void editor_worker(Editor *editor);
|
||||
void word_boundaries(Editor *editor, Coord coord, uint32_t *prev_col,
|
||||
uint32_t *next_col, uint32_t *prev_clusters,
|
||||
uint32_t *next_clusters);
|
||||
void word_boundaries_exclusive(Editor *editor, Coord coord, uint32_t *prev_col,
|
||||
uint32_t *next_col);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
#define UTILS_H
|
||||
|
||||
#include "./ts_def.h"
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#define PCRE_WORKSPACE_SIZE 512
|
||||
@@ -61,5 +64,25 @@ char *detect_file_type(const char *filename);
|
||||
Language language_for_file(const char *filename);
|
||||
void copy_to_clipboard(const char *text, size_t len);
|
||||
char *get_from_clipboard(uint32_t *out_len);
|
||||
uint32_t count_clusters(const char *line, size_t len, size_t from, size_t to);
|
||||
|
||||
template <typename Func, typename... Args>
|
||||
auto throttle(std::chrono::milliseconds min_duration, Func &&func,
|
||||
Args &&...args) {
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
if constexpr (std::is_void_v<std::invoke_result_t<Func, Args...>>) {
|
||||
std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
|
||||
} else {
|
||||
auto result =
|
||||
std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
|
||||
auto elapsed = std::chrono::steady_clock::now() - start;
|
||||
if (elapsed < min_duration)
|
||||
std::this_thread::sleep_for(min_duration - elapsed);
|
||||
return result;
|
||||
}
|
||||
auto elapsed = std::chrono::steady_clock::now() - start;
|
||||
if (elapsed < min_duration)
|
||||
std::this_thread::sleep_for(min_duration - elapsed);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -74,14 +74,51 @@ void render_editor(Editor *editor) {
|
||||
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
uint32_t render_x = editor->position.col + numlen;
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
if (editor->selection_active) {
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
uint32_t prev_col, next_col;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, editor->selection, &prev_col, &next_col,
|
||||
nullptr, nullptr);
|
||||
start = {editor->selection.row, prev_col};
|
||||
end = editor->cursor;
|
||||
break;
|
||||
case LINE:
|
||||
start = {editor->selection.row, 0};
|
||||
end = editor->cursor;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
start = editor->cursor;
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
uint32_t prev_col, next_col, line_len;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, editor->selection, &prev_col, &next_col,
|
||||
nullptr, nullptr);
|
||||
end = {editor->selection.row, next_col};
|
||||
break;
|
||||
case LINE:
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->selection.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
free(line);
|
||||
end = {editor->selection.row, line_len};
|
||||
break;
|
||||
}
|
||||
}
|
||||
sel_start = line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
sel_end = line_to_byte(editor->root, end.row, nullptr) + end.col;
|
||||
@@ -89,13 +126,14 @@ void render_editor(Editor *editor) {
|
||||
Coord cursor = {UINT32_MAX, UINT32_MAX};
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
SpanCursor span_cursor(editor->spans);
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
SpanCursor def_span_cursor(editor->def_spans);
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t rendered_rows = 0;
|
||||
uint32_t global_byte_offset = line_to_byte(editor->root, line_index, nullptr);
|
||||
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) {
|
||||
@@ -153,9 +191,17 @@ void render_editor(Editor *editor) {
|
||||
uint32_t absolute_byte_pos =
|
||||
global_byte_offset + current_byte_offset + local_render_offset;
|
||||
Highlight *hl = span_cursor.get_highlight(absolute_byte_pos);
|
||||
Highlight *def_hl = def_span_cursor.get_highlight(absolute_byte_pos);
|
||||
uint32_t fg = hl ? hl->fg : 0xFFFFFF;
|
||||
uint32_t bg = hl ? hl->bg : 0;
|
||||
uint8_t fl = hl ? hl->flags : 0;
|
||||
if (def_hl) {
|
||||
if (def_hl->fg != 0)
|
||||
fg = def_hl->fg;
|
||||
if (def_hl->bg != 0)
|
||||
bg = def_hl->bg;
|
||||
fl |= def_hl->flags;
|
||||
}
|
||||
if (editor->selection_active && absolute_byte_pos >= sel_start &&
|
||||
absolute_byte_pos < sel_end)
|
||||
bg = 0x555555;
|
||||
@@ -243,10 +289,10 @@ void render_editor(Editor *editor) {
|
||||
}
|
||||
set_cursor(cursor.row, cursor.col, type, true);
|
||||
}
|
||||
while (rendered_rows < render_width) {
|
||||
for (uint32_t col = 0; col < render_width; col++)
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ",
|
||||
0xFFFFFF, 0, 0);
|
||||
while (rendered_rows < editor->size.row) {
|
||||
for (uint32_t col = 0; col < editor->size.col; col++)
|
||||
update(editor->position.row + rendered_rows, editor->position.col + col,
|
||||
" ", 0xFFFFFF, 0, 0);
|
||||
rendered_rows++;
|
||||
}
|
||||
free(it);
|
||||
|
||||
@@ -4,29 +4,17 @@ extern "C" {
|
||||
}
|
||||
#include "../include/editor.h"
|
||||
#include "../include/main.h"
|
||||
#include "../include/ts.h"
|
||||
#include "../include/utils.h"
|
||||
#include <cmath>
|
||||
|
||||
static Highlight HL_UNDERLINE = {0, 0, 1 << 2, 100};
|
||||
|
||||
void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
static std::chrono::steady_clock::time_point last_click_time =
|
||||
std::chrono::steady_clock::now();
|
||||
static uint32_t click_count = 0;
|
||||
static Coord last_click_pos = {UINT32_MAX, UINT32_MAX};
|
||||
if (event.key_type == KEY_SPECIAL) {
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
cursor_right(editor, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (event.key_type == KEY_MOUSE) {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
@@ -53,31 +41,94 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
break;
|
||||
case PRESS:
|
||||
if (event.mouse_button == LEFT_BTN) {
|
||||
if (duration < 250 &&
|
||||
last_click_pos == (Coord){event.mouse_x, event.mouse_y}) {
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
} else {
|
||||
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||
Coord cur_pos = {event.mouse_x, event.mouse_y};
|
||||
if (duration < 250 && last_click_pos == cur_pos)
|
||||
click_count++;
|
||||
else
|
||||
click_count = 1;
|
||||
last_click_time = now;
|
||||
last_click_pos = cur_pos;
|
||||
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (click_count == 1) {
|
||||
editor->cursor = p;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->selection = p;
|
||||
if (mode == SELECT) {
|
||||
mode = NORMAL;
|
||||
editor->selection_active = false;
|
||||
}
|
||||
last_click_pos = (Coord){event.mouse_x, event.mouse_y};
|
||||
last_click_time = now;
|
||||
} else if (click_count == 2) {
|
||||
uint32_t prev_col, next_col;
|
||||
word_boundaries(editor, editor->cursor, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
if (editor->cursor < editor->selection)
|
||||
editor->cursor = {editor->cursor.row, prev_col};
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, next_col};
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->selection_type = WORD;
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
} else if (click_count >= 3) {
|
||||
if (editor->cursor < editor->selection) {
|
||||
editor->cursor = {p.row, 0};
|
||||
} else {
|
||||
uint32_t line_len;
|
||||
LineIterator *it = begin_l_iter(editor->root, p.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
free(line);
|
||||
editor->cursor = {p.row, line_len};
|
||||
}
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->selection_type = LINE;
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
click_count = 3;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DRAG:
|
||||
if (event.mouse_button == LEFT_BTN) {
|
||||
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||
editor->cursor = p;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
if (!editor->selection_active) {
|
||||
editor->selection_active = true;
|
||||
editor->selection_type = CHAR;
|
||||
}
|
||||
uint32_t prev_col, next_col, line_len;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
editor->cursor = p;
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, p, &prev_col, &next_col, nullptr, nullptr);
|
||||
if (editor->cursor < editor->selection)
|
||||
editor->cursor = {p.row, prev_col};
|
||||
else
|
||||
editor->cursor = {p.row, next_col};
|
||||
break;
|
||||
case LINE:
|
||||
if (editor->cursor < editor->selection) {
|
||||
editor->cursor = {p.row, 0};
|
||||
} else {
|
||||
LineIterator *it = begin_l_iter(editor->root, p.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
free(line);
|
||||
editor->cursor = {p.row, line_len};
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case RELEASE:
|
||||
@@ -90,6 +141,57 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (event.key_type == KEY_SPECIAL) {
|
||||
switch (event.special_modifier) {
|
||||
case 0:
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
cursor_right(editor, 1);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CNTRL:
|
||||
uint32_t prev_col, next_col;
|
||||
word_boundaries(editor, editor->cursor, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (prev_col == editor->cursor.col)
|
||||
cursor_left(editor, 1);
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, prev_col};
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (next_col == editor->cursor.col)
|
||||
cursor_right(editor, 1);
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, next_col};
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ALT:
|
||||
// TODO: For up/down in insert/normal move line and in select move lines
|
||||
// overlapping selection up/down. right/left are normal
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch (mode) {
|
||||
case NORMAL:
|
||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||
@@ -133,6 +235,16 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
scroll_up(editor, 1);
|
||||
ensure_cursor(editor);
|
||||
break;
|
||||
case 'p':
|
||||
uint32_t len;
|
||||
char *text = get_from_clipboard(&len);
|
||||
if (text) {
|
||||
edit_insert(editor, editor->cursor, text, len);
|
||||
uint32_t grapheme_len = count_clusters(text, len, 0, len);
|
||||
cursor_right(editor, grapheme_len);
|
||||
free(text);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -147,6 +259,14 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
cursor_right(editor, 1);
|
||||
} else if (event.c[0] == 0x7F) {
|
||||
edit_erase(editor, editor->cursor, -1);
|
||||
} else if (event.c[0] == CTRL('W')) {
|
||||
uint32_t prev_col_byte, prev_col_cluster;
|
||||
word_boundaries(editor, editor->cursor, &prev_col_byte, nullptr,
|
||||
&prev_col_cluster, nullptr);
|
||||
if (prev_col_byte == editor->cursor.col)
|
||||
edit_erase(editor, editor->cursor, -1);
|
||||
else
|
||||
edit_erase(editor, editor->cursor, -(int64_t)prev_col_cluster);
|
||||
} else if (isprint((unsigned char)(event.c[0]))) {
|
||||
edit_insert(editor, editor->cursor, event.c, 1);
|
||||
cursor_right(editor, 1);
|
||||
@@ -160,7 +280,20 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
}
|
||||
} else if (event.key_type == KEY_SPECIAL &&
|
||||
event.special_key == KEY_DELETE) {
|
||||
edit_erase(editor, editor->cursor, 1);
|
||||
switch (event.special_modifier) {
|
||||
case 0:
|
||||
edit_erase(editor, editor->cursor, 1);
|
||||
break;
|
||||
case CNTRL:
|
||||
uint32_t next_col_byte, next_col_cluster;
|
||||
word_boundaries(editor, editor->cursor, nullptr, &next_col_byte,
|
||||
nullptr, &next_col_cluster);
|
||||
if (next_col_byte == editor->cursor.col)
|
||||
edit_erase(editor, editor->cursor, 1);
|
||||
else
|
||||
edit_erase(editor, editor->cursor, next_col_cluster);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SELECT:
|
||||
@@ -229,6 +362,156 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
free(event.c);
|
||||
}
|
||||
|
||||
void editor_worker(Editor *editor) {
|
||||
if (!editor || !editor->root)
|
||||
return;
|
||||
if (editor->parser && editor->query)
|
||||
ts_collect_spans(editor);
|
||||
uint32_t prev_col, next_col;
|
||||
word_boundaries_exclusive(editor, editor->cursor, &prev_col, &next_col);
|
||||
if (next_col - prev_col > 0 && next_col - prev_col < 256 - 4) {
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
uint32_t offset = line_to_byte(editor->root, editor->cursor.row, nullptr);
|
||||
char *word = read(editor->root, offset + prev_col, next_col - prev_col);
|
||||
if (word) {
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf), "\\b%s\\b", word);
|
||||
std::vector<std::pair<size_t, size_t>> results =
|
||||
search_rope(editor->root, buf);
|
||||
std::unique_lock lock(editor->def_spans.mtx);
|
||||
editor->def_spans.spans.clear();
|
||||
for (const auto &match : results) {
|
||||
Span s;
|
||||
s.start = match.first;
|
||||
s.end = match.first + match.second;
|
||||
s.hl = &HL_UNDERLINE;
|
||||
editor->def_spans.spans.push_back(s);
|
||||
}
|
||||
std::sort(editor->def_spans.spans.begin(), editor->def_spans.spans.end());
|
||||
lock.unlock();
|
||||
free(word);
|
||||
}
|
||||
} else {
|
||||
std::unique_lock lock(editor->def_spans.mtx);
|
||||
editor->def_spans.spans.clear();
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t scan_left(const char *line, uint32_t len, uint32_t off) {
|
||||
if (off > len)
|
||||
off = len;
|
||||
uint32_t i = off;
|
||||
while (i > 0) {
|
||||
unsigned char c = (unsigned char)line[i - 1];
|
||||
if ((c & 0x80) != 0)
|
||||
break;
|
||||
if (!((c == '_') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
|
||||
(c >= 'a' && c <= 'z')))
|
||||
break;
|
||||
--i;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
uint32_t scan_right(const char *line, uint32_t len, uint32_t off) {
|
||||
if (off > len)
|
||||
off = len;
|
||||
uint32_t i = off;
|
||||
while (i < len) {
|
||||
unsigned char c = (unsigned char)line[i];
|
||||
if ((c & 0x80) != 0)
|
||||
break;
|
||||
if (!((c == '_') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
|
||||
(c >= 'a' && c <= 'z')))
|
||||
break;
|
||||
++i;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
void word_boundaries_exclusive(Editor *editor, Coord coord, uint32_t *prev_col,
|
||||
uint32_t *next_col) {
|
||||
if (!editor)
|
||||
return;
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
LineIterator *it = begin_l_iter(editor->root, coord.row);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t col = coord.col;
|
||||
if (col > line_len)
|
||||
col = line_len;
|
||||
uint32_t left = scan_left(line, line_len, col);
|
||||
uint32_t right = scan_right(line, line_len, col);
|
||||
if (prev_col)
|
||||
*prev_col = left;
|
||||
if (next_col)
|
||||
*next_col = right;
|
||||
free(line);
|
||||
}
|
||||
|
||||
uint32_t word_jump_right(const char *line, size_t len, uint32_t pos) {
|
||||
if (pos >= len)
|
||||
return len;
|
||||
size_t next = grapheme_next_word_break_utf8(line + pos, len - pos);
|
||||
return static_cast<uint32_t>(pos + next);
|
||||
}
|
||||
|
||||
uint32_t word_jump_left(const char *line, size_t len, uint32_t col) {
|
||||
if (col == 0)
|
||||
return 0;
|
||||
size_t pos = 0;
|
||||
size_t last = 0;
|
||||
size_t cursor = col;
|
||||
while (pos < len) {
|
||||
size_t next = pos + grapheme_next_word_break_utf8(line + pos, len - pos);
|
||||
if (next >= cursor)
|
||||
break;
|
||||
last = next;
|
||||
pos = next;
|
||||
}
|
||||
return static_cast<uint32_t>(last);
|
||||
}
|
||||
|
||||
void word_boundaries(Editor *editor, Coord coord, uint32_t *prev_col,
|
||||
uint32_t *next_col, uint32_t *prev_clusters,
|
||||
uint32_t *next_clusters) {
|
||||
if (!editor)
|
||||
return;
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
LineIterator *it = begin_l_iter(editor->root, coord.row);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
size_t col = coord.col;
|
||||
if (col > line_len)
|
||||
col = line_len;
|
||||
size_t left = word_jump_left(line, line_len, col);
|
||||
size_t right = word_jump_right(line, line_len, col);
|
||||
if (prev_col)
|
||||
*prev_col = static_cast<uint32_t>(left);
|
||||
if (next_col)
|
||||
*next_col = static_cast<uint32_t>(right);
|
||||
if (prev_clusters)
|
||||
*prev_clusters = count_clusters(line, line_len, left, col);
|
||||
if (next_clusters)
|
||||
*next_clusters = count_clusters(line, line_len, col, right);
|
||||
free(line);
|
||||
}
|
||||
|
||||
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
|
||||
if (mode == INSERT)
|
||||
x++;
|
||||
@@ -535,6 +818,8 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||
apply_edit(editor->spans.spans, start, start - byte_pos);
|
||||
if (editor->spans.mid_parse)
|
||||
editor->spans.edits.push({start, start - byte_pos});
|
||||
std::unique_lock lock_4(editor->def_spans.mtx);
|
||||
apply_edit(editor->def_spans.spans, byte_pos, start - byte_pos);
|
||||
} else {
|
||||
std::shared_lock lock_1(editor->knot_mtx);
|
||||
uint32_t cursor_original =
|
||||
@@ -573,6 +858,8 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||
apply_edit(editor->spans.spans, byte_pos, byte_pos - end);
|
||||
if (editor->spans.mid_parse)
|
||||
editor->spans.edits.push({byte_pos, byte_pos - end});
|
||||
std::unique_lock lock_4(editor->def_spans.mtx);
|
||||
apply_edit(editor->def_spans.spans, byte_pos, byte_pos - end);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,17 +908,54 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
|
||||
apply_edit(editor->spans.spans, byte_pos, len);
|
||||
if (editor->spans.mid_parse)
|
||||
editor->spans.edits.push({byte_pos, len});
|
||||
std::unique_lock lock_4(editor->def_spans.mtx);
|
||||
apply_edit(editor->def_spans.spans, byte_pos, len);
|
||||
}
|
||||
|
||||
char *get_selection(Editor *editor, uint32_t *out_len) {
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
uint32_t prev_col, next_col;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, editor->selection, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
start = {editor->selection.row, prev_col};
|
||||
end = editor->cursor;
|
||||
break;
|
||||
case LINE:
|
||||
start = {editor->selection.row, 0};
|
||||
end = editor->cursor;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
start = editor->cursor;
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
uint32_t prev_col, next_col, line_len;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, editor->selection, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
end = {editor->selection.row, next_col};
|
||||
break;
|
||||
case LINE:
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->selection.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
free(it);
|
||||
if (!line)
|
||||
return nullptr;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
end = {editor->selection.row, line_len};
|
||||
break;
|
||||
}
|
||||
}
|
||||
uint32_t start_byte =
|
||||
line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
@@ -648,11 +972,11 @@ void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y) {
|
||||
spans.begin(), spans.end(), key,
|
||||
[](const Span &a, const Span &b) { return a.start < b.start; });
|
||||
size_t idx = std::distance(spans.begin(), it);
|
||||
while (idx > 0 && spans.at(idx - 1).end > x)
|
||||
while (idx > 0 && spans.at(idx - 1).end >= x)
|
||||
--idx;
|
||||
for (size_t i = idx; i < spans.size();) {
|
||||
Span &s = spans.at(i);
|
||||
if (s.start < x && s.end > x) {
|
||||
if (s.start < x && s.end >= x) {
|
||||
s.end += y;
|
||||
} else if (s.start > x) {
|
||||
s.start += y;
|
||||
|
||||
@@ -171,12 +171,8 @@ void scroll_down(Editor *editor, uint32_t number) {
|
||||
line_index++;
|
||||
}
|
||||
if (q_size > 0) {
|
||||
uint32_t idx;
|
||||
if (q_size < max_visual_lines)
|
||||
idx = (q_head + q_size - 1) % max_visual_lines;
|
||||
else
|
||||
idx = q_head;
|
||||
editor->scroll = scroll_queue[idx];
|
||||
uint32_t advance = (q_size > number) ? number : (q_size - 1);
|
||||
editor->scroll = scroll_queue[(q_head + advance) % max_visual_lines];
|
||||
}
|
||||
free(it);
|
||||
free(scroll_queue);
|
||||
@@ -249,7 +245,7 @@ void ensure_cursor(Editor *editor) {
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
if (editor->cursor.col >= offset &&
|
||||
editor->cursor.col < offset + advance) {
|
||||
editor->cursor.col <= offset + advance) {
|
||||
free(line);
|
||||
free(it);
|
||||
return;
|
||||
|
||||
@@ -156,8 +156,8 @@ KeyEvent read_key() {
|
||||
pos = 2;
|
||||
ret.special_modifier = 0;
|
||||
} else {
|
||||
pos = 4;
|
||||
switch (buf[3]) {
|
||||
pos = 5;
|
||||
switch (buf[4]) {
|
||||
case '2':
|
||||
ret.special_modifier = SHIFT;
|
||||
break;
|
||||
|
||||
29
src/main.cc
29
src/main.cc
@@ -3,11 +3,12 @@
|
||||
#include "../include/ts.h"
|
||||
#include "../include/ui.h"
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <sys/ioctl.h>
|
||||
#include <thread>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
std::atomic<bool> running{true};
|
||||
Queue<KeyEvent> event_queue;
|
||||
std::vector<Editor *> editors;
|
||||
@@ -16,24 +17,22 @@ uint8_t current_editor = 0;
|
||||
uint8_t mode = NORMAL;
|
||||
|
||||
void background_worker() {
|
||||
while (running) {
|
||||
ts_collect_spans(editors[current_editor]);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(16));
|
||||
}
|
||||
while (running)
|
||||
throttle(16ms, editor_worker, editors[current_editor]);
|
||||
}
|
||||
|
||||
void input_listener() {
|
||||
while (running) {
|
||||
KeyEvent event = read_key();
|
||||
KeyEvent event = throttle(1ms, read_key);
|
||||
if (event.key_type == KEY_NONE)
|
||||
continue;
|
||||
if (event.key_type == KEY_CHAR && event.len == 1 && *event.c == CTRL('q')) {
|
||||
if (event.key_type == KEY_CHAR && event.len == 1 &&
|
||||
event.c[0] == CTRL('q')) {
|
||||
free(event.c);
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
event_queue.push(event);
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,10 +48,9 @@ Editor *editor_at(uint8_t x, uint8_t y) {
|
||||
}
|
||||
|
||||
uint8_t index_of(Editor *ed) {
|
||||
for (uint8_t i = 0; i < editors.size(); i++) {
|
||||
for (uint8_t i = 0; i < editors.size(); i++)
|
||||
if (editors[i] == ed)
|
||||
return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -85,16 +83,13 @@ int main(int argc, char *argv[]) {
|
||||
current_editor = index_of(target);
|
||||
event.mouse_x -= target->position.col;
|
||||
event.mouse_y -= target->position.row;
|
||||
handle_editor_event(target, event);
|
||||
throttle(4ms, handle_editor_event, target, event);
|
||||
} else {
|
||||
handle_editor_event(editors[current_editor], event);
|
||||
throttle(4ms, handle_editor_event, editors[current_editor], event);
|
||||
}
|
||||
}
|
||||
|
||||
render_editor(editors[current_editor]);
|
||||
render();
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(8));
|
||||
throttle(4ms, render_editor, editors[current_editor]);
|
||||
throttle(4ms, render);
|
||||
}
|
||||
|
||||
if (input_thread.joinable())
|
||||
|
||||
@@ -21,19 +21,15 @@ void enable_raw_mode() {
|
||||
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1)
|
||||
exit(EXIT_FAILURE);
|
||||
atexit(disable_raw_mode);
|
||||
|
||||
struct termios raw = orig_termios;
|
||||
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
||||
raw.c_oflag &= ~(OPOST);
|
||||
raw.c_cflag |= (CS8);
|
||||
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
||||
raw.c_lflag |= ISIG;
|
||||
raw.c_cc[VMIN] = 0;
|
||||
raw.c_cc[VTIME] = 0;
|
||||
|
||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
std::string os = "\x1b[?1049h\x1b[2 q\x1b[?1002h\x1b[?25l";
|
||||
write(STDOUT_FILENO, os.c_str(), os.size());
|
||||
}
|
||||
@@ -44,8 +40,8 @@ Coord start_screen() {
|
||||
ioctl(0, TIOCGWINSZ, &w);
|
||||
rows = w.ws_row;
|
||||
cols = w.ws_col;
|
||||
screen.assign(rows * cols, {}); // allocate & zero-init
|
||||
old_screen.assign(rows * cols, {}); // allocate & zero-init
|
||||
screen.assign(rows * cols, {});
|
||||
old_screen.assign(rows * cols, {});
|
||||
return {rows, cols};
|
||||
}
|
||||
|
||||
|
||||
14
src/utils.cc
14
src/utils.cc
@@ -148,6 +148,20 @@ uint32_t get_bytes_from_visual_col(const char *line, uint32_t len,
|
||||
return current_byte;
|
||||
}
|
||||
|
||||
uint32_t count_clusters(const char *line, size_t len, size_t from, size_t to) {
|
||||
uint32_t count = 0;
|
||||
size_t pos = from;
|
||||
while (pos < to && pos < len) {
|
||||
size_t next =
|
||||
pos + grapheme_next_character_break_utf8(line + pos, len - pos);
|
||||
if (next > to)
|
||||
break;
|
||||
pos = next;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void log(const char *fmt, ...) {
|
||||
FILE *fp = fopen("/tmp/log.txt", "a");
|
||||
if (!fp)
|
||||
|
||||
Reference in New Issue
Block a user