Syntax highlight two types of keywords.

This commit is contained in:
Saumya Bhatnagar 2018-06-03 09:17:44 +05:30
parent 374f5ba474
commit 0418c00ea4
3 changed files with 44 additions and 3 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"termios.h": "c"
}
}

BIN
editor

Binary file not shown.

View File

@ -44,7 +44,9 @@ enum editorHighlight {
HL_COMMENT, HL_COMMENT,
HL_STRING, HL_STRING,
HL_NUMBER, HL_NUMBER,
HL_MATCH HL_MATCH,
HL_KEYWORD1,
HL_KEYWORD2
}; };
/*** data ***/ /*** data ***/
@ -54,6 +56,7 @@ struct editorSyntax {
char **filematch; char **filematch;
int flags; int flags;
char *singleline_comment_start; // single comment line pattern for this language char *singleline_comment_start; // single comment line pattern for this language
char **keywords;
}; };
typedef struct editorrow { typedef struct editorrow {
@ -85,12 +88,20 @@ struct editorConfig {
/*** filetypes ***/ /*** filetypes ***/
char *C_HL_extensions[] = { ".c", ".h", ".cpp", NULL }; char *C_HL_extensions[] = { ".c", ".h", ".cpp", NULL };
char *C_HL_keywords[] = {
"switch", "if", "while", "for", "break", "continue", "return", "else",
"struct", "union", "typedef", "static", "enum", "class", "case",
"int|", "long|", "double|", "float|", "char|", "unsigned|", "signed|",
"void|", "define", "include", 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,
"//" "//",
C_HL_keywords
}, },
}; };
@ -253,6 +264,8 @@ void editorUpdateSyntax(editorrow *row) {
if (E.syntax == NULL) return; if (E.syntax == NULL) return;
char **keywords = E.syntax->keywords;
char *scs = E.syntax->singleline_comment_start; char *scs = E.syntax->singleline_comment_start;
int scs_len = scs ? strlen(scs) : 0; int scs_len = scs ? strlen(scs) : 0;
@ -306,6 +319,25 @@ void editorUpdateSyntax(editorrow *row) {
} }
} }
if (prev_sep) {
int j;
for (j = 0; keywords[j]; j++) {
int klen = strlen(keywords[j]);
int kw2 = keywords[j][klen - 1] == '|';
if (kw2) klen--;
if (!strncmp(&row->render[i], keywords[j], klen) &&
is_separator(row->render[i + klen])) {
memset(&row->hl[i], kw2 ? HL_KEYWORD2 : HL_KEYWORD1, klen);
i += klen;
break;
}
}
if (keywords[j] != NULL) {
prev_sep = 0;
continue;
}
}
prev_sep = is_separator(c); prev_sep = is_separator(c);
i++; i++;
} }
@ -316,10 +348,14 @@ int editorSyntaxToColor(int hl) {
case HL_NUMBER: case HL_NUMBER:
return 32; return 32;
case HL_MATCH: // search query match case HL_MATCH: // search query match
return 31; return 91;
case HL_STRING: case HL_STRING:
return 33; return 33;
case HL_COMMENT: case HL_COMMENT:
return 92;
case HL_KEYWORD1:
return 31;
case HL_KEYWORD2:
return 34; return 34;
default: default:
return 37; return 37;