Skip to content

Commit

Permalink
wip(feat): partial attribute completion
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Mar 15, 2024
1 parent 12da224 commit 41ed2e3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/service/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,24 @@ export function getCompletions(doc: DocumentLike, sourceFile: SourceFile, positi
}
}

if (node.kind === SyntaxKind.TextIdentifier && parent?.kind === SyntaxKind.NodeId) {
const exclusions = node.symbol
? [node.symbol.name]
: undefined;
return getNodeCompletions(symbols, exclusions);
}
const prevNodeNi = findNodeAtOffset(g, node.pos - 1, false);

if (node.kind === SyntaxKind.AttributeContainer
// FIXME: conflitcs with #issue 17 9 (last test of nodeCompletion.spec.ts)
// || (node.kind == SyntaxKind.TextIdentifier && prevNodeNi?.kind === SyntaxKind.AttributeContainer)
|| (node.kind == SyntaxKind.TextIdentifier && prevNodeNi?.kind === SyntaxKind.CommaToken)
|| (node.kind == SyntaxKind.CommaToken && parent?.kind === SyntaxKind.Assignment)
) {
return getAttributeCompletions(position);
}

if (node.kind === SyntaxKind.TextIdentifier && parent?.kind === SyntaxKind.NodeId) {
const exclusions = node.symbol
? [node.symbol.name]
: undefined;
return getNodeCompletions(symbols, exclusions);
}

const prevNode = findNodeAtOffset(g, node.pos - 1, true);
if (!prevNode)
return [];
Expand Down
41 changes: 41 additions & 0 deletions tests/completion/attributeCompletion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,47 @@ describe("Attribute completion", () => {
expect(completions).toHaveLength(attributes.length);
});

// FIXME: currently failing
test("should provide completion for attributes (partial input)", () => {
const content = `graph {
node_name_a -- node_name_b [no];
}`;
const requestOffset = invokeIndex(content)("[no");

const [doc, sf] = ensureDocAndSourceFile(content);

const completions = getCompletions(doc, sf, doc.positionAt(requestOffset));

expect(completions).toBeDefined();
assertExists(completions);

expect(completions.length).toBeGreaterThan(0);
expect(completions.map(getLabel)).not.toContain("node_name_a");
expect(completions.map(getLabel)).not.toContain("node_name_b");
expect(completions.map(getLabel)).toEqual(attributes);
expect(completions).toHaveLength(attributes.length);
});

test("should provide completion for attributes (partial input, preceding item)", () => {
const content = `graph {
node_name_a -- node_name_b [color=blue, no];
}`;
const requestOffset = invokeIndex(content)(", no");

const [doc, sf] = ensureDocAndSourceFile(content);

const completions = getCompletions(doc, sf, doc.positionAt(requestOffset));

expect(completions).toBeDefined();
assertExists(completions);

expect(completions.length).toBeGreaterThan(0);
expect(completions.map(getLabel)).not.toContain("node_name_a");
expect(completions.map(getLabel)).not.toContain("node_name_b");
expect(completions.map(getLabel)).toEqual(attributes);
expect(completions).toHaveLength(attributes.length);
});

test("should provide completion for attributes (preceding item)", () => {
const content = `graph {
node_name_a -- node_name_b [color=blue,];
Expand Down

0 comments on commit 41ed2e3

Please sign in to comment.