Skip to content

Commit

Permalink
Add annotation highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Jan 6, 2025
1 parent 8edff25 commit 70b22d9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/client/src/highlighter/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Node } from 'wollok-ts'
export const tokenModifiers = ['declaration', 'definition', 'documentation', 'keyword']

export const WollokTokenKinds = {
'Annotation': 'decorator',
'Assignment': 'property',
'Body': 'property',
'Catch': 'property',
'Class': 'class',
'Comment':'comment',
'CommentMultiline':'comment-multiline',
'Describe': 'class',
'Environment': 'property',
'Field': 'property',
Expand Down Expand Up @@ -105,7 +105,7 @@ export const tokenTypes = [

export type NodeContext = {
name: string,
type: string
type: string,
}

export type NamedNode = Node & { name: string }
Expand Down
31 changes: 25 additions & 6 deletions packages/client/src/highlighter/tokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const getLine = (node: Node, documentLines: string[]): LineResult => {
const nullHighlighting = { result: undefined, references: undefined }

// ******************* References helpers
const saveAnnotationReference = (annotation: Annotation) => [{ name: annotation.name, type: 'Annotation' } ]
const saveReference = (node: NamedNode): NodeContext[] => [{ name: node.name, type: node.kind }]
const dropSingleReference = (node: WollokNodePlotter): HighlightingResult => dropReference([node])
const dropReference = (node: WollokNodePlotter[]): HighlightingResult => ({ result: node, references: undefined })
Expand Down Expand Up @@ -249,26 +250,44 @@ function processNode(node: Node, textDocument: string[], context: NodeContext[])
)
}

const processCommentForNode = (node: Node, textDocument: string[]): HighlightingResult => {
const processAnnotationsForNode = (node: Node, textDocument: string[], references: NodeContext[]): HighlightingResult => {
const fullDocument = textDocument.join('\n')

const commentPlotter = (comment: string) => {
const commentPlotter = (comment: string): WollokNodePlotter[] => {
const offset = textDocument.join('\n').indexOf(comment)
const start = getLineColumn(textDocument, offset)
const end = getLineColumn(textDocument, offset + comment.length)
return plotRange(textDocument, start, end, 'Comment')
}

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

View check run for this annotation

Codecov / codecov/patch

packages/client/src/highlighter/tokenProvider.ts#L257-L261

Added lines #L257 - L261 were not covered by tests

const annotationPlotter = (node: Node, annotationName: string): WollokNodePlotter[] => {
if (references.find(reference => reference.name === annotationName)) return []

let after = -1
const offsets = []
while ((after = fullDocument.indexOf(`@${annotationName}`, after + 1)) >= 0) {
offsets.push(after)
}
return offsets.map(offset => {
const { line, column } = getLineColumn(textDocument, offset)
return plotSingleLine({ line, column, length: annotationName.length + 1 }, 'Annotation')
})
}

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

View check run for this annotation

Codecov / codecov/patch

packages/client/src/highlighter/tokenProvider.ts#L264-L275

Added lines #L264 - L275 were not covered by tests

const processAnnotation = (annotation: Annotation): HighlightingResult =>
({ result: annotation.name === 'comment' ? commentPlotter(annotation.args.text as unknown as string) : annotationPlotter(node, annotation.name), references: saveAnnotationReference(annotation) })

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

View check run for this annotation

Codecov / codecov/patch

packages/client/src/highlighter/tokenProvider.ts#L278

Added line #L278 was not covered by tests

if (!node.sourceMap) return nullHighlighting

const commentsAnnotations = node.metadata.filter(({ name }: Annotation) => name == 'comment')
return commentsAnnotations.length ?
{ result: commentsAnnotations.flatMap((commentAnnotation) => commentPlotter(commentAnnotation.args.text as unknown as string)), references: undefined } : nullHighlighting
return node.metadata.reduce((finalResult, annotation) => mergeHighlightingResults(
finalResult, processAnnotation(annotation)

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

View check run for this annotation

Codecov / codecov/patch

packages/client/src/highlighter/tokenProvider.ts#L283

Added line #L283 was not covered by tests
), nullHighlighting)
}

export function processCode(node: Node, textDocument: string[]): WollokNodePlotter[] {
return node.reduce((acumResults, node: Node) =>
{
const nodeResults = mergeHighlightingResults(processNode(node, textDocument, acumResults.references), processCommentForNode(node, textDocument))
const nodeResults = mergeHighlightingResults(processNode(node, textDocument, acumResults.references), processAnnotationsForNode(node, textDocument, acumResults.references))
return mergeHighlightingResults(acumResults, nodeResults)
}, { result: [], references: [{ name: 'console', type: 'Reference' }] }).result
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ suite('comments & annotations sample', () => {
})

test('highlights comments', () => {
console.info(JSON.stringify(processed.filter(t => t.tokenType === 'comment')))

const parameterTokens = processedByTokenType(processed, 'comment')

const nextRange = () => parameterTokens.next().value.range
Expand Down Expand Up @@ -110,4 +108,24 @@ suite('comments & annotations sample', () => {
expect(multilineComment2_3Range.end).toEqual({ line: 17, column: 2 })
})

test('highlights annotations', () => {
console.info(JSON.stringify(processed.filter(t => t.tokenType === 'decorator')))

const parameterTokens = processedByTokenType(processed, 'decorator')

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

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

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

const parameterTypeDecoratorRange = nextRange()
expect(parameterTypeDecoratorRange.start).toEqual({ line: 12, column: 22 })
expect(parameterTypeDecoratorRange.end).toEqual({ line: 12, column: 27 })
})

})

0 comments on commit 70b22d9

Please sign in to comment.