Rearrange code and cleanup
This commit is contained in:
238
src/editor/adjustment.cc
Normal file
238
src/editor/adjustment.cc
Normal file
@@ -0,0 +1,238 @@
|
||||
#include "editor/editor.h"
|
||||
#include "editor/folds.h"
|
||||
|
||||
void ensure_cursor(Editor *editor) {
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
if (editor->cursor < editor->scroll) {
|
||||
uint32_t line_idx = next_unfolded_row(editor, editor->scroll.row);
|
||||
editor->cursor.row = line_idx;
|
||||
editor->cursor.col =
|
||||
line_idx == editor->scroll.row ? editor->scroll.col : 0;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
return;
|
||||
}
|
||||
uint32_t numlen =
|
||||
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
uint32_t visual_rows = 0;
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
bool first_visual_line = true;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
Coord last_visible = editor->scroll;
|
||||
while (true) {
|
||||
if (visual_rows >= editor->size.row)
|
||||
break;
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
Coord c = {fold->start, 0};
|
||||
last_visible = c;
|
||||
visual_rows++;
|
||||
uint32_t skip_until = fold->end;
|
||||
while (line_index <= skip_until) {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line)
|
||||
break;
|
||||
line_index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t offset = first_visual_line ? editor->scroll.col : 0;
|
||||
first_visual_line = false;
|
||||
while (offset < line_len || (line_len == 0 && offset == 0)) {
|
||||
Coord current = {line_index, offset};
|
||||
last_visible = current;
|
||||
visual_rows++;
|
||||
if (visual_rows >= editor->size.row)
|
||||
break;
|
||||
uint32_t col = 0;
|
||||
uint32_t advance = 0;
|
||||
uint32_t left = line_len - offset;
|
||||
while (left > 0 && col < render_width) {
|
||||
uint32_t g =
|
||||
grapheme_next_character_break_utf8(line + offset + advance, left);
|
||||
int w = display_width(line + offset + advance, g);
|
||||
if (col + w > render_width)
|
||||
break;
|
||||
advance += g;
|
||||
left -= g;
|
||||
col += w;
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
if (editor->cursor.col >= offset &&
|
||||
editor->cursor.col <= offset + advance) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (advance == 0)
|
||||
break;
|
||||
offset += advance;
|
||||
if (line_len == 0)
|
||||
break;
|
||||
}
|
||||
line_index++;
|
||||
}
|
||||
uint32_t last_real_row = last_visible.row;
|
||||
const Fold *last_fold = fold_for_line(editor->folds, last_visible.row);
|
||||
if (last_fold) {
|
||||
last_visible.row = last_fold->start == 0 ? 0 : last_fold->start - 1;
|
||||
last_visible.col = 0;
|
||||
}
|
||||
editor->cursor.row = last_visible.row;
|
||||
editor->cursor.col = last_visible.row == last_real_row ? last_visible.col : 0;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
|
||||
void ensure_scroll(Editor *editor) {
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
uint32_t numlen =
|
||||
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
if (editor->cursor < editor->scroll) {
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
uint32_t rows = 1;
|
||||
uint32_t cols = 0;
|
||||
uint32_t offset = 0;
|
||||
uint32_t old_offset = 0;
|
||||
while (offset < len) {
|
||||
uint32_t inc =
|
||||
grapheme_next_character_break_utf8(line + offset, len - offset);
|
||||
int width = display_width(line + offset, inc);
|
||||
if (cols + width > render_width) {
|
||||
rows++;
|
||||
cols = 0;
|
||||
if (editor->cursor.col > old_offset && editor->cursor.col <= offset) {
|
||||
editor->scroll.row = editor->cursor.row;
|
||||
editor->scroll.col = old_offset;
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
old_offset = offset;
|
||||
}
|
||||
cols += width;
|
||||
offset += inc;
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
editor->scroll.row = editor->cursor.row;
|
||||
editor->scroll.col = (editor->cursor.col == 0) ? 0 : old_offset;
|
||||
} else {
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t max_visual_lines = editor->size.row;
|
||||
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * max_visual_lines);
|
||||
uint32_t q_head = 0;
|
||||
uint32_t q_size = 0;
|
||||
bool first_visual_line = true;
|
||||
while (true) {
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
Coord fold_coord = {fold->start, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
if (fold->start <= editor->cursor.row &&
|
||||
editor->cursor.row <= fold->end) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
uint32_t skip_until = fold->end;
|
||||
while (line_index <= skip_until) {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line)
|
||||
break;
|
||||
line_index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t current_byte_offset = 0;
|
||||
if (first_visual_line) {
|
||||
current_byte_offset += editor->scroll.col;
|
||||
first_visual_line = false;
|
||||
}
|
||||
while (current_byte_offset < line_len ||
|
||||
(line_len == 0 && current_byte_offset == 0)) {
|
||||
Coord current_coord = {line_index, current_byte_offset};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = current_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = current_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
uint32_t col = 0;
|
||||
uint32_t local_render_offset = 0;
|
||||
uint32_t line_left = line_len - current_byte_offset;
|
||||
while (line_left > 0 && col < render_width) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
line + current_byte_offset + local_render_offset, line_left);
|
||||
int width = display_width(
|
||||
line + current_byte_offset + local_render_offset, cluster_len);
|
||||
if (col + width > render_width)
|
||||
break;
|
||||
local_render_offset += cluster_len;
|
||||
line_left -= cluster_len;
|
||||
col += width;
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
bool cursor_found = false;
|
||||
if (editor->cursor.col >= current_byte_offset &&
|
||||
editor->cursor.col < current_byte_offset + local_render_offset)
|
||||
cursor_found = true;
|
||||
else if (editor->cursor.col == line_len &&
|
||||
current_byte_offset + local_render_offset == line_len)
|
||||
cursor_found = true;
|
||||
if (cursor_found) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
free(scroll_queue);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
current_byte_offset += local_render_offset;
|
||||
if (line_len == 0)
|
||||
break;
|
||||
}
|
||||
line_index++;
|
||||
}
|
||||
free(scroll_queue);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
}
|
||||
115
src/editor/boundaries.cc
Normal file
115
src/editor/boundaries.cc
Normal file
@@ -0,0 +1,115 @@
|
||||
#include "editor/editor.h"
|
||||
|
||||
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);
|
||||
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(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
|
||||
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);
|
||||
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(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
97
src/editor/click.cc
Normal file
97
src/editor/click.cc
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "editor/editor.h"
|
||||
#include "editor/folds.h"
|
||||
#include "main.h"
|
||||
|
||||
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
|
||||
if (mode == INSERT)
|
||||
x++;
|
||||
uint32_t numlen =
|
||||
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
bool is_gutter_click = (x < numlen);
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
x = MAX(x, numlen) - numlen;
|
||||
uint32_t target_visual_row = y;
|
||||
uint32_t visual_row = 0;
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
uint32_t last_line_index = editor->scroll.row;
|
||||
uint32_t last_col = editor->scroll.col;
|
||||
bool first_visual_line = true;
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return editor->scroll;
|
||||
while (visual_row <= target_visual_row) {
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
if (visual_row == target_visual_row) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
if (is_gutter_click) {
|
||||
remove_fold(editor, fold->start);
|
||||
return {UINT32_MAX, UINT32_MAX};
|
||||
}
|
||||
return {fold->start > 0 ? fold->start - 1 : 0, 0};
|
||||
}
|
||||
visual_row++;
|
||||
while (line_index <= fold->end) {
|
||||
char *l = next_line(it, nullptr);
|
||||
if (!l)
|
||||
break;
|
||||
line_index++;
|
||||
}
|
||||
last_line_index = fold->end;
|
||||
last_col = 0;
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
last_line_index = line_index;
|
||||
last_col = line_len;
|
||||
uint32_t offset = first_visual_line ? editor->scroll.col : 0;
|
||||
first_visual_line = false;
|
||||
while (offset < line_len || (line_len == 0 && offset == 0)) {
|
||||
uint32_t col = 0;
|
||||
uint32_t advance = 0;
|
||||
uint32_t left = line_len - offset;
|
||||
uint32_t last_good_offset = offset;
|
||||
while (left > 0 && col < render_width) {
|
||||
uint32_t g =
|
||||
grapheme_next_character_break_utf8(line + offset + advance, left);
|
||||
int w = display_width(line + offset + advance, g);
|
||||
if (col + w > render_width)
|
||||
break;
|
||||
if (visual_row == target_visual_row && x < col + w) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return {line_index, offset + advance};
|
||||
}
|
||||
advance += g;
|
||||
last_good_offset = offset + advance;
|
||||
left -= g;
|
||||
col += w;
|
||||
}
|
||||
last_col = last_good_offset;
|
||||
if (visual_row == target_visual_row) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return {line_index, last_good_offset};
|
||||
}
|
||||
visual_row++;
|
||||
if (visual_row > target_visual_row)
|
||||
break;
|
||||
if (advance == 0)
|
||||
break;
|
||||
offset += advance;
|
||||
if (line_len == 0)
|
||||
break;
|
||||
}
|
||||
line_index++;
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return {last_line_index, last_col};
|
||||
}
|
||||
325
src/editor/cursor.cc
Normal file
325
src/editor/cursor.cc
Normal file
@@ -0,0 +1,325 @@
|
||||
#include "editor/editor.h"
|
||||
#include "editor/folds.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
|
||||
Coord result = cursor;
|
||||
if (!editor || !editor->root || number == 0)
|
||||
return result;
|
||||
uint32_t row = result.row;
|
||||
uint32_t col = result.col;
|
||||
uint32_t line_len = 0;
|
||||
LineIterator *it = begin_l_iter(editor->root, row);
|
||||
if (!it)
|
||||
return result;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return result;
|
||||
}
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
--line_len;
|
||||
while (number > 0) {
|
||||
if (col >= line_len) {
|
||||
uint32_t next_row = row + 1;
|
||||
if (next_row >= editor->root->line_count) {
|
||||
col = line_len;
|
||||
break;
|
||||
}
|
||||
row = next_row;
|
||||
col = 0;
|
||||
line = next_line(it, &line_len);
|
||||
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--;
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
result.row = row;
|
||||
result.col = col;
|
||||
return result;
|
||||
}
|
||||
|
||||
Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number) {
|
||||
Coord result = cursor;
|
||||
if (!editor || !editor->root || number == 0)
|
||||
return result;
|
||||
uint32_t row = result.row;
|
||||
uint32_t col = result.col;
|
||||
uint32_t len = 0;
|
||||
LineIterator *it = begin_l_iter(editor->root, row);
|
||||
char *line = next_line(it, &len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return result;
|
||||
}
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
bool iterator_ahead = true;
|
||||
while (number > 0) {
|
||||
if (col == 0) {
|
||||
if (row == 0)
|
||||
break;
|
||||
if (iterator_ahead) {
|
||||
prev_line(it, nullptr);
|
||||
iterator_ahead = false;
|
||||
}
|
||||
line = nullptr;
|
||||
row--;
|
||||
line = prev_line(it, &len);
|
||||
if (!line)
|
||||
break;
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
col = len;
|
||||
} else {
|
||||
uint32_t new_col = 0;
|
||||
while (new_col < col) {
|
||||
uint32_t inc =
|
||||
grapheme_next_character_break_utf8(line + new_col, len - new_col);
|
||||
if (new_col + inc >= col)
|
||||
break;
|
||||
new_col += inc;
|
||||
}
|
||||
col = new_col;
|
||||
}
|
||||
number--;
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
result.row = row;
|
||||
result.col = col;
|
||||
return result;
|
||||
}
|
||||
|
||||
Coord move_right(Editor *editor, Coord cursor, uint32_t number) {
|
||||
Coord result = cursor;
|
||||
if (!editor || !editor->root || number == 0)
|
||||
return result;
|
||||
uint32_t row = result.row;
|
||||
uint32_t col = result.col;
|
||||
uint32_t line_len = 0;
|
||||
LineIterator *it = begin_l_iter(editor->root, row);
|
||||
if (!it)
|
||||
return result;
|
||||
uint32_t target_row = next_unfolded_row(editor, row);
|
||||
while (row < target_row) {
|
||||
if (!next_line(it, &line_len)) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return result;
|
||||
}
|
||||
++row;
|
||||
}
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return result;
|
||||
}
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
--line_len;
|
||||
while (number > 0) {
|
||||
if (col >= line_len) {
|
||||
uint32_t next_row = next_unfolded_row(editor, row + 1);
|
||||
if (next_row >= editor->root->line_count) {
|
||||
col = line_len;
|
||||
break;
|
||||
}
|
||||
while (row < next_row) {
|
||||
line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
result.row = row;
|
||||
result.col = col;
|
||||
return result;
|
||||
}
|
||||
++row;
|
||||
}
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
--line_len;
|
||||
col = 0;
|
||||
--number;
|
||||
continue;
|
||||
} else {
|
||||
uint32_t inc =
|
||||
grapheme_next_character_break_utf8(line + col, line_len - col);
|
||||
if (inc == 0)
|
||||
break;
|
||||
col += inc;
|
||||
--number;
|
||||
}
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
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);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return result;
|
||||
}
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
bool iterator_ahead = true;
|
||||
while (number > 0) {
|
||||
if (col == 0) {
|
||||
if (row == 0)
|
||||
break;
|
||||
if (iterator_ahead) {
|
||||
prev_line(it, nullptr);
|
||||
iterator_ahead = false;
|
||||
}
|
||||
line = nullptr;
|
||||
while (row > 0) {
|
||||
row--;
|
||||
line = prev_line(it, &len);
|
||||
if (!line)
|
||||
break;
|
||||
const Fold *fold = fold_for_line(editor->folds, row);
|
||||
if (fold) {
|
||||
while (line && row > fold->start) {
|
||||
line = prev_line(it, &len);
|
||||
row--;
|
||||
}
|
||||
line = nullptr;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
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--;
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
result.row = row;
|
||||
result.col = col;
|
||||
return result;
|
||||
}
|
||||
|
||||
void cursor_down(Editor *editor, uint32_t number) {
|
||||
if (!editor || !editor->root || number == 0)
|
||||
return;
|
||||
uint32_t len;
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
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;
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
uint32_t target_row = editor->cursor.row;
|
||||
while (number > 0 && target_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;
|
||||
}
|
||||
number--;
|
||||
}
|
||||
it = begin_l_iter(editor->root, target_row);
|
||||
line_content = next_line(it, &len);
|
||||
if (!line_content)
|
||||
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);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
|
||||
void cursor_up(Editor *editor, uint32_t number) {
|
||||
if (!editor || !editor->root || number == 0 || editor->cursor.row == 0)
|
||||
return;
|
||||
uint32_t len;
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
char *line_content = next_line(it, &len);
|
||||
if (!line_content)
|
||||
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(it->buffer);
|
||||
free(it);
|
||||
uint32_t target_row = editor->cursor.row;
|
||||
while (number > 0 && target_row > 0) {
|
||||
target_row = prev_unfolded_row(editor, target_row - 1);
|
||||
if (target_row == 0) {
|
||||
number--;
|
||||
break;
|
||||
}
|
||||
number--;
|
||||
}
|
||||
it = begin_l_iter(editor->root, target_row);
|
||||
line_content = next_line(it, &len);
|
||||
if (line_content) {
|
||||
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);
|
||||
} else {
|
||||
editor->cursor.row = 0;
|
||||
editor->cursor.col = 0;
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
|
||||
void cursor_right(Editor *editor, uint32_t number) {
|
||||
if (!editor || !editor->root || number == 0)
|
||||
return;
|
||||
editor->cursor = move_right(editor, editor->cursor, number);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
}
|
||||
|
||||
void cursor_left(Editor *editor, uint32_t number) {
|
||||
if (!editor || !editor->root || number == 0)
|
||||
return;
|
||||
editor->cursor = move_left(editor, editor->cursor, number);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
}
|
||||
274
src/editor/edit.cc
Normal file
274
src/editor/edit.cc
Normal file
@@ -0,0 +1,274 @@
|
||||
#include "editor/editor.h"
|
||||
#include "editor/folds.h"
|
||||
#include "lsp/lsp.h"
|
||||
|
||||
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_pure(editor, pos, -len);
|
||||
json lsp_range;
|
||||
bool do_lsp = (editor->lsp != nullptr);
|
||||
if (do_lsp) {
|
||||
LineIterator *it = begin_l_iter(editor->root, point.row);
|
||||
char *line = next_line(it, nullptr);
|
||||
int utf16_start = 0;
|
||||
if (line)
|
||||
utf16_start = utf8_byte_offset_to_utf16(line, point.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
it = begin_l_iter(editor->root, pos.row);
|
||||
line = next_line(it, nullptr);
|
||||
int utf16_end = 0;
|
||||
if (line)
|
||||
utf16_end = utf8_byte_offset_to_utf16(line, pos.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
lsp_range = {{"start", {{"line", point.row}, {"character", utf16_start}}},
|
||||
{"end", {{"line", pos.row}, {"character", utf16_end}}}};
|
||||
}
|
||||
uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
||||
if (cursor_original > start && cursor_original <= byte_pos) {
|
||||
editor->cursor = point;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
} 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};
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
}
|
||||
lock_1.unlock();
|
||||
uint32_t start_row = point.row;
|
||||
uint32_t end_row = pos.row;
|
||||
apply_line_deletion(editor, start_row + 1, end_row);
|
||||
apply_hook_deletion(editor, start_row + 1, end_row);
|
||||
std::unique_lock lock_2(editor->knot_mtx);
|
||||
editor->root = erase(editor->root, start, byte_pos - start);
|
||||
lock_2.unlock();
|
||||
if (editor->ts.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);
|
||||
}
|
||||
if (do_lsp) {
|
||||
if (editor->lsp->incremental_sync) {
|
||||
json message = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/didChange"},
|
||||
{"params",
|
||||
{{"textDocument",
|
||||
{{"uri", editor->uri}, {"version", ++editor->lsp_version}}},
|
||||
{"contentChanges",
|
||||
json::array({{{"range", lsp_range}, {"text", ""}}})}}}};
|
||||
lsp_send(editor->lsp, message, nullptr);
|
||||
} else {
|
||||
char *buf = read(editor->root, 0, editor->root->char_count);
|
||||
std::string text(buf);
|
||||
free(buf);
|
||||
json message = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/didChange"},
|
||||
{"params",
|
||||
{{"textDocument",
|
||||
{{"uri", editor->uri}, {"version", ++editor->lsp_version}}},
|
||||
{"contentChanges", json::array({{{"text", text}}})}}}};
|
||||
lsp_send(editor->lsp, message, nullptr);
|
||||
}
|
||||
}
|
||||
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});
|
||||
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 =
|
||||
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_pure(editor, pos, len);
|
||||
json lsp_range;
|
||||
bool do_lsp = (editor->lsp != nullptr);
|
||||
if (do_lsp) {
|
||||
LineIterator *it = begin_l_iter(editor->root, pos.row);
|
||||
char *line = next_line(it, nullptr);
|
||||
int utf16_start = 0;
|
||||
if (line)
|
||||
utf16_start = utf8_byte_offset_to_utf16(line, pos.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
it = begin_l_iter(editor->root, point.row);
|
||||
line = next_line(it, nullptr);
|
||||
int utf16_end = 0;
|
||||
if (line)
|
||||
utf16_end = utf8_byte_offset_to_utf16(line, point.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
lsp_range = {{"start", {{"line", pos.row}, {"character", utf16_start}}},
|
||||
{"end", {{"line", point.row}, {"character", utf16_end}}}};
|
||||
}
|
||||
uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
||||
if (cursor_original > byte_pos && cursor_original <= end) {
|
||||
editor->cursor = pos;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
} 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};
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
}
|
||||
lock_1.unlock();
|
||||
uint32_t start_row = pos.row;
|
||||
uint32_t end_row = point.row;
|
||||
apply_line_deletion(editor, start_row + 1, end_row);
|
||||
apply_hook_deletion(editor, start_row + 1, end_row);
|
||||
std::unique_lock lock_2(editor->knot_mtx);
|
||||
editor->root = erase(editor->root, byte_pos, end - byte_pos);
|
||||
lock_2.unlock();
|
||||
if (editor->ts.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);
|
||||
}
|
||||
if (do_lsp) {
|
||||
if (editor->lsp->incremental_sync) {
|
||||
json message = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/didChange"},
|
||||
{"params",
|
||||
{{"textDocument",
|
||||
{{"uri", editor->uri}, {"version", ++editor->lsp_version}}},
|
||||
{"contentChanges",
|
||||
json::array({{{"range", lsp_range}, {"text", ""}}})}}}};
|
||||
lsp_send(editor->lsp, message, nullptr);
|
||||
} else {
|
||||
char *buf = read(editor->root, 0, editor->root->char_count);
|
||||
std::string text(buf);
|
||||
free(buf);
|
||||
json message = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/didChange"},
|
||||
{"params",
|
||||
{{"textDocument",
|
||||
{{"uri", editor->uri}, {"version", ++editor->lsp_version}}},
|
||||
{"contentChanges", json::array({{{"text", text}}})}}}};
|
||||
lsp_send(editor->lsp, message, nullptr);
|
||||
}
|
||||
}
|
||||
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});
|
||||
std::unique_lock lock_4(editor->def_spans.mtx);
|
||||
apply_edit(editor->def_spans.spans, 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);
|
||||
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++;
|
||||
}
|
||||
}
|
||||
apply_line_insertion(editor, pos.row, rows);
|
||||
apply_hook_insertion(editor, pos.row, rows);
|
||||
if (editor->ts.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);
|
||||
}
|
||||
if (editor->lsp) {
|
||||
if (editor->lsp->incremental_sync) {
|
||||
lock_1.lock();
|
||||
LineIterator *it = begin_l_iter(editor->root, pos.row);
|
||||
char *line = next_line(it, nullptr);
|
||||
int utf16_col = 0;
|
||||
if (line)
|
||||
utf16_col = utf8_byte_offset_to_utf16(line, pos.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
lock_1.unlock();
|
||||
json message = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/didChange"},
|
||||
{"params",
|
||||
{{"textDocument",
|
||||
{{"uri", editor->uri}, {"version", ++editor->lsp_version}}},
|
||||
{"contentChanges",
|
||||
json::array(
|
||||
{{{"range",
|
||||
{{"start", {{"line", pos.row}, {"character", utf16_col}}},
|
||||
{"end", {{"line", pos.row}, {"character", utf16_col}}}}},
|
||||
{"text", std::string(data, len)}}})}}}};
|
||||
lsp_send(editor->lsp, message, nullptr);
|
||||
} else {
|
||||
char *buf = read(editor->root, 0, editor->root->char_count);
|
||||
std::string text(buf);
|
||||
free(buf);
|
||||
json message = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/didChange"},
|
||||
{"params",
|
||||
{{"textDocument",
|
||||
{{"uri", editor->uri}, {"version", ++editor->lsp_version}}},
|
||||
{"contentChanges", json::array({{{"text", text}}})}}}};
|
||||
lsp_send(editor->lsp, message, nullptr);
|
||||
}
|
||||
}
|
||||
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});
|
||||
std::unique_lock lock_4(editor->def_spans.mtx);
|
||||
apply_edit(editor->def_spans.spans, byte_pos, len);
|
||||
}
|
||||
79
src/editor/editor.cc
Normal file
79
src/editor/editor.cc
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "editor/editor.h"
|
||||
#include "lsp/lsp.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
Editor *new_editor(const char *filename_arg, Coord position, Coord size) {
|
||||
Editor *editor = new Editor();
|
||||
if (!editor)
|
||||
return nullptr;
|
||||
uint32_t len = 0;
|
||||
std::string filename = path_abs(filename_arg);
|
||||
char *str = load_file(filename.c_str(), &len);
|
||||
if (!str) {
|
||||
free_editor(editor);
|
||||
return nullptr;
|
||||
}
|
||||
editor->filename = filename;
|
||||
editor->uri = path_to_file_uri(filename);
|
||||
editor->position = position;
|
||||
editor->size = size;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (len == 0) {
|
||||
free(str);
|
||||
str = (char *)malloc(1);
|
||||
*str = '\n';
|
||||
len = 1;
|
||||
}
|
||||
editor->root = load(str, len, optimal_chunk_size(len));
|
||||
free(str);
|
||||
Language language = language_for_file(filename.c_str());
|
||||
if (language.name != "unknown" && len <= (1024 * 128)) {
|
||||
editor->ts.parser = ts_parser_new();
|
||||
editor->ts.language = language.fn();
|
||||
ts_parser_set_language(editor->ts.parser, editor->ts.language);
|
||||
editor->ts.query_file =
|
||||
get_exe_dir() + "/../grammar/" + language.name + ".scm";
|
||||
request_add_to_lsp(language, editor);
|
||||
}
|
||||
return editor;
|
||||
}
|
||||
|
||||
void free_tsset(TSSetMain *set) {
|
||||
if (set->parser)
|
||||
ts_parser_delete(set->parser);
|
||||
if (set->tree)
|
||||
ts_tree_delete(set->tree);
|
||||
if (set->query)
|
||||
ts_query_delete(set->query);
|
||||
for (auto &inj : set->injections) {
|
||||
if (inj.second.parser)
|
||||
ts_parser_delete(inj.second.parser);
|
||||
if (inj.second.query)
|
||||
ts_query_delete(inj.second.query);
|
||||
if (inj.second.tree)
|
||||
ts_tree_delete(inj.second.tree);
|
||||
}
|
||||
}
|
||||
|
||||
void free_editor(Editor *editor) {
|
||||
remove_from_lsp(editor);
|
||||
free_tsset(&editor->ts);
|
||||
free_rope(editor->root);
|
||||
delete editor;
|
||||
}
|
||||
|
||||
void save_file(Editor *editor) {
|
||||
if (!editor || !editor->root)
|
||||
return;
|
||||
char *str = read(editor->root, 0, editor->root->char_count);
|
||||
if (!str)
|
||||
return;
|
||||
std::ofstream out(editor->filename);
|
||||
out.write(str, editor->root->char_count);
|
||||
free(str);
|
||||
json msg = {{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/didSave"},
|
||||
{"params", {{"textDocument", {{"uri", editor->uri}}}}}};
|
||||
if (editor->lsp)
|
||||
lsp_send(editor->lsp, msg, nullptr);
|
||||
}
|
||||
665
src/editor/events.cc
Normal file
665
src/editor/events.cc
Normal file
@@ -0,0 +1,665 @@
|
||||
#include "editor/editor.h"
|
||||
#include "editor/folds.h"
|
||||
#include "lsp/lsp.h"
|
||||
#include "main.h"
|
||||
|
||||
void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
static std::chrono::steady_clock::time_point last_click_time =
|
||||
std::chrono::steady_clock::now();
|
||||
static uint32_t click_count = 0;
|
||||
static Coord last_click_pos = {UINT32_MAX, UINT32_MAX};
|
||||
Coord start = editor->cursor;
|
||||
if (editor->hover_active)
|
||||
editor->hover_active = false;
|
||||
if (event.key_type == KEY_MOUSE) {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
now - last_click_time)
|
||||
.count();
|
||||
switch (event.mouse_state) {
|
||||
case SCROLL:
|
||||
switch (event.mouse_direction) {
|
||||
case SCROLL_UP:
|
||||
scroll_up(editor, 10);
|
||||
ensure_cursor(editor);
|
||||
break;
|
||||
case SCROLL_DOWN:
|
||||
scroll_down(editor, 10);
|
||||
ensure_cursor(editor);
|
||||
break;
|
||||
case SCROLL_LEFT:
|
||||
cursor_left(editor, 10);
|
||||
break;
|
||||
case SCROLL_RIGHT:
|
||||
cursor_right(editor, 10);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case PRESS:
|
||||
if (event.mouse_button == LEFT_BTN) {
|
||||
Coord cur_pos = {event.mouse_x, event.mouse_y};
|
||||
if (duration < 250 && last_click_pos == cur_pos)
|
||||
click_count++;
|
||||
else
|
||||
click_count = 1;
|
||||
last_click_time = now;
|
||||
last_click_pos = cur_pos;
|
||||
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||
if (p.row == UINT32_MAX && p.col == UINT32_MAX)
|
||||
return;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (click_count == 1) {
|
||||
editor->cursor = p;
|
||||
editor->selection = p;
|
||||
if (mode == SELECT) {
|
||||
mode = NORMAL;
|
||||
editor->selection_active = false;
|
||||
}
|
||||
} else if (click_count == 2) {
|
||||
uint32_t prev_col, next_col;
|
||||
word_boundaries(editor, editor->cursor, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
if (editor->cursor < editor->selection)
|
||||
editor->cursor = {editor->cursor.row, prev_col};
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, next_col};
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->selection_type = WORD;
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
} else if (click_count >= 3) {
|
||||
if (editor->cursor < editor->selection) {
|
||||
editor->cursor = {p.row, 0};
|
||||
} else {
|
||||
uint32_t line_len;
|
||||
LineIterator *it = begin_l_iter(editor->root, p.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
editor->cursor = {p.row, line_len};
|
||||
}
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->selection_type = LINE;
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
click_count = 3;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DRAG:
|
||||
if (event.mouse_button == LEFT_BTN) {
|
||||
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||
if (p.row == UINT32_MAX && p.col == UINT32_MAX)
|
||||
return;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
mode = SELECT;
|
||||
if (!editor->selection_active) {
|
||||
editor->selection_active = true;
|
||||
editor->selection_type = CHAR;
|
||||
}
|
||||
uint32_t prev_col, next_col, line_len;
|
||||
switch (editor->selection_type) {
|
||||
case CHAR:
|
||||
editor->cursor = p;
|
||||
break;
|
||||
case WORD:
|
||||
word_boundaries(editor, p, &prev_col, &next_col, nullptr, nullptr);
|
||||
if (editor->cursor < editor->selection)
|
||||
editor->cursor = {p.row, prev_col};
|
||||
else
|
||||
editor->cursor = {p.row, next_col};
|
||||
break;
|
||||
case LINE:
|
||||
if (editor->cursor < editor->selection) {
|
||||
editor->cursor = {p.row, 0};
|
||||
} else {
|
||||
LineIterator *it = begin_l_iter(editor->root, p.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
editor->cursor = {p.row, line_len};
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case RELEASE:
|
||||
if (event.mouse_button == LEFT_BTN)
|
||||
if (editor->cursor.row == editor->selection.row &&
|
||||
editor->cursor.col == editor->selection.col) {
|
||||
mode = NORMAL;
|
||||
editor->selection_active = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (event.key_type == KEY_SPECIAL) {
|
||||
switch (event.special_modifier) {
|
||||
case 0:
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
cursor_right(editor, 1);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CNTRL:
|
||||
uint32_t prev_col, next_col;
|
||||
word_boundaries(editor, editor->cursor, &prev_col, &next_col, nullptr,
|
||||
nullptr);
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 5);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 5);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (prev_col == editor->cursor.col)
|
||||
cursor_left(editor, 1);
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, prev_col};
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (next_col == editor->cursor.col)
|
||||
cursor_right(editor, 1);
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, next_col};
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ALT:
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
move_line_down(editor);
|
||||
break;
|
||||
case KEY_UP:
|
||||
move_line_up(editor);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
cursor_left(editor, 8);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
cursor_right(editor, 8);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch (mode) {
|
||||
case NORMAL:
|
||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||
switch (event.c[0]) {
|
||||
case 'u':
|
||||
if (editor->root->line_count > 0) {
|
||||
editor->cursor.row = editor->root->line_count - 1;
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
if (!it)
|
||||
break;
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
line_len = count_clusters(line, line_len, 0, line_len);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
editor->cursor.col = line_len;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
editor->selection = {0, 0};
|
||||
editor->selection_type = LINE;
|
||||
}
|
||||
break;
|
||||
case CTRL('h'):
|
||||
editor->hover.scroll(-1);
|
||||
editor->hover_active = true;
|
||||
break;
|
||||
case CTRL('l'):
|
||||
editor->hover.scroll(1);
|
||||
editor->hover_active = true;
|
||||
break;
|
||||
case 'h':
|
||||
if (editor->lsp && editor->lsp->allow_hover) {
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
break;
|
||||
}
|
||||
uint32_t col = utf8_byte_offset_to_utf16(line, editor->cursor.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
json hover_request = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/hover"},
|
||||
{"params",
|
||||
{{"textDocument", {{"uri", editor->uri}}},
|
||||
{"position",
|
||||
{{"line", editor->cursor.row}, {"character", col}}}}}};
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->editor = editor;
|
||||
pending->method = "textDocument/hover";
|
||||
pending->callback = [](Editor *editor, std::string, json hover) {
|
||||
if (hover.contains("result") && !hover["result"].is_null()) {
|
||||
auto &contents = hover["result"]["contents"];
|
||||
std::string hover_text = "";
|
||||
bool is_markup = false;
|
||||
if (contents.is_object()) {
|
||||
hover_text += contents["value"].get<std::string>();
|
||||
is_markup = (contents["kind"].get<std::string>() == "markdown");
|
||||
} else if (contents.is_array()) {
|
||||
for (auto &block : contents) {
|
||||
if (block.is_string()) {
|
||||
hover_text += block.get<std::string>() + "\n";
|
||||
} else if (block.is_object() && block.contains("language") &&
|
||||
block.contains("value")) {
|
||||
std::string lang = block["language"].get<std::string>();
|
||||
std::string val = block["value"].get<std::string>();
|
||||
is_markup = true;
|
||||
hover_text += "```" + lang + "\n" + val + "\n```\n";
|
||||
}
|
||||
}
|
||||
} else if (contents.is_string()) {
|
||||
hover_text += contents.get<std::string>();
|
||||
}
|
||||
if (!hover_text.empty()) {
|
||||
editor->hover.clear();
|
||||
editor->hover.text = clean_text(hover_text);
|
||||
editor->hover.is_markup = is_markup;
|
||||
editor->hover.render_first();
|
||||
editor->hover_active = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
lsp_send(editor->lsp, hover_request, pending);
|
||||
}
|
||||
break;
|
||||
case 'a':
|
||||
mode = INSERT;
|
||||
cursor_right(editor, 1);
|
||||
if (start.row != editor->cursor.row)
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
case 'i':
|
||||
mode = INSERT;
|
||||
break;
|
||||
case 'n':
|
||||
mode = JUMPER;
|
||||
editor->jumper_set = true;
|
||||
break;
|
||||
case 'm':
|
||||
mode = JUMPER;
|
||||
editor->jumper_set = false;
|
||||
break;
|
||||
case 'N':
|
||||
for (uint8_t i = 0; i < 94; i++)
|
||||
if (editor->hooks[i] == editor->cursor.row + 1) {
|
||||
editor->hooks[i] = 0;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 's':
|
||||
case 'v':
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
editor->selection = editor->cursor;
|
||||
editor->selection_type = CHAR;
|
||||
break;
|
||||
case ';':
|
||||
case ':':
|
||||
mode = RUNNER;
|
||||
break;
|
||||
case 0x7F:
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
case ' ':
|
||||
cursor_right(editor, 1);
|
||||
break;
|
||||
case '\r':
|
||||
case '\n':
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case '\\':
|
||||
case '|':
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case CTRL('d'):
|
||||
scroll_down(editor, 1);
|
||||
ensure_cursor(editor);
|
||||
break;
|
||||
case CTRL('u'):
|
||||
scroll_up(editor, 1);
|
||||
ensure_cursor(editor);
|
||||
break;
|
||||
case '>':
|
||||
case '.':
|
||||
indent_line(editor, editor->cursor.row);
|
||||
break;
|
||||
case '<':
|
||||
case ',':
|
||||
dedent_line(editor, editor->cursor.row);
|
||||
break;
|
||||
case CTRL('s'):
|
||||
save_file(editor);
|
||||
break;
|
||||
case CTRL(' '):
|
||||
if (editor->lsp) {
|
||||
json msg = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/completion"},
|
||||
{
|
||||
"params",
|
||||
{
|
||||
{
|
||||
"textDocument",
|
||||
{
|
||||
{"uri", editor->uri},
|
||||
},
|
||||
},
|
||||
{
|
||||
"position",
|
||||
{
|
||||
{"line", editor->cursor.row},
|
||||
{"character", editor->cursor.col},
|
||||
},
|
||||
},
|
||||
{
|
||||
"context",
|
||||
{
|
||||
{"triggerKind", 1},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->editor = editor;
|
||||
pending->method = "textDocument/completion";
|
||||
pending->callback = [](Editor *editor, std::string, json completion) {
|
||||
log("%s\n", completion.dump().c_str());
|
||||
};
|
||||
lsp_send(editor->lsp, msg, pending);
|
||||
}
|
||||
break;
|
||||
case 'p':
|
||||
uint32_t len;
|
||||
char *text = get_from_clipboard(&len);
|
||||
if (text) {
|
||||
edit_insert(editor, editor->cursor, text, len);
|
||||
uint32_t grapheme_len = count_clusters(text, len, 0, len);
|
||||
cursor_right(editor, grapheme_len);
|
||||
free(text);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case INSERT:
|
||||
if (event.key_type == KEY_CHAR) {
|
||||
if (event.len == 1) {
|
||||
if (event.c[0] == '\t') {
|
||||
edit_insert(editor, editor->cursor, (char *)" ", 2);
|
||||
cursor_right(editor, 2);
|
||||
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
|
||||
uint32_t line_len = 0;
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
bool closing = false;
|
||||
if (line && line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t indent = get_indent(editor, editor->cursor);
|
||||
if (line) {
|
||||
if (indent == 0)
|
||||
indent = leading_indent(line, line_len);
|
||||
closing = closing_after_cursor(line, line_len, editor->cursor.col);
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
uint32_t closing_indent =
|
||||
indent >= INDENT_WIDTH ? indent - INDENT_WIDTH : 0;
|
||||
std::string insert_text("\n");
|
||||
insert_text.append(indent, ' ');
|
||||
Coord new_cursor = {editor->cursor.row + 1, indent};
|
||||
if (closing) {
|
||||
insert_text.push_back('\n');
|
||||
insert_text.append(closing_indent, ' ');
|
||||
}
|
||||
edit_insert(editor, editor->cursor, insert_text.data(),
|
||||
insert_text.size());
|
||||
editor->cursor = new_cursor;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
} else if (event.c[0] == CTRL('W')) {
|
||||
uint32_t prev_col_byte, prev_col_cluster;
|
||||
word_boundaries(editor, editor->cursor, &prev_col_byte, nullptr,
|
||||
&prev_col_cluster, nullptr);
|
||||
if (prev_col_byte == editor->cursor.col)
|
||||
edit_erase(editor, editor->cursor, -1);
|
||||
else
|
||||
edit_erase(editor, editor->cursor, -(int64_t)prev_col_cluster);
|
||||
} else if (isprint((unsigned char)(event.c[0]))) {
|
||||
char c = event.c[0];
|
||||
uint32_t col = editor->cursor.col;
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
bool skip_insert = false;
|
||||
if (line && col < len) {
|
||||
char next = line[col];
|
||||
if ((c == '}' && next == '}') || (c == ')' && next == ')') ||
|
||||
(c == ']' && next == ']') || (c == '"' && next == '"') ||
|
||||
(c == '\'' && next == '\'')) {
|
||||
cursor_right(editor, 1);
|
||||
skip_insert = true;
|
||||
}
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
if (!skip_insert) {
|
||||
char closing = 0;
|
||||
switch (c) {
|
||||
case '{':
|
||||
closing = '}';
|
||||
break;
|
||||
case '(':
|
||||
closing = ')';
|
||||
break;
|
||||
case '[':
|
||||
closing = ']';
|
||||
break;
|
||||
case '"':
|
||||
closing = '"';
|
||||
break;
|
||||
case '\'':
|
||||
closing = '\'';
|
||||
break;
|
||||
}
|
||||
if (closing) {
|
||||
char pair[2] = {c, closing};
|
||||
edit_insert(editor, editor->cursor, pair, 2);
|
||||
cursor_right(editor, 1);
|
||||
} else {
|
||||
edit_insert(editor, editor->cursor, &c, 1);
|
||||
cursor_right(editor, 1);
|
||||
}
|
||||
}
|
||||
} else if (event.c[0] == 0x7F || event.c[0] == 0x08) {
|
||||
Coord prev_pos = editor->cursor;
|
||||
if (prev_pos.col > 0)
|
||||
prev_pos.col--;
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
if (!it)
|
||||
return;
|
||||
char *line = next_line(it, nullptr);
|
||||
char prev_char = line[prev_pos.col];
|
||||
char next_char = line[editor->cursor.col];
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
bool is_pair = (prev_char == '{' && next_char == '}') ||
|
||||
(prev_char == '(' && next_char == ')') ||
|
||||
(prev_char == '[' && next_char == ']') ||
|
||||
(prev_char == '"' && next_char == '"') ||
|
||||
(prev_char == '\'' && next_char == '\'');
|
||||
if (is_pair) {
|
||||
edit_erase(editor, editor->cursor, 1);
|
||||
edit_erase(editor, prev_pos, 1);
|
||||
} else {
|
||||
edit_erase(editor, editor->cursor, -1);
|
||||
}
|
||||
} else if (event.c[0] == 0x1B) {
|
||||
Coord prev_pos = editor->cursor;
|
||||
mode = NORMAL;
|
||||
cursor_left(editor, 1);
|
||||
if (prev_pos.row != editor->cursor.row)
|
||||
cursor_right(editor, 1);
|
||||
}
|
||||
} else if (event.len > 1) {
|
||||
edit_insert(editor, editor->cursor, event.c, event.len);
|
||||
cursor_right(editor, 1);
|
||||
}
|
||||
} else if (event.key_type == KEY_SPECIAL &&
|
||||
event.special_key == KEY_DELETE) {
|
||||
switch (event.special_modifier) {
|
||||
case 0:
|
||||
edit_erase(editor, editor->cursor, 1);
|
||||
break;
|
||||
case CNTRL:
|
||||
uint32_t next_col_byte, next_col_cluster;
|
||||
word_boundaries(editor, editor->cursor, nullptr, &next_col_byte,
|
||||
nullptr, &next_col_cluster);
|
||||
if (next_col_byte == editor->cursor.col)
|
||||
edit_erase(editor, editor->cursor, 1);
|
||||
else
|
||||
edit_erase(editor, editor->cursor, next_col_cluster);
|
||||
break;
|
||||
}
|
||||
} else if (event.key_type == KEY_PASTE) {
|
||||
if (event.c) {
|
||||
edit_insert(editor, editor->cursor, event.c, event.len);
|
||||
uint32_t grapheme_len =
|
||||
count_clusters(event.c, event.len, 0, event.len);
|
||||
cursor_right(editor, grapheme_len);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SELECT:
|
||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||
uint32_t len;
|
||||
char *text;
|
||||
Coord start;
|
||||
switch (event.c[0]) {
|
||||
case 'f':
|
||||
if (editor->cursor.row != editor->selection.row) {
|
||||
uint32_t start = MIN(editor->cursor.row, editor->selection.row);
|
||||
uint32_t end = MAX(editor->cursor.row, editor->selection.row);
|
||||
add_fold(editor, start, end);
|
||||
}
|
||||
cursor_left(editor, 1);
|
||||
cursor_down(editor, 1);
|
||||
editor->selection_active = false;
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case 0x1B:
|
||||
case 's':
|
||||
case 'v':
|
||||
editor->selection_active = false;
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case 'y':
|
||||
text = get_selection(editor, &len, nullptr);
|
||||
copy_to_clipboard(text, len);
|
||||
free(text);
|
||||
editor->selection_active = false;
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case 'x':
|
||||
text = get_selection(editor, &len, &start);
|
||||
copy_to_clipboard(text, len);
|
||||
len = count_clusters(text, len, 0, len);
|
||||
edit_erase(editor, start, len);
|
||||
free(text);
|
||||
editor->selection_active = false;
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case 'p':
|
||||
text = get_from_clipboard(&len);
|
||||
if (text) {
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
} else {
|
||||
start = editor->cursor;
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
}
|
||||
uint32_t start_byte =
|
||||
line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
uint32_t end_byte =
|
||||
line_to_byte(editor->root, end.row, nullptr) + end.col;
|
||||
edit_erase(editor, start, end_byte - start_byte);
|
||||
edit_insert(editor, editor->cursor, text, len);
|
||||
free(text);
|
||||
}
|
||||
editor->selection_active = false;
|
||||
mode = NORMAL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case JUMPER:
|
||||
if (event.key_type == KEY_CHAR && event.len == 1 &&
|
||||
(event.c[0] >= '!' && event.c[0] <= '~')) {
|
||||
if (editor->jumper_set) {
|
||||
for (uint8_t i = 0; i < 94; i++)
|
||||
if (editor->hooks[i] == editor->cursor.row + 1) {
|
||||
editor->hooks[i] = 0;
|
||||
break;
|
||||
}
|
||||
editor->hooks[event.c[0] - '!'] = editor->cursor.row + 1;
|
||||
} else {
|
||||
uint32_t line = editor->hooks[event.c[0] - '!'];
|
||||
if (line > 0) {
|
||||
if (line_is_folded(editor->folds, --line))
|
||||
break;
|
||||
editor->cursor = {line, 0};
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
}
|
||||
}
|
||||
}
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case RUNNER:
|
||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||
switch (event.c[0]) {
|
||||
case 0x1B:
|
||||
mode = NORMAL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
ensure_scroll(editor);
|
||||
if ((event.key_type == KEY_CHAR || event.key_type == KEY_PASTE) && event.c)
|
||||
free(event.c);
|
||||
}
|
||||
87
src/editor/indents.cc
Normal file
87
src/editor/indents.cc
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "editor/editor.h"
|
||||
|
||||
uint32_t leading_indent(const char *line, uint32_t len) {
|
||||
uint32_t indent = 0;
|
||||
for (uint32_t i = 0; i < len; i++) {
|
||||
if (line[i] == ' ')
|
||||
indent++;
|
||||
else if (line[i] == '\t')
|
||||
indent += INDENT_WIDTH;
|
||||
else
|
||||
break;
|
||||
}
|
||||
return indent;
|
||||
}
|
||||
|
||||
uint32_t get_indent(Editor *editor, Coord cursor) {
|
||||
if (!editor)
|
||||
return 0;
|
||||
LineIterator *it = begin_l_iter(editor->root, cursor.row);
|
||||
next_line(it, nullptr);
|
||||
uint32_t line_len;
|
||||
char *line;
|
||||
while ((line = prev_line(it, &line_len)) != nullptr) {
|
||||
if (line_len == 0)
|
||||
continue;
|
||||
uint32_t indent = leading_indent(line, line_len);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return indent;
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool closing_after_cursor(const char *line, uint32_t len, uint32_t col) {
|
||||
uint32_t i = col;
|
||||
while (i < len && (line[i] == ' ' || line[i] == '\t'))
|
||||
i++;
|
||||
if (i >= len)
|
||||
return false;
|
||||
return line[i] == '}' || line[i] == ']' || line[i] == ')';
|
||||
}
|
||||
|
||||
void indent_line(Editor *editor, uint32_t row) {
|
||||
if (!editor)
|
||||
return;
|
||||
LineIterator *it = begin_l_iter(editor->root, row);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
char *spaces = (char *)malloc(INDENT_WIDTH);
|
||||
memset(spaces, ' ', INDENT_WIDTH);
|
||||
Coord cursor = editor->cursor;
|
||||
edit_insert(editor, {row, 0}, spaces, INDENT_WIDTH);
|
||||
editor->cursor = cursor;
|
||||
free(spaces);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
|
||||
void dedent_line(Editor *editor, uint32_t row) {
|
||||
if (!editor)
|
||||
return;
|
||||
LineIterator *it = begin_l_iter(editor->root, row);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t indent = leading_indent(line, line_len);
|
||||
if (indent == 0) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t remove = indent >= INDENT_WIDTH ? INDENT_WIDTH : indent;
|
||||
edit_erase(editor, {row, 0}, remove);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
63
src/editor/lsp.cc
Normal file
63
src/editor/lsp.cc
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "editor/editor.h"
|
||||
|
||||
void editor_lsp_handle(Editor *editor, json msg) {
|
||||
if (msg.contains("method") &&
|
||||
msg["method"] == "textDocument/publishDiagnostics") {
|
||||
std::unique_lock lock(editor->v_mtx);
|
||||
editor->warnings.clear();
|
||||
json diagnostics = msg["params"]["diagnostics"];
|
||||
for (size_t i = 0; i < diagnostics.size(); i++) {
|
||||
json d = diagnostics[i];
|
||||
VWarn w;
|
||||
// HACK: convert back to utf-8 but as this is only visually affecting it
|
||||
// is not worth getting the line string from the rope.
|
||||
w.line = d["range"]["start"]["line"];
|
||||
w.start = d["range"]["start"]["character"];
|
||||
uint32_t end = d["range"]["end"]["character"];
|
||||
if (d["range"]["end"]["line"] == w.line)
|
||||
w.end = end;
|
||||
std::string text = trim(d["message"].get<std::string>());
|
||||
w.text_full = text;
|
||||
auto pos = text.find('\n');
|
||||
w.text = (pos == std::string::npos) ? text : text.substr(0, pos);
|
||||
if (d.contains("source"))
|
||||
w.source = d["source"].get<std::string>();
|
||||
if (d.contains("code")) {
|
||||
w.code = "[";
|
||||
if (d["code"].is_string())
|
||||
w.code += d["code"].get<std::string>() + "] ";
|
||||
else if (d["code"].is_number())
|
||||
w.code += std::to_string(d["code"].get<int>()) + "] ";
|
||||
else
|
||||
w.code.clear();
|
||||
if (d.contains("codeDescription") &&
|
||||
d["codeDescription"].contains("href"))
|
||||
w.code += d["codeDescription"]["href"].get<std::string>();
|
||||
}
|
||||
if (d.contains("relatedInformation")) {
|
||||
json related = d["relatedInformation"];
|
||||
for (size_t j = 0; j < related.size(); j++) {
|
||||
json rel = related[j];
|
||||
std::string message = rel["message"].get<std::string>();
|
||||
auto pos = message.find('\n');
|
||||
message =
|
||||
(pos == std::string::npos) ? message : message.substr(0, pos);
|
||||
std::string uri =
|
||||
percent_decode(rel["location"]["uri"].get<std::string>());
|
||||
auto pos2 = uri.find_last_of('/');
|
||||
if (pos2 != std::string::npos)
|
||||
uri = uri.substr(pos2 + 1);
|
||||
std::string row = std::to_string(
|
||||
rel["location"]["range"]["start"]["line"].get<int>());
|
||||
w.see_also.push_back(uri + ":" + row + ": " + message);
|
||||
}
|
||||
}
|
||||
w.type = 1;
|
||||
if (d.contains("severity"))
|
||||
w.type = d["severity"].get<int>();
|
||||
editor->warnings.push_back(w);
|
||||
}
|
||||
std::sort(editor->warnings.begin(), editor->warnings.end());
|
||||
editor->warnings_dirty = true;
|
||||
}
|
||||
}
|
||||
121
src/editor/move_line.cc
Normal file
121
src/editor/move_line.cc
Normal file
@@ -0,0 +1,121 @@
|
||||
#include "editor/editor.h"
|
||||
#include "editor/folds.h"
|
||||
#include "main.h"
|
||||
|
||||
void move_line_up(Editor *editor) {
|
||||
if (!editor || !editor->root || editor->cursor.row == 0)
|
||||
return;
|
||||
if (mode == NORMAL || mode == INSERT) {
|
||||
uint32_t line_len, line_cluster_len;
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
lock.unlock();
|
||||
return;
|
||||
}
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
line_cluster_len = count_clusters(line, line_len, 0, line_len);
|
||||
uint32_t target_row = prev_unfolded_row(editor, editor->cursor.row - 1);
|
||||
uint32_t up_by = editor->cursor.row - target_row;
|
||||
if (up_by > 1)
|
||||
up_by--;
|
||||
lock.unlock();
|
||||
Coord cursor = editor->cursor;
|
||||
edit_erase(editor, {cursor.row, 0}, line_cluster_len);
|
||||
edit_erase(editor, {cursor.row, 0}, -1);
|
||||
edit_insert(editor, {cursor.row - up_by, 0}, (char *)"\n", 1);
|
||||
edit_insert(editor, {cursor.row - up_by, 0}, line, line_len);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
editor->cursor = {cursor.row - up_by, cursor.col};
|
||||
} else if (mode == SELECT) {
|
||||
uint32_t start_row = MIN(editor->cursor.row, editor->selection.row);
|
||||
uint32_t end_row = MAX(editor->cursor.row, editor->selection.row);
|
||||
uint32_t start_byte = line_to_byte(editor->root, start_row, nullptr);
|
||||
uint32_t end_byte = line_to_byte(editor->root, end_row + 1, nullptr);
|
||||
char *selected_text = read(editor->root, start_byte, end_byte - start_byte);
|
||||
if (!selected_text)
|
||||
return;
|
||||
uint32_t selected_len = count_clusters(selected_text, end_byte - start_byte,
|
||||
0, end_byte - start_byte);
|
||||
Coord cursor = editor->cursor;
|
||||
Coord selection = editor->selection;
|
||||
edit_erase(editor, {start_row, 0}, selected_len);
|
||||
edit_insert(editor, {start_row - 1, 0}, selected_text,
|
||||
end_byte - start_byte);
|
||||
free(selected_text);
|
||||
editor->cursor = {cursor.row - 1, cursor.col};
|
||||
editor->selection = {selection.row - 1, selection.col};
|
||||
}
|
||||
}
|
||||
|
||||
void move_line_down(Editor *editor) {
|
||||
if (!editor || !editor->root)
|
||||
return;
|
||||
if (mode == NORMAL || mode == INSERT) {
|
||||
if (editor->cursor.row >= editor->root->line_count - 1)
|
||||
return;
|
||||
uint32_t line_len, line_cluster_len;
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
lock.unlock();
|
||||
return;
|
||||
}
|
||||
if (line_len && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
line_cluster_len = count_clusters(line, line_len, 0, line_len);
|
||||
uint32_t target_row = next_unfolded_row(editor, editor->cursor.row + 1);
|
||||
if (target_row >= editor->root->line_count) {
|
||||
free(line);
|
||||
lock.unlock();
|
||||
return;
|
||||
}
|
||||
uint32_t down_by = target_row - editor->cursor.row;
|
||||
if (down_by > 1)
|
||||
down_by--;
|
||||
uint32_t ln;
|
||||
line_to_byte(editor->root, editor->cursor.row + down_by - 1, &ln);
|
||||
lock.unlock();
|
||||
Coord cursor = editor->cursor;
|
||||
edit_erase(editor, {cursor.row, 0}, line_cluster_len);
|
||||
edit_erase(editor, {cursor.row, 0}, -1);
|
||||
edit_insert(editor, {cursor.row + down_by, 0}, (char *)"\n", 1);
|
||||
edit_insert(editor, {cursor.row + down_by, 0}, line, line_len);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
editor->cursor = {cursor.row + down_by, cursor.col};
|
||||
} else if (mode == SELECT) {
|
||||
if (editor->cursor.row >= editor->root->line_count - 1 ||
|
||||
editor->selection.row >= editor->root->line_count - 1)
|
||||
return;
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
uint32_t start_row = MIN(editor->cursor.row, editor->selection.row);
|
||||
uint32_t end_row = MAX(editor->cursor.row, editor->selection.row);
|
||||
uint32_t target_row = next_unfolded_row(editor, end_row + 1);
|
||||
if (target_row >= editor->root->line_count)
|
||||
return;
|
||||
uint32_t down_by = target_row - end_row;
|
||||
if (down_by > 1)
|
||||
down_by--;
|
||||
uint32_t start_byte = line_to_byte(editor->root, start_row, nullptr);
|
||||
uint32_t end_byte = line_to_byte(editor->root, end_row + 1, nullptr);
|
||||
char *selected_text = read(editor->root, start_byte, end_byte - start_byte);
|
||||
lock.unlock();
|
||||
if (!selected_text)
|
||||
return;
|
||||
uint32_t selected_len = count_clusters(selected_text, end_byte - start_byte,
|
||||
0, end_byte - start_byte);
|
||||
Coord cursor = editor->cursor;
|
||||
Coord selection = editor->selection;
|
||||
edit_erase(editor, {start_row, 0}, selected_len);
|
||||
edit_insert(editor, {start_row + down_by, 0}, selected_text,
|
||||
end_byte - start_byte);
|
||||
free(selected_text);
|
||||
editor->cursor = {cursor.row + down_by, cursor.col};
|
||||
editor->selection = {selection.row + down_by, selection.col};
|
||||
}
|
||||
}
|
||||
489
src/editor/renderer.cc
Normal file
489
src/editor/renderer.cc
Normal file
@@ -0,0 +1,489 @@
|
||||
#include "editor/editor.h"
|
||||
#include "editor/folds.h"
|
||||
#include "main.h"
|
||||
|
||||
void render_editor(Editor *editor) {
|
||||
uint32_t sel_start = 0, sel_end = 0;
|
||||
uint32_t numlen =
|
||||
EXTRA_META + 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::vector<std::pair<uint32_t, char>> v;
|
||||
for (size_t i = 0; i < 94; ++i)
|
||||
if (editor->hooks[i] != 0)
|
||||
v.push_back({editor->hooks[i], '!' + i});
|
||||
std::sort(v.begin(), v.end());
|
||||
auto hook_it = v.begin();
|
||||
while (hook_it != v.end() && hook_it->first <= editor->scroll.row)
|
||||
++hook_it;
|
||||
std::unique_lock warn_lock(editor->v_mtx);
|
||||
auto warn_it = editor->warnings.begin();
|
||||
while (warn_it != editor->warnings.end() &&
|
||||
warn_it->line < editor->scroll.row)
|
||||
++warn_it;
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
if (editor->selection_active) {
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
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;
|
||||
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);
|
||||
if (!line)
|
||||
return;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
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;
|
||||
}
|
||||
Coord cursor = {UINT32_MAX, UINT32_MAX};
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
SpanCursor span_cursor(editor->spans);
|
||||
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) {
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
update(editor->position.row + rendered_rows, editor->position.col, "",
|
||||
0xAAAAAA, 0, 0);
|
||||
char buf[16];
|
||||
int len = snprintf(buf, sizeof(buf), "%*u ", numlen - 3, fold->start + 1);
|
||||
uint32_t num_color =
|
||||
editor->cursor.row == fold->start ? 0xFFFFFF : 0x555555;
|
||||
for (int i = 0; i < len; i++)
|
||||
update(editor->position.row + rendered_rows,
|
||||
editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color, 0,
|
||||
0);
|
||||
const char marker[15] = "... folded ...";
|
||||
uint32_t i = 0;
|
||||
for (; i < 14 && i < render_width; i++)
|
||||
update(rendered_rows, i + render_x, (char[2]){marker[i], 0}, 0xc6c6c6,
|
||||
0, 0);
|
||||
for (; i < render_width; i++)
|
||||
update(rendered_rows, i + render_x, " ", 0xc6c6c6, 0, 0);
|
||||
rendered_rows++;
|
||||
uint32_t skip_until = fold->end;
|
||||
while (line_index <= skip_until) {
|
||||
if (hook_it != v.end() && hook_it->first == line_index + 1)
|
||||
hook_it++;
|
||||
while (warn_it != editor->warnings.end() && warn_it->line == line_index)
|
||||
++warn_it;
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
global_byte_offset += line_len;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
global_byte_offset--;
|
||||
global_byte_offset++;
|
||||
line_index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t content_end = line_len;
|
||||
while (content_end > 0 &&
|
||||
(line[content_end - 1] == ' ' || line[content_end - 1] == '\t'))
|
||||
content_end--;
|
||||
uint32_t content_start = 0;
|
||||
while (content_start < line_len &&
|
||||
(line[content_start] == ' ' || line[content_start] == '\t'))
|
||||
content_start++;
|
||||
std::vector<VWarn> line_warnings;
|
||||
while (warn_it != editor->warnings.end() && warn_it->line == line_index) {
|
||||
line_warnings.push_back(*warn_it);
|
||||
++warn_it;
|
||||
}
|
||||
uint32_t current_byte_offset = 0;
|
||||
if (rendered_rows == 0)
|
||||
current_byte_offset += editor->scroll.col;
|
||||
while (current_byte_offset < line_len && rendered_rows < editor->size.row) {
|
||||
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
|
||||
if (current_byte_offset == 0 || rendered_rows == 0) {
|
||||
const char *hook = nullptr;
|
||||
char h[2] = {0, 0};
|
||||
if (hook_it != v.end() && hook_it->first == line_index + 1) {
|
||||
h[0] = hook_it->second;
|
||||
hook = h;
|
||||
hook_it++;
|
||||
}
|
||||
update(editor->position.row + rendered_rows, editor->position.col, hook,
|
||||
0xAAAAAA, 0, 0);
|
||||
char buf[16];
|
||||
int len =
|
||||
snprintf(buf, sizeof(buf), "%*u ", numlen - 3, line_index + 1);
|
||||
uint32_t num_color =
|
||||
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
|
||||
for (int i = 0; i < len; i++)
|
||||
update(editor->position.row + rendered_rows,
|
||||
editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color,
|
||||
0, 0);
|
||||
} else {
|
||||
for (uint32_t i = 0; i < numlen; i++)
|
||||
update(editor->position.row + rendered_rows, editor->position.col + i,
|
||||
" ", 0, 0, 0);
|
||||
}
|
||||
uint32_t col = 0;
|
||||
uint32_t local_render_offset = 0;
|
||||
uint32_t line_left = line_len - current_byte_offset;
|
||||
while (line_left > 0 && col < render_width) {
|
||||
if (line_index == editor->cursor.row &&
|
||||
editor->cursor.col == (current_byte_offset + local_render_offset)) {
|
||||
cursor.row = editor->position.row + rendered_rows;
|
||||
cursor.col = render_x + col;
|
||||
}
|
||||
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;
|
||||
uint32_t u_color = 0;
|
||||
for (const auto &w : line_warnings) {
|
||||
if (w.start <= current_byte_offset + local_render_offset &&
|
||||
current_byte_offset + local_render_offset < w.end) {
|
||||
switch (w.type) {
|
||||
case 1:
|
||||
u_color = 0xff0000;
|
||||
fl |= CF_UNDERLINE;
|
||||
break;
|
||||
case 2:
|
||||
u_color = 0xffff00;
|
||||
fl |= CF_UNDERLINE;
|
||||
break;
|
||||
case 3:
|
||||
u_color = 0xff00ff;
|
||||
fl |= CF_UNDERLINE;
|
||||
break;
|
||||
case 4:
|
||||
u_color = 0xA0A0A0;
|
||||
fl |= CF_UNDERLINE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
line + current_byte_offset + local_render_offset, line_left);
|
||||
std::string cluster(line + current_byte_offset + local_render_offset,
|
||||
cluster_len);
|
||||
int width = display_width(cluster.c_str(), cluster_len);
|
||||
if (col + width > render_width)
|
||||
break;
|
||||
if (current_byte_offset + local_render_offset >= content_start &&
|
||||
current_byte_offset + local_render_offset < content_end) {
|
||||
update(editor->position.row + rendered_rows, render_x + col,
|
||||
cluster.c_str(), fg, bg | color, fl, u_color);
|
||||
} else {
|
||||
if (cluster[0] == ' ') {
|
||||
update(editor->position.row + rendered_rows, render_x + col, "·",
|
||||
0x282828, bg | color, fl, u_color);
|
||||
} else {
|
||||
update(editor->position.row + rendered_rows, render_x + col, "-> ",
|
||||
0x282828, bg | color, (fl & ~CF_BOLD) | CF_ITALIC, u_color);
|
||||
}
|
||||
}
|
||||
local_render_offset += cluster_len;
|
||||
line_left -= cluster_len;
|
||||
col += width;
|
||||
while (width-- > 1)
|
||||
update(editor->position.row + rendered_rows, render_x + col - width,
|
||||
"\x1b", fg, bg | color, fl);
|
||||
}
|
||||
if (line_index == editor->cursor.row &&
|
||||
editor->cursor.col == (current_byte_offset + local_render_offset)) {
|
||||
cursor.row = editor->position.row + rendered_rows;
|
||||
cursor.col = render_x + col;
|
||||
}
|
||||
if (editor->selection_active &&
|
||||
global_byte_offset + line_len + 1 > sel_start &&
|
||||
global_byte_offset + line_len + 1 <= sel_end && col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0x555555 | color, 0);
|
||||
col++;
|
||||
}
|
||||
if (!line_warnings.empty() && line_left == 0) {
|
||||
VWarn warn = line_warnings.front();
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
color, 0);
|
||||
col++;
|
||||
for (size_t i = 0; i < line_warnings.size(); i++) {
|
||||
if (line_warnings[i].type < warn.type)
|
||||
warn = line_warnings[i];
|
||||
std::string err_sym = " ";
|
||||
uint32_t fg_color = 0;
|
||||
switch (line_warnings[i].type) {
|
||||
case 1:
|
||||
err_sym = "";
|
||||
fg_color = 0xFF0000;
|
||||
goto final;
|
||||
case 2:
|
||||
err_sym = "";
|
||||
fg_color = 0xFFFF00;
|
||||
goto final;
|
||||
case 3:
|
||||
err_sym = "";
|
||||
fg_color = 0xFF00FF;
|
||||
goto final;
|
||||
case 4:
|
||||
err_sym = "";
|
||||
fg_color = 0xAAAAAA;
|
||||
goto final;
|
||||
final:
|
||||
if (col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col,
|
||||
err_sym, fg_color, color, 0);
|
||||
col++;
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ",
|
||||
fg_color, color, 0);
|
||||
col++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0 | color, 0);
|
||||
col++;
|
||||
}
|
||||
size_t warn_idx = 0;
|
||||
uint32_t fg_color = 0;
|
||||
switch (warn.type) {
|
||||
case 1:
|
||||
fg_color = 0xFF0000;
|
||||
break;
|
||||
case 2:
|
||||
fg_color = 0xFFFF00;
|
||||
break;
|
||||
case 3:
|
||||
fg_color = 0xFF00FF;
|
||||
break;
|
||||
case 4:
|
||||
fg_color = 0xAAAAAA;
|
||||
break;
|
||||
}
|
||||
while (col < render_width && warn_idx < warn.text.length()) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
warn.text.c_str() + warn_idx, warn.text.length() - warn_idx);
|
||||
std::string cluster = warn.text.substr(warn_idx, cluster_len);
|
||||
int width = display_width(cluster.c_str(), cluster_len);
|
||||
if (col + width > render_width)
|
||||
break;
|
||||
update(editor->position.row + rendered_rows, render_x + col,
|
||||
cluster.c_str(), fg_color, color, 0);
|
||||
col += width;
|
||||
warn_idx += cluster_len;
|
||||
while (width-- > 1)
|
||||
update(editor->position.row + rendered_rows, render_x + col - width,
|
||||
"\x1b", fg_color, color, 0);
|
||||
}
|
||||
line_warnings.clear();
|
||||
}
|
||||
while (col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0 | color, 0);
|
||||
col++;
|
||||
}
|
||||
rendered_rows++;
|
||||
current_byte_offset += local_render_offset;
|
||||
}
|
||||
if (line_len == 0 ||
|
||||
(current_byte_offset >= line_len && rendered_rows == 0)) {
|
||||
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
|
||||
const char *hook = nullptr;
|
||||
char h[2] = {0, 0};
|
||||
if (hook_it != v.end() && hook_it->first == line_index + 1) {
|
||||
h[0] = hook_it->second;
|
||||
hook = h;
|
||||
hook_it++;
|
||||
}
|
||||
update(editor->position.row + rendered_rows, editor->position.col, hook,
|
||||
0xAAAAAA, 0, 0);
|
||||
char buf[16];
|
||||
int len = snprintf(buf, sizeof(buf), "%*u ", numlen - 3, line_index + 1);
|
||||
uint32_t num_color =
|
||||
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
|
||||
for (int i = 0; i < len; i++)
|
||||
update(editor->position.row + rendered_rows,
|
||||
editor->position.col + i + 2, (char[2]){buf[i], 0}, num_color, 0,
|
||||
0);
|
||||
if (editor->cursor.row == line_index) {
|
||||
cursor.row = editor->position.row + rendered_rows;
|
||||
cursor.col = render_x;
|
||||
}
|
||||
uint32_t col = 0;
|
||||
if (editor->selection_active &&
|
||||
global_byte_offset + line_len + 1 > sel_start &&
|
||||
global_byte_offset + line_len + 1 <= sel_end) {
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0x555555 | color, 0);
|
||||
col++;
|
||||
}
|
||||
if (!line_warnings.empty()) {
|
||||
VWarn warn = line_warnings.front();
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
color, 0);
|
||||
col++;
|
||||
for (size_t i = 0; i < line_warnings.size(); i++) {
|
||||
if (line_warnings[i].type < warn.type)
|
||||
warn = line_warnings[i];
|
||||
std::string err_sym = " ";
|
||||
uint32_t fg_color = 0;
|
||||
switch (line_warnings[i].type) {
|
||||
case 1:
|
||||
err_sym = "";
|
||||
fg_color = 0xFF0000;
|
||||
goto final2;
|
||||
case 2:
|
||||
err_sym = "";
|
||||
fg_color = 0xFFFF00;
|
||||
goto final2;
|
||||
case 3:
|
||||
err_sym = "";
|
||||
fg_color = 0xFF00FF;
|
||||
goto final2;
|
||||
case 4:
|
||||
err_sym = "";
|
||||
fg_color = 0xAAAAAA;
|
||||
goto final2;
|
||||
final2:
|
||||
if (col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col,
|
||||
err_sym, fg_color, color, 0);
|
||||
col++;
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ",
|
||||
fg_color, color, 0);
|
||||
col++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0 | color, 0);
|
||||
col++;
|
||||
}
|
||||
size_t warn_idx = 0;
|
||||
uint32_t fg_color = 0;
|
||||
switch (warn.type) {
|
||||
case 1:
|
||||
fg_color = 0xFF0000;
|
||||
break;
|
||||
case 2:
|
||||
fg_color = 0xFFFF00;
|
||||
break;
|
||||
case 3:
|
||||
fg_color = 0xFF00FF;
|
||||
break;
|
||||
case 4:
|
||||
fg_color = 0xAAAAAA;
|
||||
break;
|
||||
}
|
||||
while (col < render_width && warn_idx < warn.text.length()) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
warn.text.c_str() + warn_idx, warn.text.length() - warn_idx);
|
||||
std::string cluster = warn.text.substr(warn_idx, cluster_len);
|
||||
int width = display_width(cluster.c_str(), cluster_len);
|
||||
if (col + width > render_width)
|
||||
break;
|
||||
update(editor->position.row + rendered_rows, render_x + col,
|
||||
cluster.c_str(), fg_color, color, 0);
|
||||
col += width;
|
||||
warn_idx += cluster_len;
|
||||
while (width-- > 1)
|
||||
update(editor->position.row + rendered_rows, render_x + col - width,
|
||||
"\x1b", fg_color, color, 0);
|
||||
}
|
||||
}
|
||||
while (col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0 | color, 0);
|
||||
col++;
|
||||
}
|
||||
rendered_rows++;
|
||||
}
|
||||
global_byte_offset += line_len + 1;
|
||||
line_index++;
|
||||
}
|
||||
if (cursor.row != UINT32_MAX && cursor.col != UINT32_MAX) {
|
||||
int type = 0;
|
||||
switch (mode) {
|
||||
case NORMAL:
|
||||
type = BLOCK;
|
||||
break;
|
||||
case INSERT:
|
||||
type = CURSOR;
|
||||
break;
|
||||
case JUMPER:
|
||||
case SELECT:
|
||||
type = UNDERLINE;
|
||||
break;
|
||||
}
|
||||
set_cursor(cursor.row, cursor.col, type, true);
|
||||
if (editor->hover_active)
|
||||
editor->hover.render(cursor);
|
||||
else if (editor->diagnostics_active)
|
||||
editor->diagnostics.render(cursor);
|
||||
}
|
||||
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->buffer);
|
||||
free(it);
|
||||
}
|
||||
234
src/editor/scroll.cc
Normal file
234
src/editor/scroll.cc
Normal file
@@ -0,0 +1,234 @@
|
||||
#include "editor/editor.h"
|
||||
#include "editor/folds.h"
|
||||
|
||||
void scroll_up(Editor *editor, int32_t number) {
|
||||
if (!editor || number == 0)
|
||||
return;
|
||||
uint32_t numlen =
|
||||
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
len--;
|
||||
uint32_t current_byte_offset = 0;
|
||||
uint32_t col = 0;
|
||||
std::vector<uint32_t> segment_starts;
|
||||
segment_starts.reserve(16);
|
||||
if (current_byte_offset < editor->scroll.col)
|
||||
segment_starts.push_back(0);
|
||||
while (current_byte_offset < editor->scroll.col &&
|
||||
current_byte_offset < len) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
line + current_byte_offset, len - current_byte_offset);
|
||||
int width = display_width(line + current_byte_offset, cluster_len);
|
||||
if (col + width > render_width) {
|
||||
segment_starts.push_back(current_byte_offset);
|
||||
col = 0;
|
||||
}
|
||||
current_byte_offset += cluster_len;
|
||||
col += width;
|
||||
}
|
||||
for (auto it_seg = segment_starts.rbegin(); it_seg != segment_starts.rend();
|
||||
++it_seg) {
|
||||
if (--number == 0) {
|
||||
editor->scroll = {line_index, *it_seg};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
line = prev_line(it, &len);
|
||||
if (!line) {
|
||||
editor->scroll = {0, 0};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
do {
|
||||
line_index--;
|
||||
line = prev_line(it, &len);
|
||||
if (!line) {
|
||||
editor->scroll = {0, 0};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
while (line && line_index > fold->start) {
|
||||
free(line);
|
||||
line = prev_line(it, &len);
|
||||
line_index--;
|
||||
if (!line) {
|
||||
editor->scroll = {0, 0};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (--number == 0) {
|
||||
editor->scroll = {fold->start, 0};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
if (fold->start == 0) {
|
||||
editor->scroll = {0, 0};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
line_index = fold->start - 1;
|
||||
line = prev_line(it, &len);
|
||||
if (!line) {
|
||||
editor->scroll = {0, 0};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
len--;
|
||||
current_byte_offset = 0;
|
||||
col = 0;
|
||||
std::vector<uint32_t> segment_starts;
|
||||
segment_starts.reserve(16);
|
||||
segment_starts.push_back(0);
|
||||
while (current_byte_offset < len) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
line + current_byte_offset, len - current_byte_offset);
|
||||
int width = display_width(line + current_byte_offset, cluster_len);
|
||||
if (col + width > render_width) {
|
||||
segment_starts.push_back(current_byte_offset);
|
||||
col = 0;
|
||||
}
|
||||
current_byte_offset += cluster_len;
|
||||
col += width;
|
||||
}
|
||||
for (auto it_seg = segment_starts.rbegin(); it_seg != segment_starts.rend();
|
||||
++it_seg) {
|
||||
if (--number == 0) {
|
||||
editor->scroll = {line_index, *it_seg};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} while (number > 0);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
|
||||
void scroll_down(Editor *editor, uint32_t number) {
|
||||
if (!editor || number == 0)
|
||||
return;
|
||||
uint32_t numlen =
|
||||
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
const uint32_t max_visual_lines = editor->size.row;
|
||||
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * max_visual_lines);
|
||||
uint32_t q_head = 0;
|
||||
uint32_t q_size = 0;
|
||||
uint32_t visual_seen = 0;
|
||||
bool first_visual_line = true;
|
||||
while (true) {
|
||||
const Fold *fold = fold_for_line(editor->folds, line_index);
|
||||
if (fold) {
|
||||
Coord fold_coord = {fold->start, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
visual_seen++;
|
||||
if (visual_seen >= number + max_visual_lines) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
uint32_t skip_until = fold->end;
|
||||
while (line_index <= skip_until) {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line) {
|
||||
free(scroll_queue);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
line_index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t current_byte_offset = 0;
|
||||
if (first_visual_line) {
|
||||
current_byte_offset += editor->scroll.col;
|
||||
first_visual_line = false;
|
||||
}
|
||||
while (current_byte_offset < line_len ||
|
||||
(line_len == 0 && current_byte_offset == 0)) {
|
||||
Coord coord = {line_index, current_byte_offset};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
visual_seen++;
|
||||
if (visual_seen >= number + max_visual_lines) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
free(scroll_queue);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t col = 0;
|
||||
uint32_t local_render_offset = 0;
|
||||
uint32_t left = line_len - current_byte_offset;
|
||||
while (left > 0 && col < render_width) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
line + current_byte_offset + local_render_offset, left);
|
||||
int width = display_width(
|
||||
line + current_byte_offset + local_render_offset, cluster_len);
|
||||
if (col + width > render_width)
|
||||
break;
|
||||
local_render_offset += cluster_len;
|
||||
left -= cluster_len;
|
||||
col += width;
|
||||
}
|
||||
current_byte_offset += local_render_offset;
|
||||
if (line_len == 0)
|
||||
break;
|
||||
}
|
||||
line_index++;
|
||||
}
|
||||
if (q_size > 0) {
|
||||
uint32_t advance = (q_size > number) ? number : (q_size - 1);
|
||||
editor->scroll = scroll_queue[(q_head + advance) % max_visual_lines];
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
free(scroll_queue);
|
||||
}
|
||||
58
src/editor/selection.cc
Normal file
58
src/editor/selection.cc
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "editor/editor.h"
|
||||
|
||||
char *get_selection(Editor *editor, uint32_t *out_len, Coord *out_start) {
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
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;
|
||||
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);
|
||||
if (!line)
|
||||
return nullptr;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
end = {editor->selection.row, line_len};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (out_start)
|
||||
*out_start = start;
|
||||
uint32_t start_byte =
|
||||
line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
uint32_t end_byte = line_to_byte(editor->root, end.row, nullptr) + end.col;
|
||||
char *text = read(editor->root, start_byte, end_byte - start_byte);
|
||||
if (out_len)
|
||||
*out_len = end_byte - start_byte;
|
||||
return text;
|
||||
}
|
||||
90
src/editor/worker.cc
Normal file
90
src/editor/worker.cc
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "editor/editor.h"
|
||||
#include "ts/ts.h"
|
||||
|
||||
static Highlight HL_UNDERLINE = {0, 0, CF_UNDERLINE, UINT8_MAX - 1};
|
||||
|
||||
void hover_diagnostic(Editor *editor) {
|
||||
std::shared_lock lock(editor->v_mtx);
|
||||
static uint32_t last_line = UINT32_MAX;
|
||||
if (last_line == editor->cursor.row && !editor->warnings_dirty)
|
||||
return;
|
||||
VWarn dummy;
|
||||
dummy.line = editor->cursor.row;
|
||||
editor->warnings_dirty = false;
|
||||
last_line = editor->cursor.row;
|
||||
auto first =
|
||||
std::lower_bound(editor->warnings.begin(), editor->warnings.end(), dummy);
|
||||
auto last =
|
||||
std::upper_bound(editor->warnings.begin(), editor->warnings.end(), dummy);
|
||||
std::vector<VWarn> warnings_at_line(first, last);
|
||||
if (warnings_at_line.size() == 0) {
|
||||
editor->diagnostics_active = false;
|
||||
return;
|
||||
}
|
||||
editor->diagnostics.clear();
|
||||
editor->diagnostics.warnings.swap(warnings_at_line);
|
||||
editor->diagnostics.render_first();
|
||||
editor->diagnostics_active = true;
|
||||
}
|
||||
|
||||
void editor_worker(Editor *editor) {
|
||||
if (!editor || !editor->root)
|
||||
return;
|
||||
if (editor->root->char_count > (1024 * 200))
|
||||
return;
|
||||
if (editor->ts.query_file != "" && !editor->ts.query)
|
||||
editor->ts.query = load_query(editor->ts.query_file.c_str(), &editor->ts);
|
||||
if (editor->ts.parser && editor->ts.query)
|
||||
ts_collect_spans(editor);
|
||||
uint32_t prev_col, next_col;
|
||||
word_boundaries_exclusive(editor, editor->cursor, &prev_col, &next_col);
|
||||
std::unique_lock lock(editor->def_spans.mtx);
|
||||
editor->def_spans.spans.clear();
|
||||
if (next_col - prev_col > 0 && next_col - prev_col < 256 - 4) {
|
||||
std::shared_lock lockk(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);
|
||||
lockk.unlock();
|
||||
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);
|
||||
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);
|
||||
}
|
||||
free(word);
|
||||
}
|
||||
}
|
||||
uint8_t top = 0;
|
||||
static Highlight *hl_s = (Highlight *)calloc(200, sizeof(Highlight));
|
||||
if (!hl_s)
|
||||
exit(ENOMEM);
|
||||
std::shared_lock lockk(editor->knot_mtx);
|
||||
std::vector<std::pair<size_t, size_t>> results =
|
||||
search_rope(editor->root, "(0x|#)[0-9a-fA-F]{6}");
|
||||
for (int i = 0; i < results.size() && top < 200; i++) {
|
||||
Span s;
|
||||
s.start = results[i].first;
|
||||
s.end = results[i].first + results[i].second;
|
||||
char *buf = read(editor->root, s.start, s.end - s.start);
|
||||
int x = buf[0] == '#' ? 1 : 2;
|
||||
uint32_t bg = HEX(buf + x);
|
||||
free(buf);
|
||||
uint8_t r = bg >> 16, g = (bg >> 8) & 0xFF, b = bg & 0xFF;
|
||||
double luminance = 0.299 * r + 0.587 * g + 0.114 * b;
|
||||
uint32_t fg = (luminance > 128) ? 0x010101 : 0xFEFEFE;
|
||||
hl_s[top] = {fg, bg, CF_BOLD, UINT8_MAX};
|
||||
s.hl = &hl_s[top];
|
||||
editor->def_spans.spans.push_back(s);
|
||||
top++;
|
||||
}
|
||||
std::sort(editor->def_spans.spans.begin(), editor->def_spans.spans.end());
|
||||
lock.unlock();
|
||||
lockk.unlock();
|
||||
hover_diagnostic(editor);
|
||||
}
|
||||
Reference in New Issue
Block a user