diff --git a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts index 6097ff797..f1fb49300 100644 --- a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts +++ b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts @@ -6,12 +6,12 @@ import { stopAtStopTokens } from "./charStream"; import { avoidEmptyComments, avoidPathLine, - noDoubleNewlineAfterClosingBracket, showWhateverWeHaveAtXMs, skipPrefixes, stopAtLines, stopAtRepeatingLines, stopAtSimilarLine, + stopNCharsAfterClosingBracket, streamWithNewLines, } from "./lineStream"; @@ -48,7 +48,7 @@ export class StreamTransformPipeline { ); lineGenerator = avoidPathLine(lineGenerator, helper.lang.singleLineComment); lineGenerator = skipPrefixes(lineGenerator); - lineGenerator = noDoubleNewlineAfterClosingBracket(lineGenerator); + lineGenerator = stopNCharsAfterClosingBracket(lineGenerator); for (const lineFilter of helper.lang.lineFilters ?? []) { lineGenerator = lineFilter({ lines: lineGenerator, fullStop }); diff --git a/core/autocomplete/filtering/streamTransforms/lineStream.ts b/core/autocomplete/filtering/streamTransforms/lineStream.ts index 160fc9d85..e1ca83492 100644 --- a/core/autocomplete/filtering/streamTransforms/lineStream.ts +++ b/core/autocomplete/filtering/streamTransforms/lineStream.ts @@ -547,32 +547,21 @@ export async function* showWhateverWeHaveAtXMs( } } -export async function* noDoubleNewlineAfterClosingBracket( +export async function* stopNCharsAfterClosingBracket( lines: LineStream, + n: number = 20, ): LineStream { const bracketTypeCounts = new Map(); + let charsToStopAt: number | null = null; for await (const line of lines) { - if (line.trim() === "") { - // Double newline detected - // Check if any bracket counts are negative - let hasNegativeCount = false; - for (const count of bracketTypeCounts.values()) { - if (count < 0) { - hasNegativeCount = true; - break; - } - } - if (hasNegativeCount) { - // Stop the generator if we've closed brackets we didn't open - return; - } - } + let outputLine = ""; + let i = 0; - yield line; + while (i < line.length) { + const char = line[i]; - // Update bracket counts - for (const char of line) { + // Update bracket counts if (BRACKETS[char]) { // It's an opening bracket const count = bracketTypeCounts.get(char) || 0; @@ -581,8 +570,32 @@ export async function* noDoubleNewlineAfterClosingBracket( // It's a closing bracket const openingBracket = BRACKETS_REVERSE[char]; const count = bracketTypeCounts.get(openingBracket) || 0; - bracketTypeCounts.set(openingBracket, count - 1); + const newCount = count - 1; + bracketTypeCounts.set(openingBracket, newCount); + + if (newCount < 0 && charsToStopAt === null) { + // Unmatched closing bracket detected + charsToStopAt = n; + } } + + // Add the character to the output line + outputLine += char; + + // If we've started counting down, decrement the remaining characters + if (charsToStopAt !== null) { + charsToStopAt -= 1; + if (charsToStopAt <= 0) { + // Yield the output line up to this point and stop the generator + yield outputLine; + return; + } + } + + i += 1; } + + // Yield whatever we've accumulated for this line + yield outputLine; } } diff --git a/core/autocomplete/filtering/streamTransforms/noDoubleNewlineAfterClosingBracket.test.ts b/core/autocomplete/filtering/streamTransforms/stopNCharsAfterClosingBracket.test.ts similarity index 57% rename from core/autocomplete/filtering/streamTransforms/noDoubleNewlineAfterClosingBracket.test.ts rename to core/autocomplete/filtering/streamTransforms/stopNCharsAfterClosingBracket.test.ts index a25b5d4bf..e5e003f80 100644 --- a/core/autocomplete/filtering/streamTransforms/noDoubleNewlineAfterClosingBracket.test.ts +++ b/core/autocomplete/filtering/streamTransforms/stopNCharsAfterClosingBracket.test.ts @@ -1,4 +1,4 @@ -import { noDoubleNewlineAfterClosingBracket } from "./lineStream"; +import { stopNCharsAfterClosingBracket } from "./lineStream"; describe("noDoubleNewlineAfterClosingBracket", () => { // Helper function to convert an array to an async generator @@ -17,7 +17,7 @@ describe("noDoubleNewlineAfterClosingBracket", () => { return result; } - test("should yield all lines when no double newline and no unmatched brackets", async () => { + test("should yield all lines when no unmatched brackets", async () => { const inputLines = [ "function test() {", ' console.log("Hello World");', @@ -25,50 +25,84 @@ describe("noDoubleNewlineAfterClosingBracket", () => { ]; const lines = arrayToAsyncGenerator(inputLines); const outputLines = await collectLines( - noDoubleNewlineAfterClosingBracket(lines), + stopNCharsAfterClosingBracket(lines), ); expect(outputLines).toEqual(inputLines); }); - test("should yield all lines when double newline but brackets are matched", async () => { + test("should yield all lines when >20 chars after brackets but brackets are matched", async () => { const inputLines = [ "function test() {", ' console.log("Hello World");', "}", - "", - "", - "// End of function", + "// End of function and start of something else", ]; const lines = arrayToAsyncGenerator(inputLines); const outputLines = await collectLines( - noDoubleNewlineAfterClosingBracket(lines), + stopNCharsAfterClosingBracket(lines), ); expect(outputLines).toEqual(inputLines); }); - test("should stop yielding after double newline when unmatched closing bracket is found before double newline", async () => { + test("should stop yielding after 20 chars when unmatched closing bracket is found", async () => { const inputLines = [ // "function test() {", ' console.log("Hello World");', // Unmatched closing bracket "}", - "", - "", - "// This should not be yielded", + "1234567890123456789", 'console.log("End");', ]; - const expectedOutputLines = [' console.log("Hello World");', "}"]; + const expectedOutputLines = [ + ' console.log("Hello World");', + "}", + "1234567890123456789", + ]; const lines = arrayToAsyncGenerator(inputLines); const outputLines = await collectLines( - noDoubleNewlineAfterClosingBracket(lines), + stopNCharsAfterClosingBracket(lines), ); expect(outputLines).toEqual(expectedOutputLines); }); - test("should yield all lines when unmatched brackets but no double newline", async () => { + test("should stop yielding after 2 chars when unmatched closing bracket is found", async () => { + const inputLines = [ + // "function test() {", + ' console.log("Hello World");', + // Unmatched closing bracket + "}", + "12", + 'console.log("End");', + ]; + const expectedOutputLines = [' console.log("Hello World");', "}", "1"]; + const lines = arrayToAsyncGenerator(inputLines); + const outputLines = await collectLines( + stopNCharsAfterClosingBracket(lines, 2), + ); + + expect(outputLines).toEqual(expectedOutputLines); + }); + + test("should stop yielding after 4 chars when unmatched closing bracket is found, on same line", async () => { + const inputLines = [ + // "function test() {", + ' console.log("Hello World");', + // Unmatched closing bracket + "}23456", + ]; + const expectedOutputLines = [' console.log("Hello World");', "}234"]; + const lines = arrayToAsyncGenerator(inputLines); + const outputLines = await collectLines( + stopNCharsAfterClosingBracket(lines, 4), + ); + + expect(outputLines).toEqual(expectedOutputLines); + }); + + test("should yield all lines when unmatched brackets but less <20 chars after", async () => { const inputLines = [ "function test() {", ' console.log("Hello World");', @@ -76,36 +110,16 @@ describe("noDoubleNewlineAfterClosingBracket", () => { // Unmatched closing bracket ")", "// Continue code", - 'console.log("End");', ]; const lines = arrayToAsyncGenerator(inputLines); const outputLines = await collectLines( - noDoubleNewlineAfterClosingBracket(lines), + stopNCharsAfterClosingBracket(lines), ); expect(outputLines).toEqual(inputLines); }); - test("should continue yielding if unmatched closing bracket occurs after double newline", async () => { - const inputLines = [ - "function test() {", - ' console.log("Hello World");', - "}", - "", - "", - // Unmatched closing bracket after double newline - ")", - 'console.log("End");', - ]; - const lines = arrayToAsyncGenerator(inputLines); - const outputLines = await collectLines( - noDoubleNewlineAfterClosingBracket(lines), - ); - - expect(outputLines).toEqual(inputLines); - }); - - test("should yield all lines when brackets are matched even with double newline", async () => { + test("should yield all lines when brackets are matched even with many characters", async () => { const inputLines = [ "const arr = [", " 1, 2, 3,", @@ -113,52 +127,15 @@ describe("noDoubleNewlineAfterClosingBracket", () => { "", "", "console.log(arr);", + "console.log(arr);", + "console.log(arr);", + "console.log(arr);", + "console.log(arr);", + "console.log(arr);", ]; const lines = arrayToAsyncGenerator(inputLines); const outputLines = await collectLines( - noDoubleNewlineAfterClosingBracket(lines), - ); - - expect(outputLines).toEqual(inputLines); - }); - - test("should stop yielding when unmatched closing bracket occurs before double newline", async () => { - const inputLines = [ - "if (condition) {", - " doSomething();", - "}", - // Unmatched closing bracket - "}", - "", - "", - "doSomethingElse();", - ]; - const expectedOutputLines = [ - "if (condition) {", - " doSomething();", - "}", - "}", - ]; - const lines = arrayToAsyncGenerator(inputLines); - const outputLines = await collectLines( - noDoubleNewlineAfterClosingBracket(lines), - ); - - expect(outputLines).toEqual(expectedOutputLines); - }); - - test("should yield all lines when no unmatched closing brackets and double newline", async () => { - const inputLines = [ - "while (true) {", - " // Infinite loop", - "}", - "", - "", - "// Loop exited", - ]; - const lines = arrayToAsyncGenerator(inputLines); - const outputLines = await collectLines( - noDoubleNewlineAfterClosingBracket(lines), + stopNCharsAfterClosingBracket(lines), ); expect(outputLines).toEqual(inputLines);