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); ::move(r - file_offset, 0);
clrtoeol(); 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 // Print line
void Editor::print_line(count_type r) { void Editor::print_line(count_type r) {
clear_line(r); clear_line(r);
@ -262,7 +255,7 @@ bool Editor::take_action() {
print_line(file_offset + cur.r); print_line(file_offset + cur.r);
break; break;
case ENTER: case ENTER:
split_line(cur + file_offset); file.split_line(cur + file_offset);
print_file(file_offset + cur.r++); print_file(file_offset + cur.r++);
jump_line_end(); jump_line_end();
break; break;

View file

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

View file

@ -110,6 +110,7 @@ string Treap::get_line(count_type r, bool substitute_tab) {
} }
return ret; return ret;
} }
// TODO tabs are a grid, not just N tabs
count_type Treap::get_tab_offset(position p) { count_type Treap::get_tab_offset(position p) {
count_type tab_offset = 0; count_type tab_offset = 0;
string line = get_line(p.r, false); 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) { void Treap::remove(position p, count_type len) {
find(p.r)->text.erase(p.c-get_tab_offset(p),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);
}