From 99fc372c7dde95142e806adab337341624b4bbbe Mon Sep 17 00:00:00 2001 From: DaanV2 Date: Mon, 8 Apr 2024 20:39:19 +0200 Subject: [PATCH] Checking is json path completion is at the at the value and not the propery key, #563 --- server/src/Lib/Completion/JsonPath.ts | 5 +- server/src/Lib/Json/Path.test.ts | 66 ---------------------- server/src/Lib/Json/Path.ts | 13 ----- server/src/Lib/Minecraft/Json/Path.test.ts | 17 ++++-- server/src/Lib/Minecraft/Json/Path.ts | 13 ++++- 5 files changed, 27 insertions(+), 87 deletions(-) delete mode 100644 server/src/Lib/Json/Path.test.ts delete mode 100644 server/src/Lib/Json/Path.ts diff --git a/server/src/Lib/Completion/JsonPath.ts b/server/src/Lib/Completion/JsonPath.ts index d1f86d9a6..51be9ee4d 100644 --- a/server/src/Lib/Completion/JsonPath.ts +++ b/server/src/Lib/Completion/JsonPath.ts @@ -20,7 +20,10 @@ export class JsonPathCompletion { } onCompletion(context: SimpleContext) { - 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) { diff --git a/server/src/Lib/Json/Path.test.ts b/server/src/Lib/Json/Path.test.ts deleted file mode 100644 index e6fa7b249..000000000 --- a/server/src/Lib/Json/Path.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { expect } from 'chai'; -import { getJsonPath } from './Path'; - -interface TestCase { - element: string; - expectedPath: string; -} - -describe("JsonPath", () => { - const json = `{ - "format_version": "1.20.41", - "minecraft:volume": { - "description": { - "identifier": "bar:foo" - }, - "components": { - "minecraft:bounds": { - "min": [-50, 0, -50], - "max": [50, 256, 50] - }, - "minecraft:fog": { - "fog_identifier": "minecraft:fog_savanna", - "priority": 1 - } - } - } - }`; - - const properties: TestCase[] = [ - { - element: "fog_identifier", - expectedPath: "minecraft:volume/components/minecraft:fog/fog_identifier" - }, - { - element: "min", - expectedPath: "minecraft:volume/components/minecraft:bounds/min" - }, - { - element: "max", - expectedPath: "minecraft:volume/components/minecraft:bounds/max" - }, - { - element: "identifier", - expectedPath: "minecraft:volume/description/identifier" - }, - { - element: "format_version", - expectedPath: "format_version" - } - ]; - - properties.forEach(({ element, expectedPath }) => { - const search = `"${element}":`; - const index = json.indexOf(search); - - it(`should return the correct path for ${element}`, () => { - const path = getJsonPath(index, json); - expect(path).to.equal(expectedPath); - }); - - 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); - }) - }); -}); diff --git a/server/src/Lib/Json/Path.ts b/server/src/Lib/Json/Path.ts deleted file mode 100644 index 01ba80374..000000000 --- a/server/src/Lib/Json/Path.ts +++ /dev/null @@ -1,13 +0,0 @@ -import * as jsonc from 'jsonc-parser'; - -import { TextDocument } from '../Types/Document'; - -export function getJsonPath(cursor: number, text: string | TextDocument) { - if (typeof text !== "string") { - text = text.getText(); - } - - const pos = jsonc.getLocation(text, cursor); - - return pos.path.join('/'); -} \ No newline at end of file diff --git a/server/src/Lib/Minecraft/Json/Path.test.ts b/server/src/Lib/Minecraft/Json/Path.test.ts index e6fa7b249..8ffb4709e 100644 --- a/server/src/Lib/Minecraft/Json/Path.test.ts +++ b/server/src/Lib/Minecraft/Json/Path.test.ts @@ -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; + }); }); }); diff --git a/server/src/Lib/Minecraft/Json/Path.ts b/server/src/Lib/Minecraft/Json/Path.ts index d09197ae8..2cc2c01c1 100644 --- a/server/src/Lib/Minecraft/Json/Path.ts +++ b/server/src/Lib/Minecraft/Json/Path.ts @@ -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 + } } \ No newline at end of file