This commit is contained in:
Dana 2025-03-05 17:36:47 +01:00 committed by GitHub
commit 32bc235e64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 7 deletions

View file

@ -1,5 +1,6 @@
import { escapeMappingString } from '@/utils/mappingUtils';
import {
type CompletionContext,
insertCompletionText,
pickedCompletion,
type Completion,
@ -16,16 +17,21 @@ import { blockCommentSnippet, snippets } from './snippets';
const START_CHARACTERS = ['"', "'", '(', '.', '@'];
const START_CHARACTERS_REGEX = /[\.\(\'\"\@]/;
export const typescriptCompletionSource: CompletionSource = async (context) => {
const { worker } = context.state.facet(typescriptWorkerFacet);
export const matchText = (context: CompletionContext) => {
let word = context.matchBefore(START_CHARACTERS_REGEX);
if (!word?.text) {
word = context.matchBefore(/[\"\'].*/);
}
if (!word?.text) {
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(/\/\*?\*?/);
if (blockComment) {
@ -46,7 +52,14 @@ export const typescriptCompletionSource: CompletionSource = async (context) => {
if (isGlobal) {
options = options
.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 [
opt,
...autocompletableNodeNames().map((name) => ({

View file

@ -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);
});
});
});