Skip to content

Commit

Permalink
Refactor completionService to handle empty documents and improve sugg…
Browse files Browse the repository at this point in the history
…estions
  • Loading branch information
davelopez committed Jun 22, 2024
1 parent 4f3be1f commit d5b39c2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -111,6 +111,9 @@ export class TextBuffer {
currentLine--;
}
}
if (!found) {
return undefined;
}
return currentLine;
}

Expand All @@ -122,4 +125,8 @@ export class TextBuffer {
}
return tokenIndex < position.character;
}

public isEmpty(): boolean {
return this.doc.getText().trim().length === 0;
}
}

0 comments on commit d5b39c2

Please sign in to comment.