Fix parsing bugs and add better indentation support
This commit is contained in:
@@ -78,8 +78,7 @@ void completion_request(Editor *editor) {
|
||||
editor->completion.version = editor->lsp_version;
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->editor = editor;
|
||||
pending->method = "textDocument/completion";
|
||||
pending->callback = [](Editor *editor, std::string, json message) {
|
||||
pending->callback = [](Editor *editor, const json &message) {
|
||||
auto &session = editor->completion;
|
||||
std::unique_lock lock(session.mtx);
|
||||
std::vector<json> items_json;
|
||||
@@ -359,8 +358,7 @@ void completion_resolve_doc(Editor *editor) {
|
||||
item.documentation = "";
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->editor = editor;
|
||||
pending->method = "completionItem/resolve";
|
||||
pending->callback = [](Editor *editor, std::string, json message) {
|
||||
pending->callback = [](Editor *editor, const json &message) {
|
||||
std::unique_lock lock(editor->completion.mtx);
|
||||
auto &item = editor->completion.items[editor->completion.select];
|
||||
if (message["result"].contains("documentation")) {
|
||||
|
||||
@@ -52,7 +52,7 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||
editor->root = erase(editor->root, start, byte_pos - start);
|
||||
lock_2.unlock();
|
||||
if (editor->parser)
|
||||
editor->parser->edit(start_row, end_row, 0);
|
||||
editor->parser->edit(start_row, end_row - start_row, 0);
|
||||
if (do_lsp) {
|
||||
if (editor->lsp->incremental_sync) {
|
||||
json message = {
|
||||
@@ -125,7 +125,7 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||
editor->root = erase(editor->root, byte_pos, end - byte_pos);
|
||||
lock_2.unlock();
|
||||
if (editor->parser)
|
||||
editor->parser->edit(start_row, end_row, 0);
|
||||
editor->parser->edit(start_row, end_row - start_row, 0);
|
||||
if (do_lsp) {
|
||||
if (editor->lsp->incremental_sync) {
|
||||
json message = {
|
||||
@@ -184,7 +184,7 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
|
||||
apply_hook_insertion(editor, pos.row, rows);
|
||||
lock_2.unlock();
|
||||
if (editor->parser)
|
||||
editor->parser->edit(pos.row, pos.row, rows);
|
||||
editor->parser->edit(pos.row, 0, rows);
|
||||
if (editor->lsp) {
|
||||
if (editor->lsp->incremental_sync) {
|
||||
json message = {
|
||||
@@ -245,8 +245,10 @@ 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 (rows > 0)
|
||||
rows--;
|
||||
if (editor->parser)
|
||||
editor->parser->edit(start.row, end.row - 1, rows);
|
||||
editor->parser->edit(start.row, end.row - start.row, rows);
|
||||
if (editor->lsp) {
|
||||
if (editor->lsp->incremental_sync) {
|
||||
json message = {
|
||||
|
||||
@@ -97,9 +97,8 @@ void save_file(Editor *editor) {
|
||||
{"trimFinalNewlines", true}}}}}};
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->editor = editor;
|
||||
pending->method = "textDocument/formatting";
|
||||
pending->callback = [save_msg, version](Editor *editor, std::string,
|
||||
json message) {
|
||||
pending->callback = [save_msg, version](Editor *editor,
|
||||
const json &message) {
|
||||
if (version != editor->lsp_version)
|
||||
return;
|
||||
auto &edits = message["result"];
|
||||
|
||||
@@ -153,8 +153,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
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);
|
||||
editor->indents.insert_tab(editor->cursor);
|
||||
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
|
||||
editor->indents.insert_new_line(editor->cursor);
|
||||
} else if (event.c[0] == CTRL('W')) {
|
||||
@@ -205,6 +204,14 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
paste(editor);
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case '<':
|
||||
case ',':
|
||||
dedent_selection(editor);
|
||||
break;
|
||||
case '>':
|
||||
case '.':
|
||||
indent_selection(editor);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -4,8 +4,14 @@
|
||||
#include "lsp/lsp.h"
|
||||
#include "main.h"
|
||||
#include "utils/utils.h"
|
||||
#include <sys/types.h>
|
||||
|
||||
void cut(Editor *editor) {
|
||||
if (ABS((int64_t)editor->cursor.row - (int64_t)editor->selection.row) >
|
||||
1500) {
|
||||
bar.log("Selection too large!");
|
||||
return;
|
||||
}
|
||||
if (mode != SELECT)
|
||||
return;
|
||||
Coord start;
|
||||
@@ -19,6 +25,11 @@ void cut(Editor *editor) {
|
||||
}
|
||||
|
||||
void copy(Editor *editor) {
|
||||
if (ABS((int64_t)editor->cursor.row - (int64_t)editor->selection.row) >
|
||||
1500) {
|
||||
bar.log("Selection too large!");
|
||||
return;
|
||||
}
|
||||
if (mode != SELECT)
|
||||
return;
|
||||
uint32_t len;
|
||||
@@ -72,6 +83,53 @@ void dedent_current_line(Editor *editor) {
|
||||
editor->cursor.row = start.row;
|
||||
}
|
||||
|
||||
static void move_coord_by_delta(Coord &c, uint32_t row, int64_t delta) {
|
||||
if (c.row == row) {
|
||||
int64_t new_col = (int64_t)c.col + delta;
|
||||
c.col = (uint32_t)MAX(new_col, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void indent_selection(Editor *editor) {
|
||||
uint32_t top = MIN(editor->cursor.row, editor->selection.row);
|
||||
uint32_t bot = MAX(editor->cursor.row, editor->selection.row);
|
||||
if (bot - top > 1500) {
|
||||
bar.log("Can't indent more than 1500 lines at once!");
|
||||
return;
|
||||
}
|
||||
if (bot - top >= 2)
|
||||
editor->indents.indent_block(top + 1, bot - 1);
|
||||
uint32_t delta_top = editor->indents.indent_line(top);
|
||||
uint32_t delta_bot =
|
||||
(bot == top) ? delta_top : editor->indents.indent_line(bot);
|
||||
move_coord_by_delta(editor->cursor, top, delta_top);
|
||||
move_coord_by_delta(editor->selection, top, delta_top);
|
||||
if (bot != top) {
|
||||
move_coord_by_delta(editor->cursor, bot, delta_bot);
|
||||
move_coord_by_delta(editor->selection, bot, delta_bot);
|
||||
}
|
||||
}
|
||||
|
||||
void dedent_selection(Editor *editor) {
|
||||
uint32_t top = MIN(editor->cursor.row, editor->selection.row);
|
||||
uint32_t bot = MAX(editor->cursor.row, editor->selection.row);
|
||||
if (bot - top > 1500) {
|
||||
bar.log("Can't dedent more than 1500 lines at once!");
|
||||
return;
|
||||
}
|
||||
if (bot - top >= 2)
|
||||
editor->indents.dedent_block(top + 1, bot - 1);
|
||||
uint32_t delta_top = editor->indents.dedent_line(top);
|
||||
uint32_t delta_bot =
|
||||
(bot == top) ? delta_top : editor->indents.dedent_line(bot);
|
||||
move_coord_by_delta(editor->cursor, top, -(int64_t)delta_top);
|
||||
move_coord_by_delta(editor->selection, top, -(int64_t)delta_top);
|
||||
if (bot != top) {
|
||||
move_coord_by_delta(editor->cursor, bot, -(int64_t)delta_bot);
|
||||
move_coord_by_delta(editor->selection, bot, -(int64_t)delta_bot);
|
||||
}
|
||||
}
|
||||
|
||||
void insert_char(Editor *editor, char c) {
|
||||
uint32_t col = editor->cursor.col;
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
@@ -155,9 +213,7 @@ void insert_char(Editor *editor, char c) {
|
||||
{"trimFinalNewlines", true}}}}}};
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->editor = editor;
|
||||
pending->method = "textDocument/onTypeFormatting";
|
||||
pending->callback = [version](Editor *editor, std::string,
|
||||
json message) {
|
||||
pending->callback = [version](Editor *editor, const json &message) {
|
||||
if (version != editor->lsp_version)
|
||||
return;
|
||||
auto &edits = message["result"];
|
||||
@@ -201,11 +257,32 @@ void backspace_edit(Editor *editor) {
|
||||
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];
|
||||
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;
|
||||
char prev_char = (prev_pos.col < len) ? line[prev_pos.col] : 0;
|
||||
char next_char = (editor->cursor.col < len) ? line[editor->cursor.col] : 0;
|
||||
bool before_content = false;
|
||||
if (editor->cursor.col > 0) {
|
||||
before_content = true;
|
||||
for (uint32_t i = 0; i < editor->cursor.col; i++)
|
||||
if (line[i] != ' ' && line[i] != '\t') {
|
||||
before_content = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
if (before_content) {
|
||||
dedent_current_line(editor);
|
||||
return;
|
||||
}
|
||||
bool is_pair = (prev_char == '{' && next_char == '}') ||
|
||||
(prev_char == '(' && next_char == ')') ||
|
||||
(prev_char == '[' && next_char == ']') ||
|
||||
@@ -312,8 +389,7 @@ void fetch_lsp_hover(Editor *editor) {
|
||||
{"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) {
|
||||
pending->callback = [](Editor *editor, const json &hover) {
|
||||
if (hover.contains("result") && !hover["result"].is_null()) {
|
||||
auto &contents = hover["result"]["contents"];
|
||||
std::string hover_text = "";
|
||||
|
||||
@@ -38,6 +38,18 @@ void IndentationEngine::compute_indent(Editor *n_editor) {
|
||||
indent = 2;
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
auto x = kLangtoBlockStartsEnd.find(editor->lang.name);
|
||||
if (x != kLangtoBlockStartsEnd.end())
|
||||
start_end = &x->second;
|
||||
x = kLangtoBlockStartsStart.find(editor->lang.name);
|
||||
if (x != kLangtoBlockStartsStart.end())
|
||||
start_start = &x->second;
|
||||
x = kLangtoBlockEndsFull.find(editor->lang.name);
|
||||
if (x != kLangtoBlockEndsFull.end())
|
||||
end_full = &x->second;
|
||||
x = kLangtoBlockEndsStart.find(editor->lang.name);
|
||||
if (x != kLangtoBlockEndsStart.end())
|
||||
end_start = &x->second;
|
||||
}
|
||||
|
||||
uint32_t IndentationEngine::indent_real(char *line, uint32_t len) {
|
||||
@@ -75,18 +87,16 @@ uint32_t IndentationEngine::indent_expected(uint32_t row) {
|
||||
if (len == 0)
|
||||
continue;
|
||||
c_indent = indent_real(line, len);
|
||||
auto is_end_set = kLangtoBlockStartsEnd.find(editor->lang.name);
|
||||
auto is_start_set = kLangtoBlockStartsStart.find(editor->lang.name);
|
||||
bool is_end = false;
|
||||
if (is_end_set != kLangtoBlockStartsEnd.end())
|
||||
for (auto end : is_end_set->second)
|
||||
if (start_end)
|
||||
for (auto end : *start_end)
|
||||
if (ends_with(line, end)) {
|
||||
c_indent++;
|
||||
is_end = true;
|
||||
break;
|
||||
}
|
||||
if (!is_end && is_start_set != kLangtoBlockStartsStart.end())
|
||||
for (auto end : is_start_set->second)
|
||||
if (!is_end && start_start)
|
||||
for (auto end : *start_start)
|
||||
if (starts_with(line, end)) {
|
||||
c_indent++;
|
||||
break;
|
||||
@@ -195,6 +205,100 @@ uint32_t IndentationEngine::dedent_line(uint32_t row) {
|
||||
return (uint32_t)ABS((int64_t)ws_len - (new_indent * indent));
|
||||
}
|
||||
|
||||
void IndentationEngine::indent_block(uint32_t start_row, uint32_t end_row) {
|
||||
indent_block(start_row, end_row, +1);
|
||||
}
|
||||
|
||||
void IndentationEngine::dedent_block(uint32_t start_row, uint32_t end_row) {
|
||||
indent_block(start_row, end_row, -1);
|
||||
}
|
||||
|
||||
void IndentationEngine::indent_block(uint32_t start_row, uint32_t end_row,
|
||||
int delta) {
|
||||
if (start_row > end_row)
|
||||
std::swap(start_row, end_row);
|
||||
uint32_t start_len, end_len;
|
||||
uint32_t start_off = line_to_byte(editor->root, start_row, &start_len);
|
||||
uint32_t end_off = line_to_byte(editor->root, end_row, &end_len);
|
||||
uint32_t total_len = (end_off - start_off) + end_len;
|
||||
char *block = read(editor->root, start_off, total_len);
|
||||
if (!block)
|
||||
return;
|
||||
uint32_t cap = total_len + 128;
|
||||
char *out = (char *)malloc(cap);
|
||||
uint32_t out_len = 0;
|
||||
char *p = block;
|
||||
char *end = block + total_len;
|
||||
while (p < end) {
|
||||
char *line_start = p;
|
||||
while (p < end && *p != '\n')
|
||||
p++;
|
||||
uint32_t len = (uint32_t)(p - line_start);
|
||||
uint32_t ws = 0;
|
||||
while (ws < len && (line_start[ws] == ' ' || line_start[ws] == '\t'))
|
||||
ws++;
|
||||
uint32_t real_indent = indent_real(line_start, len);
|
||||
int64_t new_indent = (int64_t)real_indent + delta;
|
||||
if (new_indent < 0)
|
||||
new_indent = 0;
|
||||
uint32_t indent_chars = (indent == 1) ? new_indent : new_indent * indent;
|
||||
uint32_t new_line_len = indent_chars + (len - ws);
|
||||
if (out_len + new_line_len + 2 >= cap) {
|
||||
cap = (cap * 2) + new_line_len + 32;
|
||||
out = (char *)realloc(out, cap);
|
||||
}
|
||||
if (indent == 1) {
|
||||
memset(out + out_len, '\t', indent_chars);
|
||||
out_len += indent_chars;
|
||||
} else {
|
||||
memset(out + out_len, ' ', indent_chars);
|
||||
out_len += indent_chars;
|
||||
}
|
||||
memcpy(out + out_len, line_start + ws, len - ws);
|
||||
out_len += (len - ws);
|
||||
if (p < end && *p == '\n') {
|
||||
out[out_len++] = '\n';
|
||||
p++;
|
||||
}
|
||||
}
|
||||
free(block);
|
||||
edit_replace(editor, {start_row, 0}, {end_row, end_len}, out, out_len);
|
||||
free(out);
|
||||
}
|
||||
|
||||
void IndentationEngine::insert_tab(Coord cursor) {
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
LineIterator *it = begin_l_iter(editor->root, cursor.row);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
lock.unlock();
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
uint32_t ws_len = 0;
|
||||
while (ws_len < len && (line[ws_len] == ' ' || line[ws_len] == '\t'))
|
||||
ws_len++;
|
||||
std::string insert;
|
||||
if (cursor.col <= ws_len) {
|
||||
if (indent == 1)
|
||||
insert = "\t";
|
||||
else
|
||||
insert.assign(indent - ((cursor.col) % indent), ' ');
|
||||
} else {
|
||||
insert = "\t";
|
||||
}
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
edit_insert(editor, cursor, (char *)insert.c_str(), insert.size());
|
||||
editor->cursor.col += insert.size();
|
||||
}
|
||||
|
||||
void IndentationEngine::insert_new_line(Coord cursor) {
|
||||
std::string formatted;
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
@@ -212,19 +316,17 @@ void IndentationEngine::insert_new_line(Coord cursor) {
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
if (cursor.col >= len) {
|
||||
auto is_end_full = kLangtoBlockEndsFull.find(editor->lang.name);
|
||||
auto is_end_start = kLangtoBlockEndsStart.find(editor->lang.name);
|
||||
bool end_matched = false;
|
||||
if (is_end_full != kLangtoBlockEndsFull.end())
|
||||
for (auto end : is_end_full->second)
|
||||
if (end_full)
|
||||
for (auto end : *end_full)
|
||||
if (end == trim(line)) {
|
||||
cursor.col = set_indent(
|
||||
cursor.row, (int64_t)indent_expected(cursor.row) - (int64_t)1);
|
||||
end_matched = true;
|
||||
break;
|
||||
}
|
||||
if (!end_matched && is_end_start != kLangtoBlockEndsStart.end())
|
||||
for (auto end : is_end_start->second)
|
||||
if (!end_matched && end_start)
|
||||
for (auto end : *end_start)
|
||||
if (starts_with(trim(line), end)) {
|
||||
cursor.col = set_indent(
|
||||
cursor.row, (int64_t)indent_expected(cursor.row) - (int64_t)1);
|
||||
@@ -252,30 +354,26 @@ void IndentationEngine::insert_new_line(Coord cursor) {
|
||||
if (!ending.empty()) {
|
||||
bool ending_valid = false;
|
||||
bool starting_valid = false;
|
||||
auto is_end_full = kLangtoBlockEndsFull.find(editor->lang.name);
|
||||
auto is_end_start = kLangtoBlockEndsStart.find(editor->lang.name);
|
||||
auto is_end_set = kLangtoBlockStartsEnd.find(editor->lang.name);
|
||||
auto is_start_set = kLangtoBlockStartsStart.find(editor->lang.name);
|
||||
if (is_end_full != kLangtoBlockEndsFull.end())
|
||||
for (auto end : is_end_full->second)
|
||||
if (end_full)
|
||||
for (auto end : *end_full)
|
||||
if (ending == end) {
|
||||
ending_valid = true;
|
||||
break;
|
||||
}
|
||||
if (!ending_valid && is_end_start != kLangtoBlockEndsStart.end())
|
||||
for (auto end : is_end_start->second)
|
||||
if (!ending_valid && end_start)
|
||||
for (auto end : *end_start)
|
||||
if (starts_with(ending, end)) {
|
||||
ending_valid = true;
|
||||
break;
|
||||
}
|
||||
if (is_end_set != kLangtoBlockStartsEnd.end())
|
||||
for (auto end : is_end_set->second)
|
||||
if (start_end)
|
||||
for (auto end : *start_end)
|
||||
if (ends_with(before, end)) {
|
||||
starting_valid = true;
|
||||
break;
|
||||
}
|
||||
if (!starting_valid && is_start_set != kLangtoBlockStartsStart.end())
|
||||
for (auto end : is_start_set->second)
|
||||
if (!starting_valid && start_start)
|
||||
for (auto end : *start_start)
|
||||
if (starts_with(before, end)) {
|
||||
starting_valid = true;
|
||||
break;
|
||||
@@ -288,18 +386,16 @@ void IndentationEngine::insert_new_line(Coord cursor) {
|
||||
else if (ending_valid)
|
||||
c_indent--;
|
||||
}
|
||||
auto is_end_set = kLangtoBlockStartsEnd.find(editor->lang.name);
|
||||
auto is_start_set = kLangtoBlockStartsStart.find(editor->lang.name);
|
||||
bool is_end = false;
|
||||
if (is_end_set != kLangtoBlockStartsEnd.end())
|
||||
for (auto end : is_end_set->second)
|
||||
if (start_end)
|
||||
for (auto end : *start_end)
|
||||
if (ends_with(before, end)) {
|
||||
c_indent++;
|
||||
is_end = true;
|
||||
break;
|
||||
}
|
||||
if (!is_end && is_start_set != kLangtoBlockStartsStart.end())
|
||||
for (auto end : is_start_set->second)
|
||||
if (!is_end && start_start)
|
||||
for (auto end : *start_start)
|
||||
if (starts_with(before, end)) {
|
||||
c_indent++;
|
||||
break;
|
||||
@@ -349,8 +445,7 @@ void IndentationEngine::insert_new_line(Coord cursor) {
|
||||
{"trimFinalNewlines", true}}}}}};
|
||||
LSPPending *pending = new LSPPending();
|
||||
pending->editor = editor;
|
||||
pending->method = "textDocument/onTypeFormatting";
|
||||
pending->callback = [version](Editor *editor, std::string, json message) {
|
||||
pending->callback = [version](Editor *editor, const json &message) {
|
||||
if (version != editor->lsp_version)
|
||||
return;
|
||||
auto &edits = message["result"];
|
||||
|
||||
Reference in New Issue
Block a user