Random stuff to do with scripting
This commit is contained in:
@@ -12,15 +12,14 @@ inline static std::string completion_prefix(Editor *editor) {
|
||||
if (hook.row != cur.row || cur.col < hook.col)
|
||||
return "";
|
||||
LineIterator *it = begin_l_iter(editor->root, hook.row);
|
||||
char *line = next_line(it, nullptr);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return "";
|
||||
}
|
||||
uint32_t start = utf16_offset_to_utf8(line, hook.col);
|
||||
uint32_t end = editor->cursor.col;
|
||||
std::string prefix(line + start, end - start);
|
||||
std::string prefix(line + hook.col, cur.col - hook.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return prefix;
|
||||
@@ -212,13 +211,14 @@ void completion_request(Editor *editor) {
|
||||
};
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
LineIterator *it = begin_l_iter(editor->root, hook.row);
|
||||
char *line = next_line(it, nullptr);
|
||||
uint32_t length;
|
||||
char *line = next_line(it, &length);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t col = utf8_byte_offset_to_utf16(line, editor->cursor.col);
|
||||
uint32_t col = utf8_offset_to_utf16(line, length, editor->cursor.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
lock.unlock();
|
||||
|
||||
@@ -16,17 +16,18 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||
bool do_lsp = (editor->lsp != nullptr);
|
||||
if (do_lsp) {
|
||||
LineIterator *it = begin_l_iter(editor->root, point.row);
|
||||
char *line = next_line(it, nullptr);
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
int utf16_start = 0;
|
||||
if (line)
|
||||
utf16_start = utf8_byte_offset_to_utf16(line, point.col);
|
||||
utf16_start = utf8_offset_to_utf16(line, len, point.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
it = begin_l_iter(editor->root, pos.row);
|
||||
line = next_line(it, nullptr);
|
||||
line = next_line(it, &len);
|
||||
int utf16_end = 0;
|
||||
if (line)
|
||||
utf16_end = utf8_byte_offset_to_utf16(line, pos.col);
|
||||
utf16_end = utf8_offset_to_utf16(line, len, pos.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
lsp_range = {{"start", {{"line", point.row}, {"character", utf16_start}}},
|
||||
@@ -88,17 +89,18 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||
bool do_lsp = (editor->lsp != nullptr);
|
||||
if (do_lsp) {
|
||||
LineIterator *it = begin_l_iter(editor->root, pos.row);
|
||||
char *line = next_line(it, nullptr);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
int utf16_start = 0;
|
||||
if (line)
|
||||
utf16_start = utf8_byte_offset_to_utf16(line, pos.col);
|
||||
utf16_start = utf8_offset_to_utf16(line, line_len, pos.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
it = begin_l_iter(editor->root, point.row);
|
||||
line = next_line(it, nullptr);
|
||||
line = next_line(it, &line_len);
|
||||
int utf16_end = 0;
|
||||
if (line)
|
||||
utf16_end = utf8_byte_offset_to_utf16(line, point.col);
|
||||
utf16_end = utf8_offset_to_utf16(line, line_len, point.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
lsp_range = {{"start", {{"line", pos.row}, {"character", utf16_start}}},
|
||||
@@ -164,6 +166,14 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
|
||||
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
||||
editor->cursor = {new_row, new_col};
|
||||
}
|
||||
LineIterator *it = begin_l_iter(editor->root, pos.row);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
int utf16_col = 0;
|
||||
if (line)
|
||||
utf16_col = utf8_offset_to_utf16(line, line_len, pos.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
lock_1.unlock();
|
||||
std::unique_lock lock_2(editor->knot_mtx);
|
||||
editor->root = insert(editor->root, byte_pos, data, len);
|
||||
@@ -177,15 +187,6 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
|
||||
editor->parser->edit(pos.row, pos.row, rows);
|
||||
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"},
|
||||
@@ -222,17 +223,18 @@ void edit_replace(Editor *editor, Coord start, Coord end, const char *text,
|
||||
line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
uint32_t end_byte = line_to_byte(editor->root, end.row, nullptr) + end.col;
|
||||
LineIterator *it = begin_l_iter(editor->root, start.row);
|
||||
char *line = next_line(it, nullptr);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
int utf16_start = 0;
|
||||
if (line)
|
||||
utf16_start = utf8_byte_offset_to_utf16(line, start.col);
|
||||
utf16_start = utf8_offset_to_utf16(line, line_len, start.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
it = begin_l_iter(editor->root, end.row);
|
||||
line = next_line(it, nullptr);
|
||||
line = next_line(it, &line_len);
|
||||
int utf16_end = 0;
|
||||
if (line)
|
||||
utf16_end = utf8_byte_offset_to_utf16(line, end.col);
|
||||
utf16_end = utf8_offset_to_utf16(line, line_len, end.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
if (start_byte != end_byte)
|
||||
@@ -243,10 +245,8 @@ void edit_replace(Editor *editor, Coord start, Coord end, const char *text,
|
||||
for (uint32_t i = 0; i < len; i++)
|
||||
if (text[i] == '\n')
|
||||
rows++;
|
||||
if (editor->parser) {
|
||||
editor->parser->edit(start.row, end.row - 1, 0);
|
||||
editor->parser->edit(start.row, start.row, rows);
|
||||
}
|
||||
if (editor->parser)
|
||||
editor->parser->edit(start.row, end.row - 1, rows);
|
||||
if (editor->lsp) {
|
||||
if (editor->lsp->incremental_sync) {
|
||||
json message = {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "editor/editor.h"
|
||||
#include "editor/decl.h"
|
||||
#include "lsp/lsp.h"
|
||||
#include "syntax/langs.h"
|
||||
#include "utils/utils.h"
|
||||
#include <shared_mutex>
|
||||
|
||||
Editor *new_editor(const char *filename_arg, Coord position, Coord size) {
|
||||
Editor *editor = new Editor();
|
||||
@@ -29,7 +29,7 @@ Editor *new_editor(const char *filename_arg, Coord position, Coord size) {
|
||||
editor->root = load(str, len, optimal_chunk_size(len));
|
||||
free(str);
|
||||
editor->lang = language_for_file(filename.c_str());
|
||||
if (editor->lang.name != "unknown")
|
||||
if (parsers.find(editor->lang.name) != parsers.end())
|
||||
editor->parser = new Parser(editor, editor->lang.name, size.row + 5);
|
||||
if (editor->lang.name == "css" || editor->lang.name == "html" ||
|
||||
editor->lang.name == "javascript" || editor->lang.name == "markdown" ||
|
||||
|
||||
@@ -1,147 +1,14 @@
|
||||
#include "editor/editor.h"
|
||||
#include "lsp/lsp.h"
|
||||
#include "editor/helpers.h"
|
||||
#include "io/sysio.h"
|
||||
#include "main.h"
|
||||
#include "utils/utils.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;
|
||||
uint8_t old_mode = mode;
|
||||
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, 4);
|
||||
ensure_cursor(editor);
|
||||
break;
|
||||
case SCROLL_DOWN:
|
||||
scroll_down(editor, 4);
|
||||
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;
|
||||
}
|
||||
}
|
||||
handle_mouse(editor, event);
|
||||
if (event.key_type == KEY_SPECIAL) {
|
||||
switch (event.special_modifier) {
|
||||
case 0:
|
||||
@@ -161,9 +28,6 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
}
|
||||
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);
|
||||
@@ -172,18 +36,9 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
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;
|
||||
cursor_prev_word(editor);
|
||||
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};
|
||||
cursor_next_word(editor);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@@ -210,27 +65,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
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;
|
||||
}
|
||||
select_all(editor);
|
||||
break;
|
||||
case CTRL('h'):
|
||||
editor->hover.scroll(-1);
|
||||
@@ -241,68 +76,15 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
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);
|
||||
}
|
||||
fetch_lsp_hover(editor);
|
||||
break;
|
||||
case 'a':
|
||||
case 'a': {
|
||||
mode = INSERT;
|
||||
Coord start = editor->cursor;
|
||||
cursor_right(editor, 1);
|
||||
if (start.row != editor->cursor.row)
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
} break;
|
||||
case 'i':
|
||||
mode = INSERT;
|
||||
break;
|
||||
@@ -315,11 +97,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
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;
|
||||
}
|
||||
clear_hooks_at_line(editor, editor->cursor.row);
|
||||
break;
|
||||
case 's':
|
||||
case 'v':
|
||||
@@ -355,29 +133,18 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
ensure_cursor(editor);
|
||||
break;
|
||||
case '>':
|
||||
case '.': {
|
||||
uint32_t delta = editor->indents.indent_line(editor->cursor.row);
|
||||
editor->cursor.col = start.col + delta;
|
||||
editor->cursor.row = start.row;
|
||||
} break;
|
||||
case '.':
|
||||
indent_current_line(editor);
|
||||
break;
|
||||
case '<':
|
||||
case ',': {
|
||||
uint32_t delta = editor->indents.dedent_line(editor->cursor.row);
|
||||
editor->cursor.col = MAX((int64_t)start.col - delta, 0);
|
||||
editor->cursor.row = start.row;
|
||||
} break;
|
||||
case ',':
|
||||
dedent_current_line(editor);
|
||||
break;
|
||||
case CTRL('s'):
|
||||
save_file(editor);
|
||||
break;
|
||||
case 'p':
|
||||
uint32_t len;
|
||||
char *text = get_from_clipboard(&len);
|
||||
if (text) {
|
||||
edit_insert(editor, editor->cursor, text, len);
|
||||
uint32_t grapheme_len = count_clusters(text, len, 0, len);
|
||||
cursor_right(editor, grapheme_len);
|
||||
free(text);
|
||||
}
|
||||
paste(editor);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -391,156 +158,13 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
|
||||
editor->indents.insert_new_line(editor->cursor);
|
||||
} 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);
|
||||
delete_prev_word(editor);
|
||||
} 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);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
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);
|
||||
}
|
||||
if (editor->lsp && editor->lsp->allow_formatting_on_type) {
|
||||
for (char ch : editor->lsp->format_chars) {
|
||||
if (ch == c) {
|
||||
LineIterator *it =
|
||||
begin_l_iter(editor->root, editor->cursor.row);
|
||||
if (!it)
|
||||
return;
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t col =
|
||||
utf8_byte_offset_to_utf16(line, editor->cursor.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
int version = editor->lsp_version;
|
||||
json message = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/onTypeFormatting"},
|
||||
{"params",
|
||||
{{"textDocument", {{"uri", editor->uri}}},
|
||||
{"position",
|
||||
{{"line", editor->cursor.row}, {"character", col}}},
|
||||
{"ch", std::string(1, c)},
|
||||
{"options",
|
||||
{{"tabSize", 2},
|
||||
{"insertSpaces", true},
|
||||
{"trimTrailingWhitespace", true},
|
||||
{"trimFinalNewlines", true}}}}}};
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->editor = editor;
|
||||
pending->method = "textDocument/onTypeFormatting";
|
||||
pending->callback = [version](Editor *editor, std::string,
|
||||
json message) {
|
||||
if (version != editor->lsp_version)
|
||||
return;
|
||||
auto &edits = message["result"];
|
||||
if (edits.is_array()) {
|
||||
std::vector<TextEdit> t_edits;
|
||||
t_edits.reserve(edits.size());
|
||||
for (auto &edit : edits) {
|
||||
TextEdit t_edit;
|
||||
t_edit.text = edit.value("newText", "");
|
||||
t_edit.start.row = edit["range"]["start"]["line"];
|
||||
t_edit.start.col = edit["range"]["start"]["character"];
|
||||
t_edit.end.row = edit["range"]["end"]["line"];
|
||||
t_edit.end.col = edit["range"]["end"]["character"];
|
||||
utf8_normalize_edit(editor, &t_edit);
|
||||
t_edits.push_back(t_edit);
|
||||
}
|
||||
apply_lsp_edits(editor, t_edits, false);
|
||||
ensure_scroll(editor);
|
||||
}
|
||||
};
|
||||
lsp_send(editor->lsp, message, pending);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
insert_char(editor, event.c[0]);
|
||||
} 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);
|
||||
}
|
||||
backspace_edit(editor);
|
||||
} 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);
|
||||
normal_mode(editor);
|
||||
}
|
||||
} else if (event.len > 1) {
|
||||
edit_insert(editor, editor->cursor, event.c, event.len);
|
||||
@@ -553,29 +177,17 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
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);
|
||||
delete_next_word(editor);
|
||||
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);
|
||||
}
|
||||
insert_str(editor, event.c, event.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 0x1B:
|
||||
case 's':
|
||||
@@ -584,41 +196,15 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case 'y':
|
||||
text = get_selection(editor, &len, nullptr);
|
||||
copy_to_clipboard(text, len);
|
||||
free(text);
|
||||
editor->selection_active = false;
|
||||
copy(editor);
|
||||
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;
|
||||
cut(editor);
|
||||
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;
|
||||
paste(editor);
|
||||
mode = NORMAL;
|
||||
break;
|
||||
}
|
||||
|
||||
491
src/editor/helpers.cc
Normal file
491
src/editor/helpers.cc
Normal file
@@ -0,0 +1,491 @@
|
||||
#include "editor/helpers.h"
|
||||
#include "editor/editor.h"
|
||||
#include "io/sysio.h"
|
||||
#include "lsp/lsp.h"
|
||||
#include "main.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
void cut(Editor *editor) {
|
||||
if (mode != SELECT)
|
||||
return;
|
||||
Coord start;
|
||||
uint32_t len;
|
||||
char *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;
|
||||
}
|
||||
|
||||
void copy(Editor *editor) {
|
||||
if (mode != SELECT)
|
||||
return;
|
||||
uint32_t len;
|
||||
char *text = get_selection(editor, &len, nullptr);
|
||||
copy_to_clipboard(text, len);
|
||||
free(text);
|
||||
editor->selection_active = false;
|
||||
}
|
||||
|
||||
void paste(Editor *editor) {
|
||||
uint32_t len;
|
||||
if (mode == NORMAL) {
|
||||
char *text = get_from_clipboard(&len);
|
||||
if (text) {
|
||||
insert_str(editor, text, len);
|
||||
free(text);
|
||||
}
|
||||
} else if (mode == SELECT) {
|
||||
char *text = get_from_clipboard(&len);
|
||||
if (text) {
|
||||
Coord start, end;
|
||||
selection_bounds(editor, &start, &end);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void insert_str(Editor *editor, char *c, uint32_t len) {
|
||||
if (c) {
|
||||
edit_insert(editor, editor->cursor, c, len);
|
||||
uint32_t grapheme_len = count_clusters(c, len, 0, len);
|
||||
cursor_right(editor, grapheme_len);
|
||||
}
|
||||
}
|
||||
|
||||
void indent_current_line(Editor *editor) {
|
||||
Coord start = editor->cursor;
|
||||
uint32_t delta = editor->indents.indent_line(editor->cursor.row);
|
||||
editor->cursor.col = start.col + delta;
|
||||
editor->cursor.row = start.row;
|
||||
}
|
||||
|
||||
void dedent_current_line(Editor *editor) {
|
||||
Coord start = editor->cursor;
|
||||
uint32_t delta = editor->indents.dedent_line(editor->cursor.row);
|
||||
editor->cursor.col = MAX((int64_t)start.col - delta, 0);
|
||||
editor->cursor.row = start.row;
|
||||
}
|
||||
|
||||
void insert_char(Editor *editor, char c) {
|
||||
uint32_t col = editor->cursor.col;
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
if (editor->lsp && editor->lsp->allow_formatting_on_type) {
|
||||
for (char ch : editor->lsp->format_chars) {
|
||||
if (ch == c) {
|
||||
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;
|
||||
}
|
||||
uint32_t col = utf8_offset_to_utf16(line, len, editor->cursor.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
int version = editor->lsp_version;
|
||||
json message = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/onTypeFormatting"},
|
||||
{"params",
|
||||
{{"textDocument", {{"uri", editor->uri}}},
|
||||
{"position",
|
||||
{{"line", editor->cursor.row}, {"character", col}}},
|
||||
{"ch", std::string(1, c)},
|
||||
{"options",
|
||||
{{"tabSize", 2},
|
||||
{"insertSpaces", true},
|
||||
{"trimTrailingWhitespace", true},
|
||||
{"trimFinalNewlines", true}}}}}};
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->editor = editor;
|
||||
pending->method = "textDocument/onTypeFormatting";
|
||||
pending->callback = [version](Editor *editor, std::string,
|
||||
json message) {
|
||||
if (version != editor->lsp_version)
|
||||
return;
|
||||
auto &edits = message["result"];
|
||||
if (edits.is_array()) {
|
||||
std::vector<TextEdit> t_edits;
|
||||
t_edits.reserve(edits.size());
|
||||
for (auto &edit : edits) {
|
||||
TextEdit t_edit;
|
||||
t_edit.text = edit.value("newText", "");
|
||||
t_edit.start.row = edit["range"]["start"]["line"];
|
||||
t_edit.start.col = edit["range"]["start"]["character"];
|
||||
t_edit.end.row = edit["range"]["end"]["line"];
|
||||
t_edit.end.col = edit["range"]["end"]["character"];
|
||||
utf8_normalize_edit(editor, &t_edit);
|
||||
t_edits.push_back(t_edit);
|
||||
}
|
||||
apply_lsp_edits(editor, t_edits, false);
|
||||
ensure_scroll(editor);
|
||||
}
|
||||
};
|
||||
lsp_send(editor->lsp, message, pending);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void normal_mode(Editor *editor) {
|
||||
Coord prev_pos = editor->cursor;
|
||||
mode = NORMAL;
|
||||
cursor_left(editor, 1);
|
||||
if (prev_pos.row != editor->cursor.row)
|
||||
cursor_right(editor, 1);
|
||||
}
|
||||
|
||||
void backspace_edit(Editor *editor) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void delete_prev_word(Editor *editor) {
|
||||
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);
|
||||
}
|
||||
|
||||
void delete_next_word(Editor *editor) {
|
||||
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);
|
||||
}
|
||||
|
||||
void clear_hooks_at_line(Editor *editor, uint32_t line) {
|
||||
for (uint8_t i = 0; i < 94; i++)
|
||||
if (editor->hooks[i] == line + 1) {
|
||||
editor->hooks[i] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void cursor_prev_word(Editor *editor) {
|
||||
uint32_t prev_col;
|
||||
word_boundaries(editor, editor->cursor, &prev_col, nullptr, nullptr, nullptr);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (prev_col == editor->cursor.col)
|
||||
cursor_left(editor, 1);
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, prev_col};
|
||||
}
|
||||
|
||||
void cursor_next_word(Editor *editor) {
|
||||
uint32_t next_col;
|
||||
word_boundaries(editor, editor->cursor, nullptr, &next_col, nullptr, nullptr);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
if (next_col == editor->cursor.col)
|
||||
cursor_right(editor, 1);
|
||||
else
|
||||
editor->cursor = {editor->cursor.row, next_col};
|
||||
}
|
||||
|
||||
void select_all(Editor *editor) {
|
||||
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)
|
||||
return;
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
return;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void fetch_lsp_hover(Editor *editor) {
|
||||
if (editor->lsp && editor->lsp->allow_hover) {
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t col = utf8_offset_to_utf16(line, line_len, 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);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_mouse(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};
|
||||
uint8_t old_mode = mode;
|
||||
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, 4);
|
||||
ensure_cursor(editor);
|
||||
break;
|
||||
case SCROLL_DOWN:
|
||||
scroll_down(editor, 4);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -323,13 +323,14 @@ void IndentationEngine::insert_new_line(Coord cursor) {
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
if (!it)
|
||||
return;
|
||||
char *line = next_line(it, nullptr);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t col = utf8_byte_offset_to_utf16(line, editor->cursor.col);
|
||||
uint32_t col = utf8_offset_to_utf16(line, line_len, editor->cursor.col);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
int version = editor->lsp_version;
|
||||
|
||||
@@ -60,10 +60,10 @@ void editor_lsp_handle(Editor *editor, json msg) {
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
lock.unlock();
|
||||
w.start = utf16_offset_to_utf8(line, w.start);
|
||||
w.start = utf16_offset_to_utf8(line, len, w.start);
|
||||
uint32_t end = d["range"]["end"]["character"];
|
||||
if (d["range"]["end"]["line"] == w.line)
|
||||
w.end = utf16_offset_to_utf8(line, end);
|
||||
w.end = utf16_offset_to_utf8(line, len, end);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
std::string text = trim(d["message"].get<std::string>());
|
||||
|
||||
@@ -107,10 +107,12 @@ void render_editor(Editor *editor) {
|
||||
while (rendered_rows < editor->size.row) {
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (line_data)
|
||||
line_data = editor->parser->line_tree.next();
|
||||
else
|
||||
line_data = editor->parser->line_tree.start_iter(line_index);
|
||||
if (editor->parser) {
|
||||
if (line_data)
|
||||
line_data = editor->parser->line_tree.next();
|
||||
else
|
||||
line_data = editor->parser->line_tree.start_iter(line_index);
|
||||
}
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
@@ -186,7 +188,7 @@ void render_editor(Editor *editor) {
|
||||
: 0);
|
||||
if (editor->selection_active && absolute_byte_pos >= sel_start &&
|
||||
absolute_byte_pos < sel_end)
|
||||
bg = 0x555555;
|
||||
bg = bg | 0x555555;
|
||||
uint32_t u_color = 0;
|
||||
for (const auto &w : line_warnings) {
|
||||
if (w.start <= current_byte_offset + local_render_offset &&
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
#include "editor/editor.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
char *get_selection(Editor *editor, uint32_t *out_len, Coord *out_start) {
|
||||
void selection_bounds(Editor *editor, Coord *out_start, Coord *out_end) {
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
uint32_t prev_col, next_col;
|
||||
uint32_t prev_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,
|
||||
word_boundaries(editor, editor->selection, &prev_col, nullptr, nullptr,
|
||||
nullptr);
|
||||
start = {editor->selection.row, prev_col};
|
||||
end = editor->cursor;
|
||||
@@ -23,13 +24,65 @@ char *get_selection(Editor *editor, uint32_t *out_len, Coord *out_start) {
|
||||
}
|
||||
} else {
|
||||
start = editor->cursor;
|
||||
uint32_t prev_col, next_col, line_len;
|
||||
uint32_t 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,
|
||||
word_boundaries(editor, editor->selection, nullptr, &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--;
|
||||
end = {editor->selection.row, line_len};
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (out_start)
|
||||
*out_start = start;
|
||||
if (out_end)
|
||||
*out_end = end;
|
||||
}
|
||||
|
||||
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;
|
||||
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, nullptr, 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 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, nullptr, &next_col, nullptr,
|
||||
nullptr);
|
||||
end = {editor->selection.row, next_col};
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user