Skip to content

Commit

Permalink
refac functionalities + definitions testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ivojawer committed Jan 4, 2024
1 parent b0e0fd5 commit fc10220
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 54 deletions.
21 changes: 21 additions & 0 deletions client/src/test/definition.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as assert from 'assert'
import { commands, Location, LocationLink, Position, Range, Uri } from 'vscode'
import { activate, getDocumentURI } from './helper'

/** ATTENTION
* These tests are NOT ATOMIC, they depend on each other, order matters. (Resolve TODO)
* */
test('go to definition', async () => {
const definitionURI = getDocumentURI('definition.wlk')
await testDefinition(definitionURI, new Position(8, 8), [
new Location(definitionURI, new Range(new Position(0, 0), new Position(6, 0))),
])
})


async function testDefinition(uri: Uri, at: Position, expected: Array<Location | LocationLink>): Promise<void> {
await activate(uri)
const result = await commands.executeCommand('vscode.executeDefinitionProvider', uri, at)
uri['_formatted'] = null
assert.deepEqual(result, expected)
}
11 changes: 11 additions & 0 deletions client/testFixture/definition.wlk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object pepita {
method vola() {

}
}

object manolo {
method hacerVolarAPepita() {
pepita.vola()
}
}
10 changes: 10 additions & 0 deletions server/src/functionalities/definition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Location, TextDocumentPositionParams } from 'vscode-languageserver'
import { Environment, Method, Module, New, Node, Reference, Self, Send, Singleton, Super } from 'wollok-ts'
import { is, match, when } from 'wollok-ts/dist/extensions'
import { getNodesByPosition, nodeToLocation } from '../utils/text-documents'

export const definition = (environment: Environment) => (
textDocumentPosition: TextDocumentPositionParams
): Location[] => {
const cursorNodes = getNodesByPosition(environment, textDocumentPosition)
const definitions = getNodeDefinition(environment)(cursorNodes.reverse()[0])
return definitions.map(nodeToLocation)
}

export const getNodeDefinition = (environment: Environment) => (node: Node): Node[] => {
try {
Expand Down
24 changes: 20 additions & 4 deletions server/src/functionalities/symbols.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { DocumentSymbol, SymbolKind, WorkspaceSymbol } from 'vscode-languageserver'
import { DocumentSymbol, DocumentSymbolParams, SymbolKind, WorkspaceSymbol, WorkspaceSymbolParams } from 'vscode-languageserver'
import { Environment, Field, Method, Module, Node, Package, Program, Test, Variable } from 'wollok-ts'
import { toVSCRange } from '../utils/text-documents'
import { packageFromURI, toVSCRange } from '../utils/text-documents'
import { workspacePackage } from '../utils/vm/wollok'

type Symbolyzable = Program | Test | Module | Variable | Field | Method | Test

export const documentSymbolsFor = (document: Package): DocumentSymbol[] =>
export const documentSymbols = (environment: Environment) => (params: DocumentSymbolParams): DocumentSymbol[] => {
// ToDo this is a temporal fix for https://github.com/uqbar-project/wollok-lsp-ide/issues/61
if (!workspacePackage(environment)) {
return []
}
const document = packageFromURI(params.textDocument.uri, environment)
if (!document)
throw new Error('Could not produce symbols: document not found')
return documentSymbolsFor(document)
}

export const workspaceSymbols = (
environment: Environment
) => (params: WorkspaceSymbolParams): WorkspaceSymbol[] => workspaceSymbolsFor(environment, params.query)


const documentSymbolsFor = (document: Package): DocumentSymbol[] =>
(document.members.filter(isSymbolyzable) as Symbolyzable[]).map(documentSymbol)

export const workspaceSymbolsFor = (environment: Environment, query: string): WorkspaceSymbol[] =>
const workspaceSymbolsFor = (environment: Environment, query: string): WorkspaceSymbol[] =>
workspacePackage(environment).descendants.filter(isSymbolyzable)
.filter(node => node.sourceFileName && node.sourceMap)
.filter(node => node.name?.toLowerCase().includes(query.toLowerCase()))
Expand Down
53 changes: 6 additions & 47 deletions server/src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,25 @@ import {
Connection,
Diagnostic,
DiagnosticSeverity,
DocumentSymbol,
DocumentSymbolParams,
Location,
TextDocumentPositionParams,
WorkspaceSymbol,
WorkspaceSymbolParams,
} from 'vscode-languageserver'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { Environment, Import, Package, Problem, validate } from 'wollok-ts'
import { is, List } from 'wollok-ts/dist/extensions'
import { Environment, Import, Problem, validate } from 'wollok-ts'
import { List } from 'wollok-ts/dist/extensions'
import { completionsForNode } from './functionalities/autocomplete/node-completion'
import { completeMessages } from './functionalities/autocomplete/send-completion'
import {
getProgramCodeLenses,
getTestCodeLenses,
} from './functionalities/code-lens'
import { getNodeDefinition } from './functionalities/definition'
import { reportValidationMessage } from './functionalities/reporter'
import { updateDocumentSettings } from './settings'
import {
documentSymbolsFor,
workspaceSymbolsFor,
} from './functionalities/symbols'
import { TimeMeasurer } from './timeMeasurer'
import {
getNodesByPosition,
getWollokFileExtension,
nodeToLocation,
packageFromURI,
trimIn,
} from './utils/text-documents'
import { isNodeURI, wollokURI, workspacePackage } from './utils/vm/wollok'
import { isNodeURI, wollokURI } from './utils/vm/wollok'
import { cursorNode } from './utils/text-documents'

// ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Expand Down Expand Up @@ -146,17 +134,9 @@ export const completions = (environment: Environment) => (
return result
}

export const definition = (environment: Environment) => (
textDocumentPosition: TextDocumentPositionParams
): Location[] => {
const cursorNodes = getNodesByPosition(environment, textDocumentPosition)
const definitions = getNodeDefinition(environment)(cursorNodes.reverse()[0])
return definitions.map(nodeToLocation)
}

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

switch (fileExtension) {
Expand All @@ -167,25 +147,4 @@ export const codeLenses = (environment: Environment) => (params: CodeLensParams)
default:
return null
}
}

export const documentSymbols = (environment: Environment) => (params: DocumentSymbolParams): DocumentSymbol[] => {
// ToDo this is a temporal fix for https://github.com/uqbar-project/wollok-lsp-ide/issues/61
if (!workspacePackage(environment)) {
return []
}
const document = findPackage(params.textDocument.uri, environment)
if (!document)
throw new Error('Could not produce symbols: document not found')
return documentSymbolsFor(document)
}

export const workspaceSymbols = (
environment: Environment
) => (params: WorkspaceSymbolParams): WorkspaceSymbol[] => workspaceSymbolsFor(environment, params.query)

const findPackage = (
uri: string,
environment: Environment,
): Package | undefined =>
environment.descendants.filter(is(Package)).find((p) => isNodeURI(p, uri))
}
5 changes: 2 additions & 3 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import { formatDocument, formatRange } from './functionalities/formatter'
import {
codeLenses,
completions,
definition,
documentSymbols,
validateTextDocument,
workspaceSymbols,
} from './linter'
import { documentSymbols, workspaceSymbols } from './functionalities/symbols'
import { definition } from './functionalities/definition'
import { initializeSettings, WollokLSPSettings } from './settings'
import { ProgressReporter } from './utils/progress-reporter'
import { EnvironmentProvider } from './utils/vm/environment'
Expand Down

0 comments on commit fc10220

Please sign in to comment.