Simplify selection into a vector of strings

This commit is contained in:
Matúš Púll 2025-06-25 20:58:59 +02:00
parent f874852629
commit 3cf1f2f52b
2 changed files with 16 additions and 8 deletions

View file

@ -107,7 +107,7 @@ public:
//// Selection //// Selection
class Selection { class Selection {
Treap *file; Treap *file;
Treap content; vector<string> content;
template <typename F, typename G> template <typename F, typename G>
void apply_on_selection(position p, F func, G func2); void apply_on_selection(position p, F func, G func2);

View file

@ -26,16 +26,24 @@ void Selection::apply_on_selection(position p, F func, G func2) {
} }
// Copying selection // Copying selection
void Selection::copy_selection(position p) { void Selection::copy_selection(position p) {
// Insert in the wrong order
apply_on_selection(p, apply_on_selection(p,
[](count_type r, Treap *file, Treap *sel){ sel->insert(0, file->get_line(r, false)); }, [](count_type r, Treap *file, vector<string> *sel){ sel->push_back(file->get_line(r, false)); },
[](count_type r, count_type start_c, count_type size, Treap *file, Treap *sel) { sel->insert(0, file->get_line(r, false).substr(start_c - file->get_tab_offset({r, start_c}), size)); } [](count_type r, count_type start_c, count_type size, Treap *file, vector<string> *sel) { sel->push_back(file->get_line(r, false).substr(start_c - file->get_tab_offset({r, start_c}), size)); }
); );
// Reverse order of the vector
for(count_type i = 0; i < content.size()/2; ++i) {
string tmp = content[content.size()-1 - i];
content[content.size()-1 - i] = content[i];
content[i] = tmp;
}
} }
// Removing selection // Removing selection
count_type Selection::remove_selection(position p) { count_type Selection::remove_selection(position p) {
apply_on_selection(p, apply_on_selection(p,
[](count_type r, Treap *file, Treap *sel){ file->remove(r); }, [](count_type r, Treap *file, vector<string> *sel){ file->remove(r); },
[](count_type r, count_type start_c, count_type size, Treap *file, Treap *sel) { file->remove({r, start_c-file->get_tab_offset({r, start_c})}, size); } [](count_type r, count_type start_c, count_type size, Treap *file, vector<string> *sel) { file->remove({r, start_c-file->get_tab_offset({r, start_c})}, size); }
); );
count_type min = p.r, max = pos.r; count_type min = p.r, max = pos.r;
@ -54,17 +62,17 @@ count_type Selection::paste_selection(position p) {
if(content.size() == 0) return 0; if(content.size() == 0) return 0;
// Insert last line inside of this // Insert last line inside of this
file->insert(p, content.get_line(content.size()-1, false)); file->insert(p, content.back());
if(content.size() == 1) return 1; if(content.size() == 1) return 1;
// Insert first line in front and split // Insert first line in front and split
file->split_line(p, false); file->split_line(p, false);
file->insert(p, content.get_line(0, false)); file->insert(p, content.front());
if(content.size() == 2) return 4; if(content.size() == 2) return 4;
// Insert lines // Insert lines
for(count_type i = content.size()-2; i > 0; --i) for(count_type i = content.size()-2; i > 0; --i)
file->insert(p.r+1, content.get_line(i, false)); file->insert(p.r+1, content[i]);
return content.size()+2; return content.size()+2;
} }