refactor multi-line decision
This commit is contained in:
parent
40dfede087
commit
3e1eca6c88
5 changed files with 35 additions and 77 deletions
|
|
@ -173,12 +173,11 @@ export class CompletionProvider {
|
|||
// or they might separately track recently edited ranges)
|
||||
const extraSnippets = await this._getExtraSnippets(helper);
|
||||
|
||||
let { prefix, suffix, completeMultiline, snippets } =
|
||||
await constructAutocompletePrompt(
|
||||
helper,
|
||||
extraSnippets,
|
||||
this.contextRetrievalService,
|
||||
);
|
||||
let { prefix, suffix, snippets } = await constructAutocompletePrompt(
|
||||
helper,
|
||||
extraSnippets,
|
||||
this.contextRetrievalService,
|
||||
);
|
||||
|
||||
// If prefix is manually passed
|
||||
if (helper.input.manuallyPassPrefix) {
|
||||
|
|
@ -191,7 +190,6 @@ export class CompletionProvider {
|
|||
suffix,
|
||||
snippets,
|
||||
await this.ide.getWorkspaceDirs(),
|
||||
completeMultiline,
|
||||
helper,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,33 @@
|
|||
import { TabAutocompleteOptions } from "@continuedev/config-types";
|
||||
import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo";
|
||||
import { HelperVars } from "../HelperVars";
|
||||
import { AutocompleteInput } from "../types";
|
||||
|
||||
const BLOCK_TYPES = ["body", "statement_block"];
|
||||
|
||||
function isMidlineCompletion(prefix: string, suffix: string): boolean {
|
||||
return !suffix.startsWith("\n");
|
||||
}
|
||||
|
||||
export async function shouldCompleteMultiline(
|
||||
function shouldCompleteMultilineBasedOnLanguage(
|
||||
language: AutocompleteLanguageInfo,
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
) {
|
||||
let langMultilineDecision = language.useMultiline?.({ prefix, suffix });
|
||||
return langMultilineDecision;
|
||||
}
|
||||
|
||||
export function decideMultilineEarly(
|
||||
helper: HelperVars,
|
||||
): Promise<boolean> {
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
) {
|
||||
switch (helper.options.multilineCompletions) {
|
||||
case "always":
|
||||
return true;
|
||||
case "never":
|
||||
return false;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Don't complete multi-line if you are mid-line
|
||||
if (isMidlineCompletion(helper.fullPrefix, helper.fullSuffix)) {
|
||||
return false;
|
||||
|
|
@ -28,56 +44,11 @@ export async function shouldCompleteMultiline(
|
|||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function shouldCompleteMultilineBasedOnLanguage(
|
||||
language: AutocompleteLanguageInfo,
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
) {
|
||||
let langMultilineDecision = language.useMultiline?.({ prefix, suffix });
|
||||
return langMultilineDecision;
|
||||
}
|
||||
|
||||
function shouldCompleteMultilineBasedOnSelectedCompletion(
|
||||
selectedCompletionInfo: AutocompleteInput["selectedCompletionInfo"],
|
||||
multilineCompletions: TabAutocompleteOptions["multilineCompletions"],
|
||||
completeMultiline: boolean,
|
||||
) {
|
||||
return (
|
||||
!selectedCompletionInfo && // Only ever single-line if using intellisense selected value
|
||||
multilineCompletions !== "never" &&
|
||||
(multilineCompletions === "always" || completeMultiline)
|
||||
);
|
||||
}
|
||||
|
||||
export function decideMultilineEarly({
|
||||
language,
|
||||
prefix,
|
||||
suffix,
|
||||
selectedCompletionInfo,
|
||||
multilineCompletions,
|
||||
completeMultiline,
|
||||
}: {
|
||||
language: AutocompleteLanguageInfo;
|
||||
prefix: string;
|
||||
suffix: string;
|
||||
selectedCompletionInfo: AutocompleteInput["selectedCompletionInfo"];
|
||||
multilineCompletions: TabAutocompleteOptions["multilineCompletions"];
|
||||
completeMultiline: boolean;
|
||||
}) {
|
||||
if (shouldCompleteMultilineBasedOnLanguage(language, prefix, suffix)) {
|
||||
if (shouldCompleteMultilineBasedOnLanguage(helper.lang, prefix, suffix)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
shouldCompleteMultilineBasedOnSelectedCompletion(
|
||||
selectedCompletionInfo,
|
||||
multilineCompletions,
|
||||
completeMultiline,
|
||||
)
|
||||
) {
|
||||
if (helper.input.selectedCompletionInfo) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import {
|
|||
pruneLinesFromBottom,
|
||||
pruneLinesFromTop,
|
||||
} from "../llm/countTokens.js";
|
||||
import { shouldCompleteMultiline } from "./classification/shouldCompleteMultiline.js";
|
||||
import { ContextRetrievalService } from "./context/ContextRetrievalService.js";
|
||||
import {
|
||||
fillPromptWithSnippets,
|
||||
|
|
@ -84,8 +83,6 @@ export async function constructAutocompletePrompt(
|
|||
): Promise<{
|
||||
prefix: string;
|
||||
suffix: string;
|
||||
useFim: boolean;
|
||||
completeMultiline: boolean;
|
||||
snippets: AutocompleteSnippet[];
|
||||
}> {
|
||||
// Prune prefix/suffix based on token budgets
|
||||
|
|
@ -104,13 +101,14 @@ export async function constructAutocompletePrompt(
|
|||
|
||||
const scoredSnippets = rankAndOrderSnippets(snippets, helper);
|
||||
|
||||
// This might be redundant with filterSnippetsAlreadyInCaretWindow
|
||||
let finalSnippets = removeRangeFromSnippets(
|
||||
scoredSnippets,
|
||||
helper.filepath.split("://").slice(-1)[0],
|
||||
getRangeOfPrefixAndSuffixWithBuffer(prunedPrefix, prunedSuffix, helper.pos),
|
||||
);
|
||||
|
||||
// Filter snippets for those with best scores (must be above threshold)
|
||||
// Filter snippets below similarity threshold
|
||||
finalSnippets = finalSnippets.filter(
|
||||
(snippet) =>
|
||||
snippet.score >= helper.options.recentlyEditedSimilarityThreshold,
|
||||
|
|
@ -127,8 +125,6 @@ export async function constructAutocompletePrompt(
|
|||
return {
|
||||
prefix: prunedPrefix,
|
||||
suffix: prunedSuffix,
|
||||
useFim: true,
|
||||
completeMultiline: await shouldCompleteMultiline(helper),
|
||||
snippets,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -130,7 +130,8 @@ function mergeOverlappingRangeContents(
|
|||
}
|
||||
|
||||
/**
|
||||
* Fill the allowed space with snippets
|
||||
* Fill the allowed space with snippets.
|
||||
* It is assumed that the snippets are sorted by score.
|
||||
*/
|
||||
export function fillPromptWithSnippets(
|
||||
snippets: Required<AutocompleteSnippet>[],
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { CompletionOptions } from "../..";
|
||||
import { getBasename, getLastNPathParts } from "../../util";
|
||||
import { decideMultilineEarly } from "../classification/shouldCompleteMultiline";
|
||||
import { decideMultilineEarly as shouldCompleteMultiline } from "../classification/shouldCompleteMultiline";
|
||||
import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo";
|
||||
import { AutocompleteSnippet } from "../context/ranking";
|
||||
import { HelperVars } from "../HelperVars";
|
||||
|
|
@ -65,7 +65,6 @@ export function renderPrompt(
|
|||
suffix: string,
|
||||
snippets: AutocompleteSnippet[],
|
||||
workspaceDirs: string[],
|
||||
completeMultiline: boolean,
|
||||
helper: HelperVars,
|
||||
): [string, Partial<CompletionOptions> | undefined, boolean] {
|
||||
let {
|
||||
|
|
@ -115,14 +114,7 @@ export function renderPrompt(
|
|||
|
||||
const multiline =
|
||||
!helper.options.transform ||
|
||||
decideMultilineEarly({
|
||||
multilineCompletions: helper.options.multilineCompletions,
|
||||
language: helper.lang,
|
||||
selectedCompletionInfo: helper.input.selectedCompletionInfo,
|
||||
prefix,
|
||||
suffix,
|
||||
completeMultiline,
|
||||
});
|
||||
shouldCompleteMultiline(helper, prefix, suffix);
|
||||
|
||||
const stopTokens = getStopTokens(
|
||||
completionOptions,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue