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): Capture indexed access expressions when building completions #8331

Merged
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
Expand Up @@ -197,6 +197,17 @@ describe('Resolution-based completions', () => {
});
});

describe('indexed access completions', () => {
test('should return string completions for indexed access that resolves to string literal: {{ "abc"[0].| }}', () => {
// @ts-expect-error Spied function is mistyped
resolveParameterSpy.mockReturnValueOnce('a');

expect(completions('{{ "abc"[0].| }}')).toHaveLength(
natives('string').length + extensions('string').length,
);
});
});

describe('complex expression completions', () => {
const resolveParameterSpy = vi.spyOn(workflowHelpers, 'resolveParameter');
const { $input } = mockProxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ const regexes = {
doubleQuoteStringLiteral: /(".+")\.([^"{\s])*/, // "abc".
dateLiteral: /\(?new Date\(\(?.*?\)\)?\.([^{\s])*/, // new Date(). or (new Date()).
arrayLiteral: /(\[.+\])\.([^{\s])*/, // [1, 2, 3].
indexedAccess: /([^{\s]+\[.+\])\.([^{\s])*/, // 'abc'[0]. or 'abc'.split('')[0] or similar ones
objectLiteral: /\(\{.*\}\)\.([^{\s])*/, // ({}).

mathGlobal: /Math\.([^{\s])*/, // Math.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ export function longestCommonPrefix(...strings: string[]) {
// suggestions can be matched based on it.
function extractSubExpression(userInput: string): string {
const dollarSignIndex = userInput.indexOf('$');
// If it's not a dollar sign expression just strip parentheses
if (dollarSignIndex === -1) {
userInput = userInput.replace(/^.+(\(|\[|{)/, '');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through the referenced PR, but I cannot quite tell why we needed to strip parens here. Since parens are needed for indexed access expressions and since all tests still pass, it looks safe to remove. Else let me know.

/cc @MiloradFilipovic

return userInput;
} else if (!stringLiteralRegex.test(userInput)) {
// If there is a dollar sign in the input and input is not a string literal,
// extract part of following the last $
Expand Down
Loading