mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Merge 4f53beba51
into d2dd1796a8
This commit is contained in:
commit
32bc235e64
|
@ -1,5 +1,6 @@
|
||||||
import { escapeMappingString } from '@/utils/mappingUtils';
|
import { escapeMappingString } from '@/utils/mappingUtils';
|
||||||
import {
|
import {
|
||||||
|
type CompletionContext,
|
||||||
insertCompletionText,
|
insertCompletionText,
|
||||||
pickedCompletion,
|
pickedCompletion,
|
||||||
type Completion,
|
type Completion,
|
||||||
|
@ -16,16 +17,21 @@ import { blockCommentSnippet, snippets } from './snippets';
|
||||||
const START_CHARACTERS = ['"', "'", '(', '.', '@'];
|
const START_CHARACTERS = ['"', "'", '(', '.', '@'];
|
||||||
const START_CHARACTERS_REGEX = /[\.\(\'\"\@]/;
|
const START_CHARACTERS_REGEX = /[\.\(\'\"\@]/;
|
||||||
|
|
||||||
export const typescriptCompletionSource: CompletionSource = async (context) => {
|
export const matchText = (context: CompletionContext) => {
|
||||||
const { worker } = context.state.facet(typescriptWorkerFacet);
|
|
||||||
|
|
||||||
let word = context.matchBefore(START_CHARACTERS_REGEX);
|
let word = context.matchBefore(START_CHARACTERS_REGEX);
|
||||||
if (!word?.text) {
|
|
||||||
word = context.matchBefore(/[\"\'].*/);
|
|
||||||
}
|
|
||||||
if (!word?.text) {
|
if (!word?.text) {
|
||||||
word = context.matchBefore(/[\$\w]+/);
|
word = context.matchBefore(/[\$\w]+/);
|
||||||
}
|
}
|
||||||
|
if (!word?.text) {
|
||||||
|
word = context.matchBefore(/[\"\'].*/);
|
||||||
|
}
|
||||||
|
return word;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const typescriptCompletionSource: CompletionSource = async (context) => {
|
||||||
|
const { worker } = context.state.facet(typescriptWorkerFacet);
|
||||||
|
|
||||||
|
const word = matchText(context);
|
||||||
|
|
||||||
const blockComment = context.matchBefore(/\/\*?\*?/);
|
const blockComment = context.matchBefore(/\/\*?\*?/);
|
||||||
if (blockComment) {
|
if (blockComment) {
|
||||||
|
@ -46,7 +52,14 @@ export const typescriptCompletionSource: CompletionSource = async (context) => {
|
||||||
if (isGlobal) {
|
if (isGlobal) {
|
||||||
options = options
|
options = options
|
||||||
.flatMap((opt) => {
|
.flatMap((opt) => {
|
||||||
if (opt.label === '$') {
|
let word = context.matchBefore(START_CHARACTERS_REGEX);
|
||||||
|
if (!word?.text) {
|
||||||
|
word = context.matchBefore(/[\"\'].*/);
|
||||||
|
}
|
||||||
|
if (!word?.text) {
|
||||||
|
word = context.matchBefore(/[\$\w]+/);
|
||||||
|
}
|
||||||
|
if (opt.label === '$()') {
|
||||||
return [
|
return [
|
||||||
opt,
|
opt,
|
||||||
...autocompletableNodeNames().map((name) => ({
|
...autocompletableNodeNames().map((name) => ({
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
import { matchText } from '../completions';
|
||||||
|
import { CompletionContext } from '@codemirror/autocomplete';
|
||||||
|
import { EditorState } from '@codemirror/state';
|
||||||
|
import { n8nLang } from '@/plugins/codemirror/n8nLang';
|
||||||
|
|
||||||
|
describe('matchText', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.resetAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should match on previous nodes', () => {
|
||||||
|
const previousNodes = [
|
||||||
|
{ doc: '$(|)', expected: '(' },
|
||||||
|
{ doc: '$("|', expected: '"' },
|
||||||
|
{ doc: "$('|", expected: "'" },
|
||||||
|
{ doc: '$(""|', expected: '"' },
|
||||||
|
{ doc: "$('|", expected: "'" },
|
||||||
|
{ doc: '$("|")', expected: '"' },
|
||||||
|
{ doc: "$('|')", expected: "'" },
|
||||||
|
{ doc: '$("|)', expected: '"' },
|
||||||
|
{ doc: '$("No|")', expected: 'No' },
|
||||||
|
{ doc: "$('No|')", expected: 'No' },
|
||||||
|
{ doc: '$("N|")', expected: 'N' },
|
||||||
|
{ doc: "$('N|')", expected: 'N' },
|
||||||
|
];
|
||||||
|
|
||||||
|
previousNodes.forEach((node) => {
|
||||||
|
const cursorPosition = node.doc.indexOf('|');
|
||||||
|
const state = EditorState.create({
|
||||||
|
doc: node.doc.replace('|', ''),
|
||||||
|
selection: { anchor: cursorPosition },
|
||||||
|
extensions: [n8nLang()],
|
||||||
|
});
|
||||||
|
const context = new CompletionContext(state, cursorPosition, false);
|
||||||
|
|
||||||
|
expect(matchText(context)?.text).toEqual(node.expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue