Skip to content

Commit e13a784

Browse files
authored
Fix var capturing regex (#955)
* simplify regex to capture '_var_' inside jinja brackets * update regex to check for valid vars inside jinja inline brackets * add lightspeed e2e tests for asserting correct syntax of variables returned in suggestion * add test fixture
1 parent 0897cf4 commit e13a784

4 files changed

Lines changed: 119 additions & 3 deletions

File tree

src/features/utils/lightspeed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function convertToSnippetString(suggestion: string): string {
3939
// this regex matches the content inside {{ }} with decided vars, i.e., {{ _var_ }}
4040
// TODO: once a prefix is decided for using it in from of variable names, the regex
4141
// can be changed to match it
42-
const regex = /(?:^|)[^@#\s_]*(_([^_]+)_)/gm;
42+
const regex = /{{ (_[a-zA-Z_]\w*_) }}/gm;
4343

4444
let counter = 0;
4545
const convertedSuggestion = suggestion.replace(regex, (item) => {

test/helper.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ export async function testInlineSuggestion(
242242

243243
// this is the position where we have placeholder for the task name in the test fixture
244244
// i.e., <insert task name for ansible lightspeed suggestion here>
245+
// playbook used: lightspeed/playbook_1.yml
245246
const writePosition = new vscode.Position(4, 4);
246247

247248
// replace the placeholder with valid task name for suggestions
@@ -422,3 +423,67 @@ export async function testInlineSuggestionCursorPositions(
422423
"getInlineSuggestionItems should not be called"
423424
);
424425
}
426+
427+
export async function testValidJinjaBrackets(
428+
prompt: string,
429+
expectedValidJinjaInlineVar: string
430+
): Promise<void> {
431+
let editor = vscode.window.activeTextEditor;
432+
433+
if (!editor) {
434+
throw new Error("No active editor found");
435+
}
436+
437+
// this is the position where we have placeholder for the task name in the test fixture
438+
// i.e., <insert task name for ansible lightspeed suggestion here>
439+
// playbook used: lightspeed/playbook_with_vars.yml
440+
const writePosition = new vscode.Position(17, 4);
441+
442+
// replace the placeholder with valid task name for suggestions
443+
await editor.edit(async (edit) => {
444+
const replaceRange = new vscode.Range(
445+
writePosition,
446+
new vscode.Position(integer.MAX_VALUE, integer.MAX_VALUE)
447+
);
448+
edit.replace(replaceRange, `- name: ${prompt}\n`);
449+
});
450+
451+
await vscode.commands.executeCommand("cursorMove", {
452+
to: "nextBlankLine",
453+
});
454+
455+
editor = vscode.window.activeTextEditor;
456+
if (editor) {
457+
const currentPosition = editor.selection.active;
458+
const newLine = currentPosition.line + 1;
459+
const newColumn = currentPosition.character + 4;
460+
461+
const newPosition = new vscode.Position(newLine, newColumn);
462+
463+
await editor.edit((editBuilder) => {
464+
editBuilder.insert(newPosition, " ");
465+
});
466+
467+
editor.selection = new vscode.Selection(newPosition, newPosition);
468+
editor.revealRange(new vscode.Range(newPosition, newPosition));
469+
}
470+
await vscode.commands.executeCommand(
471+
LightSpeedCommands.LIGHTSPEED_SUGGESTION_TRIGGER
472+
);
473+
await sleep(LIGHTSPEED_INLINE_SUGGESTION_WAIT_TIME);
474+
await vscode.commands.executeCommand(
475+
LightSpeedCommands.LIGHTSPEED_SUGGESTION_COMMIT
476+
);
477+
await sleep(LIGHTSPEED_INLINE_SUGGESTION_AFTER_COMMIT_WAIT_TIME);
478+
479+
// get the committed suggestion
480+
const suggestionRange = new vscode.Range(
481+
new vscode.Position(writePosition.line + 1, writePosition.character),
482+
new vscode.Position(integer.MAX_VALUE, integer.MAX_VALUE)
483+
);
484+
485+
const docContentAfterSuggestion = doc.getText(suggestionRange).trim();
486+
487+
// assert
488+
assert.include(docContentAfterSuggestion, expectedValidJinjaInlineVar);
489+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
- name: Deploy podman application
2+
hosts: app_servers
3+
become: true
4+
vars:
5+
foo_app:
6+
env:
7+
FOO_TEAM_SETTINGS_SITE_NAME: FOO NAME
8+
name: foo-preview
9+
image: docker.io/foo/foo-preview
10+
state: started
11+
generate_systemd:
12+
path: /etc/systemd/system/
13+
container_prefix: app
14+
restart_policy: always
15+
ports:
16+
- 8065:8065
17+
tasks:
18+
- <insert task name for project ansible lightspeed suggestion here>

test/testScripts/lightspeed/testLightspeed.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
canRunLightspeedTests,
99
testInlineSuggestionNotTriggered,
1010
testInlineSuggestionCursorPositions,
11+
testValidJinjaBrackets,
1112
} from "../../helper";
1213

1314
function testSuggestionPrompts() {
@@ -51,6 +52,17 @@ function testInvalidCursorPosition() {
5152
return tests;
5253
}
5354

55+
function testSuggestionWithValidJinjaBrackets() {
56+
const tests = [
57+
{
58+
taskName: "Run container with podman using foo_app var",
59+
expectedValidJinjaInlineVar: "{{ foo_app.image }}",
60+
},
61+
];
62+
63+
return tests;
64+
}
65+
5466
export function testLightspeed(): void {
5567
describe("TEST ANSIBLE LIGHTSPEED", function () {
5668
before(async function () {
@@ -108,9 +120,30 @@ export function testLightspeed(): void {
108120
);
109121
});
110122
});
111-
after(async function () {
112-
disableLightspeedSettings();
123+
});
124+
125+
describe("Test Ansible Lightspeed inline completion suggestions", function () {
126+
const docUri1 = getDocUri("lightspeed/playbook_with_vars.yml");
127+
128+
before(async function () {
129+
await vscode.commands.executeCommand(
130+
"workbench.action.closeAllEditors"
131+
);
132+
await activate(docUri1);
113133
});
134+
135+
const tests = testSuggestionWithValidJinjaBrackets();
136+
137+
tests.forEach(({ taskName, expectedValidJinjaInlineVar }) => {
138+
it(`Should provide suggestion with valid jinja brackets for task prompt '${taskName}'`, async function () {
139+
// await testInlineSuggestion(taskName, expectedValidJinjaInlineVar);
140+
await testValidJinjaBrackets(taskName, expectedValidJinjaInlineVar);
141+
});
142+
});
143+
});
144+
145+
after(async function () {
146+
disableLightspeedSettings();
114147
});
115148
});
116149
}

0 commit comments

Comments
 (0)