Skip to content

Commit

Permalink
Merge pull request #78 from davelopez/fix_some_completion_edge_cases
Browse files Browse the repository at this point in the history
Fix some completion edge cases in Format2
  • Loading branch information
davelopez authored Jun 22, 2024
2 parents e2b0394 + cdd279b commit e630dab
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
6 changes: 5 additions & 1 deletion server/gx-workflow-ls-format2/src/schema/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 Expand Up @@ -53,6 +59,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) => {
Expand Down
26 changes: 26 additions & 0 deletions server/gx-workflow-ls-format2/tests/integration/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,30 @@ 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);
});

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);
});
});
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 e630dab

Please sign in to comment.