From fe1af0156fc9db0640ef2d460ba5da289192aeb0 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Mon, 4 Nov 2024 17:58:59 +0700 Subject: [PATCH] improv: single responsibility in filter functions --- .../StreamTransformPipeline.ts | 6 ++-- .../streamTransforms/lineStream.test.ts | 27 ++++++++++++----- .../filtering/streamTransforms/lineStream.ts | 29 +++++++++++++++---- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts index 42ab89829..543c9b7b8 100644 --- a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts +++ b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts @@ -3,7 +3,8 @@ import { HelperVars } from "../../util/HelperVars"; import { BracketMatchingService } from "../BracketMatchingService"; import { stopAtStopTokens } from "./charStream"; import { - avoidPathLineAndEmptyComments, + avoidEmptyComments, + avoidPathLine, showWhateverWeHaveAtXMs, skipPrefixes, stopAtLines, @@ -59,10 +60,11 @@ export class StreamTransformPipeline { lineGenerator = stopAtLines(lineGenerator, fullStop); lineGenerator = stopAtRepeatingLines(lineGenerator, fullStop); - lineGenerator = avoidPathLineAndEmptyComments( + lineGenerator = avoidEmptyComments( lineGenerator, helper.lang.singleLineComment, ); + lineGenerator = avoidPathLine(lineGenerator, helper.lang.singleLineComment); lineGenerator = skipPrefixes(lineGenerator); // lineGenerator = noTopLevelKeywordsMidline( diff --git a/core/autocomplete/filtering/streamTransforms/lineStream.test.ts b/core/autocomplete/filtering/streamTransforms/lineStream.test.ts index df8cd1263..810ab9eec 100644 --- a/core/autocomplete/filtering/streamTransforms/lineStream.test.ts +++ b/core/autocomplete/filtering/streamTransforms/lineStream.test.ts @@ -31,21 +31,34 @@ describe("lineStream", () => { }); describe("avoidPathLine", () => { - it("should filter out path lines and empty comments", async () => { + it("should filter out path lines", async () => { const linesGenerator = await getLineGenerator([ "// Path: src/index.ts", "const x = 5;", "//", "console.log(x);", ]); - - const result = lineStream.avoidPathLineAndEmptyComments( - linesGenerator, - "//", - ); + const result = lineStream.avoidPathLine(linesGenerator, "//"); const filteredLines = await getFilteredLines(result); + expect(filteredLines).toEqual(["const x = 5;", "//", "console.log(x);"]); + }); + }); - expect(filteredLines).toEqual(["const x = 5;", "console.log(x);"]); + describe("avoidEmptyComments", () => { + it("should filter out empty comments", async () => { + const linesGenerator = await getLineGenerator([ + "// Path: src/index.ts", + "const x = 5;", + "//", + "console.log(x);", + ]); + const result = lineStream.avoidEmptyComments(linesGenerator, "//"); + const filteredLines = await getFilteredLines(result); + expect(filteredLines).toEqual([ + "// Path: src/index.ts", + "const x = 5;", + "console.log(x);", + ]); }); }); diff --git a/core/autocomplete/filtering/streamTransforms/lineStream.ts b/core/autocomplete/filtering/streamTransforms/lineStream.ts index cd8363672..dffca36b9 100644 --- a/core/autocomplete/filtering/streamTransforms/lineStream.ts +++ b/core/autocomplete/filtering/streamTransforms/lineStream.ts @@ -122,21 +122,40 @@ export async function* noTopLevelKeywordsMidline( } /** - * Filters out unwanted lines from a LineStream, specifically those starting with '// Path: ' or empty comments. + * Filters out lines starting with '// Path: ' from a LineStream. * * @param {LineStream} stream - The input stream of lines to filter. * @param {string} comment - The comment syntax to filter (e.g., '//' for JavaScript-style comments). - * @yields {string} The filtered lines, excluding unwanted path lines and empty comments. + * @yields {string} The filtered lines, excluding unwanted path lines. */ -export async function* avoidPathLineAndEmptyComments( +export async function* avoidPathLine( stream: LineStream, comment: string, ): LineStream { // Snippets are inserted as comments with a line at the start '// Path: '. // Sometimes the model with copy this pattern, which is unwanted for await (const line of stream) { - // Also filter lines that are empty comments - if (line.startsWith(`${comment} Path: `) || line.trim() === comment) { + if (line.startsWith(`${comment} Path: `)) { + continue; + } + yield line; + } +} + +/** + * Filters out empty comment lines from a LineStream. + * + * @param {LineStream} stream - The input stream of lines to filter. + * @param {string} comment - The comment syntax to filter (e.g., '//' for JavaScript-style comments). + * @yields {string} The filtered lines, excluding empty comments. + */ +export async function* avoidEmptyComments( + stream: LineStream, + comment: string, +): LineStream { + // Filter lines that are empty comments + for await (const line of stream) { + if (line.trim() === comment) { continue; } yield line;