mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-12 15:44:06 -08:00
fix(editor): Fix completion on $input.item. in Code node (#10800)
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
This commit is contained in:
parent
b2b1abc531
commit
45dccf3d0c
|
@ -0,0 +1,77 @@
|
||||||
|
import { CompletionContext } from '@codemirror/autocomplete';
|
||||||
|
import { EditorSelection, EditorState } from '@codemirror/state';
|
||||||
|
import { useItemFieldCompletions } from '../itemField.completions';
|
||||||
|
|
||||||
|
describe('inputMethodCompletions', () => {
|
||||||
|
test('should return completions for $input.item.|', () => {
|
||||||
|
const { inputMethodCompletions } = useItemFieldCompletions('javaScript');
|
||||||
|
expect(inputMethodCompletions(createContext('$input.item.|'))).toEqual({
|
||||||
|
from: 0,
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
info: expect.any(Function),
|
||||||
|
label: '$input.item.json',
|
||||||
|
type: 'variable',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
info: expect.any(Function),
|
||||||
|
label: '$input.item.binary',
|
||||||
|
type: 'variable',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return completions for $input.first().|', () => {
|
||||||
|
const { inputMethodCompletions } = useItemFieldCompletions('javaScript');
|
||||||
|
expect(inputMethodCompletions(createContext('$input.first().|'))).toEqual({
|
||||||
|
from: 0,
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
info: expect.any(Function),
|
||||||
|
label: '$input.first().json',
|
||||||
|
type: 'variable',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
info: expect.any(Function),
|
||||||
|
label: '$input.first().binary',
|
||||||
|
type: 'variable',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return completions for $input.all()[1].|', () => {
|
||||||
|
const { inputMethodCompletions } = useItemFieldCompletions('javaScript');
|
||||||
|
expect(inputMethodCompletions(createContext('$input.all()[1].|'))).toEqual({
|
||||||
|
from: 0,
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
info: expect.any(Function),
|
||||||
|
label: '$input.all()[1].json',
|
||||||
|
type: 'variable',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
info: expect.any(Function),
|
||||||
|
label: '$input.all()[1].binary',
|
||||||
|
type: 'variable',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
export function createContext(docWithCursor: string) {
|
||||||
|
const cursorPosition = docWithCursor.indexOf('|');
|
||||||
|
|
||||||
|
const doc = docWithCursor.slice(0, cursorPosition) + docWithCursor.slice(cursorPosition + 1);
|
||||||
|
|
||||||
|
return new CompletionContext(
|
||||||
|
EditorState.create({ doc, selection: EditorSelection.single(cursorPosition) }),
|
||||||
|
cursorPosition,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import { addVarType, escape } from '../utils';
|
import { addInfoRenderer, addVarType, escape } from '../utils';
|
||||||
import type { Completion, CompletionContext, CompletionResult } from '@codemirror/autocomplete';
|
import type { Completion, CompletionContext, CompletionResult } from '@codemirror/autocomplete';
|
||||||
import { useI18n } from '@/composables/useI18n';
|
import { useI18n } from '@/composables/useI18n';
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ export function useItemFieldCompletions(language: 'python' | 'javaScript') {
|
||||||
const patterns = {
|
const patterns = {
|
||||||
first: new RegExp(`\\${prefix}input\\.first\\(\\)\\..*`),
|
first: new RegExp(`\\${prefix}input\\.first\\(\\)\\..*`),
|
||||||
last: new RegExp(`\\${prefix}input\\.last\\(\\)\\..*`),
|
last: new RegExp(`\\${prefix}input\\.last\\(\\)\\..*`),
|
||||||
item: new RegExp(`\\${prefix}item\\.first\\(\\)\\..*`),
|
item: new RegExp(`\\${prefix}input\\.item\\..*`),
|
||||||
all: /\$input\.all\(\)\[(?<index>\w+)\]\..*/,
|
all: /\$input\.all\(\)\[(?<index>\w+)\]\..*/,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ export function useItemFieldCompletions(language: 'python' | 'javaScript') {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from: preCursor.from,
|
from: preCursor.from,
|
||||||
options: options.map(addVarType),
|
options: options.map(addVarType).map(addInfoRenderer),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
{
|
|
||||||
"name": "completions",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"dependencies": {
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue