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

Update inline suggestion trigger condition (#890) #892

Merged
merged 1 commit into from
May 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/features/lightspeed/inlineSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,18 @@ export class LightSpeedInlineSuggestionProvider
}
const lineToExtractPrompt = document.lineAt(position.line - 1);
const taskMatchedPattern = lineToExtractPrompt.text.match(TASK_REGEX_EP);

const currentLineText = document.lineAt(position);

if (!taskMatchedPattern || !currentLineText.isEmptyOrWhitespace) {
const spacesBeforeTaskNameStart =
lineToExtractPrompt?.text.match(/^ +/)?.[0].length || 0;
const spacesBeforeCursor =
currentLineText?.text.slice(0, position.character).match(/^ +/)?.[0]
.length || 0;

if (
!taskMatchedPattern ||
!currentLineText.isEmptyOrWhitespace ||
spacesBeforeTaskNameStart !== spacesBeforeCursor
) {
resetInlineSuggestionDisplayed();
return [];
}
Expand Down
97 changes: 95 additions & 2 deletions test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const ANSIBLE_COLLECTIONS_FIXTURES_BASE_PATH = path.resolve(
"common",
"collections"
);
const LIGHTSPEED_INLINE_SUGGESTION_WAIT_TIME = 15000;
const LIGHTSPEED_INLINE_SUGGESTION_WAIT_TIME = 10000;
const LIGHTSPEED_INLINE_SUGGESTION_AFTER_COMMIT_WAIT_TIME = 2000;
/**
* Activates the redhat.ansible extension
Expand Down Expand Up @@ -230,7 +230,7 @@ export async function testInlineSuggestion(
prompt: string,
expectedModule: string
): Promise<void> {
const editor = vscode.window.activeTextEditor;
let editor = vscode.window.activeTextEditor;

if (!editor) {
throw new Error("No active editor found");
Expand All @@ -253,6 +253,21 @@ export async function testInlineSuggestion(
to: "nextBlankLine",
});

editor = vscode.window.activeTextEditor;
if (editor) {
const currentPosition = editor.selection.active;
const newLine = currentPosition.line + 1;
const newColumn = currentPosition.character + 4;

const newPosition = new vscode.Position(newLine, newColumn);

await editor.edit((editBuilder) => {
editBuilder.insert(newPosition, " ");
});

editor.selection = new vscode.Selection(newPosition, newPosition);
editor.revealRange(new vscode.Range(newPosition, newPosition));
}
await vscode.commands.executeCommand(
LightSpeedCommands.LIGHTSPEED_SUGGESTION_TRIGGER
);
Expand Down Expand Up @@ -299,7 +314,18 @@ export async function testInlineSuggestionNotTriggered(
await vscode.commands.executeCommand("cursorMove", {
to: "nextBlankLine",
});
const currentPosition = editor.selection.active;
const newLine = currentPosition.line + 1;
const newColumn = currentPosition.character + 4;

const newPosition = new vscode.Position(newLine, newColumn);

await editor.edit((editBuilder) => {
editBuilder.insert(newPosition, " ");
});

editor.selection = new vscode.Selection(newPosition, newPosition);
editor.revealRange(new vscode.Range(newPosition, newPosition));
await vscode.commands.executeCommand(
LightSpeedCommands.LIGHTSPEED_SUGGESTION_TRIGGER
);
Expand All @@ -325,3 +351,70 @@ export async function testInlineSuggestionNotTriggered(
"getInlineSuggestionItems should not be called"
);
}

export async function testInlineSuggestionCursorPositions(
prompt: string,
newLineSpaces: number
): Promise<void> {
const editor = vscode.window.activeTextEditor;

if (!editor) {
throw new Error("No active editor found");
}
const getInlineSuggestionItemsSpy = sinon.spy(getInlineSuggestionItems);
// this is the position where we have placeholder for the task name in the test fixture
// i.e., <insert task name for ansible lightspeed suggestion here>
const writePosition = new vscode.Position(4, 4);

// replace the placeholder with task name for suggestions
await editor.edit(async (edit) => {
const replaceRange = new vscode.Range(
writePosition,
new vscode.Position(integer.MAX_VALUE, integer.MAX_VALUE)
);
edit.replace(replaceRange, `${prompt}\n`);
});

await vscode.commands.executeCommand("cursorMove", {
to: "nextBlankLine",
});
const newLineText = " ".repeat(newLineSpaces);
const currentPosition = editor.selection.active;
const newLine = currentPosition.line + 1;
const newColumn = currentPosition.character + newLineSpaces;

const newPosition = new vscode.Position(newLine, newColumn);

await editor.edit((editBuilder) => {
editBuilder.insert(newPosition, newLineText);
});

editor.selection = new vscode.Selection(newPosition, newPosition);
editor.revealRange(new vscode.Range(newPosition, newPosition));

await vscode.commands.executeCommand(
LightSpeedCommands.LIGHTSPEED_SUGGESTION_TRIGGER
);

await sleep(LIGHTSPEED_INLINE_SUGGESTION_WAIT_TIME);
await vscode.commands.executeCommand(
LightSpeedCommands.LIGHTSPEED_SUGGESTION_COMMIT
);
await sleep(LIGHTSPEED_INLINE_SUGGESTION_AFTER_COMMIT_WAIT_TIME);

// get the committed suggestion
const suggestionRange = new vscode.Range(
new vscode.Position(writePosition.line + 1, writePosition.character),
new vscode.Position(integer.MAX_VALUE, integer.MAX_VALUE)
);

const docContentAfterSuggestion = doc.getText(suggestionRange).trim();

// assert

assert.include(docContentAfterSuggestion, "");
assert.isFalse(
getInlineSuggestionItemsSpy.called,
"getInlineSuggestionItems should not be called"
);
}
31 changes: 28 additions & 3 deletions test/testScripts/lightspeed/testLightspeed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
disableLightspeedSettings,
canRunLightspeedTests,
testInlineSuggestionNotTriggered,
testInlineSuggestionCursorPositions,
} from "../../helper";

function testSuggestionPrompts() {
Expand Down Expand Up @@ -34,6 +35,20 @@ function testInvalidPrompts() {
return tests;
}

function testInvalidCursorPosition() {
const tests = [
{
taskName: "- name: Print hello world 1",
newLineSpaces: 2,
},
{
taskName: "- name: Print hello world 2",
newLineSpaces: 6,
},
];
return tests;
}

export function testLightspeed(): void {
describe("TEST ANSIBLE LIGHTSPEED", function () {
before(async function () {
Expand Down Expand Up @@ -81,9 +96,19 @@ export function testLightspeed(): void {
await testInlineSuggestionNotTriggered(promptName);
});
});
});
after(async function () {
disableLightspeedSettings();

const invalidCursorPosTest = testInvalidCursorPosition();
invalidCursorPosTest.forEach(({ taskName, newLineSpaces }) => {
it(`Should not give inline suggestion for task prompt '${taskName}' with new line spaces ${newLineSpaces}`, async function () {
await testInlineSuggestionCursorPositions(
taskName,
newLineSpaces as number
);
});
});
after(async function () {
disableLightspeedSettings();
});
});
});
}