Skip to content

Commit

Permalink
Add support for null highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Dec 31, 2024
1 parent 6b3d2da commit b816183
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
4 changes: 3 additions & 1 deletion packages/client/src/highlighter/tokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ function processNode(node: Node, textDocument: string[], context: NodeContext[])

if (node.isSynthetic) return nullHighlighting

if (node.value === null) return dropSingleReference(customPlotter(node, KEYWORDS.NULL))

const type = typeof node.value
const value = node.value.toString()
const value = node.value?.toString()
const literalKind = getKindForLiteral(type)
if (!literalKind) return nullHighlighting

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class Bird {
const name = "Pepita"
var happy = true
const born = new Date()
var nullValue = null
var closure = { value => value * 2 }
}
36 changes: 24 additions & 12 deletions packages/client/src/test/highlighter/literals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ suite('literals sample', () => {
const var4Range = nextRange()
expect(var4Range.start).toEqual({ line: 6, column: 2 })
expect(var4Range.end).toEqual({ line: 6, column: 5 })

const nullRange = nextRange()
expect(nullRange.start).toEqual({ line: 6, column: 18 })
expect(nullRange.end).toEqual({ line: 6, column: 22 })

const var5Range = nextRange()
expect(var5Range.start).toEqual({ line: 7, column: 2 })
expect(var5Range.end).toEqual({ line: 7, column: 5 })
})

test('highlights property names', () => {
Expand Down Expand Up @@ -77,9 +85,13 @@ suite('literals sample', () => {
expect(classInNewRange.start).toEqual({ line: 5, column: 19 })
expect(classInNewRange.end).toEqual({ line: 5, column: 23 })

const nullVarRange = nextRange()
expect(nullVarRange.start).toEqual({ line: 6, column: 6 })
expect(nullVarRange.end).toEqual({ line: 6, column: 15 })

const closureVarRange = nextRange()
expect(closureVarRange.start).toEqual({ line: 6, column: 6 })
expect(closureVarRange.end).toEqual({ line: 6, column: 13 })
expect(closureVarRange.start).toEqual({ line: 7, column: 6 })
expect(closureVarRange.end).toEqual({ line: 7, column: 13 })
})

test('highlights class', () => {
Expand All @@ -106,8 +118,8 @@ suite('literals sample', () => {
expect(bigEnergyValueRange.end).toEqual({ line: 2, column: 31 })

const twoAsParameterRange = nextRange()
expect(twoAsParameterRange.start).toEqual({ line: 6, column: 35 })
expect(twoAsParameterRange.end).toEqual({ line: 6, column: 36 })
expect(twoAsParameterRange.start).toEqual({ line: 7, column: 35 })
expect(twoAsParameterRange.end).toEqual({ line: 7, column: 36 })
})

test('highlights strings', () => {
Expand All @@ -122,9 +134,9 @@ suite('literals sample', () => {


test('highlights boolean', () => {
const operatorTokens = processedByTokenType(processed, 'enum')
const booleanTokens = processedByTokenType(processed, 'enum')

const nextRange = () => operatorTokens.next().value.range
const nextRange = () => booleanTokens.next().value.range

const booleanRange = nextRange()
expect(booleanRange.start).toEqual({ line: 4, column: 14 })
Expand All @@ -137,12 +149,12 @@ suite('literals sample', () => {
const nextRange = () => variableTokens.next().value.range

const parameterInClosureDefinitionRange = nextRange()
expect(parameterInClosureDefinitionRange.start).toEqual({ line: 6, column: 18 })
expect(parameterInClosureDefinitionRange.end).toEqual({ line: 6, column: 23 })
expect(parameterInClosureDefinitionRange.start).toEqual({ line: 7, column: 18 })
expect(parameterInClosureDefinitionRange.end).toEqual({ line: 7, column: 23 })

const parameterInClosureUseRange = nextRange()
expect(parameterInClosureUseRange.start).toEqual({ line: 6, column: 27 })
expect(parameterInClosureUseRange.end).toEqual({ line: 6, column: 32 })
expect(parameterInClosureUseRange.start).toEqual({ line: 7, column: 27 })
expect(parameterInClosureUseRange.end).toEqual({ line: 7, column: 32 })
})

test('highlights operator', () => {
Expand All @@ -151,8 +163,8 @@ suite('literals sample', () => {
const nextRange = () => operatorTokens.next().value.range

const lessThanOperatorRange = nextRange()
expect(lessThanOperatorRange.start).toEqual({ line: 6, column: 33 })
expect(lessThanOperatorRange.end).toEqual({ line: 6, column: 34 })
expect(lessThanOperatorRange.start).toEqual({ line: 7, column: 33 })
expect(lessThanOperatorRange.end).toEqual({ line: 7, column: 34 })
})

})

0 comments on commit b816183

Please sign in to comment.