Skip to content

Commit

Permalink
refactor: improve number highlighting
Browse files Browse the repository at this point in the history
Highlight numbers only when they are preceded by a separator character,
which includes whitespace, punctuation characters or NUL.
  • Loading branch information
cezelot committed Sep 5, 2024
1 parent 20d00aa commit 7ae2a62
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/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/05 14:13:44 by cezelot */
/* Updated: 2024/09/05 16:56:43 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara */
/* */
Expand All @@ -26,20 +26,41 @@

#include "eve.h"

static int
is_separator(int c)
{
return (isspace(c) || c == '\0'
|| strchr(",.()+-/*=~%<>[];", c) != NULL);
}

/* Highlight the characters of ROW. */
void
update_syntax(t_erow *row)
{
int i = 0;
char c;
unsigned char prev_hl;
int prev_sep = 0;
int i = 0;

row->hl = realloc(row->hl, row->rsize);
if (row->hl == NULL)
return ;
memset(row->hl, HL_NORMAL, row->rsize);
while (i < row->rsize) {
if (isdigit(row->render[i])) {
c = row->render[i];
if (i > 0) {
prev_hl = row->hl[i - 1];
} else {
prev_hl = 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;
}
}
Expand Down

0 comments on commit 7ae2a62

Please sign in to comment.