Skip to content

Commit 790dd07

Browse files
committed
Fixes file paths
1 parent fe55529 commit 790dd07

16 files changed

+228
-41
lines changed

language-server/package-lock.json

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

language-server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"jsonc-parser": "^3.2.1",
1919
"merge-anything": "^6.0.2",
2020
"vscode-languageserver": "^9.0.0",
21-
"vscode-languageserver-textdocument": "^1.0.8"
21+
"vscode-languageserver-textdocument": "^1.0.8",
22+
"vscode-uri": "^3.0.8"
2223
},
2324
"engines": {
2425
"node": ">=20.13.0"

language-server/src/features/definition.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

language-server/src/features/definition.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { some } from "@hyperjump/pact";
22
import * as SchemaDocument from "../schema-document.js";
33
import * as SchemaNode from "../schema-node.js";
4-
import { getSchemaDocument } from "./schema-registry.js";
4+
import { getSchemaDocument, getSchemaDocumentBySchemaUri } from "./schema-registry.js";
55
import { keywordNameFor } from "../util.js";
66

77
/** @import { Feature } from "../build-server.js" */
@@ -25,13 +25,10 @@ export default {
2525
if (highlightBlockDialects.has(node.dialectUri)) {
2626
// Legacy reference
2727
const legacyRefToken = keywordNameFor("https://json-schema.org/keyword/draft-04/ref", node.dialectUri);
28-
if (!node.parent) {
28+
if (!node.parent || !node.parent.parent) {
2929
return false;
3030
}
3131

32-
if (!node.parent.parent) {
33-
return false;
34-
}
3532
const referenceNode = node.parent.parent;
3633
return some((keywordNode) => SchemaNode.value(keywordNode) === legacyRefToken, SchemaNode.keys(referenceNode));
3734
} else {
@@ -56,23 +53,27 @@ export default {
5653
const offset = document.offsetAt(position);
5754
const node = SchemaDocument.findNodeAtOffset(schemaDocument, offset);
5855

59-
if (!node) {
56+
if (!node || !isReference(node)) {
6057
return [];
6158
}
6259

63-
if (!isReference(node)) {
64-
return [];
65-
}
6660
const reference = SchemaNode.value(node);
6761
const targetSchema = SchemaNode.get(reference, node);
62+
6863
if (!targetSchema) {
6964
return [];
7065
}
66+
67+
const targetSchemaDocument = getSchemaDocumentBySchemaUri(targetSchema.baseUri);
68+
if (!targetSchemaDocument) {
69+
return [];
70+
}
71+
7172
const gotoDefinitions = [{
72-
uri: schemaDocument.textDocument.uri,
73+
uri: targetSchemaDocument.textDocument.uri,
7374
range: {
74-
start: schemaDocument.textDocument.positionAt(targetSchema.offset),
75-
end: schemaDocument.textDocument.positionAt(targetSchema.offset + targetSchema.textLength)
75+
start: targetSchemaDocument.textDocument.positionAt(targetSchema.offset),
76+
end: targetSchemaDocument.textDocument.positionAt(targetSchema.offset + targetSchema.textLength)
7677
}
7778
}];
7879

language-server/src/features/definition.test.ts

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,162 @@ describe("Feature - Goto Definition", () => {
7070
}
7171
}]);
7272
});
73+
74+
test("match one reference", async () => {
75+
const documentUri = await client.openDocument("./subject.schema.json", `{
76+
"$schema":"https://json-schema.org/draft/2020-12/schema",
77+
"$ref": "#/$defs/names",
78+
"$defs":{
79+
"names": {
80+
81+
}
82+
},
83+
}`);
84+
85+
const response = await client.sendRequest(DefinitionRequest.type, {
86+
textDocument: { uri: documentUri },
87+
position: {
88+
line: 2,
89+
character: 20
90+
}
91+
});
92+
93+
expect(response).to.eql([
94+
{
95+
"uri": documentUri,
96+
"range": {
97+
"start": { "line": 4, "character": 13 },
98+
"end": { "line": 6, "character": 5 }
99+
}
100+
}
101+
]);
102+
});
103+
test("match one definition", async () => {
104+
const documentUri = await client.openDocument("./subject.schema.json", `{
105+
"$schema":"https://json-schema.org/draft/2020-12/schema",
106+
"$ref": "#/$defs/names",
107+
"$defs":{
108+
"names": {
109+
110+
}
111+
},
112+
}`);
113+
114+
const response = await client.sendRequest(DefinitionRequest.type, {
115+
textDocument: { uri: documentUri },
116+
position: {
117+
line: 2,
118+
character: 18
119+
}
120+
});
121+
122+
expect(response).to.eql([
123+
{
124+
"uri": documentUri,
125+
"range": {
126+
"start": { "line": 4, "character": 13 },
127+
"end": { "line": 6, "character": 5 }
128+
}
129+
}
130+
]);
131+
});
132+
133+
test("cross file definition", async () => {
134+
const documentUriA = await client.openDocument("./subjectA.schema.json", `{
135+
"$schema": "http://json-schema.org/draft-07/schema#",
136+
"definitions": {
137+
"person": {
138+
139+
}
140+
}
141+
}
142+
`);
143+
const documentUriB = await client.openDocument("./subjectB.schema.json", `{
144+
"$schema": "http://json-schema.org/draft-07/schema#",
145+
"$ref": "./subjectA.schema.json#/definitions/person"
146+
}
147+
`);
148+
149+
const response = await client.sendRequest(DefinitionRequest.type, {
150+
textDocument: { uri: documentUriB },
151+
position: {
152+
line: 2,
153+
character: 20
154+
}
155+
});
156+
157+
expect(response).to.eql([
158+
{
159+
"uri": documentUriA,
160+
"range": {
161+
"start": { "line": 3, "character": 14 },
162+
"end": { "line": 5, "character": 5 }
163+
}
164+
}
165+
]);
166+
});
167+
168+
test("match self identified externally", async () => {
169+
const documentUri = await client.openDocument("./subject.schema.json", `{
170+
"$schema":"http://json-schema.org/draft-07/schema#",
171+
"$ref": "https://example.com/schemas/two#/definitions/names",
172+
}`);
173+
174+
const documentUriB = await client.openDocument("./subjectB.schema.json", `{
175+
"$schema":"http://json-schema.org/draft-07/schema#",
176+
"$id": "https://example.com/schemas/two",
177+
"definitions":{
178+
"names": {
179+
180+
}
181+
}
182+
}`);
183+
184+
const response = await client.sendRequest(DefinitionRequest.type, {
185+
textDocument: { uri: documentUri },
186+
position: {
187+
line: 2,
188+
character: 37
189+
}
190+
});
191+
192+
expect(response).to.eql([
193+
{
194+
"uri": documentUriB,
195+
"range": {
196+
"start": { "line": 4, "character": 13 },
197+
"end": { "line": 6, "character": 5 }
198+
}
199+
}
200+
]);
201+
});
202+
203+
test("match self identified internally", async () => {
204+
const documentUri = await client.openDocument("./subject.schema.json", `{
205+
"$schema":"http://json-schema.org/draft-07/schema#",
206+
"$id": "https://example.com/person.json",
207+
"type": "object",
208+
"properties": {
209+
"names": { "$ref": "https://example.com/person.json" }
210+
}
211+
}`);
212+
213+
const response = await client.sendRequest(DefinitionRequest.type, {
214+
textDocument: { uri: documentUri },
215+
position: {
216+
line: 5,
217+
character: 30
218+
}
219+
});
220+
221+
expect(response).to.eql([
222+
{
223+
"uri": documentUri,
224+
"range": {
225+
"start": { "line": 0, "character": 0 },
226+
"end": { "line": 7, "character": 1 }
227+
}
228+
}
229+
]);
230+
});
73231
});

language-server/src/features/references.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export default {
6262
}
6363
}
6464
}
65+
6566
return schemaReferences;
6667
});
6768
},

language-server/src/features/schema-registry.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export const getSchemaDocument = async (connection, textDocument) => {
4545
return schemaDocument;
4646
};
4747

48-
export const clearSchemaDocuments = () => schemaDocuments.clear();
48+
export const clearSchemaDocuments = () => {
49+
schemaDocuments.clear();
50+
};
4951

5052
export const allSchemaDocuments = function* () {
5153
for (const { schemaDocument } of schemaDocuments.values()) {
@@ -63,3 +65,14 @@ export const getSchemaResource = (schemaUri) => {
6365
}
6466
}
6567
};
68+
69+
/** @type (schemaUri: string) => SchemaDocumentType | undefined */
70+
export const getSchemaDocumentBySchemaUri = (schemaUri) => {
71+
for (const schemaDocument of allSchemaDocuments()) {
72+
for (const schemaResource of schemaDocument.schemaResources) {
73+
if (schemaResource.baseUri === schemaUri) {
74+
return schemaDocument;
75+
}
76+
}
77+
}
78+
};

language-server/src/features/workspace-neovim.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
} from "vscode-languageserver";
99
import { utimes } from "node:fs/promises";
1010
import { fileURLToPath } from "node:url";
11-
import { resolveIri } from "@hyperjump/uri";
1211
import workspace from "./workspace.js";
1312
import documentSettings from "./document-settings.js";
1413
import schemaRegistry from "./schema-registry.js";
14+
import { resolveIri } from "../util.js";
1515

1616
import type { DocumentSettings } from "./document-settings.js";
1717

language-server/src/features/workspace.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { watch } from "node:fs";
22
import { readdir, readFile } from "node:fs/promises";
33
import { resolve } from "node:path";
4-
import { fileURLToPath, pathToFileURL } from "node:url";
4+
import { fileURLToPath } from "node:url";
55
import {
66
DiagnosticSeverity,
77
DidChangeWatchedFilesNotification,
@@ -10,6 +10,7 @@ import {
1010
TextDocumentSyncKind
1111
} from "vscode-languageserver";
1212
import { TextDocument } from "vscode-languageserver-textdocument";
13+
import { URI } from "vscode-uri";
1314
import { publishAsync, subscribe, unsubscribe } from "../pubsub.js";
1415
import { allSchemaDocuments, getSchemaDocument } from "./schema-registry.js";
1516
import { getDocumentSettings } from "./document-settings.js";
@@ -260,7 +261,7 @@ const workspaceSchemas = async function* (schemaFilePatterns) {
260261
if (isMatchedFile(filename, schemaFilePatterns)) {
261262
const schemaPath = resolve(path, filename);
262263

263-
yield pathToFileURL(schemaPath).toString();
264+
yield URI.file(schemaPath).toString();
264265
}
265266
}
266267
}

language-server/src/features/workspace.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import {
77
WorkDoneProgress,
88
WorkDoneProgressCreateRequest
99
} from "vscode-languageserver";
10-
import { resolveIri } from "@hyperjump/uri";
1110
import documentSettings from "./document-settings.js";
1211
import semanticTokens from "./semantic-tokens.js";
1312
import schemaRegistry from "./schema-registry.js";
1413
import workspace from "./workspace.js";
14+
import { resolveIri } from "../util.js";
1515

1616
import type { DocumentSettings } from "./document-settings.js";
1717

0 commit comments

Comments
 (0)