From ea96ece3462bfcdf228927c7629b5933bea8eacb Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Oct 2024 00:23:10 -0700 Subject: [PATCH] restore --- core/autocomplete/completionProvider.ts | 15 +-- .../services/BracketMatchingService.ts | 96 +++++++++++++++++++ core/diff/util.ts | 8 +- 3 files changed, 109 insertions(+), 10 deletions(-) diff --git a/core/autocomplete/completionProvider.ts b/core/autocomplete/completionProvider.ts index 30d706c56..a6fb941a2 100644 --- a/core/autocomplete/completionProvider.ts +++ b/core/autocomplete/completionProvider.ts @@ -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); diff --git a/core/autocomplete/services/BracketMatchingService.ts b/core/autocomplete/services/BracketMatchingService.ts index 4d9dcd16b..d4415259e 100644 --- a/core/autocomplete/services/BracketMatchingService.ts +++ b/core/autocomplete/services/BracketMatchingService.ts @@ -39,4 +39,100 @@ export class BracketMatchingService { this.openingBracketsFromLastCompletion = stack; this.lastCompletionFile = filepath; } + + async *stopOnUnmatchedClosingBracket( + stream: AsyncGenerator, + prefix: string, + suffix: string, + filepath: string, + multiline: boolean, // Whether this is a multiline completion or not + ): AsyncGenerator { + 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; + } + } } diff --git a/core/diff/util.ts b/core/diff/util.ts index 1758bb704..9dda0ff02 100644 --- a/core/diff/util.ts +++ b/core/diff/util.ts @@ -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;