Skip to content

Commit

Permalink
Added auto completion for schema values
Browse files Browse the repository at this point in the history
  • Loading branch information
Azzam1503 authored and jdesrosiers committed Mar 21, 2024
1 parent e6ce270 commit 2d7d636
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
9 changes: 8 additions & 1 deletion language-server/src/jsonc-instance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findNodeAtLocation, parseTree } from "jsonc-parser";
import { findNodeAtLocation, findNodeAtOffset, parseTree, getNodePath } from "jsonc-parser";
import * as JsonPointer from "@hyperjump/json-pointer";
import { getKeywordId } from "@hyperjump/json-schema/experimental";
import { find, some } from "@hyperjump/pact";
Expand Down Expand Up @@ -181,6 +181,13 @@ export class JsoncInstance {
textLength() {
return this.node.length;
}

getInstanceAtPosition(position) {
const node = findNodeAtOffset(this.root, this.textDocument.offsetAt(position));
const pathToNode = getNodePath(node);
const pointer = pathToNode.reduce((pointer, segment) => JsonPointer.append(segment, pointer), "");
return new JsoncInstance(this.textDocument, this.root, node, pointer, this.annotation);
}
}

const pointerSegments = function* (pointer) {
Expand Down
29 changes: 27 additions & 2 deletions language-server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import {
DidChangeConfigurationNotification,
SemanticTokensBuilder,
TextDocuments,
TextDocumentSyncKind
TextDocumentSyncKind,
CompletionItemKind
} from "vscode-languageserver/node.js";
import { TextDocument } from "vscode-languageserver-textdocument";
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";

// Hyperjump
import { setMetaSchemaOutputFormat, setShouldValidateSchema } from "@hyperjump/json-schema";
import { hasDialect, DETAILED } from "@hyperjump/json-schema/experimental";
import { hasDialect, DETAILED, getDialectIds } from "@hyperjump/json-schema/experimental";
import "@hyperjump/json-schema/draft-2020-12";
import "@hyperjump/json-schema/draft-2019-09";
import "@hyperjump/json-schema/draft-07";
Expand Down Expand Up @@ -67,6 +68,10 @@ connection.onInitialize(({ capabilities, workspaceFolders }) => {
full: {
delta: true
}
},
completionProvider: {
resolveProvider: false,
triggerCharacters: ["\"", ":", " "]
}
};

Expand Down Expand Up @@ -367,5 +372,25 @@ connection.languages.semanticTokens.onDelta(async ({ textDocument, previousResul
return builder.buildEdits();
});

// $SCHEMA COMPLETION
connection.onCompletion((textDocumentPosition) => {
const doc = documents.get(textDocumentPosition.textDocument.uri);
if (!doc) {
return [];
}

const instance = JsoncInstance.fromTextDocument(doc);

const currentProperty = instance.getInstanceAtPosition(textDocumentPosition.position);
if (currentProperty.pointer.endsWith("/$schema")) {
return getDialectIds().map((uri) => {
return {
label: uri,
kind: CompletionItemKind.Value
};
});
}
});

connection.listen();
documents.listen(connection);

0 comments on commit 2d7d636

Please sign in to comment.