Improve language colorizers (#54)

* fix detection of javascript partial comments in diff output
* colorize python annotations
* add simplistic yaml colorizer
* colorize .gitignore files
* improve shell buffer diff output colorization
This commit is contained in:
Charlie Gordon 2024-12-14 23:56:54 +01:00 • committed by GitHub
commit e4fb8c8428
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 86 additions and 22 deletions

View file

@ -340,6 +340,10 @@ static void c_colorize_line(QEColorizeContext *cp,
if (!qe_isblank(prev))
break;
}
/* ignore end of comment in grep output */
if (start > indent && str[start - 1] == '*' && cp->partial_file)
break;
if (!qe_findchar("])", prev)
&& (qe_findchar(" [({},;=<>!~^&|*/%?:", prev)
|| sbuf[i1] == C_STYLE_KEYWORD
@ -1778,8 +1782,7 @@ static void js_colorize_line(QEColorizeContext *cp,
int state = cp->colorize_state;
//int type_decl; /* unused */
indent = 0;
//indent = cp_skip_blanks(str, 0, n);
indent = cp_skip_blanks(str, 0, n);
tag = !qe_isblank(str[0]) && (cp->s->mode == syn || cp->s->mode == &htmlsrc_mode);
start = i;
@ -1866,6 +1869,10 @@ static void js_colorize_line(QEColorizeContext *cp,
if (!qe_isblank(prev))
break;
}
/* ignore end of comment in grep output */
if (start > indent && str[start - 1] == '*' && cp->partial_file)
break;
if (!qe_findchar("])", prev)
&& (qe_findchar(" [({},;=<>!~^&|*/%?:", prev)
|| sbuf[i1] == C_STYLE_KEYWORD
@ -4427,8 +4434,7 @@ static void c3_colorize_line(QEColorizeContext *cp,
int state = cp->colorize_state;
//int type_decl; /* unused */
indent = 0;
//indent = cp_skip_blanks(str, 0, n);
indent = cp_skip_blanks(str, 0, n);
tag = !indent && cp->s->mode == syn;
start = i;
//type_decl = 0;

View file

@ -57,6 +57,7 @@ enum {
PYTHON_STYLE_KEYWORD = QE_STYLE_KEYWORD,
PYTHON_STYLE_FUNCTION = QE_STYLE_FUNCTION,
PYTHON_STYLE_REGEX = QE_STYLE_STRING,
PYTHON_STYLE_ANNOTATION = QE_STYLE_PREPROCESS,
};
enum { // Python flavors
@ -103,6 +104,11 @@ static void python_colorize_line(QEColorizeContext *cp,
style = PYTHON_STYLE_COMMENT;
break;
case '@':
i += ustr_get_identifier(kbuf, countof(kbuf), c, str, i, n);
style = PYTHON_STYLE_ANNOTATION;
break;
case '\'':
case '\"':
/* parse string constant */

View file

@ -58,11 +58,14 @@ static int sharp_mode_probe(ModeDef *mode, ModeProbeData *pd)
{
const char *p = cs8(pd->buf);
if (stristart(pd->filename, ".gitignore", NULL))
return 70;
while (qe_isspace(*p))
p++;
if (*p == '#') {
if (match_extension(pd->filename, mode->extensions))
if (match_extension(pd->filename, "txt"))
return 60;
return 30;
}
@ -71,14 +74,57 @@ static int sharp_mode_probe(ModeDef *mode, ModeProbeData *pd)
static ModeDef sharp_mode = {
.name = "sharp",
.extensions = "txt",
.mode_probe = sharp_mode_probe,
.colorize_func = sharp_colorize_line,
};
/*---------------- Yaml file coloring ----------------*/
/* Very simple colorizer: # introduces comments, strings */
enum {
YAML_STYLE_TEXT = QE_STYLE_DEFAULT,
YAML_STYLE_COMMENT = QE_STYLE_COMMENT,
YAML_STYLE_STRING = QE_STYLE_STRING,
};
static void yaml_colorize_line(QEColorizeContext *cp,
const char32_t *str, int n,
QETermStyle *sbuf, ModeDef *syn)
{
int i = 0, start;
char32_t c;
while (i < n) {
start = i;
c = str[i++];
switch (c) {
case '#':
i = n;
SET_STYLE(sbuf, start, i, YAML_STYLE_COMMENT);
continue;
case '\'':
case '\"':
while (i < n && str[i++] != c)
continue;
SET_STYLE(sbuf, start, i, YAML_STYLE_STRING);
continue;
default:
break;
}
}
}
static ModeDef yaml_mode = {
.name = "Yaml",
.extensions = "yaml|yml",
.colorize_func = yaml_colorize_line,
};
static int sharp_init(QEmacsState *qs)
{
qe_register_mode(qs, &sharp_mode, MODEF_SYNTAX);
qe_register_mode(qs, &yaml_mode, MODEF_SYNTAX);
return 0;
}

View file

@ -3544,20 +3544,24 @@ static int match_string(const char32_t *buf, int n, const char *str) {
return (str[i] == '\0') ? i : 0;
}
static int shell_grab_filename(const char32_t *buf, int n, char *dest, int size) {
static int shell_grab_filename(const char32_t *buf, int n,
char *dest, int size, int filter)
{
int i, j, len = 0;
for (i = 0; i < n; i++) {
char32_t c = buf[i];
if (c == '(')
break;
if (c == ':' && i > 1)
break;
if (c == '-' && i > 1) {
/* match -[0-9]+- for grep -[ABC] output */
for (j = i + 1; qe_isdigit(buf[j]); j++)
continue;
if (j > i + 1 && buf[j] == '-')
if (filter) {
if (c == '(')
break;
if (c == ':' && i > 1)
break;
if (c == '-' && i > 1) {
/* match -[0-9]+- for grep -[ABC] output */
for (j = i + 1; qe_isdigit(buf[j]); j++)
continue;
if (j > i + 1 && buf[j] == '-')
break;
}
}
if (qe_isspace(c)) {
if (len)
@ -3614,12 +3618,11 @@ void shell_colorize_line(QEColorizeContext *cp,
/* Detect patches and colorize according to filename */
if (str[0] == '+' || str[0] == '-') {
if (match_string(str, n, "+++ ") || match_string(str, n, "--- ")) {
shell_grab_filename(str + 4, n - 4, filename, countof(filename));
shell_grab_filename(str + 4, n - 4, filename, countof(filename), FALSE);
cp->colorize_state = qe_shell_find_mode(cp->s->qs, filename);
cp->colorize_state |= STATE_SHELL_SKIP | STATE_SHELL_KEEP;
return;
} else
if (match_string(str, n, "+") || match_string(str, n, "-")) {
} else {
start = 1;
}
} else
@ -3629,6 +3632,9 @@ void shell_colorize_line(QEColorizeContext *cp,
else
return;
} else
if (match_string(str, n, "diff ") || match_string(str, n, "Only in ")) {
return;
} else
if (match_string(str, n, "@@")) {
/* patch diff location lines */
cp->colorize_state &= STATE_SHELL_MASK; /* reset potential comment state */
@ -3637,7 +3643,7 @@ void shell_colorize_line(QEColorizeContext *cp,
if (match_string(str, n, "==> ")) {
/* head and tail file marker */
i = 4;
i += shell_grab_filename(str + i, n - i, filename, countof(filename));
i += shell_grab_filename(str + i, n - i, filename, countof(filename), FALSE);
if (match_string(str + i, n - i, " <==")) {
cp->colorize_state = qe_shell_find_mode(cp->s->qs, filename);
cp->colorize_state |= STATE_SHELL_KEEP;
@ -3663,7 +3669,7 @@ void shell_colorize_line(QEColorizeContext *cp,
for (k = 0; commands[k]; k++) {
if ((w = match_string(str + i, n - i, commands[k])) != 0) {
i += w;
shell_grab_filename(str + i, n - i, filename, countof(filename));
shell_grab_filename(str + i, n - i, filename, countof(filename), FALSE);
cp->colorize_state = qe_shell_find_mode(cp->s->qs, filename);
cp->colorize_state |= STATE_SHELL_KEEP;
start = n;
@ -3673,7 +3679,7 @@ void shell_colorize_line(QEColorizeContext *cp,
}
} else {
int mc;
w = shell_grab_filename(str + i, n - i, filename, countof(filename));
w = shell_grab_filename(str + i, n - i, filename, countof(filename), TRUE);
if (i == 0) {
char *p = strchr(filename, '@');
if (p != NULL && p > filename && p[-1] != ' ') {