Basic lsp and precompiler header support
This commit is contained in:
@@ -2,10 +2,10 @@ extern "C" {
|
||||
#include "../libs/libgrapheme/grapheme.h"
|
||||
}
|
||||
#include "../include/editor.h"
|
||||
#include "../include/lsp.h"
|
||||
#include "../include/main.h"
|
||||
#include "../include/ts.h"
|
||||
#include "../include/utils.h"
|
||||
#include <cmath>
|
||||
|
||||
Editor *new_editor(const char *filename, Coord position, Coord size) {
|
||||
Editor *editor = new Editor();
|
||||
@@ -18,23 +18,26 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
|
||||
return nullptr;
|
||||
}
|
||||
editor->filename = filename;
|
||||
editor->uri = path_to_file_uri(filename);
|
||||
editor->position = position;
|
||||
editor->size = size;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->root = load(str, len, optimal_chunk_size(len));
|
||||
free(str);
|
||||
if (len <= (1024 * 128)) {
|
||||
editor->parser = ts_parser_new();
|
||||
Language language = language_for_file(filename);
|
||||
editor->parser = ts_parser_new();
|
||||
editor->language = language.fn();
|
||||
ts_parser_set_language(editor->parser, editor->language);
|
||||
std::string query = get_exe_dir() + "/../grammar/" + language.name + ".scm";
|
||||
editor->query = load_query(query.c_str(), editor);
|
||||
editor->query_file =
|
||||
get_exe_dir() + "/../grammar/" + language.name + ".scm";
|
||||
request_add_to_lsp(language, editor);
|
||||
}
|
||||
return editor;
|
||||
}
|
||||
|
||||
void free_editor(Editor *editor) {
|
||||
remove_from_lsp(editor);
|
||||
ts_parser_delete(editor->parser);
|
||||
if (editor->tree)
|
||||
ts_tree_delete(editor->tree);
|
||||
@@ -143,7 +146,6 @@ void render_editor(Editor *editor) {
|
||||
auto ai_line_span = [&](const VAI &ai,
|
||||
uint32_t n) -> std::pair<const char *, uint32_t> {
|
||||
const char *p = ai.text;
|
||||
uint32_t remaining = ai.len;
|
||||
uint32_t line_no = 0;
|
||||
const char *start = p;
|
||||
uint32_t len = 0;
|
||||
|
||||
@@ -4,7 +4,6 @@ extern "C" {
|
||||
#include "../include/editor.h"
|
||||
#include "../include/main.h"
|
||||
#include "../include/utils.h"
|
||||
#include <cmath>
|
||||
|
||||
uint32_t scan_left(const char *line, uint32_t len, uint32_t off) {
|
||||
if (off > len)
|
||||
@@ -214,328 +213,6 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
|
||||
return {last_line_index, last_col};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void move_line_up(Editor *editor) {
|
||||
if (!editor || !editor->root || editor->cursor.row == 0)
|
||||
return;
|
||||
|
||||
327
src/editor_cursor.cc
Normal file
327
src/editor_cursor.cc
Normal file
@@ -0,0 +1,327 @@
|
||||
extern "C" {
|
||||
#include "../libs/libgrapheme/grapheme.h"
|
||||
}
|
||||
#include "../include/editor.h"
|
||||
#include "../include/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;
|
||||
}
|
||||
@@ -535,6 +535,8 @@ static Highlight HL_UNDERLINE = {0, 0, 1 << 2, 100};
|
||||
void editor_worker(Editor *editor) {
|
||||
if (!editor || !editor->root)
|
||||
return;
|
||||
if (editor->query_file != "" && !editor->query)
|
||||
editor->query = load_query(editor->query_file.c_str(), editor);
|
||||
if (editor->parser && editor->query)
|
||||
ts_collect_spans(editor);
|
||||
uint32_t prev_col, next_col;
|
||||
|
||||
345
src/lsp.cc
Normal file
345
src/lsp.cc
Normal file
@@ -0,0 +1,345 @@
|
||||
#include "../include/lsp.h"
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <sys/poll.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
std::shared_mutex active_lsps_mtx;
|
||||
std::unordered_map<uint8_t, LSPInstance *> active_lsps;
|
||||
|
||||
Queue<LSPOpenRequest> lsp_open_queue;
|
||||
|
||||
std::unordered_map<uint8_t, LSP> lsp_map = {
|
||||
{
|
||||
1,
|
||||
{"clangd",
|
||||
{
|
||||
"clangd",
|
||||
"--background-index",
|
||||
"--clang-tidy",
|
||||
"--completion-style=detailed",
|
||||
"--header-insertion=iwyu",
|
||||
"--log=error",
|
||||
nullptr,
|
||||
}},
|
||||
},
|
||||
};
|
||||
|
||||
static bool init_lsp(LSPInstance *lsp) {
|
||||
log("starting %s\n", lsp->lsp->command);
|
||||
int in_pipe[2];
|
||||
int out_pipe[2];
|
||||
if (pipe(in_pipe) == -1 || pipe(out_pipe) == -1) {
|
||||
perror("pipe");
|
||||
return false;
|
||||
}
|
||||
pid_t pid = fork();
|
||||
if (pid == -1) {
|
||||
perror("fork");
|
||||
return false;
|
||||
}
|
||||
if (pid == 0) {
|
||||
dup2(in_pipe[0], STDIN_FILENO);
|
||||
dup2(out_pipe[1], STDOUT_FILENO);
|
||||
int devnull = open("/dev/null", O_WRONLY);
|
||||
if (devnull >= 0) {
|
||||
dup2(devnull, STDERR_FILENO);
|
||||
close(devnull);
|
||||
}
|
||||
close(in_pipe[1]);
|
||||
close(out_pipe[0]);
|
||||
execvp(lsp->lsp->command, (char *const *)(lsp->lsp->args.data()));
|
||||
perror("execvp");
|
||||
return false;
|
||||
}
|
||||
lsp->pid = pid;
|
||||
lsp->stdin_fd = in_pipe[1];
|
||||
lsp->stdout_fd = out_pipe[0];
|
||||
close(in_pipe[0]);
|
||||
close(out_pipe[1]);
|
||||
return true;
|
||||
}
|
||||
|
||||
LSPInstance *get_or_init_lsp(uint8_t lsp_id) {
|
||||
std::unique_lock lock(active_lsps_mtx);
|
||||
auto it = active_lsps.find(lsp_id);
|
||||
if (it == active_lsps.end()) {
|
||||
auto map_it = lsp_map.find(lsp_id);
|
||||
if (map_it == lsp_map.end())
|
||||
return nullptr;
|
||||
LSPInstance *lsp = new LSPInstance();
|
||||
lsp->lsp = &map_it->second;
|
||||
if (!init_lsp(lsp)) {
|
||||
delete lsp;
|
||||
return nullptr;
|
||||
}
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->method = "initialize";
|
||||
pending->editor = nullptr;
|
||||
pending->callback = [lsp]([[maybe_unused]] Editor *_e,
|
||||
[[maybe_unused]] std::string _m,
|
||||
[[maybe_unused]] json _j) {
|
||||
lsp->initialized = true;
|
||||
json initialized = {{"jsonrpc", "2.0"},
|
||||
{"method", "initialized"},
|
||||
{"params", json::object()}};
|
||||
lsp_send(lsp, initialized, nullptr);
|
||||
};
|
||||
json init_message = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "initialize"},
|
||||
{"params",
|
||||
{{"processId", getpid()},
|
||||
{"rootUri", "file://" + std::filesystem::current_path().string()},
|
||||
{"capabilities", json::object()}}}};
|
||||
lsp_send(lsp, init_message, pending);
|
||||
active_lsps[lsp_id] = lsp;
|
||||
return lsp;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void lsp_send(LSPInstance *lsp, json message, LSPPending *pending) {
|
||||
if (!lsp || lsp->stdin_fd == -1)
|
||||
return;
|
||||
std::unique_lock lock(lsp->mtx);
|
||||
if (pending) {
|
||||
message["id"] = lsp->last_id++;
|
||||
uint32_t id = message["id"].get<uint32_t>();
|
||||
lsp->pending[id] = pending;
|
||||
}
|
||||
lsp->outbox.push(message);
|
||||
}
|
||||
|
||||
void close_lsp(uint8_t lsp_id) {
|
||||
std::shared_lock active_lsps_lock(active_lsps_mtx);
|
||||
auto it = active_lsps.find(lsp_id);
|
||||
if (it == active_lsps.end())
|
||||
return;
|
||||
LSPInstance *lsp = it->second;
|
||||
active_lsps_lock.unlock();
|
||||
LSPPending *shutdown_pending = new LSPPending();
|
||||
shutdown_pending->method = "shutdown";
|
||||
shutdown_pending->callback = [lsp, lsp_id](Editor *, std::string, json) {
|
||||
json exit = {{"jsonrpc", "2.0"}, {"method", "exit"}};
|
||||
lsp_send(lsp, exit, nullptr);
|
||||
};
|
||||
json shutdown = {{"jsonrpc", "2.0"}, {"method", "shutdown"}};
|
||||
lsp_send(lsp, shutdown, shutdown_pending);
|
||||
std::thread t([lsp, lsp_id] {
|
||||
std::this_thread::sleep_for(100ms);
|
||||
std::unique_lock active_lsps_lock(active_lsps_mtx);
|
||||
std::unique_lock lock(lsp->mtx);
|
||||
if (kill(lsp->pid, 0) == 0)
|
||||
kill(lsp->pid, SIGKILL);
|
||||
waitpid(lsp->pid, nullptr, 0);
|
||||
close(lsp->stdin_fd);
|
||||
close(lsp->stdout_fd);
|
||||
while (!lsp->outbox.empty())
|
||||
lsp->outbox.pop();
|
||||
while (!lsp->inbox.empty())
|
||||
lsp->inbox.pop();
|
||||
for (auto &kv : lsp->pending)
|
||||
delete kv.second;
|
||||
delete lsp;
|
||||
active_lsps.erase(lsp_id);
|
||||
});
|
||||
t.detach();
|
||||
}
|
||||
|
||||
static std::optional<json> read_lsp_message(int fd) {
|
||||
std::string header;
|
||||
char c;
|
||||
while (true) {
|
||||
ssize_t n = read(fd, &c, 1);
|
||||
if (n <= 0)
|
||||
return std::nullopt;
|
||||
header.push_back(c);
|
||||
if (header.size() >= 4 && header.substr(header.size() - 4) == "\r\n\r\n")
|
||||
break;
|
||||
}
|
||||
size_t pos = header.find("Content-Length:");
|
||||
if (pos == std::string::npos)
|
||||
return std::nullopt;
|
||||
pos += strlen("Content-Length:");
|
||||
while (pos < header.size() && std::isspace(header[pos]))
|
||||
pos++;
|
||||
size_t end = pos;
|
||||
while (end < header.size() && std::isdigit(header[end]))
|
||||
end++;
|
||||
size_t len = std::stoul(header.substr(pos, end - pos));
|
||||
std::string body(len, '\0');
|
||||
size_t got = 0;
|
||||
while (got < len) {
|
||||
ssize_t n = read(fd, &body[got], len - got);
|
||||
if (n <= 0)
|
||||
return std::nullopt;
|
||||
got += n;
|
||||
}
|
||||
return json::parse(body);
|
||||
}
|
||||
|
||||
static Editor *editor_for_uri(LSPInstance *lsp, std::string uri) {
|
||||
if (uri.empty())
|
||||
return nullptr;
|
||||
for (auto &editor : lsp->editors)
|
||||
if (editor->uri == uri)
|
||||
return editor;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void lsp_worker() {
|
||||
LSPOpenRequest request;
|
||||
while (lsp_open_queue.pop(request))
|
||||
add_to_lsp(request.language, request.editor);
|
||||
std::shared_lock active_lsps_lock(active_lsps_mtx);
|
||||
for (auto &kv : active_lsps) {
|
||||
LSPInstance *lsp = kv.second;
|
||||
std::unique_lock lock(lsp->mtx);
|
||||
while (!lsp->outbox.empty()) {
|
||||
json message;
|
||||
message = lsp->outbox.front();
|
||||
if (!lsp->initialized) {
|
||||
std::string m = message.value("method", "");
|
||||
if (m != "initialize")
|
||||
break;
|
||||
}
|
||||
lsp->outbox.pop(message);
|
||||
std::string payload = message.dump();
|
||||
std::string header =
|
||||
"Content-Length: " + std::to_string(payload.size()) + "\r\n\r\n";
|
||||
std::string out = header + payload;
|
||||
const char *ptr = out.data();
|
||||
size_t remaining = out.size();
|
||||
while (remaining > 0) {
|
||||
ssize_t written = write(lsp->stdin_fd, ptr, remaining);
|
||||
if (written == 0)
|
||||
break;
|
||||
else if (written == -1) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
perror("write");
|
||||
break;
|
||||
} else {
|
||||
ptr += written;
|
||||
remaining -= written;
|
||||
}
|
||||
}
|
||||
}
|
||||
pollfd pfd{lsp->stdout_fd, POLLIN, 0};
|
||||
while (poll(&pfd, 1, 0) > 0) {
|
||||
auto msg = read_lsp_message(lsp->stdout_fd);
|
||||
if (!msg)
|
||||
break;
|
||||
if (msg->contains("id")) {
|
||||
uint32_t id = msg->at("id").get<uint32_t>();
|
||||
auto it = lsp->pending.find(id);
|
||||
if (it != lsp->pending.end()) {
|
||||
LSPPending *pend = it->second;
|
||||
lock.unlock();
|
||||
if (pend->callback)
|
||||
pend->callback(pend->editor, pend->method, *msg);
|
||||
delete pend;
|
||||
lock.lock();
|
||||
lsp->pending.erase(it);
|
||||
}
|
||||
} else if (msg->contains("method")) {
|
||||
std::string uri;
|
||||
if (msg->contains("params")) {
|
||||
auto &p = (*msg)["params"];
|
||||
if (p.contains("textDocument") && p["textDocument"].contains("uri"))
|
||||
uri = p["textDocument"]["uri"].get<std::string>();
|
||||
else if (p.contains("uri"))
|
||||
uri = p["uri"].get<std::string>();
|
||||
}
|
||||
Editor *ed = editor_for_uri(lsp, uri);
|
||||
lock.unlock();
|
||||
if (ed)
|
||||
// editor_lsp_handle(ed, *msg)
|
||||
;
|
||||
else
|
||||
lsp_handle(lsp, *msg);
|
||||
lock.lock();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void request_add_to_lsp(Language language, Editor *editor) {
|
||||
lsp_open_queue.push({language, editor});
|
||||
}
|
||||
|
||||
void add_to_lsp(Language language, Editor *editor) {
|
||||
LSPInstance *lsp = get_or_init_lsp(language.lsp_id);
|
||||
if (!lsp)
|
||||
return;
|
||||
std::unique_lock lock(lsp->mtx);
|
||||
if (editor->lsp == lsp)
|
||||
return;
|
||||
lsp->editors.push_back(editor);
|
||||
lock.unlock();
|
||||
std::unique_lock lock2(editor->lsp_mtx);
|
||||
editor->lsp = lsp;
|
||||
lock2.unlock();
|
||||
std::unique_lock lock3(editor->knot_mtx);
|
||||
char *buf = read(editor->root, 0, editor->root->char_count);
|
||||
std::string text(buf);
|
||||
free(buf);
|
||||
json message = {{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/didOpen"},
|
||||
{"params",
|
||||
{{"textDocument",
|
||||
{{"uri", editor->uri},
|
||||
{"languageId", language.name},
|
||||
{"version", 1},
|
||||
{"text", text}}}}}};
|
||||
lock3.unlock();
|
||||
lsp_send(lsp, message, nullptr);
|
||||
}
|
||||
|
||||
static uint8_t find_lsp_id(LSPInstance *needle) {
|
||||
for (const auto &[id, lsp] : active_lsps)
|
||||
if (lsp == needle)
|
||||
return id;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void remove_from_lsp(Editor *editor) {
|
||||
auto lsp = editor->lsp;
|
||||
if (!lsp)
|
||||
return;
|
||||
std::unique_lock lock1(lsp->mtx);
|
||||
lsp->editors.erase(
|
||||
std::remove(lsp->editors.begin(), lsp->editors.end(), editor),
|
||||
lsp->editors.end());
|
||||
lock1.unlock();
|
||||
std::unique_lock lock2(editor->lsp_mtx);
|
||||
editor->lsp = nullptr;
|
||||
lock2.unlock();
|
||||
json message = {{"jsonrpc", "2.0"},
|
||||
{"method", "textDocument/didClose"},
|
||||
{"params", {{"textDocument", {{"uri", editor->uri}}}}}};
|
||||
lsp_send(lsp, message, nullptr);
|
||||
uint8_t lsp_id = find_lsp_id(lsp);
|
||||
if (lsp_id && lsp->editors.empty())
|
||||
close_lsp(lsp_id);
|
||||
}
|
||||
|
||||
void lsp_handle([[maybe_unused]] LSPInstance *lsp, json message) {
|
||||
std::string method = message.value("method", "");
|
||||
if (method == "window/showMessage") {
|
||||
if (message.contains("params")) {
|
||||
auto &p = message["params"];
|
||||
if (p.contains("message"))
|
||||
log("%s\n", p["message"].get<std::string>().c_str());
|
||||
}
|
||||
} else if (method == "window/logMessage") {
|
||||
if (message.contains("params")) {
|
||||
auto &p = message["params"];
|
||||
if (p.contains("message"))
|
||||
log("%s\n", p["message"].get<std::string>().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
26
src/main.cc
26
src/main.cc
@@ -1,5 +1,6 @@
|
||||
#include "../include/main.h"
|
||||
#include "../include/editor.h"
|
||||
#include "../include/lsp.h"
|
||||
#include "../include/ts.h"
|
||||
#include "../include/ui.h"
|
||||
#include <atomic>
|
||||
@@ -7,8 +8,6 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <thread>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
std::atomic<bool> running{true};
|
||||
Queue<KeyEvent> event_queue;
|
||||
std::vector<Editor *> editors;
|
||||
@@ -18,7 +17,12 @@ uint8_t mode = NORMAL;
|
||||
|
||||
void background_worker() {
|
||||
while (running)
|
||||
throttle(16ms, editor_worker, editors[current_editor]);
|
||||
throttle(8ms, editor_worker, editors[current_editor]);
|
||||
}
|
||||
|
||||
void background_lsp() {
|
||||
while (running)
|
||||
throttle(8ms, lsp_worker);
|
||||
}
|
||||
|
||||
void input_listener() {
|
||||
@@ -71,6 +75,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
std::thread input_thread(input_listener);
|
||||
std::thread work_thread(background_worker);
|
||||
std::thread lsp_thread(background_lsp);
|
||||
|
||||
while (running) {
|
||||
KeyEvent event;
|
||||
@@ -98,9 +103,22 @@ int main(int argc, char *argv[]) {
|
||||
if (work_thread.joinable())
|
||||
work_thread.join();
|
||||
|
||||
if (lsp_thread.joinable())
|
||||
lsp_thread.join();
|
||||
|
||||
end_screen();
|
||||
|
||||
free_editor(editor);
|
||||
for (auto editor : editors)
|
||||
free_editor(editor);
|
||||
|
||||
while (true) {
|
||||
std::unique_lock lk(active_lsps_mtx);
|
||||
if (active_lsps.empty())
|
||||
break;
|
||||
lk.unlock();
|
||||
throttle(16ms, lsp_worker);
|
||||
}
|
||||
|
||||
clear_regex_cache();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// includes
|
||||
#include "../include/ui.h"
|
||||
|
||||
uint32_t rows, cols;
|
||||
|
||||
45
src/utils.cc
45
src/utils.cc
@@ -4,11 +4,13 @@ extern "C" {
|
||||
}
|
||||
#include "../include/utils.h"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <limits.h>
|
||||
#include <magic.h>
|
||||
@@ -17,6 +19,29 @@ extern "C" {
|
||||
#include <unistd.h>
|
||||
#include <unordered_map>
|
||||
|
||||
static std::string percent_encode(const std::string &s) {
|
||||
static const char *hex = "0123456789ABCDEF";
|
||||
std::string out;
|
||||
for (unsigned char c : s) {
|
||||
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~' ||
|
||||
c == '/') {
|
||||
out.push_back(c);
|
||||
} else {
|
||||
out.push_back('%');
|
||||
out.push_back(hex[c >> 4]);
|
||||
out.push_back(hex[c & 0xF]);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string path_to_file_uri(const std::string &path_str) {
|
||||
namespace fs = std::filesystem;
|
||||
fs::path p = fs::weakly_canonical(fs::absolute(fs::path(path_str)));
|
||||
std::string generic = p.generic_string();
|
||||
return "file://" + percent_encode(generic);
|
||||
}
|
||||
|
||||
uint64_t fnv1a_64(const char *s, size_t len) {
|
||||
uint64_t hash = 1469598103934665603ull;
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
@@ -234,14 +259,14 @@ char *detect_file_type(const char *filename) {
|
||||
static const std::unordered_map<std::string, Language> ext_map = {
|
||||
{"sh", {"bash", tree_sitter_bash}},
|
||||
{"bash", {"bash", tree_sitter_bash}},
|
||||
{"c", {"c", tree_sitter_c}},
|
||||
{"cpp", {"cpp", tree_sitter_cpp}},
|
||||
{"cxx", {"cpp", tree_sitter_cpp}},
|
||||
{"cc", {"cpp", tree_sitter_cpp}},
|
||||
{"hpp", {"cpp", tree_sitter_cpp}},
|
||||
{"hh", {"cpp", tree_sitter_cpp}},
|
||||
{"hxx", {"cpp", tree_sitter_cpp}},
|
||||
{"h", {"cpp", tree_sitter_cpp}},
|
||||
{"c", {"c", tree_sitter_c, 1}},
|
||||
{"cpp", {"cpp", tree_sitter_cpp, 1}},
|
||||
{"cxx", {"cpp", tree_sitter_cpp, 1}},
|
||||
{"cc", {"cpp", tree_sitter_cpp, 1}},
|
||||
{"hpp", {"cpp", tree_sitter_cpp, 1}},
|
||||
{"hh", {"cpp", tree_sitter_cpp, 1}},
|
||||
{"hxx", {"cpp", tree_sitter_cpp, 1}},
|
||||
{"h", {"cpp", tree_sitter_cpp, 1}},
|
||||
{"css", {"css", tree_sitter_css}},
|
||||
{"fish", {"fish", tree_sitter_fish}},
|
||||
{"go", {"go", tree_sitter_go}},
|
||||
@@ -258,8 +283,8 @@ static const std::unordered_map<std::string, Language> ext_map = {
|
||||
};
|
||||
|
||||
static const std::unordered_map<std::string, Language> mime_map = {
|
||||
{"text/x-c", {"c", tree_sitter_c}},
|
||||
{"text/x-c++", {"cpp", tree_sitter_cpp}},
|
||||
{"text/x-c", {"c", tree_sitter_c, 1}},
|
||||
{"text/x-c++", {"cpp", tree_sitter_cpp, 1}},
|
||||
{"text/x-shellscript", {"bash", tree_sitter_bash}},
|
||||
{"application/json", {"json", tree_sitter_json}},
|
||||
{"text/javascript", {"javascript", tree_sitter_javascript}},
|
||||
|
||||
Reference in New Issue
Block a user