Skip to content

Commit

Permalink
Checking is json path completion is at the at the value and not the p…
Browse files Browse the repository at this point in the history
…ropery key, #563
  • Loading branch information
DaanV2 committed Apr 8, 2024
1 parent 4ba3414 commit 99fc372
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 87 deletions.
5 changes: 4 additions & 1 deletion server/src/Lib/Completion/JsonPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export class JsonPathCompletion {
}

onCompletion(context: SimpleContext<CompletionBuilder>) {
const path = getJsonPath(context.cursor, context.doc);
const { isProperty, path } = getJsonPath(context.cursor, context.doc);
if (!isProperty) {
return;
}

this._items.forEach((item) => {
switch (typeof item.match) {
Expand Down
66 changes: 0 additions & 66 deletions server/src/Lib/Json/Path.test.ts

This file was deleted.

13 changes: 0 additions & 13 deletions server/src/Lib/Json/Path.ts

This file was deleted.

17 changes: 13 additions & 4 deletions server/src/Lib/Minecraft/Json/Path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,22 @@ describe("JsonPath", () => {
const index = json.indexOf(search);

it(`should return the correct path for ${element}`, () => {
const path = getJsonPath(index, json);
expect(path).to.equal(expectedPath);
const result = getJsonPath(index, json);
expect(result.path).to.equal(expectedPath);
expect(result.isProperty).to.be.false;
});

it(`should return the correct path for ${element}, if the cursor is after it`, () => {
const path = getJsonPath(index + search.length, json);
expect(path).to.equal(expectedPath);
const result = getJsonPath(index + search.length, json);
expect(result.path).to.equal(expectedPath);
expect(result.isProperty).to.be.true;
})

it(`should return the correct path for the cursor if before the property: ${element}`, () => {
const result = getJsonPath(index - 1, json);
expect(result.path).to.not.equal(expectedPath);
expect(expectedPath).to.contain(result.path);
expect(result.isProperty).to.be.false;
});
});
});
13 changes: 10 additions & 3 deletions server/src/Lib/Minecraft/Json/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import * as jsonc from 'jsonc-parser';

import { TextDocument } from '../../Types/Document';

export function getJsonPath(cursor: number, text: string | TextDocument) {
export interface Path {
path: string;
isProperty: boolean
}

export function getJsonPath(cursor: number, text: string | TextDocument): Path {
if (typeof text !== "string") {
text = text.getText();
}

const pos = jsonc.getLocation(text, cursor);

return pos.path.join('/');
return {
path: pos.path.join('/'),
isProperty: !pos.isAtPropertyKey
}
}

0 comments on commit 99fc372

Please sign in to comment.