Add simple syntax highlighting for strings

This commit is contained in:
Zachary Yedidia 2016-03-12 12:13:57 -05:00
parent bdc0c4644e
commit 38377dd5f8
3 changed files with 31 additions and 7 deletions

View File

@ -13,7 +13,7 @@ class Cursor {
@property int loc() {
int loc;
foreach (i; 0 .. y) {
loc += buf.lines[i].length + 1;
loc += buf.lines[i].count + 1;
}
loc += x;
return loc;
@ -22,12 +22,12 @@ class Cursor {
@property void loc(int value) {
int loc;
foreach (y, l; buf.lines) {
if (loc + l.length+1 > value) {
if (loc + l.count+1 > value) {
this.y = cast(int) y;
x = value - loc;
return;
} else {
loc += l.length+1;
loc += l.count+1;
}
}
}

View File

@ -1,6 +1,8 @@
import termbox;
import view;
import std.conv: to;
class StatusLine {
View view;
@ -16,11 +18,12 @@ class StatusLine {
if (view.buf.toString != view.buf.savedText) {
file ~= " +";
}
file ~= " (" ~ to!string(view.cursor.y) ~ "," ~ to!string(view.cursor.x) ~ ")";
foreach (x; 0 .. width()) {
if (x >= 1 && x < 1 + file.length) {
setCell(x, y, cast(uint) file[x - 1], Color.white, Color.blue);
setCell(x, y, cast(uint) file[x - 1], Color.black, Color.blue);
} else {
setCell(x, y, ' ', Color.white, Color.blue);
setCell(x, y, ' ', Color.black, Color.blue);
}
}
}

View File

@ -3,6 +3,8 @@ import cursor;
import buffer;
import clipboard;
import std.array: join;
import std.regex;
import std.conv: to;
import std.stdio;
@ -103,10 +105,12 @@ class View {
} else if (e.key == Key.enter) {
buf.insert(cursor.loc, "\n");
cursor.loc = cursor.loc + 1;
cursor.lastX = cursor.x;
} else if (e.key == Key.backspace2) {
if (cursor.loc != 0) {
cursor.loc = cursor.loc - 1;
buf.remove(cursor.loc, cursor.loc + 1);
cursor.lastX = cursor.x;
}
}
}
@ -123,6 +127,7 @@ class View {
void display() {
int x, y;
string[] lines;
if (topline + height > buf.lines.length) {
lines = buf.lines[topline .. $];
@ -131,6 +136,16 @@ class View {
}
ulong maxLength = to!string(buf.lines.length).length;
xOffset = cast(int) maxLength + 1;
int charNum;
string bufSrc = lines.join("\n");
auto r = regex("\".*?\"");
auto matches = bufSrc.matchAll(r);
Color[ulong] colors;
foreach (m; matches) {
colors[m.pre.length] = Color.blue;
colors[m.pre.length + m.hit.length] = Color.default_;
}
foreach (i, line; lines) {
string lineNum = to!string(i + topline + 1);
foreach (_; 0 .. maxLength - lineNum.length) {
@ -141,14 +156,20 @@ class View {
}
setCell(cast(int) x++, cast(int) y, ' ', Color.default_ | Attribute.bold, Color.black);
Color c;
foreach (dchar ch; line) {
setCell(cast(int) x++, cast(int) y, ch, Color.default_, Color.default_);
if (charNum in colors) {
c = colors[charNum];
}
setCell(cast(int) x++, cast(int) y, ch, c, Color.default_);
charNum += to!string(ch).length;
}
charNum++;
y++;
x = 0;
}
if (cursor.y - topline < 0 || cursor.y - topline > height) {
if (cursor.y - topline < 0 || cursor.y - topline > height-1) {
hideCursor();
} else {
setCursor(cursor.x + xOffset, cursor.y - topline);