Make get_tab_offset and substitute_tabs global

This commit is contained in:
Matúš Púll 2025-05-29 18:08:25 +02:00
parent 598932c9ef
commit 7391e5085a
4 changed files with 40 additions and 38 deletions

View file

@ -228,15 +228,15 @@ bool Editor::take_action() {
case ESC: case ESC:
mode = NORMAL; mode = NORMAL;
file.set_line(cur.r, current_insert); file.set_line(cur.r, current_insert);
print_text(cur.r, Treap::substitute_tabs(current_insert)); print_text(cur.r, substitute_tabs(current_insert));
current_insert = ""; current_insert = "";
break; break;
case BS: case BS:
if(cur.c > 0) { if(cur.c > 0) {
cur.c -= 1; cur.c -= 1;
current_insert.erase(cur.c - Treap::get_tab_offset(cur.c, current_insert), 1); current_insert.erase(cur.c - get_tab_offset(cur.c, current_insert), 1);
} }
print_text(cur.r, Treap::substitute_tabs(current_insert)); print_text(cur.r, substitute_tabs(current_insert));
break; break;
case ENTER: case ENTER:
file.set_line(cur.r, current_insert); file.set_line(cur.r, current_insert);
@ -246,14 +246,14 @@ bool Editor::take_action() {
print_file(file_offset); print_file(file_offset);
break; break;
case '\t': case '\t':
current_insert.insert(cur.c - Treap::get_tab_offset(cur.c, current_insert), "\t"); current_insert.insert(cur.c - get_tab_offset(cur.c, current_insert), "\t");
cur.c += TAB_SIZE - (cur.c % TAB_SIZE); cur.c += TAB_SIZE - (cur.c % TAB_SIZE);
print_text(cur.r, Treap::substitute_tabs(current_insert)); print_text(cur.r, substitute_tabs(current_insert));
break; break;
default: default:
current_insert.insert(cur.c - Treap::get_tab_offset(cur.c, current_insert), string(1, ch)); current_insert.insert(cur.c - get_tab_offset(cur.c, current_insert), string(1, ch));
cur.c += 1; cur.c += 1;
print_text(cur.r, Treap::substitute_tabs(current_insert)); print_text(cur.r, substitute_tabs(current_insert));
break; break;
} }
break; break;

View file

@ -16,6 +16,10 @@ struct position {
position operator+(count_type offset) { return {r + offset, c}; } position operator+(count_type offset) { return {r + offset, c}; }
}; };
// String utilities
string substitute_tabs(string s);
count_type get_tab_offset(count_type c, string text);
//// Treap //// Treap
// Treap node representation of a line // Treap node representation of a line
struct line { struct line {
@ -41,9 +45,7 @@ class Treap {
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);
public: public:
static string substitute_tabs(string s); count_type get_tab_offset(position p) { return ::get_tab_offset(p.c, get_line(p.r, 0)); }
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; }
@ -109,6 +111,7 @@ class Editor{
void clear_line(count_type i); void clear_line(count_type i);
void print_text(count_type i, string text); void print_text(count_type i, string text);
void print_current_line() { print_text(cur.r, file.get_line(file_offset + cur.r)); } void print_current_line() { print_text(cur.r, file.get_line(file_offset + cur.r)); }
void print_file(count_type start);
// User input // User input
template <typename F> template <typename F>
@ -138,8 +141,8 @@ public:
selection = Selection(&file); selection = Selection(&file);
current_insert = ""; current_insert = "";
print_file(0);
} }
void print_file(count_type start = 0);
// The big function // The big function
bool take_action(); bool take_action();

View file

@ -1,5 +1,31 @@
#include "everything.hpp" #include "everything.hpp"
// Substitute tab characters into regular tabs
string substitute_tabs(string s) {
string ret = "";
for(count_type i = 0; i < s.size(); ++i) {
if(s[i] == '\t') {
ret += ' ';
while(ret.size() % TAB_SIZE != 0)
ret += ' ';
}
else
ret += s[i];
}
return ret;
}
// Get how much tabs changed position on a row
count_type 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;
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -29,8 +55,6 @@ int main(int argc, char* argv[]) {
Editor ed(filename, file); Editor ed(filename, file);
ed.print_file();
// Main loop // Main loop
while(ed.take_action()) while(ed.take_action())
refresh(); refresh();

View file

@ -6,31 +6,6 @@ count_type Treap::get_size(line* l) {
return l->size; return l->size;
} }
string Treap::substitute_tabs(string s) {
string ret = "";
for(count_type i = 0; i < s.size(); ++i) {
if(s[i] == '\t') {
ret += ' ';
while(ret.size() % TAB_SIZE != 0)
ret += ' ';
}
else
ret += s[i];
}
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) {
if(l == nullptr) return {nullptr, nullptr}; if(l == nullptr) return {nullptr, nullptr};