diff --git a/packages/client/src/highlighter.ts b/packages/client/src/highlighter.ts index 344da6f..fdf59d6 100644 --- a/packages/client/src/highlighter.ts +++ b/packages/client/src/highlighter.ts @@ -1,9 +1,8 @@ ///esModuleIn import * as vscode from 'vscode' import { excludeNullish, parse } from 'wollok-ts' -import { WollokNodePlotter, WollokPosition } from './highlighter/utils' import { processCode } from './highlighter/tokenProvider' -import { tokenModifiers, tokenTypes } from './highlighter/definitions' +import { tokenModifiers, tokenTypes, WollokNodePlotter, WollokPosition } from './highlighter/definitions' const convertToVSCPosition = (position: WollokPosition) => new vscode.Position(position.line, position.column) diff --git a/packages/client/src/highlighter/definitions.ts b/packages/client/src/highlighter/definitions.ts index 5ab77c3..993b8c5 100644 --- a/packages/client/src/highlighter/definitions.ts +++ b/packages/client/src/highlighter/definitions.ts @@ -1,7 +1,8 @@ -import { createRange, WollokNodePlotter } from './utils' +import { Node } from 'wollok-ts' export const tokenModifiers = ['declaration', 'definition', 'documentation', 'keyword'] -export const tokenTypeObj = { + +export const WollokTokenKinds = { 'Assignment': 'property', 'Body': 'property', 'Catch': 'property', @@ -38,20 +39,23 @@ export const tokenTypeObj = { 'Variable': 'variable', } -export const keywords = { - /* - 'Parameter':'property', - 'ParameterizedType':'property', - 'NamedArgument':'property', - 'Body':'property',*/ - 'Package':'package', +export const WollokKeywords = { + 'Always': 'then always', + 'Assignment':'=', + 'Catch': 'catch', + 'Class': 'class', + 'Describe':'describe', + 'Else':'else', + 'Field':'var', + 'If':'if', 'Import':'import', + 'Method': 'method', + 'Mixin': 'class', + 'New':'new', + 'Package':'package', 'Program':'program', - 'Test':'test', - 'Describe':'describe', - 'Singleton': 'object', - //'Mixin':'property', - 'Variable': ['var', 'const'], + 'Return': 'return', + 'Self':'self', 'Send': [ // eslint-disable-next-line array-element-newline '+', '*', '-', '/', '<', '>', '<=', '>=', @@ -62,71 +66,77 @@ export const keywords = { // eslint-disable-next-line array-element-newline '==', '!=', ], - 'Field':'var', - 'Method': 'method', - 'Return': 'return', - 'Assignment':'=', - //'Reference':'property', - 'Self':'self', + 'Singleton': 'object', 'Super':'super', - 'New':'new', - 'If':'if', - 'Else':'else', - 'Try': 'try', - 'Catch': 'catch', - 'Always': 'then always', + 'Test':'test', 'Throw': 'throw', - //'Literal':'property', - /*'Super':'property', - 'Environment':'property', - */ - 'Class': 'class', + 'Try': 'try', + 'Variable': ['var', 'const'], } // Standard token types: // ID Description export const tokenTypes = [ - 'class', // For identifiers that declare or reference a class type. - 'comment', // For tokens that represent a comment. - 'decorator', // For identifiers that declare or reference decorators and annotations. - 'enum', // For identifiers that declare or reference an enumeration type. - 'enumMember', // For identifiers that declare or reference an enumeration property, constant, or member. - 'event', // For identifiers that declare an event property. - 'function', // For identifiers that declare a function. - 'interface', // For identifiers that declare or reference an interface type. - 'keyword', // For tokens that represent a language keyword. - 'label', // For identifiers that declare a label. - 'macro', // For identifiers that declare a macro. - 'method', // For identifiers that declare a member function or method. - 'namespace', // For identifiers that declare or reference a namespace, module, or package. - 'number', // For tokens that represent a number literal. - 'object', //No es parte de los tipos por default - 'operator', // For tokens that represent an operator. - 'parameter', // For identifiers that declare or reference a function or method parameters. - 'property', // For identifiers that declare or reference a member property, member field, or member variable. - 'regexp', // For tokens that represent a regular expression literal. - 'string', // For tokens that represent a string literal. - 'struct', // For identifiers that declare or reference a struct type. - 'type', // For identifiers that declare or reference a type that is not covered above. - 'typeParameter', // For identifiers that declare or reference a type parameter. - 'variable', // For identifiers that declare or reference a local or global variable. + 'class', // For identifiers that declare or reference a class type. + 'comment', // For tokens that represent a comment. + 'decorator', // For identifiers that declare or reference decorators and annotations. + 'enum', // For identifiers that declare or reference an enumeration type. + 'enumMember', // For identifiers that declare or reference an enumeration property, constant, or member. + 'event', // For identifiers that declare an event property. + 'function', // For identifiers that declare a function. + 'interface', // For identifiers that declare or reference an interface type. + 'keyword', // For tokens that represent a language keyword. + 'label', // For identifiers that declare a label. + 'macro', // For identifiers that declare a macro. + 'method', // For identifiers that declare a member function or method. + 'namespace', // For identifiers that declare or reference a namespace, module, or package. + 'number', // For tokens that represent a number literal. + 'object', // Custom Wollok token type for WKOs and unnamed objects + 'operator', // For tokens that represent an operator. + 'parameter', // For identifiers that declare or reference a function or method parameters. + 'property', // For identifiers that declare or reference a member property, member field, or member variable. + 'regexp', // For tokens that represent a regular expression literal. + 'string', // For tokens that represent a string literal. + 'struct', // For identifiers that declare or reference a struct type. + 'type', // For identifiers that declare or reference a type that is not covered above. + 'typeParameter', // For identifiers that declare or reference a type parameter. + 'variable', // For identifiers that declare or reference a local or global variable. ] -export function plotSingleLine(start: { ln, col, len }, kind: string): WollokNodePlotter { - return { - range: createRange(start.ln, start.col, start.len), - tokenType: tokenTypeObj[kind], - tokenModifiers: ['declaration'], - } +export type NodeContext = { + name: string, + type: string +} + +export type NamedNode = Node & { name: string } + +export type LineResult = { + line: number, + column: number, + word: string, +} + +export type HighlightingResult = { + result: WollokNodePlotter[]; + references: NodeContext[] | undefined; } -export function plotMultiline(start: { ln, col }, end: { ln, col }, kind: string): WollokNodePlotter { - return { - range: createRange(start.ln, start.col, start.len), - tokenType: tokenTypeObj[kind], - tokenModifiers: ['declaration'], - } +export type WollokPosition = { + line: number, + column: number, } + +export type WollokRange = { + start: WollokPosition, + end: WollokPosition, +} + +export type WollokNodePlotter = { + range: WollokRange + tokenType: string + tokenModifiers?: string[] +} + /* Standard token modifiers diff --git a/packages/client/src/highlighter/tokenProvider.ts b/packages/client/src/highlighter/tokenProvider.ts index 7d040d9..858a8dc 100644 --- a/packages/client/src/highlighter/tokenProvider.ts +++ b/packages/client/src/highlighter/tokenProvider.ts @@ -1,26 +1,6 @@ import { Annotation, Assignment, Class, Describe, Field, If, Import, KEYWORDS, last, Literal, match, Method, NamedArgument, New, Node, Package, Parameter, Program, Reference, Return, Self, Send, Singleton, Super, Test, Throw, Try, Variable, when } from 'wollok-ts' -import { keywords, plotSingleLine, tokenTypeObj } from './definitions' -import { getLineColumn, mergeHighlightingResults, WollokNodePlotter } from './utils' - -type NodeContext = { - name: string, - type: string -} - -type NamedNode = Node & { name: string } - -type LineResult = { - line: number, - column: number, - word: string, -} - -export type HighlightingResult = { - result: WollokNodePlotter[]; - references: NodeContext[] | undefined; -} - -/* ============================================================================ */ +import { WollokKeywords, WollokTokenKinds, NamedNode, NodeContext, HighlightingResult, LineResult, WollokNodePlotter } from './definitions' +import { getLineColumn, mergeHighlightingResults, plotSingleLine } from './utils' const getKindForLiteral = (node: Literal): string | undefined => { if (node.isNumeric()) return 'Literal_number' @@ -71,7 +51,7 @@ function processNode(node: Node, textDocument: string[], context: NodeContext[]) const { line, column } = node.sourceMap.end return plotSingleLine({ ln: line - 1, col: column, len: token.length }, kind) } - const plotKeyword = (node: Node) => plot(node, keywords[node.kind]) + const plotKeyword = (node: Node) => plot(node, WollokKeywords[node.kind]) const plotReference = (node: Variable | Field) => { const result = [ plot(node, node.isConstant ? KEYWORDS.CONST : KEYWORDS.VAR), @@ -127,7 +107,7 @@ function processNode(node: Node, textDocument: string[], context: NodeContext[]) return { result: [ { ...plotNode(node), - tokenType: tokenTypeObj[reference.type], + tokenType: WollokTokenKinds[reference.type], }, ], references: undefined } } @@ -171,7 +151,7 @@ function processNode(node: Node, textDocument: string[], context: NodeContext[]) } }), when(Send)(node => { - const symbols = keywords[node.kind] + const symbols = WollokKeywords[node.kind] const { line, column, word } = getLine(node, textDocument) if (symbols?.includes(node.message)){ diff --git a/packages/client/src/highlighter/utils.ts b/packages/client/src/highlighter/utils.ts index 2a3b68f..5808e75 100644 --- a/packages/client/src/highlighter/utils.ts +++ b/packages/client/src/highlighter/utils.ts @@ -1,13 +1,27 @@ - // ================================================================================================ - // - // Uncomment this to have quick answer by running - // yarn run test:highlighter - // - // ================================================================================================ +import { HighlightingResult, WollokNodePlotter, WollokPosition, WollokRange, WollokTokenKinds } from './definitions' -import { HighlightingResult } from './tokenProvider' +/* ====================================================================================== */ +/* Helpers for plotter */ +/* ====================================================================================== */ +export function plotSingleLine(start: { ln, col, len }, kind: string): WollokNodePlotter { + return { + range: createRange(start.ln, start.col, start.len), + tokenType: WollokTokenKinds[kind], + tokenModifiers: ['declaration'], + } +} + +export function plotMultiline(start: WollokPosition, end: WollokPosition, kind: string): WollokNodePlotter { + return { + range: { + start, + end, + }, + tokenType: WollokTokenKinds[kind], + tokenModifiers: ['declaration'], + } +} - // export const createRange = (line: number, column: number, length: number): WollokRange => ({ start: { @@ -19,22 +33,6 @@ export const createRange = (line: number, column: number, length: number): Wollo }, }) -export type WollokPosition = { - line: number, - column: number, -} - -export type WollokRange = { - start: WollokPosition, - end: WollokPosition, -} - -export type WollokNodePlotter = { - range: WollokRange - tokenType: string - tokenModifiers?: string[] -} - export const getLineColumn = (text: string[], offset: number): [number, number] => { let totalLength = 0 let lineStartPos = 0 diff --git a/packages/client/src/test/highlighter/commentsAndAnnotations.test.ts b/packages/client/src/test/highlighter/commentsAndAnnotations.test.ts index 305f80c..f339521 100644 --- a/packages/client/src/test/highlighter/commentsAndAnnotations.test.ts +++ b/packages/client/src/test/highlighter/commentsAndAnnotations.test.ts @@ -1,6 +1,6 @@ -import { WollokNodePlotter } from '../../highlighter/utils' import { expect } from 'expect' import { processedByTokenType, readFileForTokenProvider } from './utils' +import { WollokNodePlotter } from '../../highlighter/definitions' suite('comments & annotations sample', () => { diff --git a/packages/client/src/test/highlighter/inheritance.test.ts b/packages/client/src/test/highlighter/inheritance.test.ts index 03ce864..a3da651 100644 --- a/packages/client/src/test/highlighter/inheritance.test.ts +++ b/packages/client/src/test/highlighter/inheritance.test.ts @@ -1,6 +1,6 @@ -import { WollokNodePlotter } from '../../highlighter/utils' import { expect } from 'expect' import { processedByTokenType, readFileForTokenProvider } from './utils' +import { WollokNodePlotter } from '../../highlighter/definitions' suite('inheritance sample', () => { diff --git a/packages/client/src/test/highlighter/utils.ts b/packages/client/src/test/highlighter/utils.ts index 2b1f8ef..a95b442 100644 --- a/packages/client/src/test/highlighter/utils.ts +++ b/packages/client/src/test/highlighter/utils.ts @@ -1,7 +1,7 @@ import { excludeNullish, parse } from 'wollok-ts' import { readFileSync } from 'fs' import { processCode } from '../../highlighter/tokenProvider' -import { WollokNodePlotter } from '../../highlighter/utils' +import { WollokNodePlotter } from '../../highlighter/definitions' const validateHighlighter = (wollokNodesPlotter: WollokNodePlotter[]) => wollokNodesPlotter.every(wollokNodePlotter => {