Skip to content

Commit

Permalink
Refactor: moving types to definitions and helper functions to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Jan 4, 2025
1 parent 0016ec4 commit d4810ee
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 121 deletions.
3 changes: 1 addition & 2 deletions packages/client/src/highlighter.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
144 changes: 77 additions & 67 deletions packages/client/src/highlighter/definitions.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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
'+', '*', '-', '/', '<', '>', '<=', '>=',
Expand All @@ -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
Expand Down
30 changes: 5 additions & 25 deletions packages/client/src/highlighter/tokenProvider.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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)){

Expand Down
46 changes: 22 additions & 24 deletions packages/client/src/highlighter/utils.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/test/highlighter/inheritance.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/test/highlighter/utils.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down

0 comments on commit d4810ee

Please sign in to comment.