Syntax highlight strings.

This commit is contained in:
Saumya Bhatnagar 2018-06-03 08:36:33 +05:30
parent 083409143f
commit 67470d7707
2 changed files with 34 additions and 4 deletions

BIN
editor

Binary file not shown.

View File

@ -24,6 +24,7 @@
#define EDITOR_TAB 8
#define EDITOR_QUIT_TIMES 1
#define HL_HIGHLIGHT_NUMBERS (1<<0)
#define HL_HIGHLIGHT_STRINGS (1<<1)
enum editorKey {
BACKSPACE = 127,
@ -39,9 +40,10 @@ enum editorKey {
};
enum editorHighlight {
HL_NORMAL = 0,
HL_NUMBER,
HL_MATCH
HL_NORMAL = 0,
HL_STRING,
HL_NUMBER,
HL_MATCH
};
/*** data ***/
@ -85,7 +87,7 @@ struct editorSyntax HLDB[] = {
{
"c",
C_HL_extensions,
HL_HIGHLIGHT_NUMBERS
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS
},
};
@ -249,11 +251,37 @@ void editorUpdateSyntax(editorrow *row) {
if (E.syntax == NULL) return;
int prev_sep = 1;
int in_string = 0;
int i = 0;
while (i < row->rsize) {
char c = row->render[i];
unsigned char prev_hl = (i > 0) ? row->hl[i - 1] : HL_NORMAL;
if (E.syntax->flags & HL_HIGHLIGHT_STRINGS) {
if (in_string) {
row->hl[i] = HL_STRING;
if (c == '\\' && i + 1 < row->rsize) {
row->hl[i + 1] = HL_STRING;
i += 2;
continue;
}
if (c == in_string) in_string = 0;
i++;
prev_sep = 1;
continue;
} else {
if (c == '"' || c == '\'') {
in_string = c;
row->hl[i] = HL_STRING;
i++;
continue;
}
}
}
if (E.syntax->flags & HL_HIGHLIGHT_NUMBERS) {
if ((isdigit(c) && (prev_sep || prev_hl == HL_NUMBER)) ||
(c == '.' && prev_hl == HL_NUMBER)) {
@ -275,6 +303,8 @@ int editorSyntaxToColor(int hl) {
return 32;
case HL_MATCH: // search query match
return 31;
case HL_STRING:
return 33;
default:
return 37;
}