From 337e4aa6bc4b71dbf3dbbe1e613ddb7569c024bb Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 20 Aug 2019 16:11:51 -0400 Subject: [PATCH] Improve number highlighting --- kilo.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/kilo.c b/kilo.c index ba59e67..a8aae1b 100644 --- a/kilo.c +++ b/kilo.c @@ -259,18 +259,34 @@ int getWindowSize(int *rows, int *cols) /*** syntax highlighting ***/ +int is_separator(int c) +{ + return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL; +} + void editorUpdateSyntax(erow *row) { row->hl = realloc(row->hl, row->rsize); memset(row->hl, HL_NORMAL, row->rsize); - int i; - for (i = 0; i < row->rsize; i++) + int prev_sep = 1; + + int i = 0; + while (i < row->rsize) { - if (isdigit(row->render[i])) + 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)) { row->hl[i] = HL_NUMBER; + i++; + prev_sep = 0; + continue; } + + prev_sep = is_separator(c); + i++; } }