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