Highlight strings

This commit is contained in:
Timothy Warren 2019-08-20 16:40:39 -04:00
parent dad0cbea87
commit 4010b78fd7

40
kilo.c
View File

@ -40,11 +40,13 @@ enum editorKey {
enum editorHighlight {
HL_NORMAL = 0,
HL_STRING,
HL_NUMBER,
HL_MATCH
};
#define HL_HIGHLIGHT_NUMBERS (1<<0)
#define HL_HIGHLIGHT_STRINGS (1<<1)
/*** data ***/
@ -88,7 +90,7 @@ struct editorSyntax HLDB[] = {
{
"c",
C_HL_extensions,
HL_HIGHLIGHT_NUMBERS
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS
},
};
@ -297,6 +299,7 @@ void editorUpdateSyntax(erow *row)
}
int prev_sep = 1;
int in_string = 0;
int i = 0;
while (i < row->rsize)
@ -304,6 +307,38 @@ void editorUpdateSyntax(erow *row)
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)) ||
@ -325,6 +360,9 @@ int editorSyntaxToColor(int hl)
{
switch (hl)
{
case HL_STRING:
return 35;
case HL_NUMBER:
return 31;