From ba4701cb4c1af9ea2f3cfd5a4cb750a9482d72c8 Mon Sep 17 00:00:00 2001 From: Fernando Dodino Date: Tue, 31 Dec 2024 08:12:51 -0300 Subject: [PATCH] Add support for native keyword --- packages/client/src/highlighter/tokenProvider.ts | 5 +++-- .../highlighter/highlighter-samples/objectSample.wlk | 1 + packages/client/src/test/highlighter/object.test.ts | 10 +++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/client/src/highlighter/tokenProvider.ts b/packages/client/src/highlighter/tokenProvider.ts index dab78e9..a70b4f6 100644 --- a/packages/client/src/highlighter/tokenProvider.ts +++ b/packages/client/src/highlighter/tokenProvider.ts @@ -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') => { @@ -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 { diff --git a/packages/client/src/test/highlighter/highlighter-samples/objectSample.wlk b/packages/client/src/test/highlighter/highlighter-samples/objectSample.wlk index 01a5503..3ea3f51 100644 --- a/packages/client/src/test/highlighter/highlighter-samples/objectSample.wlk +++ b/packages/client/src/test/highlighter/highlighter-samples/objectSample.wlk @@ -8,4 +8,5 @@ object pepita { method realEnergy() = energy * self.nameValue() method nameValue() = name.length() + method nativeMethod() native } \ No newline at end of file diff --git a/packages/client/src/test/highlighter/object.test.ts b/packages/client/src/test/highlighter/object.test.ts index 78de1ad..a306621 100644 --- a/packages/client/src/test/highlighter/object.test.ts +++ b/packages/client/src/test/highlighter/object.test.ts @@ -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 @@ -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', () => {