Skip to content

Commit

Permalink
Add literals and fix highlight class in new
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Dec 31, 2024
1 parent aa093b3 commit 24e740c
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 20 deletions.
29 changes: 9 additions & 20 deletions packages/client/src/highlighter/tokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
return plotter({ ln: line - 1, col: column, len: token.length }, kind)
}

Check warning on line 62 in packages/client/src/highlighter/tokenProvider.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/highlighter/tokenProvider.ts#L60-L62

Added lines #L60 - L62 were not covered by tests
const defaultKeywordPlotter = (node: Node) => customPlotter(node, keywords[node.kind])
const keywordAndNodePlotter = (node: NamedNode) => dropReference([
defaultKeywordPlotter(node),
generatePlotterForNode(node),
])

const saveReference = (node: NamedNode) => ({ name: node.name, type: node.kind })
const dropSingleReference = (node: WollokNodePlotter): HighlightingResult => dropReference([node])
Expand Down Expand Up @@ -103,11 +99,10 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
references: saveReference(node) })

Check warning on line 99 in packages/client/src/highlighter/tokenProvider.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/highlighter/tokenProvider.ts#L95-L99

Added lines #L95 - L99 were not covered by tests
),
when(Singleton)(node => {
if (node.sourceMap == undefined) return nullHighlighting
if (node.sourceMap == undefined || node.isClosure()) return nullHighlighting
const currentNode = node as unknown as NamedNode
const validName = node.name !== undefined && node.name.trim().length
const result = []
if (!node.isClosure()) result.push(defaultKeywordPlotter(node))
const result = [defaultKeywordPlotter(node)]
if (node.supertypes.length) result.push(customPlotter(node, KEYWORDS.INHERITS))
if (validName) result.push(generatePlotterForNode(currentNode))
return {
Expand Down Expand Up @@ -190,19 +185,8 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
}),
when(Return)(defaultHighlightNoReference),
when(Literal)(node => {
if(node.isSynthetic) return nullHighlighting
if (node.isSynthetic) return nullHighlighting
const tipo = typeof node.value
if(tipo == 'object'){
const closure = node.value as unknown as Singleton
if(closure){
//Literal<Singleton> es un Closure. contiene Field y Method
/*closure.forEach(nodo => {
nodo
})*/
}
return nullHighlighting//plotter({ ln: linea, col: col, len: len }, 'Singleton')
}

const { line, column, word } = getLine(node, documentoStr)
switch (tipo) {
case 'number':
Expand Down Expand Up @@ -258,7 +242,12 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
}
return dropReference(result)

Check warning on line 243 in packages/client/src/highlighter/tokenProvider.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/highlighter/tokenProvider.ts#L239-L243

Added lines #L239 - L243 were not covered by tests
}),
when(New)(defaultHighlightNoReference),
when(New)(node => ({
result: [
defaultKeywordPlotter(node),
generatePlotterForNode(node.instantiated),
], references: undefined,

Check warning on line 249 in packages/client/src/highlighter/tokenProvider.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/highlighter/tokenProvider.ts#L246-L249

Added lines #L246 - L249 were not covered by tests
})),
when(Self)(defaultHighlightNoReference),
when(Node)(_ => nullHighlighting)
)
Expand Down
8 changes: 8 additions & 0 deletions packages/client/src/test/highlighter/class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ suite('a class sample', () => {
expect(birthdateDefinitionRange.start).toEqual({ line: 2, column: 15 })
expect(birthdateDefinitionRange.end).toEqual({ line: 2, column: 24 })

const classInNewRange = nextRange()
expect(classInNewRange.start).toEqual({ line: 2, column: 31 })
expect(classInNewRange.end).toEqual({ line: 2, column: 35 })

const energyInFlyMethodRange1 = nextRange()
expect(energyInFlyMethodRange1.start).toEqual({ line: 5, column: 4 })
expect(energyInFlyMethodRange1.end).toEqual({ line: 5, column: 10 })
Expand All @@ -91,6 +95,10 @@ suite('a class sample', () => {
expect(energyInFlyMethodRange2.start).toEqual({ line: 5, column: 13 })
expect(energyInFlyMethodRange2.end).toEqual({ line: 5, column: 19 })

const classInNewRange2 = nextRange()
expect(classInNewRange2.start).toEqual({ line: 9, column: 22 })
expect(classInNewRange2.end).toEqual({ line: 9, column: 26 })

const birthdateInIsYoungMethodRange = nextRange()
expect(birthdateInIsYoungMethodRange.start).toEqual({ line: 9, column: 40 })
expect(birthdateInIsYoungMethodRange.end).toEqual({ line: 9, column: 49 })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Bird {
var energy = 100
var bigEnergy = 5000000000000
const name = "Pepita"
var happy = true
const born = new Date()
var closure = { value => value * 2 }
}
165 changes: 165 additions & 0 deletions packages/client/src/test/highlighter/literals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { WollokNodePlotter } from '../../highlighter/utils'
import { expect } from 'expect'
import { processedByTokenType, readFileForTokenProvider } from './utils'

suite('literals sample', () => {

let processed: WollokNodePlotter[]

setup(() => {
processed = readFileForTokenProvider('src/test/highlighter/highlighter-samples/literalsSample.wlk')
})

test('highlights keywords', () => {
const keywordsTokens = processedByTokenType(processed, 'keyword')

const nextRange = () => keywordsTokens.next().value.range

const classRange = nextRange()
expect(classRange.start).toEqual({ line: 0, column: 0 })
expect(classRange.end).toEqual({ line: 0, column: 5 })

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

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

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

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

const booleanRange = nextRange()
expect(booleanRange.start).toEqual({ line: 4, column: 14 })
expect(booleanRange.end).toEqual({ line: 4, column: 18 })

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

const newRange = nextRange()
expect(newRange.start).toEqual({ line: 5, column: 15 })
expect(newRange.end).toEqual({ line: 5, column: 18 })

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

test('highlights property names', () => {
const propertyTokens = processedByTokenType(processed, 'property')

const nextRange = () => propertyTokens.next().value.range

const energyVarRange = nextRange()
expect(energyVarRange.start).toEqual({ line: 1, column: 6 })
expect(energyVarRange.end).toEqual({ line: 1, column: 12 })

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

const nombreConstRange = nextRange()
expect(nombreConstRange.start).toEqual({ line: 3, column: 8 })
expect(nombreConstRange.end).toEqual({ line: 3, column: 12 })

const happyVarRange = nextRange()
expect(happyVarRange.start).toEqual({ line: 4, column: 6 })
expect(happyVarRange.end).toEqual({ line: 4, column: 11 })

const bornConstRange = nextRange()
expect(bornConstRange.start).toEqual({ line: 5, column: 8 })
expect(bornConstRange.end).toEqual({ line: 5, column: 12 })

const classInNewRange = nextRange()
expect(classInNewRange.start).toEqual({ line: 5, column: 19 })
expect(classInNewRange.end).toEqual({ line: 5, column: 23 })

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

test('highlights class', () => {
const classTokens = processedByTokenType(processed, 'class')

const nextRange = () => classTokens.next().value.range

const classRange = nextRange()
expect(classRange.start).toEqual({ line: 0, column: 6 })
expect(classRange.end).toEqual({ line: 0, column: 10 })
})

test('highlights numbers', () => {
const numberTokens = processedByTokenType(processed, 'number')

const nextRange = () => numberTokens.next().value.range

const energyValueRange = nextRange()
expect(energyValueRange.start).toEqual({ line: 1, column: 15 })
expect(energyValueRange.end).toEqual({ line: 1, column: 18 })

const bigEnergyValueRange = nextRange()
expect(bigEnergyValueRange.start).toEqual({ line: 2, column: 18 })
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 })
})

test('highlights strings', () => {
const methodTokens = processedByTokenType(processed, 'string')

const nextRange = () => methodTokens.next().value.range

const nameValueRange = nextRange()
expect(nameValueRange.start).toEqual({ line: 3, column: 15 })
expect(nameValueRange.end).toEqual({ line: 3, column: 23 })
})

test('highlights parameter', () => {
const variableTokens = processedByTokenType(processed, 'parameter')

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 })

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

test('highlights parameter', () => {
const variableTokens = processedByTokenType(processed, 'parameter')

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 })

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

test('highlights operator', () => {
const operatorTokens = processedByTokenType(processed, 'operator')

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 })
})

})

0 comments on commit 24e740c

Please sign in to comment.