Skip to content

Commit

Permalink
Update server.js and json-instance.js for schema completion
Browse files Browse the repository at this point in the history
  • Loading branch information
Azzam1503 committed Mar 20, 2024
1 parent 72e65b9 commit eba5da0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
11 changes: 6 additions & 5 deletions language-server/src/jsonc-instance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findNodeAtLocation, findNodeAtOffset, 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 @@ -179,10 +179,11 @@ export class JsoncInstance {
return this.node.length;
}

findProperty(instance) {
const keyNode = findNodeAtOffset(instance.root, instance.node.children[0]?.children[0]?.offset);
const valNode = findNodeAtOffset(instance.root, instance.node.children[0]?.children[1]?.offset);
return { key: keyNode, value: valNode };
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);
}
}

Expand Down
19 changes: 14 additions & 5 deletions language-server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,20 +362,29 @@ connection.languages.semanticTokens.onDelta(async ({ textDocument, previousResul

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

const instance = JsoncInstance.fromTextDocument(doc);
const currentProperty = instance.findProperty(instance);

if (currentProperty?.key?.value === "$schema" && currentProperty?.value?.value === "") {
const currentProperty = instance.getInstanceAtPosition(textDocumentPosition.position);
if (currentProperty.pointer.endsWith("/$schema")) {
return getDialectIds().map((uri) => {
const range = {
start: currentProperty.startPosition(),
end: {
line: currentProperty.endPosition().line,
character: currentProperty.endPosition().character + 1
}
};
return {
label: uri,
kind: CompletionItemKind.Keyword
label: `"${uri}"`,
kind: CompletionItemKind.Keyword,
textEdit: {
range: range
}
};
});
}
Expand Down

0 comments on commit eba5da0

Please sign in to comment.