Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): Open autocompletion when starting an expression #13249

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { EditorView } from '@codemirror/view';
import userEvent from '@testing-library/user-event';
import { expressionCloseBrackets } from './expressionCloseBrackets';
import { n8nAutocompletion, n8nLang } from './n8nLang';
import { completionStatus } from '@codemirror/autocomplete';
import { EditorSelection } from '@codemirror/state';
import { setActivePinia } from 'pinia';
import { createTestingPinia } from '@pinia/testing';

describe('expressionCloseBrackets', () => {
const createEditor = () => {
const parent = document.createElement('div');
document.body.appendChild(parent);
const editor = new EditorView({
parent,
extensions: [expressionCloseBrackets(), n8nLang(), n8nAutocompletion()],
});
return editor;
};

beforeEach(() => {
setActivePinia(createTestingPinia());
});

it('should complete {{| to {{ | }} and open autocomplete', async () => {
const editor = createEditor();
// '{' is an escape character: '{{' === '{'
await userEvent.type(editor.contentDOM, '{{{{');
expect(editor.state.doc.toString()).toEqual('{{ }}');
expect(editor.state.selection).toEqual(EditorSelection.single(3));
expect(completionStatus(editor.state)).not.toBeNull();
});

it('should type over auto-closed brackets', async () => {
const editor = createEditor();
await userEvent.type(editor.contentDOM, 'foo()');
// no extra closing bracket foo())
expect(editor.state.doc.toString()).toEqual('foo()');
});

it.each([
{ char: '"', expected: '""' },
{ char: "'", expected: "''" },
{ char: '(', expected: '()' },
{ char: '{{}', expected: '{}' },
{ char: '{[}', expected: '[]' },
])('should auto-close $expected', async ({ expected, char }) => {
const editor = createEditor();
await userEvent.type(editor.contentDOM, char);
expect(editor.state.doc.toString()).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
closeBrackets,
closeBracketsKeymap,
startCompletion,
type CloseBracketConfig,
} from '@codemirror/autocomplete';
import { EditorSelection, Text } from '@codemirror/state';
Expand All @@ -21,6 +22,7 @@ const expressionBracketSpacing = EditorView.updateListener.of((update) => {
changes: [{ from: fromB + 1, insert: ' ' }],
selection: EditorSelection.cursor(toB),
});
startCompletion(update.view);
}
});
});
Expand Down
Loading