From 7aebea0e2ba811c97bec043334637c2a218b3154 Mon Sep 17 00:00:00 2001 From: adarshramiyer Date: Thu, 26 Jun 2025 15:22:12 -0700 Subject: [PATCH] refactoring and commenting to address patrick's comments --- core/core.ts | 40 ++------ core/nextEdit/context/aggregateEdits.ts | 30 +++--- core/nextEdit/context/processNextEditData.ts | 51 +++++++---- core/nextEdit/context/processSmallEdit.ts | 46 ++++++++++ .../vscode/src/extension/VsCodeExtension.ts | 69 ++------------ .../vscode/src/util/editLoggingUtils.ts | 91 +++++++++++++++++++ 6 files changed, 199 insertions(+), 128 deletions(-) create mode 100644 core/nextEdit/context/processSmallEdit.ts create mode 100644 extensions/vscode/src/util/editLoggingUtils.ts diff --git a/core/core.ts b/core/core.ts index cdd27a5af..f17c50dd2 100644 --- a/core/core.ts +++ b/core/core.ts @@ -68,13 +68,8 @@ import { walkDirCache } from "./indexing/walkDir"; import { LLMLogger } from "./llm/logger"; import { RULES_MARKDOWN_FILENAME } from "./llm/rules/constants"; import { llmStreamChat } from "./llm/streamChat"; -import { - BeforeAfterDiff, - createDiff, - DiffFormatType, -} from "./nextEdit/context/diffFormatting"; -import { processNextEditData } from "./nextEdit/context/processNextEditData.js"; -import { NextEditProvider } from "./nextEdit/NextEditProvider"; +import { BeforeAfterDiff } from "./nextEdit/context/diffFormatting"; +import { processSmallEdit } from "./nextEdit/context/processSmallEdit"; import type { FromCoreProtocol, ToCoreProtocol } from "./protocol"; import { OnboardingModes } from "./protocol/core"; import type { IMessenger, Message } from "./protocol/messenger"; @@ -728,36 +723,13 @@ export class Core { cursorPosBeforeEdit: Position, cursorPosAfterPrevEdit: Position, ) => { - NextEditProvider.getInstance().addDiffToContext( - createDiff( - beforeAfterdiff.beforeContent, - beforeAfterdiff.afterContent, - beforeAfterdiff.filePath, - DiffFormatType.Unified, - ), - ); - - // Get the current context data from the most recent message - const currentData = (global._editAggregator as any) - .latestContextData || { - configHandler: data.configHandler, - getDefsFromLspFunction: data.getDefsFromLspFunction, - recentlyEditedRanges: [], - recentlyVisitedRanges: [], - }; - - void processNextEditData( - beforeAfterdiff.filePath, - beforeAfterdiff.beforeContent, - beforeAfterdiff.afterContent, + void processSmallEdit( + beforeAfterdiff, cursorPosBeforeEdit, cursorPosAfterPrevEdit, + data.configHandler, + data.getDefsFromLspFunction, this.ide, - currentData.configHandler, - currentData.getDefsFromLspFunction, - currentData.recentlyEditedRanges, - currentData.recentlyVisitedRanges, - currentData.workspaceDir, ); }, ); diff --git a/core/nextEdit/context/aggregateEdits.ts b/core/nextEdit/context/aggregateEdits.ts index 8f4e5caa6..78e3775d3 100644 --- a/core/nextEdit/context/aggregateEdits.ts +++ b/core/nextEdit/context/aggregateEdits.ts @@ -7,33 +7,32 @@ import { } from "./diffFormatting"; interface ClusterState { - beforeState: string; + beforeState: string; // stores content of cluster before edit startRange: { minLine: number; maxLine: number }; currentRange: { minLine: number; maxLine: number }; - edits: RangeInFileWithNextEditInfo[]; + edits: RangeInFileWithNextEditInfo[]; // store small edits that form the cluster firstTimestamp: number; lastTimestamp: number; lastLine: number; - firstEditBeforeCursor: { line: number; character: number }; - lastEditAfterCursor: { line: number; character: number }; + firstEditBeforeCursor: { line: number; character: number }; // cursor position before the first edit in the cluster + lastEditAfterCursor: { line: number; character: number }; // cursor position after the last edit in the previous cluster } interface FileState { - activeClusters: ClusterState[]; - currentContent: string; - priorComparisons: string[]; - processingQueue: Array<() => Promise>; + activeClusters: ClusterState[]; // Stores active clusters for the file + currentContent: string; // Stores the current content of the file + priorComparisons: string[]; // Stores prior comparisons for the file (not currently used, but kept for future use) + processingQueue: Array<() => Promise>; // Stores a queue of small edits to be processed into clusters isProcessing: boolean; } export interface EditClusterConfig { - deltaT: number; - deltaL: number; - maxEdits: number; - maxDuration: number; - contextSize: number; - contextLines: number; - verbose: boolean; + deltaT: number; // Time threshold in seconds; if exceeded, a new cluster is created + deltaL: number; // Line threshold; if the user jumps more than this many lines, a new cluster is created + maxEdits: number; // Maximum number of edits in a cluster + maxDuration: number; // Maximum total duration of an edit in seconds + contextSize: number; // Number of previous edits to store; not currently used but kept for future use + contextLines: number; // Used for computations involving deltaL } export class EditAggregator { @@ -62,7 +61,6 @@ export class EditAggregator { maxDuration: config.maxDuration ?? 100.0, contextSize: config.contextSize ?? 5, contextLines: config.contextLines ?? 3, - verbose: config.verbose ?? false, }; this.onComparisonFinalized = onComparisonFinalized; this.previousEditFinalCursorPosition = { line: 0, character: 0 }; diff --git a/core/nextEdit/context/processNextEditData.ts b/core/nextEdit/context/processNextEditData.ts index cc0f9a5f5..a6e8411c1 100644 --- a/core/nextEdit/context/processNextEditData.ts +++ b/core/nextEdit/context/processNextEditData.ts @@ -5,6 +5,7 @@ import { ConfigHandler } from "../../config/ConfigHandler"; import { DataLogger } from "../../data/log"; import { RecentlyEditedRange } from "../types"; import { getAutocompleteContext } from "./autocompleteContextFetching"; +import { createDiff, DiffFormatType } from "./diffFormatting"; const randomNumberBetween = (min: number, max: number) => { min = Math.ceil(min); // Ensure min is an integer @@ -12,21 +13,35 @@ const randomNumberBetween = (min: number, max: number) => { return Math.floor(Math.random() * (max - min + 1)) + min; }; -export const processNextEditData = async ( - filePath: string, - beforeContent: string, - afterContent: string, - cursorPosBeforeEdit: Position, - cursorPosAfterPrevEdit: Position, - ide: IDE, - configHandler: ConfigHandler, - getDefinitionsFromLsp: GetLspDefinitionsFunction, - recentlyEditedRanges: RecentlyEditedRange[], - recentlyVisitedRanges: AutocompleteCodeSnippet[], - workspaceDir: string, - modelNameOrInstance?: string | undefined, - // eslint-disable-next-line max-params -) => { +interface ProcessNextEditDataParams { + filePath: string; + beforeContent: string; + afterContent: string; + cursorPosBeforeEdit: Position; + cursorPosAfterPrevEdit: Position; + ide: IDE; + configHandler: ConfigHandler; + getDefinitionsFromLsp: GetLspDefinitionsFunction; + recentlyEditedRanges: RecentlyEditedRange[]; + recentlyVisitedRanges: AutocompleteCodeSnippet[]; + workspaceDir: string; + modelNameOrInstance?: string | undefined; +} + +export const processNextEditData = async ({ + filePath, + beforeContent, + afterContent, + cursorPosBeforeEdit, + cursorPosAfterPrevEdit, + ide, + configHandler, + getDefinitionsFromLsp, + recentlyEditedRanges, + recentlyVisitedRanges, + workspaceDir, + modelNameOrInstance, +}: ProcessNextEditDataParams) => { // To switch to the user's autocomplete model, uncomment the following lines // const { config } = await configHandler.loadConfig(); // const autocompleteModel = @@ -50,9 +65,9 @@ export const processNextEditData = async ( modelName, ); - // console.log( - // createDiff(beforeContent, afterContent, filePath, DiffFormatType.Unified), - // ); + console.log( + createDiff(beforeContent, afterContent, filePath, DiffFormatType.Unified), + ); void DataLogger.getInstance().logDevData({ name: "nextEdit", diff --git a/core/nextEdit/context/processSmallEdit.ts b/core/nextEdit/context/processSmallEdit.ts new file mode 100644 index 000000000..f94872996 --- /dev/null +++ b/core/nextEdit/context/processSmallEdit.ts @@ -0,0 +1,46 @@ +import { IDE, Position } from "../.."; +import { GetLspDefinitionsFunction } from "../../autocomplete/types"; +import { ConfigHandler } from "../../config/ConfigHandler"; +import { NextEditProvider } from "../NextEditProvider"; +import { BeforeAfterDiff, createDiff, DiffFormatType } from "./diffFormatting"; +import { processNextEditData } from "./processNextEditData"; + +export const processSmallEdit = async ( + beforeAfterdiff: BeforeAfterDiff, + cursorPosBeforeEdit: Position, + cursorPosAfterPrevEdit: Position, + configHandler: ConfigHandler, + getDefsFromLspFunction: GetLspDefinitionsFunction, + ide: IDE, +) => { + NextEditProvider.getInstance().addDiffToContext( + createDiff( + beforeAfterdiff.beforeContent, + beforeAfterdiff.afterContent, + beforeAfterdiff.filePath, + DiffFormatType.Unified, + ), + ); + + // Get the current context data from the most recent message + const currentData = (global._editAggregator as any).latestContextData || { + configHandler: configHandler, + getDefsFromLspFunction: getDefsFromLspFunction, + recentlyEditedRanges: [], + recentlyVisitedRanges: [], + }; + + void processNextEditData({ + filePath: beforeAfterdiff.filePath, + beforeContent: beforeAfterdiff.beforeContent, + afterContent: beforeAfterdiff.afterContent, + cursorPosBeforeEdit: cursorPosBeforeEdit, + cursorPosAfterPrevEdit: cursorPosAfterPrevEdit, + ide: ide, + configHandler: currentData.configHandler, + getDefinitionsFromLsp: currentData.getDefsFromLspFunction, + recentlyEditedRanges: currentData.recentlyEditedRanges, + recentlyVisitedRanges: currentData.recentlyVisitedRanges, + workspaceDir: currentData.workspaceDir, + }); +}; diff --git a/extensions/vscode/src/extension/VsCodeExtension.ts b/extensions/vscode/src/extension/VsCodeExtension.ts index 5d42c8706..d7fbd80a5 100644 --- a/extensions/vscode/src/extension/VsCodeExtension.ts +++ b/extensions/vscode/src/extension/VsCodeExtension.ts @@ -1,6 +1,6 @@ import fs from "fs"; -import { IContextProvider, Position, RangeInFileWithNextEditInfo } from "core"; +import { IContextProvider } from "core"; import { ConfigHandler } from "core/config/ConfigHandler"; import { EXTENSION_NAME, getControlPlaneEnv } from "core/control-plane/env"; import { Core } from "core/core"; @@ -41,9 +41,8 @@ import { VsCodeIde } from "../VsCodeIde"; import { ConfigYamlDocumentLinkProvider } from "./ConfigYamlDocumentLinkProvider"; import { VsCodeMessenger } from "./VsCodeMessenger"; -import { AutocompleteCodeSnippet } from "core/autocomplete/snippets/types"; -import { RecentlyEditedRange } from "core/autocomplete/util/types"; import { getDefinitionsFromLsp } from "../autocomplete/lsp"; +import { handleTextDocumentChange } from "../util/editLoggingUtils"; import { VsCodeIdeUtils } from "../util/ideUtils"; import type { VsCodeWebviewProtocol } from "../webviewProtocol"; @@ -291,65 +290,15 @@ export class VsCodeExtension { }); vscode.workspace.onDidChangeTextDocument(async (event) => { - const changes = event.contentChanges; - const editor = vscode.window.activeTextEditor; - const { config } = await this.configHandler.loadConfig(); - - if (!config?.experimental?.logEditingData) return; - if (!editor) return; - if (event.contentChanges.length === 0) return; - - // Ensure that loggin will only happen in the open-source continue repo - const workspaceFolder = vscode.workspace.getWorkspaceFolder( - event.document.uri, - ); - if (!workspaceFolder) { - return; - } - - const workspaceDirUri = workspaceFolder.uri.toString(); - const repoName = await this.ide.getRepoName(workspaceDirUri); - if (repoName !== "continuedev/continue") return; - - const activeCursorPos = editor.selection.active; - const editActions: RangeInFileWithNextEditInfo[] = changes.map( - (change) => ({ - filepath: event.document.uri.toString(), - range: { - start: change.range.start as Position, - end: change.range.end as Position, - }, - fileContents: event.document.getText(), - editText: change.text, - - // whichever side of the range isn't the active cursor pos is the previous one - beforeCursorPos: (change.range.start.line === activeCursorPos.line && - change.range.start.character === activeCursorPos.character - ? change.range.end - : change.range.start) as Position, - - afterCursorPos: activeCursorPos as Position, - workspaceDir: workspaceDirUri, - }), + const editInfo = await handleTextDocumentChange( + event, + this.configHandler, + this.ide, + this.completionProvider, + getDefinitionsFromLsp, ); - let recentlyEditedRanges: RecentlyEditedRange[] = []; - let recentlyVisitedRanges: AutocompleteCodeSnippet[] = []; - - if (this.completionProvider) { - recentlyEditedRanges = - await this.completionProvider.recentlyEditedTracker.getRecentlyEditedRanges(); - recentlyVisitedRanges = - this.completionProvider.recentlyVisitedRanges.getSnippets(); - } - - this.core.invoke("files/smallEdit", { - actions: editActions, - configHandler: this.configHandler, - getDefsFromLspFunction: getDefinitionsFromLsp, - recentlyEditedRanges: recentlyEditedRanges, - recentlyVisitedRanges: recentlyVisitedRanges, - }); + if (editInfo) this.core.invoke("files/smallEdit", editInfo); }); vscode.workspace.onDidSaveTextDocument(async (event) => { diff --git a/extensions/vscode/src/util/editLoggingUtils.ts b/extensions/vscode/src/util/editLoggingUtils.ts new file mode 100644 index 000000000..1dffe32a9 --- /dev/null +++ b/extensions/vscode/src/util/editLoggingUtils.ts @@ -0,0 +1,91 @@ +import { IDE, Position, Range, RangeInFileWithNextEditInfo } from "core"; +import { AutocompleteCodeSnippet } from "core/autocomplete/snippets/types"; +import { GetLspDefinitionsFunction } from "core/autocomplete/types"; +import { ConfigHandler } from "core/config/ConfigHandler"; +import { RecentlyEditedRange } from "core/nextEdit/types"; +import * as vscode from "vscode"; +import { ContinueCompletionProvider } from "../autocomplete/completionProvider"; + +export const getBeforeCursorPos = (range: Range, activePos: Position) => { + // whichever side of the range isn't active is the before position + if ( + range.start.line === activePos.line && + range.start.character === activePos.character + ) { + return range.end as Position; + } else { + return range.start as Position; + } +}; + +const getWorkspaceDirInfo = async ( + event: vscode.TextDocumentChangeEvent, + ide: IDE, +) => { + // gets the workspace dir uri and checks if it's the open-source continue repo + const workspaceFolder = vscode.workspace.getWorkspaceFolder( + event.document.uri, + ); + if (!workspaceFolder) { + return false; + } + const workspaceDirUri = workspaceFolder.uri.toString(); + const repoName = await ide.getRepoName(workspaceDirUri); + return { + workspaceDirUri: workspaceDirUri, + isContinueRepo: repoName === "continuedev/continue", + }; +}; + +export const handleTextDocumentChange = async ( + event: vscode.TextDocumentChangeEvent, + configHandler: ConfigHandler, + ide: IDE, + completionProvider: ContinueCompletionProvider, + getDefinitionsFromLsp: GetLspDefinitionsFunction, +) => { + const changes = event.contentChanges; + const editor = vscode.window.activeTextEditor; + const { config } = await configHandler.loadConfig(); + + if (!config?.experimental?.logEditingData) return; + if (!editor) return; + if (event.contentChanges.length === 0) return; + + // Ensure that loggin will only happen in the open-source continue repo + const workspaceDirInfo = await getWorkspaceDirInfo(event, ide); + if (!workspaceDirInfo) return; + if (!workspaceDirInfo.isContinueRepo) return; + + const activeCursorPos = editor.selection.active; + const editActions: RangeInFileWithNextEditInfo[] = changes.map((change) => ({ + filepath: event.document.uri.toString(), + range: { + start: change.range.start as Position, + end: change.range.end as Position, + }, + fileContents: event.document.getText(), + editText: change.text, + beforeCursorPos: getBeforeCursorPos(change.range, activeCursorPos), + afterCursorPos: activeCursorPos as Position, + workspaceDir: workspaceDirInfo.workspaceDirUri, + })); + + let recentlyEditedRanges: RecentlyEditedRange[] = []; + let recentlyVisitedRanges: AutocompleteCodeSnippet[] = []; + + if (completionProvider) { + recentlyEditedRanges = + await completionProvider.recentlyEditedTracker.getRecentlyEditedRanges(); + recentlyVisitedRanges = + completionProvider.recentlyVisitedRanges.getSnippets(); + } + + return { + actions: editActions, + configHandler: configHandler, + getDefsFromLspFunction: getDefinitionsFromLsp, + recentlyEditedRanges, + recentlyVisitedRanges, + }; +};