Skip to content

Commit

Permalink
add validation instead of filtering bad nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Dec 28, 2024
1 parent 7f930ec commit f60ff91
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
7 changes: 2 additions & 5 deletions packages/client/src/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ const convertToVSCPosition = (position: WollokPosition) =>

const convertToVSCTokens = (wollokNodesPlotter: WollokNodePlotter[]) =>
wollokNodesPlotter
.filter(wollokNodePlotter => {
const { range } = wollokNodePlotter
return !!range && range.start && range.end
})
.map(wollokNodePlotter => {
const { range } = wollokNodePlotter
const { start, end } = range
Expand All @@ -38,7 +34,8 @@ export const provider: vscode.DocumentSemanticTokensProvider = {
const splittedLines = textFile.split('\n')
const processed = excludeNullish([]
.concat(convertToVSCTokens(processCode(parsedPackage.members[0], splittedLines)))
.concat(processComments(splittedLines)))
.concat(processComments(splittedLines))
)

processed.forEach((node: WollokNodePlotter) =>
tokensBuilder.push(node.range as unknown as vscode.Range, node.tokenType, node.tokenModifiers)
Expand Down
13 changes: 8 additions & 5 deletions packages/client/src/highlighter/tokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,14 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
if (node.sourceMap == undefined) return nullHighlighting
const currentNode = node as unknown as NamedNode
const validName = node.name !== undefined && node.name.trim().length
return { result: [
!node.isClosure() ? defaultKeywordPlotter(node) : [],
node.supertypes.length ? keywordPlotter(node, KEYWORDS.INHERITS) : [],
validName ? generatePlotterForNode(currentNode) : [],
], references: validName ? saveReference(currentNode) : undefined }
const result = []
if (!node.isClosure()) result.push(defaultKeywordPlotter(node))
if (node.supertypes.length) result.push(keywordPlotter(node, KEYWORDS.INHERITS))
if (validName) result.push(generatePlotterForNode(currentNode))
return {
result,
references: validName ? saveReference(currentNode) : undefined,
}
}),
when(Field)(node =>
node.isSynthetic ? nullHighlighting : resultForReference(node)
Expand Down
1 change: 0 additions & 1 deletion packages/client/src/test/highlighter/inheritance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ suite('inheritance sample', () => {
})

test('highlights class names', () => {
console.info(JSON.stringify(processed.filter(t => t.tokenType === 'class')))
const keywordsTokens = processedByTokenType(processed, 'class')

const nextRange = () => keywordsTokens.next().value.range
Expand Down
10 changes: 9 additions & 1 deletion packages/client/src/test/highlighter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { readFileSync } from 'fs'
import { processCode, processComments } from '../../highlighter/tokenProvider'
import { WollokNodePlotter } from '../../highlighter/utils'

const validateHighlighter = (wollokNodesPlotter: WollokNodePlotter[]) =>
wollokNodesPlotter.every(wollokNodePlotter => {
const { range } = wollokNodePlotter
return !!range && range.start && range.end
})

export const readFileForTokenProvider = (filePath: string): WollokNodePlotter[] => {
const parsedFile = parse.File(filePath)
const docText = readFileSync(filePath, { encoding: 'utf-8' })
const parsedPackage = parsedFile.tryParse(docText)
const splittedLines = docText.split('\n')
return excludeNullish(processCode(parsedPackage.members[0], splittedLines)).concat(processComments(splittedLines))
const processed = excludeNullish(processCode(parsedPackage.members[0], splittedLines)).concat(processComments(splittedLines))
validateHighlighter(processed)
return processed
}

export const processedByTokenType = (processed: WollokNodePlotter[], tokenType: string): IterableIterator<WollokNodePlotter> => processed.filter(token => token.tokenType === tokenType).values()

Check warning on line 22 in packages/client/src/test/highlighter/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/test/highlighter/utils.ts#L1-L22

Added lines #L1 - L22 were not covered by tests

0 comments on commit f60ff91

Please sign in to comment.