Add line numbers

This commit is contained in:
2025-12-13 16:47:07 +00:00
parent d8c281d7d7
commit a59111162c
5 changed files with 85 additions and 43 deletions

View File

@@ -6,8 +6,6 @@ A TUI IDE.
# TODO
- [ ] Add line numbers.
- [ ] Add bg highlight for current line.
- [ ] Make function to get selected text. (selection itself is done)
- [ ] Add support for copy/cut/paste.
- [ ] Add support for ctrl + arrow key for moving words.

View File

@@ -5,6 +5,7 @@ extern "C" {
#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();
@@ -69,6 +70,10 @@ void update_render_fold_marker(uint32_t row, uint32_t cols) {
void render_editor(Editor *editor) {
uint32_t sel_start = 0, sel_end = 0;
uint32_t numlen =
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen;
uint32_t render_x = editor->position.col + numlen;
if (editor->selection_active) {
uint32_t sel1 = line_to_byte(editor->root, editor->cursor.row, nullptr) +
editor->cursor.col;
@@ -90,7 +95,7 @@ void render_editor(Editor *editor) {
while (rendered_rows < editor->size.row) {
if (editor->folded[line_index]) {
if (editor->folded[line_index] == 2) {
update_render_fold_marker(rendered_rows, editor->size.col);
update_render_fold_marker(rendered_rows, render_width);
rendered_rows++;
}
do {
@@ -118,14 +123,28 @@ void render_editor(Editor *editor) {
if (rendered_rows == 0)
current_byte_offset += editor->scroll.col;
while (current_byte_offset < line_len && rendered_rows < editor->size.row) {
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
if (current_byte_offset == 0 || rendered_rows == 0) {
char buf[16];
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 1, line_index + 1);
uint32_t num_color =
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
for (int i = 0; i < len; i++)
update(editor->position.row + rendered_rows, editor->position.col + i,
std::string(1, buf[i]).c_str(), num_color, 0, 0);
} else {
for (uint32_t i = 0; i < numlen; i++)
update(editor->position.row + rendered_rows, editor->position.col + i,
" ", 0, 0, 0);
}
uint32_t col = 0;
uint32_t local_render_offset = 0;
uint32_t line_left = line_len - current_byte_offset;
while (line_left > 0 && col < editor->size.col) {
while (line_left > 0 && col < render_width) {
if (line_index == editor->cursor.row &&
editor->cursor.col == (current_byte_offset + local_render_offset)) {
cursor.row = editor->position.row + rendered_rows;
cursor.col = editor->position.col + col;
cursor.col = render_x + col;
}
uint32_t absolute_byte_pos =
global_byte_offset + current_byte_offset + local_render_offset;
@@ -141,33 +160,32 @@ void render_editor(Editor *editor) {
std::string cluster(line + current_byte_offset + local_render_offset,
cluster_len);
int width = display_width(cluster.c_str(), cluster_len);
if (col + width > editor->size.col)
if (col + width > render_width)
break;
update(editor->position.row + rendered_rows, editor->position.col + col,
cluster.c_str(), fg, bg, fl);
update(editor->position.row + rendered_rows, render_x + col,
cluster.c_str(), fg, bg | color, fl);
local_render_offset += cluster_len;
line_left -= cluster_len;
col += width;
while (width-- > 1)
update(editor->position.row + rendered_rows,
editor->position.col + col - width, "\x1b", fg, bg, fl);
update(editor->position.row + rendered_rows, render_x + col - width,
"\x1b", fg, bg | color, fl);
}
if (line_index == editor->cursor.row &&
editor->cursor.col == (current_byte_offset + local_render_offset)) {
cursor.row = editor->position.row + rendered_rows;
cursor.col = editor->position.col + col;
cursor.col = render_x + col;
}
if (editor->selection_active &&
global_byte_offset + line_len + 1 > sel_start &&
global_byte_offset + line_len + 1 <= sel_end &&
col < editor->size.col) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0, 0x555555, 0);
global_byte_offset + line_len + 1 <= sel_end && col < render_width) {
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
0x555555 | color, 0);
col++;
}
while (col < editor->size.col) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0xFFFFFF, 0, 0);
while (col < render_width) {
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
0 | color, 0);
col++;
}
rendered_rows++;
@@ -175,21 +193,29 @@ void render_editor(Editor *editor) {
}
if (line_len == 0 ||
(current_byte_offset >= line_len && rendered_rows == 0)) {
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
char buf[16];
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 1, line_index + 1);
uint32_t num_color =
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
for (int i = 0; i < len; i++)
update(editor->position.row + rendered_rows, editor->position.col + i,
std::string(1, buf[i]).c_str(), num_color, 0, 0);
if (editor->cursor.row == line_index) {
cursor.row = editor->position.row + rendered_rows;
cursor.col = editor->position.col;
cursor.col = render_x;
}
uint32_t col = 0;
if (editor->selection_active &&
global_byte_offset + line_len + 1 > sel_start &&
global_byte_offset + line_len + 1 <= sel_end) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0, 0x555555, 0);
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
0x555555 | color, 0);
col++;
}
while (col < editor->size.col) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0xFFFFFF, 0, 0);
while (col < render_width) {
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
0 | color, 0);
col++;
}
rendered_rows++;
@@ -213,10 +239,10 @@ void render_editor(Editor *editor) {
}
set_cursor(cursor.row, cursor.col, type, true);
}
while (rendered_rows < editor->size.row) {
for (uint32_t col = 0; col < editor->size.col; col++)
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0xFFFFFF, 0, 0);
while (rendered_rows < render_width) {
for (uint32_t col = 0; col < render_width; col++)
update(editor->position.row + rendered_rows, render_x + col, " ",
0xFFFFFF, 0, 0);
rendered_rows++;
}
free(it);

View File

@@ -4,6 +4,7 @@ extern "C" {
#include "../include/editor.h"
#include "../include/main.h"
#include "../include/utils.h"
#include <cmath>
void handle_editor_event(Editor *editor, KeyEvent event) {
if (event.key_type == KEY_SPECIAL) {
@@ -168,8 +169,12 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
}
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
if (mode != INSERT)
x--;
if (mode == INSERT)
x++;
uint32_t numlen =
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen;
x = MAX(x, numlen) - numlen;
uint32_t target_visual_row = y;
uint32_t visual_row = 0;
uint32_t line_index = editor->scroll.row;
@@ -209,16 +214,16 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
uint32_t advance = 0;
uint32_t left = line_len - offset;
uint32_t last_good_offset = offset;
while (left > 0 && col < editor->size.col) {
while (left > 0 && col < render_width) {
uint32_t g =
grapheme_next_character_break_utf8(line + offset + advance, left);
int w = display_width(line + offset + advance, g);
if (col + w > editor->size.col)
if (col + w > render_width)
break;
if (visual_row == target_visual_row && x < col + w) {
free(line);
free(it);
return {line_index, offset + advance + g};
return {line_index, offset + advance};
}
advance += g;
last_good_offset = offset + advance;

View File

@@ -4,9 +4,13 @@ extern "C" {
}
#include "../include/editor.h"
#include "../include/utils.h"
#include <cmath>
void scroll_up(Editor *editor, uint32_t number) {
number++;
uint32_t numlen =
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen;
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * number);
uint32_t q_head = 0;
uint32_t q_size = 0;
@@ -44,7 +48,7 @@ void scroll_up(Editor *editor, uint32_t number) {
uint32_t inc =
grapheme_next_character_break_utf8(line + offset, line_len - offset);
int width = display_width(line + offset, inc);
if (col + width > editor->size.col) {
if (col + width > render_width) {
if (q_size < number) {
scroll_queue[(q_head + q_size) % number] = {i, offset};
q_size++;
@@ -74,6 +78,9 @@ void scroll_up(Editor *editor, uint32_t number) {
void scroll_down(Editor *editor, uint32_t number) {
if (!editor || number == 0)
return;
uint32_t numlen =
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen;
uint32_t line_index = editor->scroll.row;
LineIterator *it = begin_l_iter(editor->root, line_index);
if (!it)
@@ -145,12 +152,12 @@ void scroll_down(Editor *editor, uint32_t number) {
uint32_t col = 0;
uint32_t local_render_offset = 0;
uint32_t left = line_len - current_byte_offset;
while (left > 0 && col < editor->size.col) {
while (left > 0 && col < render_width) {
uint32_t cluster_len = grapheme_next_character_break_utf8(
line + current_byte_offset + local_render_offset, left);
int width = display_width(
line + current_byte_offset + local_render_offset, cluster_len);
if (col + width > editor->size.col)
if (col + width > render_width)
break;
local_render_offset += cluster_len;
left -= cluster_len;
@@ -183,6 +190,9 @@ void ensure_cursor(Editor *editor) {
editor->cursor = editor->scroll;
return;
}
uint32_t numlen =
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen;
uint32_t visual_rows = 0;
uint32_t line_index = editor->scroll.row;
bool first_visual_line = true;
@@ -229,11 +239,11 @@ void ensure_cursor(Editor *editor) {
uint32_t col = 0;
uint32_t advance = 0;
uint32_t left = line_len - offset;
while (left > 0 && col < editor->size.col) {
while (left > 0 && col < render_width) {
uint32_t g =
grapheme_next_character_break_utf8(line + offset + advance, left);
int w = display_width(line + offset + advance, g);
if (col + w > editor->size.col)
if (col + w > render_width)
break;
advance += g;
left -= g;
@@ -262,6 +272,9 @@ void ensure_cursor(Editor *editor) {
void ensure_scroll(Editor *editor) {
std::shared_lock knot_lock(editor->knot_mtx);
uint32_t numlen =
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen;
if (editor->cursor.row < editor->scroll.row ||
(editor->cursor.row == editor->scroll.row &&
editor->cursor.col < editor->scroll.col)) {
@@ -284,7 +297,7 @@ void ensure_scroll(Editor *editor) {
uint32_t inc =
grapheme_next_character_break_utf8(line + offset, len - offset);
int width = display_width(line + offset, inc);
if (cols + width > editor->size.col) {
if (cols + width > render_width) {
rows++;
cols = 0;
if (editor->cursor.col > old_offset && editor->cursor.col <= offset) {
@@ -363,12 +376,12 @@ void ensure_scroll(Editor *editor) {
uint32_t col = 0;
uint32_t local_render_offset = 0;
uint32_t line_left = line_len - current_byte_offset;
while (line_left > 0 && col < editor->size.col) {
while (line_left > 0 && col < render_width) {
uint32_t cluster_len = grapheme_next_character_break_utf8(
line + current_byte_offset + local_render_offset, line_left);
int width = display_width(
line + current_byte_offset + local_render_offset, cluster_len);
if (col + width > editor->size.col)
if (col + width > render_width)
break;
local_render_offset += cluster_len;
line_left -= cluster_len;

View File

@@ -60,8 +60,8 @@ int main(int argc, char *argv[]) {
Coord screen = start_screen();
const char *filename = (argc > 1) ? argv[1] : "";
Editor *editor =
new_editor(filename, {10, 10}, {screen.row - 20, screen.col - 20});
Editor *editor = new_editor(filename, {0, 0}, screen);
if (!editor) {
end_screen();
fprintf(stderr, "Failed to load editor\n");