Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/dependencies-58f9c51f34
Browse files Browse the repository at this point in the history
  • Loading branch information
cidrblock authored Aug 8, 2024
2 parents b1ad58e + 21c587e commit 1b24418
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/features/lightspeed/inlineSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ async function getInlineSuggestionState(
shouldTriggerMultiTaskSuggestion(
documentInfo.documentContent,
suggestionMatchInfo.spacesBeforePromptStart,
inlinePosition.position.line,
documentInfo.ansibleFileType,
) ||
shouldRequestInlineSuggestions(
Expand Down
64 changes: 48 additions & 16 deletions src/features/lightspeed/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export function getRolePathFromPathWithinRole(roleFilePath: string): string {
export function shouldTriggerMultiTaskSuggestion(
documentContent: string,
spacesBeforePromptStart: number,
promptLine: number,
ansibleFileType: IAnsibleFileType,
): boolean {
const documentLines = documentContent.trim().split("\n");
Expand All @@ -295,6 +296,7 @@ export function shouldTriggerMultiTaskSuggestion(
shouldTriggerMultiTaskSuggestionForTaskFile(
documentLines,
spacesBeforePromptStart,
promptLine,
)
) {
return true;
Expand All @@ -304,20 +306,42 @@ export function shouldTriggerMultiTaskSuggestion(
}
}

const matchLine = (
documentLines: string[],
indentIndex: number,
validSuggestionTriggerIndents: number[],
) => {
const matched = documentLines[indentIndex].match(/^\s*-\s*/);
if (matched) {
const indentLength = Math.max(matched[0].length - 2, 0);
if (validSuggestionTriggerIndents[indentIndex] === -1) {
validSuggestionTriggerIndents[indentIndex] = indentLength;
}
return true;
}
return false;
};

function shouldTriggerMultiTaskSuggestionForTaskFile(
documentLines: string[],
spacesBeforePromptStart: number,
linePromptStart: number,
): boolean {
let firstMatchKeywordIndent = -1;
const validSuggestionTriggerIndents: number[] = [];
let matchKeywordIndex = -1;
for (let lineIndex = 0; lineIndex < documentLines.length; lineIndex++) {
validSuggestionTriggerIndents.push(-1);
}
for (let lineIndex = documentLines.length - 1; lineIndex >= 0; lineIndex--) {
if (matchKeyword(tasksFileKeywords, documentLines[lineIndex])) {
const match = documentLines[lineIndex].match(/^\s*/);
if (firstMatchKeywordIndent === -1) {
firstMatchKeywordIndent = match ? match[0].length : -1;
}
matchKeywordIndex = lineIndex;
// TODO: Calculate num of whitespaces before keyword.
validSuggestionTriggerIndents[lineIndex] = 1;
}
if (matchKeywordIndex !== -1) {
let onlyCommentsAfterKeyword = true;
Expand Down Expand Up @@ -346,12 +370,9 @@ function shouldTriggerMultiTaskSuggestionForTaskFile(
indentIndex < documentLines.length;
indentIndex++
) {
const matched = documentLines[indentIndex].match(/^\s*-\s*/);
if (matched) {
const indentLength = Math.max(matched[0].length - 2, 0);
if (!validSuggestionTriggerIndents.includes(indentLength)) {
validSuggestionTriggerIndents.push(indentLength);
}
if (
matchLine(documentLines, indentIndex, validSuggestionTriggerIndents)
) {
break;
}
}
Expand All @@ -371,25 +392,36 @@ function shouldTriggerMultiTaskSuggestionForTaskFile(
if (commentOnly && !documentLines[lineIndex].trim().startsWith("#")) {
commentOnly = false;
}
const matched = documentLines[lineIndex].match(/^\s*-\s*/);
if (matched) {
const indentLength = Math.max(matched[0].length - 2, 0);
if (!validSuggestionTriggerIndents.includes(indentLength)) {
validSuggestionTriggerIndents.push(indentLength);
}
if (matchLine(documentLines, lineIndex, validSuggestionTriggerIndents)) {
break;
}
}
if (commentOnly) {
return true;
}
if (validSuggestionTriggerIndents.length > 0) {
if (!validSuggestionTriggerIndents.includes(spacesBeforePromptStart)) {
return false;
} else {

// Check the inline position column is the same as the previous line one
const linePrompt = linePromptStart - 1;
const previousLinePrompt = linePrompt - 1;
const isValidPromptLine = previousLinePrompt > -1;
if (
isValidPromptLine &&
validSuggestionTriggerIndents[previousLinePrompt] ===
spacesBeforePromptStart
) {
if (previousLinePrompt === 0) {
return true;
}
for (let i = previousLinePrompt - 1; i > -1; i--) {
const indent = validSuggestionTriggerIndents[i];
if (indent > -1) {
if (spacesBeforePromptStart >= indent) {
return true;
}
}
}
}

if (
firstMatchKeywordIndent === -1 ||
spacesBeforePromptStart <= firstMatchKeywordIndent
Expand Down
89 changes: 89 additions & 0 deletions test/testScripts/lightspeed/e2eInlineSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { FeedbackRequestParams } from "../../../src/interfaces/lightspeed";

import { activate, getDocUri, sleep } from "../../helper";
import { integer } from "vscode-languageclient";
import { shouldTriggerMultiTaskSuggestion } from "../../../src/features/lightspeed/utils/data";

const INSERT_TEXT = "**** I'm not a pilot ****";

Expand Down Expand Up @@ -263,6 +264,94 @@ export async function testIgnorePendingSuggestion(): Promise<void> {
});
}

export async function testTriggerTaskSuggestion(): Promise<void> {
describe("Test when a inline suggestion should be triggered.", () => {
const taskFileCollection =
"collections:\n" + " - name: Deploy web servers\n";
it("Test taskFileCollection.", async () => {
const taskFileCollectionResult = shouldTriggerMultiTaskSuggestion(
taskFileCollection,
2,
3,
"tasks_in_role",
);
assert(!taskFileCollectionResult);
});

const taskFileCollectionEmpty = "collections:\n";
it("Test taskFileCollectionEmpty.", async () => {
const taskFileCollectionResultEmpty = shouldTriggerMultiTaskSuggestion(
taskFileCollectionEmpty,
0,
1,
"tasks_in_role",
);
assert(!taskFileCollectionResultEmpty);
});

const taskFileTask =
"- name: install redis on Debian based distributions\n";
it("Test taskFileTask.", async () => {
const taskFileTaskResult = shouldTriggerMultiTaskSuggestion(
taskFileTask,
0,
2,
"tasks",
);
assert(taskFileTaskResult);
});

const taskFileTaskAlreadySuggested =
"- name: install redis on Debian based distributions\n" +
" apt:\n" +
" name: redis-server\n" +
" state: present\n" +
" update_cache: true\n" +
" become: true";
it("Test taskFileTaskAlreadySuggested.", async () => {
const taskFileTaskAlreadySuggestedResult =
shouldTriggerMultiTaskSuggestion(
taskFileTaskAlreadySuggested,
0,
7,
"tasks",
);
assert(!taskFileTaskAlreadySuggestedResult);
});

const taskFileBlock =
"block:\n" + " - name: Install httpd and memcached\n";
it("Test taskFileBlock.", async () => {
const taskFileBlockResult = shouldTriggerMultiTaskSuggestion(
taskFileBlock,
2,
3,
"tasks",
);
assert(taskFileBlockResult);
});

const taskFileBlockTwoTasks =
"block:\n" +
" - name: Install httpd and memcached\n" +
" ansible.builtin.yum:\n" +
" name:\n" +
" - httpd\n" +
" - memcached\n" +
" state: present\n" +
" - name: Display something";
it("Test taskFileBlockTwoTasks.", async () => {
const taskFileBlockTwoTasksResult = shouldTriggerMultiTaskSuggestion(
taskFileBlockTwoTasks,
2,
9,
"tasks",
);
assert(taskFileBlockTwoTasksResult);
});
});
}

async function invokeInlineSuggestion(
lineToActivate: integer,
columnToActivate: integer,
Expand Down
5 changes: 5 additions & 0 deletions test/testScripts/lightspeed/testLightspeed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
testInlineSuggestionByAnotherProvider,
testInlineSuggestionProviderCoExistence,
testIgnorePendingSuggestion,
testTriggerTaskSuggestion,
} from "./e2eInlineSuggestion.test";
import {
UserAction,
Expand Down Expand Up @@ -437,6 +438,10 @@ export function testLightspeed(): void {
testLightspeedUser();
});

describe("Test when a inline suggestion should be triggered", () => {
testTriggerTaskSuggestion();
});

describe("Test suggestion event handlers.", function () {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let feedbackRequest: any;
Expand Down

0 comments on commit 1b24418

Please sign in to comment.