Skip to content

Commit

Permalink
Merge pull request #170 from uqbar-project/fix-package-definition
Browse files Browse the repository at this point in the history
Fix: go-to package definition
  • Loading branch information
PalumboN authored Jun 26, 2024
2 parents 21f2829 + ba3a106 commit f87c5af
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
58 changes: 57 additions & 1 deletion server/src/test/text-document.test.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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', () => {
Expand Down
19 changes: 13 additions & 6 deletions server/src/utils/text-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f87c5af

Please sign in to comment.