Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added goto-references #57

Merged
merged 1 commit into from
Aug 2, 2024
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
21 changes: 11 additions & 10 deletions language-server/src/features/hover.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest";
import { HoverRequest, MarkupKind } from "vscode-languageserver";
import { TestClient } from "../test-client.js";
import hover from "./hover.js";
import schemaRegistry from "./schema-registry.js";

import type { Hover, MarkupContent } from "vscode-languageserver";
import type { DocumentSettings } from "./document-settings.js";
Expand All @@ -11,7 +12,7 @@ describe("Feature - Hover", () => {
let client: TestClient<DocumentSettings>;

beforeAll(async () => {
client = new TestClient([hover]);
client = new TestClient([schemaRegistry, hover]);
await client.start();
});

Expand Down Expand Up @@ -41,7 +42,7 @@ describe("Feature - Hover", () => {
});
});

afterAll(async () => {
afterEach(async () => {
await client.closeDocument(documentUri);
});

Expand Down Expand Up @@ -77,7 +78,7 @@ describe("Feature - Hover", () => {
});
});

afterAll(async () => {
afterEach(async () => {
await client.closeDocument(documentUri);
});

Expand Down Expand Up @@ -107,7 +108,7 @@ describe("Feature - Hover", () => {
});
});

afterAll(async () => {
afterEach(async () => {
await client.closeDocument(documentUri);
});

Expand All @@ -119,7 +120,7 @@ describe("Feature - Hover", () => {
describe("2020-12", () => {
let documentUri: string;

afterAll(async () => {
afterEach(async () => {
await client.closeDocument(documentUri);
});

Expand Down Expand Up @@ -242,7 +243,7 @@ describe("Feature - Hover", () => {
describe("2019-09", () => {
let documentUri: string;

afterAll(async () => {
afterEach(async () => {
await client.closeDocument(documentUri);
});

Expand Down Expand Up @@ -363,7 +364,7 @@ describe("Feature - Hover", () => {
describe("draft-07", () => {
let documentUri: string;

afterAll(async () => {
afterEach(async () => {
await client.closeDocument(documentUri);
});

Expand Down Expand Up @@ -466,7 +467,7 @@ describe("Feature - Hover", () => {
describe("draft-06", () => {
let documentUri: string;

afterAll(async () => {
afterEach(async () => {
await client.closeDocument(documentUri);
});

Expand Down Expand Up @@ -564,7 +565,7 @@ describe("Feature - Hover", () => {
describe("draft-04", () => {
let documentUri: string;

afterAll(async () => {
afterEach(async () => {
await client.closeDocument(documentUri);
});

Expand Down
3 changes: 2 additions & 1 deletion language-server/src/features/if-then-completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CompletionRequest } from "vscode-languageserver";
import { TestClient } from "../test-client.js";
import completion from "./completion.js";
import ifThenCompletionFeature, { ifThenPatternCompletion } from "./if-then-completion.js";
import schemaRegistry from "./schema-registry.js";

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

Expand All @@ -12,7 +13,7 @@ describe("Feature - if/then completion", () => {
let documentUri: string;

beforeAll(async () => {
client = new TestClient([completion, ifThenCompletionFeature]);
client = new TestClient([schemaRegistry, completion, ifThenCompletionFeature]);
await client.start();
});

Expand Down
80 changes: 80 additions & 0 deletions language-server/src/features/references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as SchemaDocument from "../schema-document.js";
import * as SchemaNode from "../schema-node.js";
import { getSchemaDocument, allSchemaDocuments } from "./schema-registry.js";
import { references } from "./validate-references.js";

/** @import { Feature } from "../build-server.js" */
/** @import { SchemaNode as SchemaNodeType } from "../schema-node.js" */

/** @type Feature */
export default {
load(connection, documents) {
const highlightBlockDialects = new Set([
"http://json-schema.org/draft-04/schema",
"http://json-schema.org/draft-06/schema",
"http://json-schema.org/draft-07/schema"
]);
const shouldHighlightBlock = (/** @type {string | undefined} */ uri) => {
if (uri === undefined) {
return false;
}
return highlightBlockDialects.has(uri);
};

connection.onReferences(async ({ textDocument, position }) => {
const document = documents.get(textDocument.uri);
if (!document) {
return [];
}

const schemaDocument = await getSchemaDocument(connection, document);
const offset = document.offsetAt(position);
const node = SchemaDocument.findNodeAtOffset(schemaDocument, offset);

if (!node) {
return [];
}

const targetSchemaUri = SchemaNode.uri(node);
const schemaReferences = [];

for (const schemaDocument of allSchemaDocuments()) {
for (const schemaResource of schemaDocument.schemaResources) {
for (const referenceNode of references(schemaResource)) {
const reference = SchemaNode.value(referenceNode);
const referencedSchema = SchemaNode.get(reference, schemaResource);
if (!referencedSchema) {
continue;
}

if (SchemaNode.uri(referencedSchema) === targetSchemaUri) {
const hightlightNode = /** @type SchemaNodeType */ (shouldHighlightBlock(referenceNode.dialectUri)
? referenceNode.parent?.parent
: referenceNode);
schemaReferences.push({
uri: schemaDocument.textDocument.uri,
range: {
start: schemaDocument.textDocument.positionAt(hightlightNode.offset),
end: schemaDocument.textDocument.positionAt(hightlightNode.offset + hightlightNode.textLength)
}
});
}
}
}
}
return schemaReferences;
});
},

onInitialize() {
return {
referencesProvider: true
};
},

async onInitialized() {
},

onShutdown() {
}
};
203 changes: 203 additions & 0 deletions language-server/src/features/references.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { ReferencesRequest } from "vscode-languageserver";
import { TestClient } from "../test-client.js";
import documentSettings from "./document-settings.js";
import schemaRegistry from "./schema-registry.js";
import workspace from "./workspace.js";
import ReferencesFeature from "./references.js";

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


describe("Feature - References", () => {
let client: TestClient<DocumentSettings>;

beforeEach(async () => {
client = new TestClient([
workspace,
documentSettings,
schemaRegistry,
ReferencesFeature
]);
await client.start();
});

afterEach(async () => {
await client.stop();
});

test("no references", async () => {
const documentUri = await client.openDocument("./subject.schema.json", `{}`);

const response = await client.sendRequest(ReferencesRequest.type, {
textDocument: { uri: documentUri },
position: {
line: 0,
character: 1
},
context: { includeDeclaration: false }
});

expect(response).to.eql([]);
});

test("don't return references that do not match location", async () => {
const documentUri = await client.openDocument("./subject.schema.json", `{
"$schema":"https://json-schema.org/draft/2020-12/schema",
"$ref": "#/definitions/locations",
"definitions":{
"names": {

},
"locations": {

}
},
}`);

const response = await client.sendRequest(ReferencesRequest.type, {
textDocument: { uri: documentUri },
position: {
line: 5,
character: 4
},
context: { includeDeclaration: false }
});

expect(response).to.eql([]);
});


test("match one reference", async () => {
const documentUri = await client.openDocument("./subject.schema.json", `{
"$schema":"https://json-schema.org/draft/2020-12/schema",
"$ref": "#/$defs/names",
"$defs":{
"names": {

}
},
}`);

const response = await client.sendRequest(ReferencesRequest.type, {
textDocument: { uri: documentUri },
position: {
line: 5,
character: 4
},
context: { includeDeclaration: false }
});

expect(response).to.eql([
{
"uri": documentUri,
"range": {
"start": { "line": 2, "character": 10 },
"end": { "line": 2, "character": 25 }
}
}
]);
});

test("cross file reference", async () => {
const documentUriA = await client.openDocument("./subjectA.schema.json", `{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"person": {

}
}
}
`);
const documentUriB = await client.openDocument("./subjectB.schema.json", `{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "./subjectA.schema.json#/definitions/person"
}
`);

const response = await client.sendRequest(ReferencesRequest.type, {
textDocument: { uri: documentUriA },
position: {
line: 4,
character: 4
},
context: { includeDeclaration: false }
});

expect(response).to.eql([
{
"uri": documentUriB,
"range": {
"start": { "line": 0, "character": 0 },
"end": { "line": 3, "character": 1 }
}
}
]);
});

test("match self identified externally", async () => {
const documentUri = await client.openDocument("./subject.schema.json", `{
"$schema":"http://json-schema.org/draft-07/schema#",
"$ref": "https://example.com/schemas/two#/definitions/names",
}`);

const documentUriB = await client.openDocument("./subjectB.schema.json", `{
"$schema":"http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/schemas/two",
"definitions":{
"names": {

}
}
}`);

const response = await client.sendRequest(ReferencesRequest.type, {
textDocument: { uri: documentUriB },
position: {
line: 5,
character: 4
},
context: { includeDeclaration: false }
});

expect(response).to.eql([
{
"uri": documentUri,
"range": {
"start": { "line": 0, "character": 0 },
"end": { "line": 3, "character": 1 }
}
}
]);
});

test("match self identified internally", async () => {
const documentUri = await client.openDocument("./subject.schema.json", `{
"$schema":"http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/person.json",
"type": "object",
"properties": {
"names": { "$ref": "https://example.com/person.json" }
}
}`);

const response = await client.sendRequest(ReferencesRequest.type, {
textDocument: { uri: documentUri },
position: {
line: 2,
character: 46
},
context: { includeDeclaration: false }
});

expect(response).to.eql([
{
"uri": documentUri,
"range": {
"start": { "line": 5, "character": 13 },
"end": { "line": 5, "character": 58 }
}
}
]);
});
});
Loading