Skip to content

Commit

Permalink
WIP: first test running outside integration
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Dec 19, 2024
1 parent a836d98 commit 56fba2f
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 52 deletions.
3 changes: 3 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"dependencies": {
"wollok-debug-adapter": "workspace:^"
},
"scripts": {
"test:highlighter": "mocha -u tdd -r ts-node/register/transpile-only --loader=ts-node/esm src/test/highlighter/**.test.ts --timeout 2000"
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.14.1",
Expand Down
12 changes: 6 additions & 6 deletions packages/client/src/highlighter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
///esModuleIn
import * as vscode from 'vscode'
import { excludeNullish, parse } from 'wollok-ts'
import * as def from './highlighter/definition'
import { NodePlotter } from './highlighter/utils'
import { processCode, processComments } from './highlighter/tokenProvider'
import { tokenModifiers, tokenTypes } from './highlighter/definition'

export const provider: vscode.DocumentSemanticTokensProvider = {
provideDocumentSemanticTokens(
document: vscode.TextDocument
): vscode.ProviderResult<vscode.SemanticTokens> {
// analyze the document and return semantic tokens

// analyze the document and return semantic tokens
const tokensBuilder = new vscode.SemanticTokensBuilder(legend)
const parsedFile = parse.File(document.fileName)
const docText = document.getText()
Expand All @@ -20,13 +20,13 @@ export const provider: vscode.DocumentSemanticTokensProvider = {
.concat(processCode(tp.members[0], splittedLines))
.concat(processComments(splittedLines)))

processed.forEach((node: def.NodePlotter) =>
tokensBuilder.push(node.range, node.tokenType, node.tokenModifiers)
processed.forEach((node: NodePlotter) =>
tokensBuilder.push(node.range as unknown as vscode.Range, node.tokenType, node.tokenModifiers)
)

return tokensBuilder.build()
},
}

export const legend = new vscode.SemanticTokensLegend(def.tokenTypes, def.tokenModifiers)
export const legend = new vscode.SemanticTokensLegend(tokenTypes, tokenModifiers)
export const selector = { language: 'wollok', scheme: 'file' }
18 changes: 4 additions & 14 deletions packages/client/src/highlighter/definition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as vscode from 'vscode'
//const tokenTypes = ['Parameter', 'ParameterizedType', 'NamedArgument', 'Import', 'Body', 'Catch', 'Package', 'Program', 'Test', 'Class', 'Singleton', 'Mixin', 'Describe', 'Variable', 'Field', 'Method', 'Return', 'Assignment', 'Reference', 'Self', 'Literal', 'Send', 'Super', 'New', 'If', 'Throw', 'Try', 'Environment']
import { createRange, NodePlotter } from './utils'

export const tokenModifiers = ['declaration', 'definition', 'documentation', 'keyword']
export const tokenTypeObj = {
Expand All @@ -12,12 +11,12 @@ export const tokenTypeObj = {
'Package': 'property',
'Program': 'property',
'Test': 'function',
'Singleton': 'object', //'entity.name.type.class',//
'Singleton': 'object',
'Mixin': 'property',
'Describe': 'property',
'Variable': 'variable',
'Field': 'property',
'Method': 'method', //'entity.name.function.member',
'Method': 'method',
'Return': 'keyword',
'Assignment': 'property',
'Reference': 'property',
Expand Down Expand Up @@ -157,18 +156,9 @@ export const tokenTypes = [
'comment',
]

export type NodePlotter = {
range: vscode.Range
tokenType: string
tokenModifiers?: string[]
}

export function plotter(start: { ln, col, len }, kind: string): NodePlotter {
return {
range: new vscode.Range(
new vscode.Position(start.ln, start.col),
new vscode.Position(start.ln, start.col + start.len),
),
range: createRange(start.ln, start.col, start.len),
tokenType: tokenTypeObj[kind],
tokenModifiers: tokenTypeModifierObj[kind],
}
Expand Down
32 changes: 18 additions & 14 deletions packages/client/src/highlighter/tokenProvider.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import { plotter, NodePlotter, keywords, tokenTypeObj } from './definition'
import { NodePlotter } from './utils'
import { plotter, keywords, tokenTypeObj } from './definition'
import { Assignment, Class, Describe, Field, If, Import, Literal, match, Method, Node, Package, Parameter, Program, Reference, Return, Send, Singleton, Test, Variable, when } from 'wollok-ts'

//Nota: no todos los node's tienen .start (dando undefined), pueden provocar excepciones.
function extraerLineaColumna(node: Node, documentoStr: string[]) {
const linea = node.sourceMap.start.line-1
const columna = node.sourceMap.start.column-1
function getLine(node: Node, documentoStr: string[]) {
const start = node.sourceMap.start
const linea = start.line-1
const columna = start.column-1

return {
linea: linea,
columna: columna,
linea,
columna,
subStr:documentoStr[linea].substring(columna),
}
}

const nullHighlighting = { result: undefined, references: undefined }

function processNode(node: Node, documentoStr: string[], context: NodeContext[]): HighlightingResult {
if (!node.sourceMap) return nullHighlighting
const generar_plotter = node => {
const { linea, columna, subStr } = extraerLineaColumna(node, documentoStr)
const { linea, columna, subStr } = getLine(node, documentoStr)
const col = columna + subStr.indexOf(node.name)
return plotter({ ln: linea, col: col, len: node.name.length }, node.kind)
}
const keyword_plotter = (node, mensaje) => {
const { linea, columna, subStr } = extraerLineaColumna(node, documentoStr)
const { linea, columna, subStr } = getLine(node, documentoStr)
const col = columna + subStr.indexOf(mensaje)
return plotter({ ln: linea, col: col, len: mensaje.length }, 'Keyword')
}
const saveReference = node => { return { name: node.name, type: node.kind }}
const dropReference = node => { return { result: node, references: undefined }}
const nullHighlighting = { result: undefined, references: undefined }

if(node.kind === 'New' || node.kind === 'Self'){ //por alguna razon no hace match
return dropReference(keyword_plotter(node, keywords[node.kind]))
Expand Down Expand Up @@ -102,7 +106,7 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
when(Assignment)(node => {
//node.variable
//node.value
const { linea, columna, subStr } = extraerLineaColumna(node, documentoStr)
const { linea, columna, subStr } = getLine(node, documentoStr)
const col = columna + subStr.indexOf(node.variable.name)
return {
result: [
Expand All @@ -112,7 +116,7 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
}
}),
when(Parameter)(node => {
const { linea, columna, subStr } = extraerLineaColumna(node, documentoStr)
const { linea, columna, subStr } = getLine(node, documentoStr)
const col = columna + subStr.indexOf(node.name)
return {
result: [plotter({ ln: linea, col: col, len: node.name.length }, node.kind)],
Expand All @@ -124,7 +128,7 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
return nullHighlighting
}

const { linea, columna, subStr } = extraerLineaColumna(node, documentoStr)
const { linea, columna, subStr } = getLine(node, documentoStr)
const col = columna + subStr.indexOf(node.name)

return {
Expand All @@ -136,7 +140,7 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
}),
when(Send)(node => {
const curretKeyboard = keywords[node.kind]
const { linea, columna, subStr } = extraerLineaColumna(node, documentoStr)
const { linea, columna, subStr } = getLine(node, documentoStr)
if(curretKeyboard && curretKeyboard.includes(node.message)){
if(node.message == 'negate'){//es la forma alternativa del simbolo '!'
const idx_negate = subStr.indexOf('!')
Expand Down Expand Up @@ -176,7 +180,7 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[])
return nullHighlighting//plotter({ ln: linea, col: col, len: len }, 'Singleton')
}

const { linea, columna, subStr } = extraerLineaColumna(node, documentoStr)
const { linea, columna, subStr } = getLine(node, documentoStr)
switch (tipo) {
case 'number':
case 'bigint':
Expand Down
40 changes: 40 additions & 0 deletions packages/client/src/highlighter/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as vscode from 'vscode'

export const createRange = (line: number, column: number, length: number): vscode.Range =>
new vscode.Range(
new vscode.Position(line, column),
new vscode.Position(line, column + length),
)

export type NodePlotter = {
range: vscode.Range
tokenType: string
tokenModifiers?: string[]
}

// export const createRange = (line: number, column: number, length: number): Range =>
// ({
// start: {
// line, column,
// },
// end: {
// line,
// column: column + length,
// },
// })

// export type Position = {
// line: number,
// column: number,
// }

// export type Range = {
// start: Position,
// end: Position,
// }

// export type NodePlotter = {
// range: Range
// tokenType: string
// tokenModifiers?: string[]
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object pepita {
var property energy = 100
const nombre = "Pepita"

method fly(minutes) {
energy = energy + (10 * minutes)
}

method realEnergy() = energy * self.nameValue()
method nameValue() = nombre.length()
}
24 changes: 24 additions & 0 deletions packages/client/src/test/highlighter/tokenProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { excludeNullish, parse } from 'wollok-ts'
import { readFileSync } from 'fs'
import { processCode } from '../../highlighter/tokenProvider'

suite('an object sample', () => {

let processed: string[]

setup(() => {
const filePath = 'src/test/highlighter/highlighter-samples/objectSample.wlk'
const parsedFile = parse.File(filePath)
const docText = readFileSync(filePath, { encoding: 'utf-8' })
const tp = parsedFile.tryParse(docText)
const splittedLines = docText.split('\n')
processed = excludeNullish([]
.concat(processCode(tp.members[0], splittedLines)))

})

test('highlights object keyword', () => {
console.info(processed)
})

})
2 changes: 1 addition & 1 deletion packages/debug-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"mocha": "^10.7.3",
"nyc": "^17.0.0",
"ts-mocha": "^10.0.0",
"wollok-ts": "4.1.6"
"wollok-ts": "4.1.9"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isNotImportedIn } from 'wollok-ts'
import { completionsForNode } from './node-completion'
import { completeMessages } from './send-completion'
import { match, when } from 'wollok-ts/dist/extensions'
import { logger } from '../../utils/logger'

export const completions = (environment: Environment) => (
params: CompletionParams,
Expand All @@ -16,7 +17,8 @@ export const completions = (environment: Environment) => (
const selectionNode = cursorNode(environment, position, textDocument)
if(!selectionNode) {
timeMeasurer.finalReport()
throw new Error('Could not find selection node')
logger.error(`Could not find selection node for position ${JSON.stringify(position)} in text document ${textDocument.uri}`)
return []
}
timeMeasurer.addTime(`Autocomplete - ${selectionNode?.kind}`)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const completionsForNode = (node: Node): CompletionItem[] => {
when(New)(completeNew)
)
} catch (error) {
logger.error(`✘ Completions for node failed: ${error}`, error)
return completeForParent(node)
logger.error(`✘ Completions for node ${node.kind} (${node.sourceMap} - ${node.sourceFileName}) failed: ${error}`, error)
return [] // completeForParent(node)
}
}

Expand Down
15 changes: 1 addition & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8867,7 +8867,7 @@ __metadata:
mocha: "npm:^10.7.3"
nyc: "npm:^17.0.0"
ts-mocha: "npm:^10.0.0"
wollok-ts: "npm:4.1.6"
wollok-ts: "npm:4.1.9"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -8935,19 +8935,6 @@ __metadata:
languageName: unknown
linkType: soft

"wollok-ts@npm:4.1.6":
version: 4.1.6
resolution: "wollok-ts@npm:4.1.6"
dependencies:
"@types/parsimmon": "npm:^1.10.8"
parsimmon: "npm:^1.18.1"
prettier-printer: "npm:^1.1.4"
unraw: "npm:^3.0.0"
uuid: "npm:^9.0.1"
checksum: 10c0/2248c12858540699ce9c58b6e22255d2dd685ef0ae1c32ca023cc0a7c9d8489057a5b0600c0291e4f45cb831dc4c7ccd43de73b4c041b8569300d66c0c7bbbcf
languageName: node
linkType: hard

"wollok-ts@npm:4.1.9":
version: 4.1.9
resolution: "wollok-ts@npm:4.1.9"
Expand Down

0 comments on commit 56fba2f

Please sign in to comment.