Skip to content

Commit

Permalink
Improve JsoncInstance.get with jsonc-parser utility
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesrosiers committed Mar 18, 2024
1 parent ccf0008 commit 60b9052
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions language-server/src/jsonc-instance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseTree } from "jsonc-parser";
import { findNodeAtLocation, parseTree } from "jsonc-parser";
import * as JsonPointer from "@hyperjump/json-pointer";
import { getKeywordId } from "@hyperjump/json-schema/experimental";
import { drop, find, head, some } from "@hyperjump/pact";

Check failure on line 4 in language-server/src/jsonc-instance.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'drop' is defined but never used

Check failure on line 4 in language-server/src/jsonc-instance.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'head' is defined but never used

Check failure on line 4 in language-server/src/jsonc-instance.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'drop' is defined but never used

Check failure on line 4 in language-server/src/jsonc-instance.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'head' is defined but never used
Expand Down Expand Up @@ -119,25 +119,10 @@ export class JsoncInstance {
if (uri[0] !== "#") {
throw Error(`No JSON document found at '${toAbsoluteUri(uri)}'`);
}
let result = new JsoncInstance(this.textDocument, this.root, this.root, "", this.annotations);

const pointer = decodeURI(uri.substring(1));
for (const segment of pointerSegments(pointer)) {
if (!result) {
break;
}

if (result.typeOf() === "object") {
const pair = find(([propertyName]) => propertyName.value() === segment, result.entries());
result = pair?.[1];
} else if (result.typeOf() === "array") {
result = head(drop(parseInt(segment, 10), result.iter()));
} else {
result = undefined;
}
}

return result ?? new JsoncInstance(this.textDocument, this.root, undefined, pointer, this.annotations);
const node = findNodeAtLocation(this.root, [...pointerSegments(pointer)]);
return new JsoncInstance(this.textDocument, this.root, node, pointer, this.annotations);
}

asEmbedded() {
Expand Down

0 comments on commit 60b9052

Please sign in to comment.