Handle tabs correctly

This commit is contained in:
Zachary Yedidia 2016-03-14 21:34:44 -04:00
parent 4beab9da1b
commit 52a6185455
6 changed files with 37 additions and 6 deletions

1
Makefile Normal file
View File

@ -0,0 +1 @@
test

View File

@ -1,6 +1,7 @@
import rope;
import std.string, std.stdio;
import std.string: split;
import std.stdio;
class Buffer {
private Rope text;

View File

@ -4,6 +4,8 @@ class Cursor {
int x, y;
int lastX;
int offset;
this() {}
this(int x, int y) {

View File

@ -5,7 +5,7 @@ import view;
import clipboard;
import std.stdio;
import std.file;
import std.file: readText, exists, isDir;
void main(string[] args) {
string filename = "";

7
src/util.d Normal file
View File

@ -0,0 +1,7 @@
string emptyString(int size) {
string str;
foreach (i; 0 .. size) {
str ~= " ";
}
return str;
}

View File

@ -3,10 +3,14 @@ import buffer;
import clipboard;
import cursor;
import statusline;
import util;
import std.regex: regex, replaceAll;
import std.conv: to;
import std.utf: count;
enum tabSize = 4;
class View {
uint topline;
uint xOffset;
@ -84,14 +88,24 @@ class View {
void cursorRight() {
if (cursor.x < buf.lines[cursor.y].length) {
cursor.x++;
if (buf.lines[cursor.y][cursor.x] == '\t') {
cursor.x++;
cursor.offset += tabSize-1;
} else {
cursor.x++;
}
cursor.lastX = cursor.x;
}
}
void cursorLeft() {
if (cursor.x > 0) {
cursor.x--;
if (buf.lines[cursor.y][cursor.x-1] == '\t') {
cursor.x--;
cursor.offset -= tabSize-1;
} else {
cursor.x--;
}
cursor.lastX = cursor.x;
}
}
@ -146,8 +160,14 @@ class View {
cursorDown();
cursor.x = 0;
cursor.lastX = cursor.x;
} else if (e.key == Key.tab) {
buf.insert(cloc, "\t");
cursorRight();
} else if (e.key == Key.backspace2) {
if (cloc > 0) {
if (buf.lines[cursor.y][cursor.x-1] == '\t') {
cursor.offset -= tabSize-1;
}
buf.remove(cloc-1, cloc);
setCursorLoc(cloc - 1);
cursor.lastX = cursor.x;
@ -190,7 +210,7 @@ class View {
setCell(cast(int) x++, cast(int) y, ' ', Color.basic, Color.basic);
// Write the line
foreach (dchar ch; line) {
foreach (dchar ch; line.replaceAll(regex("\t"), emptyString(tabSize))) {
setCell(x++, y, ch, Color.basic, Color.basic);
}
y++;
@ -200,7 +220,7 @@ class View {
if (cursor.y - topline < 0 || cursor.y - topline > height-1) {
hideCursor();
} else {
setCursor(cursor.x + xOffset, cursor.y - topline);
setCursor(cursor.x + xOffset + cursor.offset, cursor.y - topline);
}
sl.display();