From 4f699028be4173bdd802b076728b04c9db4c18c4 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Sat, 22 Jun 2024 14:35:54 +0200 Subject: [PATCH 1/4] Refactor EnumSchemaNode to improve handling of "Any" type --- server/gx-workflow-ls-format2/src/schema/definitions.ts | 6 +++++- .../src/services/completionService.ts | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/server/gx-workflow-ls-format2/src/schema/definitions.ts b/server/gx-workflow-ls-format2/src/schema/definitions.ts index 45b13e9..93e8e83 100644 --- a/server/gx-workflow-ls-format2/src/schema/definitions.ts +++ b/server/gx-workflow-ls-format2/src/schema/definitions.ts @@ -195,12 +195,16 @@ export class EnumSchemaNode implements SchemaNode { return false; } + public get canBeAny(): boolean { + return this.name === "Any"; + } + public get typeRef(): string { return this._schemaEnum.name; } public matchesType(typeName: string): boolean { - return this.name === "Any" || this.symbols.includes(typeName); + return this.canBeAny || this.symbols.includes(typeName); } //Override toString for debugging purposes diff --git a/server/gx-workflow-ls-format2/src/services/completionService.ts b/server/gx-workflow-ls-format2/src/services/completionService.ts index feb374a..d147535 100644 --- a/server/gx-workflow-ls-format2/src/services/completionService.ts +++ b/server/gx-workflow-ls-format2/src/services/completionService.ts @@ -53,6 +53,8 @@ export class GxFormat2CompletionService { const position = textBuffer.getPosition(offset); const isPositionAfterColon = textBuffer.isPositionAfterToken(position, ":"); if (schemaNode instanceof EnumSchemaNode) { + if (schemaNode.canBeAny) return result; + schemaNode.symbols .filter((v) => v.startsWith(currentWord)) .forEach((value) => { From 4f3be1f0ea77ac0380e326549e671f6f206b176f Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Sat, 22 Jun 2024 14:37:39 +0200 Subject: [PATCH 2/4] Add test coverage for suggestions of type Any --- .../tests/integration/completion.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/server/gx-workflow-ls-format2/tests/integration/completion.test.ts b/server/gx-workflow-ls-format2/tests/integration/completion.test.ts index e1c8e81..36c520e 100644 --- a/server/gx-workflow-ls-format2/tests/integration/completion.test.ts +++ b/server/gx-workflow-ls-format2/tests/integration/completion.test.ts @@ -354,4 +354,16 @@ report: const completionLabels = getCompletionItemsLabels(completions); expect(completionLabels).toEqual(EXPECTED_COMPLETION_LABELS); }); + + it("should not suggest properties when the type of the property is 'Any'", async () => { + const template = ` +class: GalaxyWorkflow +creator: + $`; + const { contents, position } = parseTemplate(template); + + const completions = await getCompletions(contents, position); + + expect(completions?.items).toHaveLength(0); + }); }); From d5b39c25a9d20b90dbf1ab2f535ba91a16d67942 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Sat, 22 Jun 2024 14:58:48 +0200 Subject: [PATCH 3/4] Refactor completionService to handle empty documents and improve suggestions --- .../src/services/completionService.ts | 6 ++++++ .../yaml-language-service/src/parser/yamlDocument.ts | 1 + .../yaml-language-service/src/utils/textBuffer.ts | 9 ++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/server/gx-workflow-ls-format2/src/services/completionService.ts b/server/gx-workflow-ls-format2/src/services/completionService.ts index d147535..1b53c7e 100644 --- a/server/gx-workflow-ls-format2/src/services/completionService.ts +++ b/server/gx-workflow-ls-format2/src/services/completionService.ts @@ -25,6 +25,12 @@ export class GxFormat2CompletionService { const offset = textBuffer.getOffsetAt(position); let node = nodeManager.getNodeFromOffset(offset); + if (node === undefined && !textBuffer.isEmpty()) { + // Do not suggest completions if we cannot find a node at the current position + // If the document is empty, we can still suggest the root properties + return Promise.resolve(result); + } + const nodePath = nodeManager.getPathFromNode(node); let schemaNode = this.schemaNodeResolver.resolveSchemaContext(nodePath); if (schemaNode === undefined) { diff --git a/server/packages/yaml-language-service/src/parser/yamlDocument.ts b/server/packages/yaml-language-service/src/parser/yamlDocument.ts index 78dc7fe..26adc7c 100644 --- a/server/packages/yaml-language-service/src/parser/yamlDocument.ts +++ b/server/packages/yaml-language-service/src/parser/yamlDocument.ts @@ -111,6 +111,7 @@ export class YAMLDocument implements ParsedDocument { if (indentation === 0) return this.root; const parentIndentation = Math.max(0, indentation - this._indentation); const parentLine = this._textBuffer.findPreviousLineWithSameIndentation(offset, parentIndentation); + if (parentLine === undefined) return undefined; const parentOffset = this._textBuffer.getOffsetAt(Position.create(parentLine, parentIndentation)); const rootNode = this.root as ObjectASTNodeImpl; diff --git a/server/packages/yaml-language-service/src/utils/textBuffer.ts b/server/packages/yaml-language-service/src/utils/textBuffer.ts index b29896b..f3ad19f 100644 --- a/server/packages/yaml-language-service/src/utils/textBuffer.ts +++ b/server/packages/yaml-language-service/src/utils/textBuffer.ts @@ -97,7 +97,7 @@ export class TextBuffer { return indentation; } - public findPreviousLineWithSameIndentation(offset: number, indentation: number): number { + public findPreviousLineWithSameIndentation(offset: number, indentation: number): number | undefined { const position = this.getPosition(offset); const indentationSpaces = " ".repeat(indentation); let currentLine = position.line - 1; @@ -111,6 +111,9 @@ export class TextBuffer { currentLine--; } } + if (!found) { + return undefined; + } return currentLine; } @@ -122,4 +125,8 @@ export class TextBuffer { } return tokenIndex < position.character; } + + public isEmpty(): boolean { + return this.doc.getText().trim().length === 0; + } } From cdd279bb1c7399988a8c3e534b501cf7b3b4dbfc Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Sat, 22 Jun 2024 14:59:25 +0200 Subject: [PATCH 4/4] Add test to check incorrect indent in suggestions --- .../tests/integration/completion.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/server/gx-workflow-ls-format2/tests/integration/completion.test.ts b/server/gx-workflow-ls-format2/tests/integration/completion.test.ts index 36c520e..d5b7f16 100644 --- a/server/gx-workflow-ls-format2/tests/integration/completion.test.ts +++ b/server/gx-workflow-ls-format2/tests/integration/completion.test.ts @@ -366,4 +366,18 @@ creator: expect(completions?.items).toHaveLength(0); }); + + it("should not suggest any properties when the indent is not correct", async () => { + const template = ` +class: GalaxyWorkflow +inputs: + My input: + $`; // Incorrect indent + + const { contents, position } = parseTemplate(template); + + const completions = await getCompletions(contents, position); + + expect(completions?.items).toHaveLength(0); + }); });