Remake insert-mode so operations are done on lines, not on all letters

This commit is contained in:
Matúš Púll 2025-05-29 17:19:25 +02:00
parent 3562a62da7
commit 598932c9ef
3 changed files with 30 additions and 31 deletions

View file

@ -185,6 +185,7 @@ bool Editor::take_action() {
break; break;
case 'i': case 'i':
mode = INSERT; mode = INSERT;
current_insert = file.get_line(file_offset + cur.r, 0);
break; break;
case 'v': case 'v':
mode = SELECT; mode = SELECT;
@ -223,40 +224,36 @@ bool Editor::take_action() {
} }
break; break;
case INSERT: case INSERT:
// TODO remake insert mode for better undo
switch(ch) { switch(ch) {
case ESC: case ESC:
mode = NORMAL; mode = NORMAL;
// Change current row into current_insert TODO file.set_line(cur.r, current_insert);
print_current_line(); print_text(cur.r, Treap::substitute_tabs(current_insert));
current_insert = "";
break; break;
case BS: case BS:
// Modify current_insert TODO
if(cur.c > 0) { if(cur.c > 0) {
cur.c -= 1; cur.c -= 1;
file.remove(cur + file_offset); current_insert.erase(cur.c - Treap::get_tab_offset(cur.c, current_insert), 1);
} }
print_current_line(); print_text(cur.r, Treap::substitute_tabs(current_insert));
break; break;
case ENTER: case ENTER:
// Modify current_insert and update TODO file.set_line(cur.r, current_insert);
file.split_line(cur + file_offset); file.split_line(cur + file_offset);
cur.r++; cur.c = 0; move(cur);
current_insert = file.get_line(cur.r);
print_file(file_offset); print_file(file_offset);
cur.r++;
cur.c = 0;
move(cur);
break; break;
case '\t': case '\t':
// Modify current_insert TODO current_insert.insert(cur.c - Treap::get_tab_offset(cur.c, current_insert), "\t");
file.insert(cur + file_offset, "\t");
cur.c += TAB_SIZE - (cur.c % TAB_SIZE); cur.c += TAB_SIZE - (cur.c % TAB_SIZE);
print_current_line(); print_text(cur.r, Treap::substitute_tabs(current_insert));
break; break;
default: default:
// Modify current_insert TODO current_insert.insert(cur.c - Treap::get_tab_offset(cur.c, current_insert), string(1, ch));
file.insert(cur + file_offset, string(1, ch));
cur.c += 1; cur.c += 1;
print_current_line(); print_text(cur.r, Treap::substitute_tabs(current_insert));
break; break;
} }
break; break;

View file

@ -40,8 +40,11 @@ class Treap {
line* find(count_type k); line* find(count_type k);
count_type bulk_find(vector<string> *vec, line *l, count_type k, count_type count); count_type bulk_find(vector<string> *vec, line *l, count_type k, count_type count);
string substitute_tabs(string s);
public: public:
static string substitute_tabs(string s);
static count_type get_tab_offset(count_type c, string text);
count_type get_tab_offset(position p) { return get_tab_offset(p.c, get_line(p.r, 0)); }
// General operations // General operations
Treap() { srand(120); root = nullptr; } Treap() { srand(120); root = nullptr; }
count_type size() { return get_size(root); } count_type size() { return get_size(root); }
@ -50,11 +53,11 @@ public:
// Line operations // Line operations
string get_line(count_type r, bool substitute_tab = 1); string get_line(count_type r, bool substitute_tab = 1);
vector<string> bulk_get_line(count_type r, count_type count); vector<string> bulk_get_line(count_type r, count_type count);
count_type get_tab_offset(position p);
void split_line(position p);
void split_line(position p);
void insert(count_type k, string s); void insert(count_type k, string s);
void remove(count_type k); void remove(count_type k);
void set_line(count_type k, string s) { find(k)->text = s; }
// Substring operations // Substring operations
void insert(position p, string t); void insert(position p, string t);

View file

@ -19,6 +19,17 @@ string Treap::substitute_tabs(string s) {
} }
return ret; return ret;
} }
// Get how much tabs changed position on a row
count_type Treap::get_tab_offset(count_type c, string text) {
count_type tab_offset = 0;
for(count_type i = 0; i + tab_offset < c; ++i) {
if(text[i] == '\t')
tab_offset += TAB_SIZE - (i+tab_offset) % TAB_SIZE - 1;
if(i + tab_offset > c)
tab_offset -= i + tab_offset - c;
}
return tab_offset;
}
// Split treap by k-th element // Split treap by k-th element
two_lines Treap::split(line *l, count_type k) { two_lines Treap::split(line *l, count_type k) {
@ -147,18 +158,6 @@ vector<string> Treap::bulk_get_line(count_type r, count_type count) {
return ret; return ret;
} }
// Get how much tabs changed position on a row
count_type Treap::get_tab_offset(position p) {
count_type tab_offset = 0;
string line = get_line(p.r, false);
for(count_type i = 0; i + tab_offset < p.c; ++i) {
if(line[i] == '\t')
tab_offset += TAB_SIZE - (i+tab_offset) % TAB_SIZE - 1;
if(i + tab_offset > p.c)
tab_offset -= i + tab_offset - p.c;
}
return tab_offset;
}
void Treap::insert(position p, string t) { void Treap::insert(position p, string t) {
find(p.r)->text.insert(p.c-get_tab_offset(p), t); find(p.r)->text.insert(p.c-get_tab_offset(p), t);
} }