From 01b8535c5e66b50a503569bb27d7bc3a61e06fff Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 16 Jul 2024 11:17:45 -0400 Subject: [PATCH] Highlight numbers properly --- README.md | 5 +++-- src/common/fns.ts | 9 +++++++++ src/common/row.ts | 21 ++++++++++++++++++--- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f6f7e37..244f71b 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,9 @@ To simplify running, I'm using [Just](https://github.com/casey/just). - Deno: `just deno-run [filename]` - TSX: `just tsx-run [filename]` -Alternatively, there are shell scripts for each runtime in the `bin` folder. -So you can run the editor by calling `./bin/deno.sh [filename]` without installing Just. +Alternatively, there are shell scripts for each runtime in the `bin` folder. So +you can run the editor by calling `./bin/deno.sh [filename]` without installing +Just. Deno is generally used for dev tools, but each runtime should be functionally equivalent running the text editor. diff --git a/src/common/fns.ts b/src/common/fns.ts index 9e95888..8e651c4 100644 --- a/src/common/fns.ts +++ b/src/common/fns.ts @@ -179,6 +179,15 @@ export function isControl(char: string): boolean { return isAscii(char) && (code === 0x7f || code < 0x20); } +/** + * Is the one char string a common separator/operator character + * + * @param char - a one character string to check + */ +export function isSeparator(char: string): boolean { + return /\s/.test(char) || char === '\0' || ',.()+-/*=~%<>[];'.includes(char); +} + /** * Get the key code for a ctrl chord * diff --git a/src/common/row.ts b/src/common/row.ts index 811616e..d143276 100644 --- a/src/common/row.ts +++ b/src/common/row.ts @@ -1,7 +1,13 @@ import Ansi from './ansi.ts'; import { SCROLL_TAB_SIZE } from './config.ts'; -import { arrayInsert, isAsciiDigit, strChars, strlen } from './fns.ts'; +import { + arrayInsert, + isAsciiDigit, + isSeparator, + strChars, + strlen, +} from './fns.ts'; import { highlightToColor, HighlightType } from './highlight.ts'; import Option, { None, Some } from './option.ts'; import { SearchDirection } from './types.ts'; @@ -240,8 +246,11 @@ export class Row { } } + let prevIsSeparator = true; let i = 0; for (; i < this.rsize;) { + const prevHighlight = (i > 0) ? highlighting[i - 1] : HighlightType.None; + // Highlight search matches if (word.isSome()) { if (matches.includes(i)) { @@ -254,14 +263,20 @@ export class Row { } } - // Highlight other syntax types + // Highlight numbers const ch = this.rchars[i]; - if (isAsciiDigit(ch)) { + const isNumeric = isAsciiDigit(ch) && + (prevIsSeparator || prevHighlight === HighlightType.Number); + const isDecimalNumeric = ch === '.' && + prevHighlight === HighlightType.Number; + const isHexNumeric = ch === 'x' && prevHighlight === HighlightType.Number; + if (isNumeric || isDecimalNumeric || isHexNumeric) { highlighting.push(HighlightType.Number); } else { highlighting.push(HighlightType.None); } + prevIsSeparator = isSeparator(ch); i += 1; }