Rework tabs, so they are a grid, not just n spaces

This commit is contained in:
Matúš Púll 2025-05-27 21:13:40 +02:00
parent 69f45fe50c
commit f8e0a06a62
3 changed files with 8 additions and 6 deletions

View file

@ -257,7 +257,7 @@ bool Editor::take_action() {
break; break;
case '\t': case '\t':
file.insert(cur + file_offset, "\t"); file.insert(cur + file_offset, "\t");
cur.c += TAB_SIZE; cur.c += (cur.c - 1) % TAB_SIZE + 1;
print_line(file_offset + cur.r); print_line(file_offset + cur.r);
break; break;
default: default:

View file

@ -5,7 +5,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#define TAB_SIZE 2 #define TAB_SIZE 8
using std::string; using std::string;
using std::vector; using std::vector;

View file

@ -9,9 +9,11 @@ count_type Treap::get_size(line* l) {
string Treap::substitute_tabs(string s) { string Treap::substitute_tabs(string s) {
string ret = ""; string ret = "";
for(count_type i = 0; i < s.size(); ++i) { for(count_type i = 0; i < s.size(); ++i) {
if(s[i] == '\t') if(s[i] == '\t') {
for(int j = 0; j < TAB_SIZE; j++) ret += ' ';
while(ret.size() % TAB_SIZE != 0)
ret += ' '; ret += ' ';
}
else else
ret += s[i]; ret += s[i];
} }
@ -145,13 +147,13 @@ vector<string> Treap::bulk_get_line(count_type r, count_type count) {
return ret; return ret;
} }
// TODO tabs are a grid, not just N tabs // Get how much tabs changed position on a row
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);
for(count_type i = 0; i + tab_offset < p.c; ++i) { for(count_type i = 0; i + tab_offset < p.c; ++i) {
if(line[i] == '\t') if(line[i] == '\t')
tab_offset += TAB_SIZE - 1; tab_offset += ((i+tab_offset-1) % TAB_SIZE);
if(i + tab_offset > p.c) if(i + tab_offset > p.c)
tab_offset -= i + tab_offset - p.c; tab_offset -= i + tab_offset - p.c;
} }