diff --git a/server/src/test/text-document.test.ts b/server/src/test/text-document.test.ts index 1e628c87..176344e0 100644 --- a/server/src/test/text-document.test.ts +++ b/server/src/test/text-document.test.ts @@ -1,6 +1,8 @@ import { expect } from 'expect' import path from 'path' -import { between, findPackageJSON, relativeFilePath, setWorkspaceUri } from '../utils/text-documents' +import { Position, Range } from 'vscode-languageserver' +import { Literal, Node, Package, SourceMap } from 'wollok-ts' +import { between, findPackageJSON, nodeToLocation, relativeFilePath, setWorkspaceUri } from '../utils/text-documents' const { join, resolve } = path.posix @@ -35,6 +37,60 @@ describe('text document utilities', () => { }) }) + + describe('node to location', () => { + + const pepitaPackage: Package = new Package({ name: 'pepita', fileName: 'src/pepita.wlk' }) + const testNodeLocation = (node: Node, expectedFile: string, expectedRange: Range) => { + const location = nodeToLocation(node) + expect(location.uri).toEqual('examples/example-project/' + expectedFile) + expect(location.range).toEqual(expectedRange) + } + + beforeEach(() => { + setWorkspaceUri(join('examples', 'example-project')) + }) + + it('package location', () => { + testNodeLocation( + pepitaPackage, + 'src/pepita.wlk', + Range.create(Position.create(0, 0), Position.create(0, 0)) + ) + }) + + it('node location', () => { + const literal = new Literal({ + value: 42, + sourceMap: new SourceMap({ + start: { line: 1, column: 2, offset: 2 }, + end: { line: 1, column: 4, offset: 4 }, + }), + parent: pepitaPackage, + }) + + testNodeLocation( + literal, + 'src/pepita.wlk', + Range.create(Position.create(0, 1), Position.create(0, 3)) + ) + }) + + it('missing file location', () => { + const brokenPackage = new Package({ name: 'broken' }) + + expect(() => nodeToLocation(brokenPackage)).toThrowError('No source file found for node') + }) + + it('missing source map location', () => { + const literal = new Literal({ + value: 42, + parent: pepitaPackage, + }) + + expect(() => nodeToLocation(literal)).toThrowError('No source map found for node') + }) + }) }) describe('relative file path', () => { diff --git a/server/src/utils/text-documents.ts b/server/src/utils/text-documents.ts index a7b893a0..cd677f9f 100644 --- a/server/src/utils/text-documents.ts +++ b/server/src/utils/text-documents.ts @@ -52,14 +52,21 @@ export const toVSCRange = (sourceMap: SourceMap): Range => Range.create(toVSCPosition(sourceMap.start), toVSCPosition(sourceMap.end)) export const nodeToLocation = (node: Node): Location => { - if (!node.sourceMap || !node.sourceFileName) { - throw new Error('No source map found for node') - } + if(!node.sourceFileName) throw new Error('No source file found for node') - return { - uri: uriFromRelativeFilePath(node.sourceFileName!), - range: toVSCRange(node.sourceMap), + if(node.is(Package)){ + return Location.create( + uriFromRelativeFilePath(node.sourceFileName!), + Range.create(Position.create(0, 0), Position.create(0, 0)), + ) } + + if (!node.sourceMap) throw new Error('No source map found for node') + + return Location.create( + uriFromRelativeFilePath(node.sourceFileName!), + toVSCRange(node.sourceMap), + ) } export function trimIn(range: Range, textDocument: TextDocument): Range {