From fc10220a42f54c625091a84cb4b34dbc8d8d7d8a Mon Sep 17 00:00:00 2001 From: ivojawer Date: Thu, 4 Jan 2024 03:10:07 -0300 Subject: [PATCH] refac functionalities + definitions testing --- client/src/test/definition.test.ts | 21 ++++++++++ client/testFixture/definition.wlk | 11 +++++ server/src/functionalities/definition.ts | 10 +++++ server/src/functionalities/symbols.ts | 24 +++++++++-- server/src/linter.ts | 53 +++--------------------- server/src/server.ts | 5 +-- 6 files changed, 70 insertions(+), 54 deletions(-) create mode 100644 client/src/test/definition.test.ts create mode 100644 client/testFixture/definition.wlk diff --git a/client/src/test/definition.test.ts b/client/src/test/definition.test.ts new file mode 100644 index 00000000..ba4dd665 --- /dev/null +++ b/client/src/test/definition.test.ts @@ -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): Promise { + await activate(uri) + const result = await commands.executeCommand('vscode.executeDefinitionProvider', uri, at) + uri['_formatted'] = null + assert.deepEqual(result, expected) +} \ No newline at end of file diff --git a/client/testFixture/definition.wlk b/client/testFixture/definition.wlk new file mode 100644 index 00000000..7dcf9d7c --- /dev/null +++ b/client/testFixture/definition.wlk @@ -0,0 +1,11 @@ +object pepita { + method vola() { + + } +} + +object manolo { + method hacerVolarAPepita() { + pepita.vola() + } +} \ No newline at end of file diff --git a/server/src/functionalities/definition.ts b/server/src/functionalities/definition.ts index b65d901a..8e99af48 100644 --- a/server/src/functionalities/definition.ts +++ b/server/src/functionalities/definition.ts @@ -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 { diff --git a/server/src/functionalities/symbols.ts b/server/src/functionalities/symbols.ts index b70aa866..19990392 100644 --- a/server/src/functionalities/symbols.ts +++ b/server/src/functionalities/symbols.ts @@ -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())) diff --git a/server/src/linter.ts b/server/src/linter.ts index 80d027c3..90b36504 100644 --- a/server/src/linter.ts +++ b/server/src/linter.ts @@ -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' // ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════ @@ -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) { @@ -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)) +} \ No newline at end of file diff --git a/server/src/server.ts b/server/src/server.ts index 3a89031d..faa399fb 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -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'