Skip to content

Commit

Permalink
Make autocompletion less aggressive
Browse files Browse the repository at this point in the history
  • Loading branch information
valtzu committed May 18, 2024
1 parent b8df7f2 commit d429b23
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ function completeMember(state: EditorState, config: ExpressionLanguageConfig, tr
function expressionLanguageCompletionFor(config: ExpressionLanguageConfig, context: CompletionContext): CompletionResult | null {
const { state, pos, explicit } = context;
const tree = syntaxTree(state);
const prevNode = tree.resolveInner(pos, state.sliceDoc(pos - 1, pos) === ')' ? 0 : -1);
const lastChar = state.sliceDoc(pos - 1, pos);
const prevNode = tree.resolveInner(pos, lastChar === ')' ? 0 : -1);

const isIdentifier = (node: SyntaxNode|undefined) => ['Variable', 'Function'].includes(node?.name ?? '');
const isMember = (node: SyntaxNode|undefined) => ['Property', 'Method'].includes(node?.name ?? '');
Expand All @@ -427,9 +428,11 @@ function expressionLanguageCompletionFor(config: ExpressionLanguageConfig, conte
}

if (
['Expression', 'UnaryExpression', 'BinaryExpression', 'TernaryExpression'].includes(prevNode.name) && prevNode.lastChild?.type.isError
|| ['Expression', 'UnaryExpression', 'BinaryExpression', 'TernaryExpression', 'Arguments'].includes(prevNode.parent?.name ?? '') && !prevNode.type.isError
|| ['Arguments', 'Array'].includes(prevNode.name ?? '')
!/[0-9]/.test(lastChar) && !['OperatorKeyword'].includes(prevNode.name ?? '') && (
['Expression', 'UnaryExpression', 'BinaryExpression', 'TernaryExpression'].includes(prevNode.name) && prevNode.lastChild?.type.isError
|| ['Expression', 'UnaryExpression', 'BinaryExpression', 'TernaryExpression', 'Arguments'].includes(prevNode.parent?.name ?? '') && !prevNode.type.isError
|| ['Arguments', 'Array'].includes(prevNode.name ?? '')
)
) {
return completeIdentifier(state, config, prevNode, isIdentifier(prevNode) ? prevNode.from : pos, pos);
}
Expand Down
8 changes: 8 additions & 0 deletions test/test-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,12 @@ describe("Expression language completion", () => {
ist("property22", c[1].label);
ist("firstMethod()", c[2].label);
});

it("does not complete right after numbers", () => {
ist(null, get("123‸"));
});

it("does not complete right after operator keywords", () => {
ist(null, get("1 and‸"));
});
});

0 comments on commit d429b23

Please sign in to comment.