Move split_line function into treap

This commit is contained in:
Matúš Púll 2025-05-25 19:59:44 +02:00
parent e1646c6098
commit 7ae0a5c622
4 changed files with 25 additions and 24 deletions

View file

@ -17,13 +17,6 @@ void Editor::clear_line(count_type r) {
::move(r - file_offset, 0);
clrtoeol();
}
// Split line
void Editor::split_line(position p) {
string line = file.get_line(p.r);
string newline = line.substr(p.c, line.size());
file.remove(p, line.size()-p.c);
file.insert(p.r+1, newline);
}
// Print line
void Editor::print_line(count_type r) {
clear_line(r);
@ -262,7 +255,7 @@ bool Editor::take_action() {
print_line(file_offset + cur.r);
break;
case ENTER:
split_line(cur + file_offset);
file.split_line(cur + file_offset);
print_file(file_offset + cur.r++);
jump_line_end();
break;

View file

@ -54,6 +54,7 @@ public:
void set(position p, char ch);
void insert(position p, string t);
void remove(position p, count_type len = 1);
void split_line(position p);
};
//// Selection
@ -80,22 +81,24 @@ public:
enum mode_type { INSERT, NORMAL, SELECT };
class Editor{
// Editor state
mode_type mode;
Treap file; // file representation
position cur;
count_type file_offset;
// File
string filename;
position cur; // cursor position
count_type file_offset; // terminal file offset
// Selections
string last_find, last_replace; // last find/replace inputs
Treap file;
// Selection
string last_find, last_replace;
Selection selection;
// Write to file
void save();
// Printing
void clear_line(count_type r);
void print_line(count_type r);
void split_line(position p);
// User input
void print_input(string text);
@ -116,10 +119,12 @@ class Editor{
public:
Editor(string _filename, Treap &_file) {
mode = NORMAL;
filename = _filename;
file = _file;
cur = {0, 0};
file_offset = 0;
filename = _filename;
file = _file;
selection = Selection(&file);
}
void print_file(count_type start = 0);

View file

@ -46,13 +46,8 @@ void Selection::paste_selection(position p) {
file->insert(p, content.get(content.size()-1));
if(content.size() == 1) return;
// Split line
string line = file->get_line(p.r);
string newline = line.substr(p.c, line.size());
file->remove(p, line.size()-p.c);
file->insert(p.r+1, newline);
// Insert first line in front and split
file->split_line(p);
file->insert(p, content.get(0));
if(content.size() == 2) return;

View file

@ -110,6 +110,7 @@ string Treap::get_line(count_type r, bool substitute_tab) {
}
return ret;
}
// TODO tabs are a grid, not just N tabs
count_type Treap::get_tab_offset(position p) {
count_type tab_offset = 0;
string line = get_line(p.r, false);
@ -130,3 +131,10 @@ void Treap::insert(position p, string t) {
void Treap::remove(position p, count_type len) {
find(p.r)->text.erase(p.c-get_tab_offset(p),len);
}
// Split line
void Treap::split_line(position p) {
string line = get_line(p.r);
string newline = line.substr(p.c, line.size());
remove(p, line.size()-p.c);
insert(p.r+1, newline);
}