Fix syntax highlight for number and decimal numbers

This commit is contained in:
Night Kaly 2024-07-03 13:23:38 +01:00
parent f3c3ed06c8
commit 4540698f70
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
2 changed files with 41 additions and 14 deletions

View file

@ -128,6 +128,15 @@ void find_callback(char *query, int key)
{ {
static int last_match = -1; static int last_match = -1;
static int direction = 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') { if (key == '\r' || key == '\x1b') {
last_match = -1; last_match = -1;
direction = 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.cx = row_rx_to_cx(row, match - row->render);
vip.rowoff = vip.rows; 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], HL_MATCH, strlen(query));
memset(&row->hl[match - row->render + strlen(query)], HL_RESET, row->render_size - (match - row->render + strlen(query))); memset(&row->hl[match - row->render + strlen(query)], HL_RESET, row->render_size - (match - row->render + strlen(query)));
break; break;

View file

@ -5,16 +5,30 @@
#include "vip.h" #include "vip.h"
int is_separator(int c) {
return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL;
}
void update_highlight(row *row) void update_highlight(row *row)
{ {
row->hl = realloc(row->hl, row->render_size); row->hl = realloc(row->hl, row->render_size);
memset(row->hl, HL_NORMAL, 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; 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) 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); return strdup(PEACH_BG);
case HL_MATCH:; case HL_MATCH:;
char *str = malloc(COLOR_LEN * 2 + 1); char *str = malloc(COLOR_LEN * 2 + 1);
snprintf(str, COLOR_LEN * 2 + 1, "%s%s", BLACK_BG, SKY_FG); snprintf(str, COLOR_LEN * 2 + 1, "%s%s", BLACK_BG, SKY_FG);
*len = COLOR_LEN * 2; *len = COLOR_LEN * 2;
return str; return str;
case HL_RESET:; case HL_RESET:;
char *res = malloc(COLOR_LEN * 2 + 1); char *res = malloc(COLOR_LEN * 2 + 1);
snprintf(res, COLOR_LEN * 2 + 1, "%s%s", WHITE_BG, BLACK_FG); snprintf(res, COLOR_LEN * 2 + 1, "%s%s", WHITE_BG, BLACK_FG);
*len = COLOR_LEN * 2; *len = COLOR_LEN * 2;
return res; return res;
default: default:
*len = COLOR_LEN; *len = COLOR_LEN;
return strdup(WHITE_BG); return strdup(WHITE_BG);
} }
} }