Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions server/src/providers/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ function createDefinitionLink(
];
}

function isLocalReference(targetPath: string): boolean {
return targetPath.startsWith('#');
}

function resolveTargetUriAndFragment(documentUri: string, targetPath: string) {
if (isLocalReference(targetPath)) {
return { targetUri: documentUri, fragment: targetPath.slice(1) };
}
const uri = URI.parse(targetPath);
const currentDir = path.dirname(URI.parse(documentUri).fsPath);
const absolutePath = path.resolve(currentDir, uri.path.slice(1));
Expand Down
28 changes: 28 additions & 0 deletions server/src/test/definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,32 @@ suite('Definition Test Suite', () => {
// ^--- value {} is at character 27
assert.strictEqual(link.targetRange.start.character, 27);
});

test('Should return a definition for local $ref', () => {
const text = '{"foo": { "$ref": "#/a" }, "a": 42}';
const uri = URI.file(path.resolve('/abs/path/local.jref')).toString();
const doc = TextDocument.create(uri, 'jref', 1, text);

const { symbols } = analyze(text);

const context = {
documents: new MockTextDocuments([doc]) as any,
documentSymbols: new WeakMap([[doc, symbols]]),
};

const params: DefinitionParams = {
textDocument: { uri: uri },
position: { line: 0, character: 19 }, // Inside "#/a"
};

const result = onDefinition(params, context);

assert.ok(result && result.length > 0);
const link = result![0];
assert.ok(link.targetUri.endsWith('local.jref'));
// Target range should point to the "a" property value in schema.jref
// {"foo": { "$ref": "#/a" }, "a": 42}
// ^--- value 42 is at character 32
assert.strictEqual(link.targetRange.start.character, 32);
});
});
8 changes: 8 additions & 0 deletions testfiles/local-references.jref
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"foo": { "$ref": "#/a" },
"a": 42,
"bar": { "$ref": "#/bob/age" },
"bob": {
"age": 20
}
}
Loading