done syntax highlighting
This commit is contained in:
parent
ef959b69be
commit
682d102c88
53
tted.c
53
tted.c
@ -38,6 +38,7 @@ enum editorKey {
|
|||||||
enum editorHighlight {
|
enum editorHighlight {
|
||||||
HL_NORMAL = 0,
|
HL_NORMAL = 0,
|
||||||
HL_COMMENT,
|
HL_COMMENT,
|
||||||
|
HL_MLCOMMENT,
|
||||||
HL_KEYWORD1,
|
HL_KEYWORD1,
|
||||||
HL_KEYWORD2,
|
HL_KEYWORD2,
|
||||||
HL_STRING,
|
HL_STRING,
|
||||||
@ -54,15 +55,19 @@ struct editorSyntax {
|
|||||||
char **filematch;
|
char **filematch;
|
||||||
char **keywords;
|
char **keywords;
|
||||||
char *singleline_comment_start;
|
char *singleline_comment_start;
|
||||||
|
char *multiline_comment_start;
|
||||||
|
char *multiline_comment_end;
|
||||||
int flags;
|
int flags;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct erow {
|
typedef struct erow {
|
||||||
|
int idx;
|
||||||
int size;
|
int size;
|
||||||
int rsize;
|
int rsize;
|
||||||
char *chars;
|
char *chars;
|
||||||
char *render;
|
char *render;
|
||||||
unsigned char *hl;
|
unsigned char *hl;
|
||||||
|
int hl_open_comment;
|
||||||
} erow;
|
} erow;
|
||||||
|
|
||||||
struct editorConfig {
|
struct editorConfig {
|
||||||
@ -97,7 +102,7 @@ struct editorSyntax HLDB[] = {
|
|||||||
"c",
|
"c",
|
||||||
C_HL_extensions,
|
C_HL_extensions,
|
||||||
C_HL_keywords,
|
C_HL_keywords,
|
||||||
"//",
|
"//", "/*", "*/",
|
||||||
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS
|
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -234,9 +239,12 @@ int getWindowSize(int *rows, int *cols) {
|
|||||||
|
|
||||||
int is_separator(int c){
|
int is_separator(int c){
|
||||||
return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];\"", c) != NULL;
|
return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];\"", c) != NULL;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
hello
|
||||||
|
*/
|
||||||
|
|
||||||
void editorUpdateSyntax(erow *row){
|
void editorUpdateSyntax(erow *row){
|
||||||
row->hl = realloc(row->hl, row->rsize);
|
row->hl = realloc(row->hl, row->rsize);
|
||||||
memset(row->hl, HL_NORMAL, row->rsize);
|
memset(row->hl, HL_NORMAL, row->rsize);
|
||||||
@ -246,23 +254,48 @@ void editorUpdateSyntax(erow *row){
|
|||||||
char **keywords = E.syntax->keywords;
|
char **keywords = E.syntax->keywords;
|
||||||
|
|
||||||
char *scs = E.syntax->singleline_comment_start;
|
char *scs = E.syntax->singleline_comment_start;
|
||||||
|
char *mcs = E.syntax->multiline_comment_start;
|
||||||
|
char *mce = E.syntax->multiline_comment_end;
|
||||||
|
|
||||||
int scs_len = scs ? strlen(scs) : 0;
|
int scs_len = scs ? strlen(scs) : 0;
|
||||||
|
int mcs_len = mcs ? strlen(mcs) : 0;
|
||||||
|
int mce_len = mce ? strlen(mce) : 0;
|
||||||
|
|
||||||
int prev_sep = 1;
|
int prev_sep = 1;
|
||||||
int in_string = 0;
|
int in_string = 0;
|
||||||
|
int in_comment = (row->idx > 0 && E.row[row->idx - 1].hl_open_comment);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < row->rsize){
|
while(i < row->rsize){
|
||||||
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 (scs_len && !in_string && !in_comment){
|
||||||
if (!strncmp(&row->render[i], scs, scs_len)) {
|
if (!strncmp(&row->render[i], scs, scs_len)) {
|
||||||
memset(&row->hl[i], HL_COMMENT, row->rsize - i);
|
memset(&row->hl[i], HL_COMMENT, row->rsize - i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (mcs_len && mce_len && !in_string) {
|
||||||
|
if (in_comment) {
|
||||||
|
row->hl[i] = HL_MLCOMMENT;
|
||||||
|
if (!strncmp(&row->render[i], mce, mce_len)) {
|
||||||
|
memset(&row->hl[i], HL_MLCOMMENT, mce_len);
|
||||||
|
i += mce_len;
|
||||||
|
in_comment = 0;
|
||||||
|
prev_sep = 1;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else if (!strncmp(&row->render[i], mcs, mcs_len)) {
|
||||||
|
memset(&row->hl[i], HL_MLCOMMENT, mcs_len);
|
||||||
|
i += mcs_len;
|
||||||
|
in_comment = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (E.syntax->flags & HL_HIGHLIGHT_STRINGS) {
|
if (E.syntax->flags & HL_HIGHLIGHT_STRINGS) {
|
||||||
if (in_string) {
|
if (in_string) {
|
||||||
row->hl[i] = HL_STRING;
|
row->hl[i] = HL_STRING;
|
||||||
@ -320,10 +353,17 @@ void editorUpdateSyntax(erow *row){
|
|||||||
prev_sep = is_separator(c);
|
prev_sep = is_separator(c);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int changed = (row->hl_open_comment != in_comment);
|
||||||
|
row->hl_open_comment = in_comment;
|
||||||
|
if (changed && row->idx + 1< E.numrows){
|
||||||
|
editorUpdateSyntax(&E.row[row->idx+1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int editorSyntaxToColor(int hl){
|
int editorSyntaxToColor(int hl){
|
||||||
switch(hl){
|
switch(hl){
|
||||||
|
case HL_MLCOMMENT:
|
||||||
case HL_COMMENT: return 36;
|
case HL_COMMENT: return 36;
|
||||||
case HL_KEYWORD1: return 33;
|
case HL_KEYWORD1: return 33;
|
||||||
case HL_KEYWORD2: return 32;
|
case HL_KEYWORD2: return 32;
|
||||||
@ -418,6 +458,9 @@ void editorInsertRow(int at, char *s, size_t len) {
|
|||||||
if (at < 0 || at > E.numrows) return;
|
if (at < 0 || at > E.numrows) return;
|
||||||
E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
|
E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
|
||||||
memmove(&E.row[at +1], &E.row[at], sizeof(erow) * (E.numrows-at));
|
memmove(&E.row[at +1], &E.row[at], sizeof(erow) * (E.numrows-at));
|
||||||
|
for (int j = at + 1; j <= E.numrows; j++) E.row[j].idx++;
|
||||||
|
|
||||||
|
E.row[at].idx = at;
|
||||||
|
|
||||||
E.row[at].size = len;
|
E.row[at].size = len;
|
||||||
E.row[at].chars = malloc(len + 1);
|
E.row[at].chars = malloc(len + 1);
|
||||||
@ -426,6 +469,7 @@ void editorInsertRow(int at, char *s, size_t len) {
|
|||||||
E.row[at].rsize = 0;
|
E.row[at].rsize = 0;
|
||||||
E.row[at].render = NULL;
|
E.row[at].render = NULL;
|
||||||
E.row[at].hl = NULL;
|
E.row[at].hl = NULL;
|
||||||
|
E.row[at].hl_open_comment = 0;
|
||||||
editorUpdateRow(&E.row[at]);
|
editorUpdateRow(&E.row[at]);
|
||||||
E.numrows++;
|
E.numrows++;
|
||||||
E.dirty++;
|
E.dirty++;
|
||||||
@ -442,6 +486,7 @@ void editorDelRow(int at) {
|
|||||||
|
|
||||||
editorFreeRow(&E.row[at]);
|
editorFreeRow(&E.row[at]);
|
||||||
memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1));
|
memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1));
|
||||||
|
for (int j = at; j = E.numrows -1; j++) E.row[j].idx--;
|
||||||
E.numrows--;
|
E.numrows--;
|
||||||
E.dirty++;
|
E.dirty++;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user