highlight single line comments

This commit is contained in:
Timothy Warren 2019-08-20 16:53:28 -04:00
parent 4010b78fd7
commit 2f768daf31

26
kilo.c
View File

@ -40,6 +40,7 @@ enum editorKey {
enum editorHighlight { enum editorHighlight {
HL_NORMAL = 0, HL_NORMAL = 0,
HL_COMMENT,
HL_STRING, HL_STRING,
HL_NUMBER, HL_NUMBER,
HL_MATCH HL_MATCH
@ -53,6 +54,7 @@ enum editorHighlight {
struct editorSyntax { struct editorSyntax {
char *filetype; char *filetype;
char **filematch; char **filematch;
char *singleline_comment_start;
int flags; int flags;
}; };
@ -84,12 +86,14 @@ struct editorConfig {
struct editorConfig E; struct editorConfig E;
/*** filetypes ***/ /*** filetypes ***/
char *C_HL_extensions[] = { ".c", ".h", ".cpp", NULL }; char *C_HL_extensions[] = { ".c", ".h", ".cpp", NULL };
struct editorSyntax HLDB[] = { struct editorSyntax HLDB[] = {
{ {
"c", "c",
C_HL_extensions, C_HL_extensions,
"//",
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS
}, },
}; };
@ -154,6 +158,7 @@ int editorReadKey()
} }
} }
// The character code starts with the escape sequence (\x1b)
if (c == '\x1b') if (c == '\x1b')
{ {
char seq[3]; char seq[3];
@ -298,6 +303,9 @@ void editorUpdateSyntax(erow *row)
return; return;
} }
char *scs = E.syntax->singleline_comment_start;
int scs_len = scs ? strlen(scs) : 0;
int prev_sep = 1; int prev_sep = 1;
int in_string = 0; int in_string = 0;
@ -307,6 +315,15 @@ void editorUpdateSyntax(erow *row)
char c = row->render[i]; char c = row->render[i];
unsigned char prev_hl = (i > 0) ? row->hl[i - 1] : HL_NORMAL; unsigned char prev_hl = (i > 0) ? row->hl[i - 1] : HL_NORMAL;
if (scs_len && ! in_string)
{
if ( ! strncmp(&row->render[i], scs, scs_len))
{
memset(&row->hl[i], HL_COMMENT, row->rsize - i);
break;
}
}
if (E.syntax->flags & HL_HIGHLIGHT_STRINGS) if (E.syntax->flags & HL_HIGHLIGHT_STRINGS)
{ {
if (in_string) if (in_string)
@ -360,14 +377,17 @@ int editorSyntaxToColor(int hl)
{ {
switch (hl) switch (hl)
{ {
case HL_COMMENT:
return 36; // cyan
case HL_STRING: case HL_STRING:
return 35; return 35; // magenta
case HL_NUMBER: case HL_NUMBER:
return 31; return 31; // red
case HL_MATCH: case HL_MATCH:
return 34; return 34; // blue
default: default:
return 37; return 37;