This commit is contained in:
Nate 2024-10-30 00:23:10 -07:00
parent 15bd3dd22b
commit ea96ece346
3 changed files with 109 additions and 10 deletions

View file

@ -36,7 +36,6 @@ import { RecentlyEditedRange } from "./recentlyEdited.js";
import { RootPathContextService } from "./services/RootPathContextService.js";
import {
avoidPathLineAndEmptyComments,
noTopLevelKeywordsMidline,
skipPrefixes,
stopAtLines,
stopAtRepeatingLines,
@ -50,11 +49,7 @@ import Handlebars from "handlebars";
import { getConfigJsonPath } from "../util/paths.js";
import { BracketMatchingService } from "./services/BracketMatchingService.js";
import { ImportDefinitionsService } from "./services/ImportDefinitionsService.js";
import {
noFirstCharNewline,
onlyWhitespaceAfterEndOfLine,
stopAtStopTokens,
} from "./streamTransforms/charStream.js";
import { stopAtStopTokens } from "./streamTransforms/charStream.js";
export interface AutocompleteInput {
completionId: string;
@ -733,6 +728,14 @@ export class CompletionProvider {
// fullStop,
// );
charGenerator = stopAtStopTokens(charGenerator, stop);
charGenerator =
this.bracketMatchingService.stopOnUnmatchedClosingBracket(
charGenerator,
prefix,
suffix,
filepath,
multiline,
);
}
let lineGenerator = streamLines(charGenerator);

View file

@ -39,4 +39,100 @@ export class BracketMatchingService {
this.openingBracketsFromLastCompletion = stack;
this.lastCompletionFile = filepath;
}
async *stopOnUnmatchedClosingBracket(
stream: AsyncGenerator<string>,
prefix: string,
suffix: string,
filepath: string,
multiline: boolean, // Whether this is a multiline completion or not
): AsyncGenerator<string> {
let stack: string[] = [];
if (multiline) {
// Add opening brackets from the previous response
if (this.lastCompletionFile === filepath) {
stack = [...this.openingBracketsFromLastCompletion];
} else {
this.lastCompletionFile = undefined;
}
} else {
// If single line completion, then allow completing bracket pairs that are
// started on the current line but not finished on the current line
if (!multiline) {
const currentLine =
(prefix.split("\n").pop() ?? "") + (suffix.split("\n")[0] ?? "");
for (let i = 0; i < currentLine.length; i++) {
const char = currentLine[i];
if (Object.keys(BracketMatchingService.BRACKETS).includes(char)) {
// It's an opening bracket
stack.push(char);
} else if (
Object.values(BracketMatchingService.BRACKETS).includes(char)
) {
// It's a closing bracket
if (
stack.length === 0 ||
BracketMatchingService.BRACKETS[stack.pop()!] !== char
) {
break;
}
}
}
}
}
// Add corresponding open brackets from suffix to stack
// because we overwrite them and the diff is displayed, and this allows something to be edited after that
for (let i = 0; i < suffix.length; i++) {
if (suffix[i] === " ") {
continue;
}
const openBracket = BracketMatchingService.BRACKETS_REVERSE[suffix[i]];
if (!openBracket) {
break;
}
stack.unshift(openBracket);
}
let all = "";
let seenNonWhitespaceOrClosingBracket = false;
for await (let chunk of stream) {
// Allow closing brackets before any non-whitespace characters
if (!seenNonWhitespaceOrClosingBracket) {
const firstNonWhitespaceOrClosingBracketIndex =
chunk.search(/[^\s\)\}\]]/);
if (firstNonWhitespaceOrClosingBracketIndex !== -1) {
yield chunk.slice(0, firstNonWhitespaceOrClosingBracketIndex);
chunk = chunk.slice(firstNonWhitespaceOrClosingBracketIndex);
seenNonWhitespaceOrClosingBracket = true;
} else {
yield chunk;
continue;
}
}
all += chunk;
const allLines = all.split("\n");
for (let i = 0; i < chunk.length; i++) {
const char = chunk[i];
if (Object.values(BracketMatchingService.BRACKETS).includes(char)) {
// It's a closing bracket
if (
stack.length === 0 ||
BracketMatchingService.BRACKETS[stack.pop()!] !== char
) {
// If the stack is empty or the top of the stack doesn't match the current closing bracket
yield chunk.slice(0, i);
return; // Stop the generator if the closing bracket doesn't have a matching opening bracket in the stream
}
} else if (
Object.keys(BracketMatchingService.BRACKETS).includes(char)
) {
// It's an opening bracket
stack.push(char);
}
}
yield chunk;
}
}
}

View file

@ -111,10 +111,10 @@ export async function* streamLines(
allLines.push(line);
}
if (buffer === "" && chunk.endsWith("\n")) {
yield "";
allLines.push("");
}
// if (buffer === "" && chunk.endsWith("\n")) {
// yield "";
// allLines.push("");
// }
}
if (buffer.length > 0) {
yield buffer;