From 660f3ff4eb468882a0b110e2a6a5e59d3526017f Mon Sep 17 00:00:00 2001 From: Fernando Dodino Date: Tue, 31 Dec 2024 18:42:47 -0300 Subject: [PATCH] delegating to Literal node --- .../client/src/highlighter/tokenProvider.ts | 47 ++++++++----------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/packages/client/src/highlighter/tokenProvider.ts b/packages/client/src/highlighter/tokenProvider.ts index 13e6c1e..d7ede50 100644 --- a/packages/client/src/highlighter/tokenProvider.ts +++ b/packages/client/src/highlighter/tokenProvider.ts @@ -31,11 +31,24 @@ type ProcesamientoComentario = { presetIndex?: number; } -type LiteralType = 'number' | 'bigint' | 'boolean' | 'string' | 'object' | 'function' | 'symbol' | 'undefined' +/* ============================================================================ */ + +const getKindForLiteral = (node: Literal): string | undefined => { + if (node.isNumeric()) return 'Literal_number' + if (node.isBoolean()) return 'Literal_bool' + if (node.isString()) return 'Literal_string' + return undefined +} + +const getLengthForLiteral = (node: Literal, value: string): number => + value.length + (node.isString() ? 2 : 0) + +const getColumnForLiteral = (node: Literal, word: string, value: string): number => + word.indexOf(value) - (node.isString() ? 1 : 0) /* ============================================================================ */ -function getLine(node: Node, documentLines: string[]): LineResult { +const getLine = (node: Node, documentLines: string[]): LineResult => { const start = node.sourceMap.start const line = start.line - 1 const column = start.column - 1 @@ -194,40 +207,18 @@ function processNode(node: Node, textDocument: string[], context: NodeContext[]) }), when(Return)(defaultHighlightNoReference), when(Literal)(node => { - const getKindForLiteral = (type: LiteralType): string | undefined => { - switch (type) { - case 'number': - case 'bigint': - return 'Literal_number' - case 'boolean': - return 'Literal_bool' - case 'string': - return 'Literal_string' - default: - return undefined - } - } - - const getLengthForLiteral = (type: LiteralType, value: string): number => - value.length + (type === 'string' ? 2 : 0) - - const getColumnForLiteral = (word: string, value: string): number => - word.indexOf(value) - (type === 'string' ? 1 : 0) - if (node.isSynthetic) return nullHighlighting + if (node.isNull()) return dropSingleReference(customPlotter(node, KEYWORDS.NULL)) - if (node.value === null) return dropSingleReference(customPlotter(node, KEYWORDS.NULL)) - - const type = typeof node.value const value = node.value?.toString() - const literalKind = getKindForLiteral(type) + const literalKind = getKindForLiteral(node) if (!literalKind) return nullHighlighting const { line, column, word } = getLine(node, textDocument) return dropSingleReference(plotter({ ln: line, - col: column + getColumnForLiteral(word, value), - len: getLengthForLiteral(type, value), + col: column + getColumnForLiteral(node, word, value), + len: getLengthForLiteral(node, value), }, literalKind)) }), when(Package)(node => {