Skip to content

Commit

Permalink
feat: highlight keywords
Browse files Browse the repository at this point in the history
Color keywords light blue or green.
  • Loading branch information
cezelot committed Oct 3, 2024
1 parent db6b71f commit f12da9e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/eve.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eve.h */
/* */
/* Created: 2023/11/27 17:17:10 by cezelot */
/* Updated: 2024/09/08 22:03:23 by cezelot */
/* Updated: 2024/10/01 08:39:14 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara, Alberto Rodriguez */
/* */
Expand Down Expand Up @@ -77,6 +77,8 @@ enum e_highlight_flags {
enum e_highlight {
HL_NORMAL = 0,
HL_COMMENT,
HL_KEYWORD1,
HL_KEYWORD2,
HL_STRING,
HL_NUMBER,
HL_MATCH
Expand All @@ -85,6 +87,7 @@ enum e_highlight {
typedef struct s_syntax {
char *filetype;
char **filematch;
char **keywords;
char *singleline_comment_start;
int flags;
} t_syntax;
Expand Down Expand Up @@ -158,6 +161,7 @@ void update_syntax(t_env *env, t_erow *row);
int is_separator(int c);
int match_extension(t_env *env, char *ext, t_syntax *syntax);
// -------------------------------------------------- syntax_highlighting_3.c --
int highlight_keyword(t_erow *row, char **keywords, int *i, int *prev_sep);
int highlight_number(t_erow *row, int *i,
int *prev_sep, unsigned char prev_hl);
int highlight_string(t_erow *row, int *i, int *prev_sep, int *in_string);
Expand Down
26 changes: 22 additions & 4 deletions src/syntax_highlighting/syntax_highlighting.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* syntax_highlighting.c */
/* */
/* Created: 2024/09/01 15:58:19 by cezelot */
/* Updated: 2024/09/08 22:44:55 by cezelot */
/* Updated: 2024/10/03 19:16:39 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara */
/* */
Expand Down Expand Up @@ -73,6 +73,12 @@ update_syntax(t_env *env, t_erow *row)
continue ;
}
}
if (prev_sep) {
if (highlight_keyword(row, env->syntax->keywords, &i,
&prev_sep)) {
continue ;
}
}
prev_sep = is_separator(row->render[i]);
++i;
}
Expand All @@ -87,10 +93,18 @@ select_syntax_highlight(t_env *env)
".cpp",
NULL
};
static char *c_keywords[] = {
"char", "short", "int", "long", "double", "float", "signed",
"unsigned", "void", "const", "static", "extern", "if", "else",
"while", "for", "switch", "case", "break", "continue", "return",

"struct|", "union|", "typedef|", "enum|", "class|", NULL
};
static t_syntax hldb[] = {
{
"c",
c_extensions,
c_keywords,
"//",
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS
}
Expand All @@ -117,15 +131,19 @@ syntax_to_color(int hl)
case HL_COMMENT:
/* foreground dark gray */
return (37);
case HL_MATCH:
case HL_KEYWORD1:
/* foreground light blue */
return (94);
case HL_KEYWORD2:
/* foreground green */
return (32);
case HL_STRING:
/* foreground cyan */
return (36);
case HL_NUMBER:
/* foreground light red */
return (91);
case HL_MATCH:
/* foreground light blue */
return (94);
default:
/* default foreground color */
return (39);
Expand Down
31 changes: 30 additions & 1 deletion src/syntax_highlighting/syntax_highlighting_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* syntax_highlighting_3.c */
/* */
/* Created: 2024/09/08 10:58:46 by cezelot */
/* Updated: 2024/09/08 15:38:45 by cezelot */
/* Updated: 2024/10/01 08:42:49 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara */
/* */
Expand All @@ -26,6 +26,35 @@

#include "../eve.h"

int
highlight_keyword(t_erow *row, char **keywords, int *i, int *prev_sep)
{
size_t n = 0;
size_t len;
size_t kw2;

while (keywords[n]) {
len = strlen(keywords[n]);
kw2 = keywords[n][len - 1] == '|';
if (kw2) {
--len;
}
if (!strncmp(&row->render[*i], keywords[n], len)
&& is_separator(row->render[*i + len])) {
memset(&row->hl[*i], kw2 ? HL_KEYWORD2 : HL_KEYWORD1,
len);
*i += len;
break ;
}
++n;
}
if (keywords[n] != NULL) {
*prev_sep = 0;
return (1);
}
return (0);
}

int
highlight_string(t_erow *row, int *i, int *prev_sep, int *in_string)
{
Expand Down

0 comments on commit f12da9e

Please sign in to comment.