continue-custom/core/autocomplete/classification/shouldCompleteMultiline.ts
2024-10-31 16:26:51 -07:00

57 lines
1.3 KiB
TypeScript

import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo";
import { HelperVars } from "../util/HelperVars";
function isMidlineCompletion(prefix: string, suffix: string): boolean {
return !suffix.startsWith("\n");
}
function shouldCompleteMultilineBasedOnLanguage(
language: AutocompleteLanguageInfo,
prefix: string,
suffix: string,
) {
let langMultilineDecision = language.useMultiline?.({ prefix, suffix });
return langMultilineDecision;
}
export function shouldCompleteMultiline(
helper: HelperVars,
prefix: string,
suffix: string,
) {
switch (helper.options.multilineCompletions) {
case "always":
return true;
case "never":
return false;
default:
break;
}
// Always single-line if an intellisense option is selected
if (helper.input.selectedCompletionInfo) {
return true;
}
// Don't complete multi-line if you are mid-line
if (isMidlineCompletion(helper.fullPrefix, helper.fullSuffix)) {
return false;
}
// Don't complete multi-line for single-line comments
if (
helper.fullPrefix
.split("\n")
.slice(-1)[0]
?.trimStart()
.startsWith(helper.lang.singleLineComment)
) {
return false;
}
if (shouldCompleteMultilineBasedOnLanguage(helper.lang, prefix, suffix)) {
return true;
}
return false;
}