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; + } }