Skip to content
Draft
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: 3 additions & 4 deletions language-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion language-server/src/build-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { SchemaValidation } from "./features/SchemaValidation.ts";
import { Formatting } from "./features/Formatting.ts";
import { Hover } from "./features/Hover.ts";
import { Completion } from "./features/Completion.ts";
import { PropertyCompletion } from "./features/PropertyCompletion.ts";
import { ValueCompletion } from "./features/ValueCompletion.ts";
import { FoldingRanges } from "./features/FoldingRanges.ts";

import "@hyperjump/json-schema/draft-2020-12";
Expand Down Expand Up @@ -37,7 +39,10 @@ export const buildServer = (connection: Connection): Server => {

new Formatting(server, documents);
new Hover(server, documents);
new Completion(server, documents);
new Completion(server, documents, [
new PropertyCompletion(),
new ValueCompletion()
]);
new FoldingRanges(server, documents);

return server;
Expand Down
44 changes: 14 additions & 30 deletions language-server/src/features/Completion.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { CompletionItemKind } from "vscode-languageserver";
import { JsonDocuments } from "../services/JsonDocuments.ts";

import type { CompletionItem, Position, ServerCapabilities } from "vscode-languageserver";
import type { Server } from "../services/Server.ts";
import type { CompletionItem, ServerCapabilities } from "vscode-languageserver";
import type { JsonDocument } from "../models/JsonDocument.ts";
import type { JsonDocuments } from "../services/JsonDocuments.ts";

export type CompletionsProvider = {
getCompletions(jsonDocument: JsonDocument, position: Position): Promise<CompletionItem[]>;
};

export class Completion {
constructor(server: Server, jsonDocuments: JsonDocuments) {
constructor(server: Server, jsonDocuments: JsonDocuments, providers: CompletionsProvider[]) {
server.onInitialize(() => {
const serverCapabilities: ServerCapabilities = {
completionProvider: {
Expand All @@ -18,34 +21,15 @@ export class Completion {
};
});

server.onCompletion(async (params) => {
const jsonDocument = jsonDocuments.get(params.textDocument.uri)!;
const keyNode = jsonDocument.findNodeAtPosition(params.position)!;
const propertyNode = keyNode.parent;

if (propertyNode?.type !== "property" || propertyNode.children![0] !== keyNode) {
return [];
}

const objectNode = propertyNode.parent!;

const propertyNames = await jsonDocument.getDeclaredProperties(objectNode);
for (const node of objectNode.children!) {
if (node === propertyNode) {
continue;
}
server.onCompletion(async ({ textDocument, position }) => {
const jsonDocument = jsonDocuments.get(textDocument.uri)!;

propertyNames.delete(node.children![0].value);
const completions: CompletionItem[] = [];
for (const provider of providers) {
completions.push(...await provider.getCompletions(jsonDocument, position));
}

const completionItems: CompletionItem[] = [];
for (const propertyName of propertyNames) {
completionItems.push({
label: propertyName,
kind: CompletionItemKind.Property
});
}
return completionItems;
return completions;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { describe, test, expect, beforeEach, afterEach } from "vitest";
import { CompletionRequest, CompletionItemKind, PublishDiagnosticsNotification } from "vscode-languageserver";
import { TestClient } from "../test/TestClient.ts";

import type { CompletionItem } from "vscode-languageserver";

describe("Completions", () => {
let client: TestClient;
let fixtureSchemaUri: string;
Expand Down Expand Up @@ -42,7 +44,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([]);
expect(labels(completions)).toEqual([]);
});

test("completion returns properties", async () => {
Expand Down Expand Up @@ -75,7 +77,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "name", kind: CompletionItemKind.Property }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry i forgot what we decided for the property completion tests, should we assert the complete CompletionItem shape (label, kind, filterText, textEdit, command) every time, or just the (label + kind)?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, assert on the complete response with ranges and everything.

]);
});
Expand Down Expand Up @@ -119,7 +121,7 @@ describe("Completions", () => {
position: { line: 3, character: 9 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "street", kind: CompletionItemKind.Property },
{ label: "city", kind: CompletionItemKind.Property },
{ label: "zipCode", kind: CompletionItemKind.Property }
Expand Down Expand Up @@ -159,7 +161,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "age", kind: CompletionItemKind.Property },
{ label: "city", kind: CompletionItemKind.Property }
]);
Expand Down Expand Up @@ -208,7 +210,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "foo", kind: CompletionItemKind.Property },
{ label: "bar", kind: CompletionItemKind.Property },
{ label: "baz", kind: CompletionItemKind.Property }
Expand Down Expand Up @@ -259,7 +261,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "bar", kind: CompletionItemKind.Property },
{ label: "baz", kind: CompletionItemKind.Property }
]);
Expand Down Expand Up @@ -308,7 +310,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "foo", kind: CompletionItemKind.Property },
{ label: "bar", kind: CompletionItemKind.Property },
{ label: "baz", kind: CompletionItemKind.Property }
Expand Down Expand Up @@ -359,7 +361,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "bar", kind: CompletionItemKind.Property }
]);
});
Expand Down Expand Up @@ -407,7 +409,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "foo", kind: CompletionItemKind.Property },
{ label: "bar", kind: CompletionItemKind.Property },
{ label: "baz", kind: CompletionItemKind.Property }
Expand Down Expand Up @@ -458,7 +460,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "bar", kind: CompletionItemKind.Property }
]);
});
Expand Down Expand Up @@ -495,7 +497,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "baz", kind: CompletionItemKind.Property }
]);
});
Expand Down Expand Up @@ -544,7 +546,7 @@ describe("Completions", () => {
position: { line: 4, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "baz", kind: CompletionItemKind.Property }
]);
});
Expand Down Expand Up @@ -595,7 +597,7 @@ describe("Completions", () => {
position: { line: 4, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "baz", kind: CompletionItemKind.Property }
]);
});
Expand Down Expand Up @@ -642,7 +644,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "a", kind: CompletionItemKind.Property },
{ label: "b", kind: CompletionItemKind.Property }
]);
Expand Down Expand Up @@ -695,7 +697,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "foo", kind: CompletionItemKind.Property },
{ label: "c", kind: CompletionItemKind.Property }
]);
Expand Down Expand Up @@ -746,7 +748,7 @@ describe("Completions", () => {
position: { line: 4, character: 7 }
});

expect(completions).toEqual([]);
expect(labels(completions)).toEqual([]);
});

test("patternProperties: suggests only the properties declared by properties", async () => {
Expand Down Expand Up @@ -783,7 +785,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "name", kind: CompletionItemKind.Property }
]);
});
Expand Down Expand Up @@ -834,7 +836,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "a", kind: CompletionItemKind.Property }
]);
});
Expand Down Expand Up @@ -885,7 +887,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "a", kind: CompletionItemKind.Property },
{ label: "b", kind: CompletionItemKind.Property }
]);
Expand Down Expand Up @@ -926,7 +928,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "bar", kind: CompletionItemKind.Property },
{ label: "foo", kind: CompletionItemKind.Property }
]);
Expand Down Expand Up @@ -960,7 +962,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "foo", kind: CompletionItemKind.Property }
]);
});
Expand Down Expand Up @@ -1000,7 +1002,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "bar", kind: CompletionItemKind.Property },
{ label: "foo", kind: CompletionItemKind.Property }
]);
Expand Down Expand Up @@ -1037,7 +1039,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "foo", kind: CompletionItemKind.Property }
]);
});
Expand Down Expand Up @@ -1074,7 +1076,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "a", kind: CompletionItemKind.Property },
{ label: "b", kind: CompletionItemKind.Property }
]);
Expand Down Expand Up @@ -1113,7 +1115,7 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([]);
expect(labels(completions)).toEqual([]);
});

test("not: excludes required properties wrapped in an anyOf branch", async () => {
Expand Down Expand Up @@ -1153,7 +1155,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([]);
expect(labels(completions)).toEqual([]);
});

test("not: excludes required properties wrapped in a oneOf branch", async () => {
Expand Down Expand Up @@ -1193,7 +1195,7 @@ describe("Completions", () => {
position: { line: 2, character: 7 }
});

expect(completions).toEqual([]);
expect(labels(completions)).toEqual([]);
});

test("anyOf: omits a candidate property whose type would violate the additionalProperties constraint of a compatible branch", async () => {
Expand Down Expand Up @@ -1243,8 +1245,13 @@ describe("Completions", () => {
position: { line: 3, character: 7 }
});

expect(completions).toEqual([
expect(labels(completions)).toEqual([
{ label: "c", kind: CompletionItemKind.Property }
]);
});
});

const labels = (completions: CompletionItem[] | { items: CompletionItem[] } | null) => {
const items = Array.isArray(completions) ? completions : completions?.items ?? [];
return items.map((item) => ({ label: item.label, kind: item.kind }));
};
Loading
Loading