Start of highlighting multi-line comments

This commit is contained in:
Timothy Warren 2019-08-21 09:01:54 -04:00
parent 1c0db7e5da
commit 85335749fa

83
kilo.c
View File

@ -41,6 +41,7 @@ enum editorKey {
enum editorHighlight {
HL_NORMAL = 0,
HL_COMMENT,
HL_MLCOMMENT,
HL_KEYWORD1,
HL_KEYWORD2,
HL_STRING,
@ -53,36 +54,49 @@ enum editorHighlight {
/*** data ***/
/**
* Struct representing parsing parameters for a file type
*/
struct editorSyntax {
char *filetype;
char **filematch;
char **keywords;
char *singleline_comment_start;
char *multiline_comment_start;
char *multiline_comment_end;
int flags;
};
/**
* Struct representing a row in the editor
*/
typedef struct erow {
int size;
int rsize;
char *chars;
char *render;
unsigned char *hl;
int size; // Number of characters
int rsize; // Number of characters rendered to screen
char *chars; // Input characters
char *render; // Display characters
unsigned char *hl; // Highlighted representation of characters
} erow;
/**
* Global editor state
*
* // Nested comment to check for double highlight
*/
struct editorConfig {
int cx, cy;
int rx;
int rowoff;
int coloff;
int screenrows;
int screencols;
int cx, cy; // Cursor position
int rx; // Cursor render position
int rowoff; // Vertical scroll offset
int coloff; // Horizontal scroll offset
int screenrows; // Number of rows visible in the current terminal
int screencols; // Number of columns visible in the current terminal
int numrows;
erow *row;
int dirty;
char *filename;
char statusmsg[80];
erow *row; // Current row
int dirty; // File modification check flag
char *filename; // Name of the current file
char statusmsg[80]; // Message for the status bar
time_t statusmsg_time;
struct editorSyntax *syntax;
struct editorSyntax *syntax; // Type of syntax for current file
struct termios orig_termios;
};
@ -104,7 +118,7 @@ struct editorSyntax HLDB[] = {
"c",
C_HL_extensions,
C_HL_keywords,
"//",
"//", "/*", "*/",
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS
},
};
@ -317,10 +331,16 @@ void editorUpdateSyntax(erow *row)
char **keywords = E.syntax->keywords;
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 mcs_len = mcs ? strlen(mcs): 0;
int mce_len = mce ? strlen(mce): 0;
int prev_sep = 1;
int in_string = 0;
int in_comment = 0;
int i = 0;
while (i < row->rsize)
@ -337,6 +357,34 @@ void editorUpdateSyntax(erow *row)
}
}
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 (in_string)
@ -418,6 +466,7 @@ int editorSyntaxToColor(int hl)
switch (hl)
{
case HL_COMMENT:
case HL_MLCOMMENT:
return 36; // cyan
case HL_KEYWORD1: