commit 4540698f702df9c2339e72d6a41a5c3ba71afe9d
parent f3c3ed06c8378065c7acd865be558dc91c097f3e
Author: night0721 <[email protected]>
Date: Wed, 3 Jul 2024 13:23:38 +0100
Fix syntax highlight for number and decimal numbers
Diffstat:
2 files changed, 41 insertions(+), 14 deletions(-)
diff --git a/src/editor.c b/src/editor.c
@@ -128,6 +128,15 @@ void find_callback(char *query, int key)
{
static int last_match = -1;
static int direction = 1;
+
+ static int saved_hl_line;
+ static char *saved_hl = NULL;
+ if (saved_hl) {
+ memcpy(vip.row[saved_hl_line].hl, saved_hl, vip.row[saved_hl_line].render_size);
+ free(saved_hl);
+ saved_hl = NULL;
+ }
+
if (key == '\r' || key == '\x1b') {
last_match = -1;
direction = 1;
@@ -157,6 +166,10 @@ void find_callback(char *query, int key)
vip.cx = row_rx_to_cx(row, match - row->render);
vip.rowoff = vip.rows;
+ saved_hl_line = current;
+ saved_hl = malloc(row->render_size);
+ memcpy(saved_hl, row->hl, row->render_size);
+
memset(&row->hl[match - row->render], HL_MATCH, strlen(query));
memset(&row->hl[match - row->render + strlen(query)], HL_RESET, row->render_size - (match - row->render + strlen(query)));
break;
diff --git a/src/syntax.c b/src/syntax.c
@@ -5,16 +5,30 @@
#include "vip.h"
+int is_separator(int c) {
+ return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL;
+}
+
void update_highlight(row *row)
{
row->hl = realloc(row->hl, row->render_size);
memset(row->hl, HL_NORMAL, row->render_size);
- for (int i = 0; i < row->render_size; i++) {
- if (isdigit(row->render[i])) {
+
+ int prev_sep = 1;
+ int i = 0;
+ while (i < row->render_size) {
+ char c = row->render[i];
+ unsigned char prev_hl = (i > 0) ? row->hl[i - 1] : HL_NORMAL;
+ if ((isdigit(c) && (prev_sep || prev_hl == HL_NUMBER)) ||
+ (c == '.' && prev_hl == HL_NUMBER)) {
row->hl[i] = HL_NUMBER;
+ i++;
+ prev_sep = 0;
+ continue;
}
- }
-}
+ prev_sep = is_separator(c);
+ i++;
+ }}
char *syntax_to_color(int hl, size_t *len)
{
@@ -24,20 +38,20 @@ char *syntax_to_color(int hl, size_t *len)
return strdup(PEACH_BG);
case HL_MATCH:;
- char *str = malloc(COLOR_LEN * 2 + 1);
- snprintf(str, COLOR_LEN * 2 + 1, "%s%s", BLACK_BG, SKY_FG);
- *len = COLOR_LEN * 2;
- return str;
+ char *str = malloc(COLOR_LEN * 2 + 1);
+ snprintf(str, COLOR_LEN * 2 + 1, "%s%s", BLACK_BG, SKY_FG);
+ *len = COLOR_LEN * 2;
+ return str;
case HL_RESET:;
- char *res = malloc(COLOR_LEN * 2 + 1);
- snprintf(res, COLOR_LEN * 2 + 1, "%s%s", WHITE_BG, BLACK_FG);
- *len = COLOR_LEN * 2;
- return res;
+ char *res = malloc(COLOR_LEN * 2 + 1);
+ snprintf(res, COLOR_LEN * 2 + 1, "%s%s", WHITE_BG, BLACK_FG);
+ *len = COLOR_LEN * 2;
+ return res;
default:
- *len = COLOR_LEN;
- return strdup(WHITE_BG);
+ *len = COLOR_LEN;
+ return strdup(WHITE_BG);
}
}