Skip to content

Commit

Permalink
WIP: bringing research project
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Dec 16, 2024
1 parent 6b8d196 commit a836d98
Show file tree
Hide file tree
Showing 5 changed files with 591 additions and 1 deletion.
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"hoverProvider": true
},
"main": "./out/client/src/extension",
"configurationDefaults": {
"[wollok]": {
"editor.semanticHighlighting.enabled": true
}
},
"contributes": {
"menus": {
"editor/title/run": [
Expand Down Expand Up @@ -316,6 +321,29 @@
"mac": "cmd+r",
"when": "editorTextFocus && resourceExtname == .wtest"
}
],
"semanticTokenTypes": [
{
"id": "keyword",
"superType": "type",
"description": "palabra reservada de wollok"
},
{
"id": "object",
"superType": "type",
"description": "singleton"
},
{
"id": "parameter",
"superType": "type",
"description": "parametro de metodos o funciones"
}
],
"semanticTokenModifiers": [
{
"id": "native",
"description": "simbolo nativo de wollok"
}
]
},
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion packages/client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { subscribeWollokCommands } from './commands'
import { getLSPMessage } from './messages'
import { wollokLSPExtensionId } from './shared-definitions'
import { allWollokFiles } from './utils'
import { legend, provider, selector } from './highlighter'

let client: LanguageClient

Expand All @@ -50,7 +51,7 @@ export function activate(context: ExtensionContext): void {
// Options to control the language client
const clientOptions: LanguageClientOptions = {
// Register the server for Wollok documents
documentSelector: [{ scheme: 'file', language: 'wollok' }],
documentSelector: [selector],
synchronize: {
configurationSection: wollokLSPExtensionId,
// Notify the server about file changes to '.clientrc files contained in the workspace
Expand All @@ -61,6 +62,8 @@ export function activate(context: ExtensionContext): void {
// Subscribe Wollok Commands
subscribeWollokCommands(context)

const semanticTokensProvider = languages.registerDocumentSemanticTokensProvider(selector, provider, legend)
context.subscriptions.push(semanticTokensProvider)

// Subscribe Wollok Debug Adapter
const debuggerFactory = new WollokDebugAdapterFactory(context, vscode.workspace)
Expand Down
32 changes: 32 additions & 0 deletions packages/client/src/highlighter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
///esModuleIn
import * as vscode from 'vscode'
import { excludeNullish, parse } from 'wollok-ts'
import * as def from './highlighter/definition'
import { processCode, processComments } from './highlighter/tokenProvider'

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

const tokensBuilder = new vscode.SemanticTokensBuilder(legend)
const parsedFile = parse.File(document.fileName)
const docText = document.getText()
const tp = parsedFile.tryParse(docText)

const splittedLines = docText.split('\n')
const processed = excludeNullish([]
.concat(processCode(tp.members[0], splittedLines))
.concat(processComments(splittedLines)))

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

return tokensBuilder.build()
},
}

export const legend = new vscode.SemanticTokensLegend(def.tokenTypes, def.tokenModifiers)
export const selector = { language: 'wollok', scheme: 'file' }
191 changes: 191 additions & 0 deletions packages/client/src/highlighter/definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
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']

export const tokenModifiers = ['declaration', 'definition', 'documentation', 'keyword']
export const tokenTypeObj = {
'Parameter': 'parameter',
'ParameterizedType': 'property',
'NamedArgument': 'property',
'Import': 'namespace',
'Body': 'property',
'Catch': 'property',
'Package': 'property',
'Program': 'property',
'Test': 'function',
'Singleton': 'object', //'entity.name.type.class',//
'Mixin': 'property',
'Describe': 'property',
'Variable': 'variable',
'Field': 'property',
'Method': 'method', //'entity.name.function.member',
'Return': 'keyword',
'Assignment': 'property',
'Reference': 'property',
'Self': 'property',
'Literal': 'property',
'Literal_number': 'number',
'Literal_string': 'string',
'Literal_bool': 'keyword',
'Send': 'operator',
'Super': 'property',
'New': 'property',
'If': 'property',
'Throw': 'property',
'Try': 'property',
'Environment': 'property',
'Class': 'class', //'class', //'entity.name.type.class',

'Comment':'comment',
'CommentMultiline':'comment-multiline',
'Keyword': 'keyword',
'Unknow': 'unknow',
}

export const tokenTypeModifierObj = {
'Parameter': ['declaration'],
'ParameterizedType': ['declaration'],
'NamedArgument': ['declaration'],
'Import': ['declaration'],
'Body': ['declaration'],
'Catch': ['declaration'],
'Package': ['declaration'],
'Program': ['declaration'],
'Test': ['declaration'],
'Singleton': ['declaration'],
'Mixin': ['declaration'],
'Describe': ['declaration'],
'Variable': ['declaration'],
'Field': ['declaration'],
'Method': ['declaration'],
'Return': ['declaration'],
'Assignment': ['declaration'],
'Reference': ['declaration'],
'Self': ['declaration'],
'Literal': ['declaration'], //['readonly'],
'Literal_number': ['declaration'], //['readonly'],
'Literal_string': ['declaration'], //['readonly'],
'Send': ['declaration'],
'Super': ['declaration'],
'New': ['declaration'],
'If': ['declaration'],
'Throw': ['declaration'],
'Try': ['declaration'],
'Environment': ['declaration'],
'Class': ['declaration'], //'class', //'entity.name.type.class',

//este rompia....!!!
'Keyword': ['declaration'], //['static'],
'Comment': ['declaration'], //['static'],
//['readonly'],
//'Unknow': 'unknow',
}
//Standard token types:


export const keywords = {
/*
'Parameter':'property',
'ParameterizedType':'property',
'NamedArgument':'property',
'Body':'property',*/
'Catch':'catch',
'Package':'package',
'Import':'import',
'Program':'program',
'Test':'test',
'Describe':'describe',
'Singleton': 'object',
//'Mixin':'property',
'Variable': ['var', 'const'],
'Send': [
// eslint-disable-next-line array-element-newline
'+', '*', '-', '/', '<', '>', '<=', '>=',
// eslint-disable-next-line array-element-newline
'and', 'or', 'not', 'negate',
// eslint-disable-next-line array-element-newline
'&&', '||', '!',
// eslint-disable-next-line array-element-newline
'==', '!=',
],
'Field':'var',
'Method': 'method',
'Return': 'return',
'Assignment':'=',
//'Reference':'property',
'Self':'self',
'New':'new',
'If':'if',
'Else':'else',
//'Literal':'property',
/*'Super':'property',
'Throw':'property',
'Try':'property',
'Environment':'property',
*/
'Class': 'class',
}

//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']

//Standard token types:
//ID Description
export const tokenTypes = [
'namespace', // For identifiers that declare or reference a namespace, module, or package.
'class', // For identifiers that declare or reference a class type.
'object', //No es parte de los tipos por default
'enum', // For identifiers that declare or reference an enumeration type.
'interface', // For identifiers that declare or reference an interface type.
'struct', // For identifiers that declare or reference a struct type.
'typeParameter', // For identifiers that declare or reference a type parameter.
'type', // For identifiers that declare or reference a type that is not covered above.
'parameter', // For identifiers that declare or reference a function or method parameters.
'variable', // For identifiers that declare or reference a local or global variable.
'property', // For identifiers that declare or reference a member property, member field, or member variable.
'enumMember', // For identifiers that declare or reference an enumeration property, constant, or member.
'decorator', // For identifiers that declare or reference decorators and annotations.
'event', // For identifiers that declare an event property.
'function', // For identifiers that declare a function.
'method', // For identifiers that declare a member function or method.
'macro', // For identifiers that declare a macro.
'label', // For identifiers that declare a label.
'comment', // For tokens that represent a comment.
'string', // For tokens that represent a string literal.
'keyword', // For tokens that represent a language keyword.
'number', // For tokens that represent a number literal.
'regexp', // For tokens that represent a regular expression literal.
'operator', // For tokens that represent an operator.
'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),
),
tokenType: tokenTypeObj[kind],
tokenModifiers: tokenTypeModifierObj[kind],
}
}

/*
Standard token modifiers:
ID Description
declaration For declarations of symbols.
definition For definitions of symbols, for example, in header files.
readonly For readonly variables and member fields (constants).
static For class members (static members).
deprecated For symbols that should no longer be used.
abstract For types and member functions that are abstract.
async For functions that are marked async.
modification For variable references where the variable is assigned to.
documentation For occurrences of symbols in documentation.
defaultLibrary For symbols that are part of the standard library.
*/
Loading

0 comments on commit a836d98

Please sign in to comment.