Skip to content

Commit

Permalink
Add support for native keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Dec 31, 2024
1 parent c2c53d8 commit ba4701c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/client/src/highlighter/tokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ function processNode(node: Node, textDocument: string[], context: NodeContext[])
if (!node.sourceMap) return nullHighlighting

const generatePlotterForNode = (node: NamedNode) => customPlotter(node, node.name, node.kind)
const customPlotter = (node: Node, token: string, kind = 'Keyword') => {
const customPlotter = (node: Node, token: string, kind = 'Keyword', after?: number) => {
if (!token) throw new Error(`Invalid token for node ${node.kind}`)
const { line, column, word } = getLine(node, textDocument)
const col = column + word.indexOf(token)
const col = column + word.indexOf(token, after)
return plotter({ ln: line, col, len: token.length }, kind)
}
const generatePlotterAfterNode = (node: Node, token: string, kind = 'Keyword') => {
Expand Down Expand Up @@ -160,6 +160,7 @@ function processNode(node: Node, textDocument: string[], context: NodeContext[])
plotter({ ln: line, col, len: node.name.length }, node.kind),
defaultKeywordPlotter(node),
]
.concat(node.isNative() ? [customPlotter(node, KEYWORDS.NATIVE, 'Keyword', KEYWORDS.METHOD.length + 1 + node.name.length)] : [])
)

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ object pepita {

method realEnergy() = energy * self.nameValue()
method nameValue() = name.length()
method nativeMethod() native
}
10 changes: 9 additions & 1 deletion packages/client/src/test/highlighter/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ suite('an object sample', () => {
processed = readFileForTokenProvider('src/test/highlighter/highlighter-samples/objectSample.wlk')
})

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

const nextRange = () => keywordsTokens.next().value.range
Expand Down Expand Up @@ -50,6 +50,14 @@ suite('an object sample', () => {
const methodNameValueRange = nextRange()
expect(methodNameValueRange.start).toEqual({ line: 9, column: 2 })
expect(methodNameValueRange.end).toEqual({ line: 9, column: 8 })

const nativeMethodNameValueRange = nextRange()
expect(nativeMethodNameValueRange.start).toEqual({ line: 10, column: 2 })
expect(nativeMethodNameValueRange.end).toEqual({ line: 10, column: 8 })

const nativeKeywordNameValueRange = nextRange()
expect(nativeKeywordNameValueRange.start).toEqual({ line: 10, column: 24 })
expect(nativeKeywordNameValueRange.end).toEqual({ line: 10, column: 30 })
})

test('highlights properties', () => {
Expand Down

0 comments on commit ba4701c

Please sign in to comment.