Skip to content

Commit

Permalink
Merge pull request #157 from uqbar-project/fix-#150-repl-lens
Browse files Browse the repository at this point in the history
Add repl lens
  • Loading branch information
PalumboN authored Mar 28, 2024
2 parents f5711b0 + fb1cc03 commit df35b09
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 40 deletions.
19 changes: 11 additions & 8 deletions client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,36 @@ import {
fsToShell,
unknownToShell,
} from './platform-string-utils'
import { wollokLSPExtensionCode } from './constants'
import { COMMAND_RUN_ALL_TESTS, COMMAND_RUN_GAME, COMMAND_RUN_PROGRAM, COMMAND_RUN_TEST, COMMAND_START_REPL, wollokLSPExtensionCode } from './shared-definitions'

export const subscribeWollokCommands = (context: ExtensionContext): void => {
context.subscriptions.push(registerCLICommand('wollok.start.repl', startRepl))
context.subscriptions.push(registerCLICommand(COMMAND_START_REPL, startRepl))
context.subscriptions.push(
registerCLICommand('wollok.run.allTests', runAllTests),
registerCLICommand(COMMAND_RUN_ALL_TESTS, runAllTests),
)
context.subscriptions.push(registerCLICommand('wollok.run.test', runTest))
context.subscriptions.push(registerCLICommand(COMMAND_RUN_TEST, runTest))
context.subscriptions.push(
registerCLICommand('wollok.run.program', runProgram()),
registerCLICommand(COMMAND_RUN_PROGRAM, runProgram()),
)
context.subscriptions.push(
registerCLICommand('wollok.run.game', runProgram(true)),
registerCLICommand(COMMAND_RUN_GAME, runProgram(true)),
)
}

/**
* CLI Commands
*/

export const runProgram = (isGame = false) => (fqn: string): Task =>
wollokCLITask('run program', `Wollok run ${isGame ? 'game' : 'program'}`, [
export const runProgram = (isGame = false) => (fqn: string): Task => {
// Terminate previous terminal session
vscode.commands.executeCommand('workbench.action.terminal.killAll')
return wollokCLITask('run program', `Wollok run ${isGame ? 'game' : 'program'}`, [
'run',
...isGame ? ['-g'] : [],
`'${fqn}'`,
'--skipValidations',
])
}

export const runTest = ([filter, file, describe, test]: [string|null, string|null, string|null, string|null]): Task =>
wollokCLITask('run tests', 'Wollok run test', [
Expand Down
2 changes: 0 additions & 2 deletions client/src/constants.ts

This file was deleted.

2 changes: 1 addition & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from 'vscode-languageclient/node'
import { subscribeWollokCommands } from './commands'
import { allWollokFiles } from './utils'
import { wollokLSPExtensionId } from './constants'
import { wollokLSPExtensionId } from './shared-definitions'

let client: LanguageClient

Expand Down
12 changes: 12 additions & 0 deletions client/src/shared-definitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This file is linked from both client and server

// IDE
export const wollokLSPExtensionCode = 'wollokLSP'
export const wollokLSPExtensionId = 'wollok-lsp-ide'

// Commands
export const COMMAND_START_REPL = 'wollok.start.repl'
export const COMMAND_RUN_GAME = 'wollok.run.game'
export const COMMAND_RUN_PROGRAM = 'wollok.run.program'
export const COMMAND_RUN_ALL_TESTS = 'wollok.run.allTests'
export const COMMAND_RUN_TEST = 'wollok.run.test'
23 changes: 20 additions & 3 deletions client/src/test/code-lens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getDocumentURI, activate } from './helper'
suite('Should do code lenses', () => {
const docUri = getDocumentURI('test.wtest')

test('Shows program code lenses for wpgm file', async () => {
test('Shows program code lenses for Wollok Program file', async () => {
await testCodeLenses(getDocumentURI('pepitaGame.wpgm'), [
new CodeLens(
new Range(new Position(0, 0), new Position(2, 1)),
Expand Down Expand Up @@ -55,8 +55,25 @@ suite('Should do code lenses', () => {
])
})

test('Shows no code lenses for Wollok file', async () => {
await testCodeLenses(getDocumentURI('pepita.wlk'), [])
test('Shows test code lenses for Wollok Definition file', async () => {
await testCodeLenses(getDocumentURI('pepita.wlk'), [
new CodeLens(
new Range(new Position(0, 0), new Position(4, 0)),
{
title : 'Run in REPL',
command : 'wollok.start.repl',
arguments : ['pepita.Pepita'],
},
),
new CodeLens(
new Range(new Position(4, 0), new Position(6, 0)),
{
title: 'Run in REPL',
command: 'wollok.start.repl',
arguments: ['pepita.a'],
},
),
])
})
})

Expand Down
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@
"when": "resourceExtname == .wtest",
"category": "Wollok"
}
],
"keybindings": [
{
"command": "wollok.start.repl",
"key": "ctrl+r",
"mac": "cmd+r",
"when": "editorTextFocus && resourceExtname == .wlk"
},
{
"command": "wollok.run.allTests",
"key": "ctrl+r",
"mac": "cmd+r",
"when": "editorTextFocus && resourceExtname == .wtest"
}
]
},
"scripts": {
Expand Down
59 changes: 35 additions & 24 deletions server/src/functionalities/code-lens.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
import { CodeLens, CodeLensParams, Position, Range } from 'vscode-languageserver'
import { Describe, Node, Package, Test, Program, Environment, is } from 'wollok-ts'
import { Describe, Node, Package, Test, Program, Environment, is, PROGRAM_FILE_EXTENSION, TEST_FILE_EXTENSION, WOLLOK_FILE_EXTENSION, Class, Singleton, Entity } from 'wollok-ts'
import { getWollokFileExtension, packageFromURI, toVSCRange } from '../utils/text-documents'
import { removeQuotes } from '../utils/strings'
import { COMMAND_RUN_GAME, COMMAND_RUN_PROGRAM, COMMAND_RUN_TEST, COMMAND_START_REPL } from '../shared-definitions'

export const codeLenses = (environment: Environment) => (params: CodeLensParams): CodeLens[] | null => {
const fileExtension = getWollokFileExtension(params.textDocument.uri)
const file = packageFromURI(params.textDocument.uri, environment)
if (!file) return null

switch (fileExtension) {
case 'wpgm':
case PROGRAM_FILE_EXTENSION:
return getProgramCodeLenses(file)
case 'wtest':
case TEST_FILE_EXTENSION:
return getTestCodeLenses(file)
case WOLLOK_FILE_EXTENSION:
return getWollokFileCodeLenses(file)
default:
return null
}
}

export const getProgramCodeLenses = (file: Package): CodeLens[] =>
file.members.filter(is(Program)).flatMap(program => [
{
range: toVSCRange(program.sourceMap!),
command: {
command: 'wollok.run.game',
title: 'Run game',
arguments: [program.fullyQualifiedName],
},
},
{
range: toVSCRange(program.sourceMap!),
command: {
command: 'wollok.run.program',
title: 'Run program',
arguments: [program.fullyQualifiedName],
},
},
buildLens(program, COMMAND_RUN_GAME, 'Run game'),
buildLens(program, COMMAND_RUN_PROGRAM, 'Run program'),
])


Expand All @@ -52,7 +41,22 @@ export const getTestCodeLenses = (file: Package): CodeLens[] => {
]
}

const buildRunAllTestsCodeLens = (file: Package): CodeLens =>
export const getWollokFileCodeLenses = (file: Package): CodeLens[] =>
file.members.filter(isWollokDefinition).map(definition =>
buildLens(definition, COMMAND_START_REPL, 'Run in REPL'),
)

/************************************************************************************************/
/* HELPER FUNCTIONS
/************************************************************************************************/

const isTesteable = (node: Node): node is Test | Describe =>
node.is(Test) || node.is(Describe)

const isWollokDefinition = (node: Node): node is Class | Singleton =>
node.is(Class) || node.is(Singleton)

const buildRunAllTestsCodeLens = (file: Package): CodeLens =>
buildTestsCodeLens(
Range.create(Position.create(0, 0), Position.create(0, 0)),
'wollok.run.test',
Expand All @@ -67,7 +71,7 @@ const buildTestCodeLens = (file: Package, node: Test | Describe): CodeLens => {

return buildTestsCodeLens(
toVSCRange(node.sourceMap!),
'wollok.run.test',
COMMAND_RUN_TEST,
`Run ${node.is(Test) ? 'test' : 'describe'}`,
[
null,
Expand All @@ -87,6 +91,13 @@ const buildTestsCodeLens = (range: Range, command: string, title: string, args:
},
})

function isTesteable(node: Node): node is Test | Describe {
return node.is(Test) || node.is(Describe)
}
const buildLens = (node: Entity, command: string, title: string) => (
{
range: toVSCRange(node.sourceMap!),
command: {
command,
title,
arguments: [node.fullyQualifiedName],
},
}
)
12 changes: 12 additions & 0 deletions server/src/shared-definitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This file is linked from both client and server

// IDE
export const wollokLSPExtensionCode = 'wollokLSP'
export const wollokLSPExtensionId = 'wollok-lsp-ide'

// Commands
export const COMMAND_START_REPL = 'wollok.start.repl'
export const COMMAND_RUN_GAME = 'wollok.run.game'
export const COMMAND_RUN_PROGRAM = 'wollok.run.program'
export const COMMAND_RUN_ALL_TESTS = 'wollok.run.allTests'
export const COMMAND_RUN_TEST = 'wollok.run.test'
4 changes: 2 additions & 2 deletions server/src/utils/text-documents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs'
import path from 'path'
import * as fs from 'fs'
import * as path from 'path'
import { Location, Position, Range, TextDocumentIdentifier, TextDocumentPositionParams } from 'vscode-languageserver'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { Environment, FileContent, Node, PROGRAM_FILE_EXTENSION, Package, SourceIndex, SourceMap, TEST_FILE_EXTENSION, WOLLOK_FILE_EXTENSION } from 'wollok-ts'
Expand Down

0 comments on commit df35b09

Please sign in to comment.