feat: set up event handler for selection changes

This commit is contained in:
Jacob Kim 2025-07-31 19:24:35 -07:00
parent b324391e35
commit 878fb9474f

View file

@ -44,10 +44,6 @@ import { VsCodeMessenger } from "./VsCodeMessenger";
import { getAst } from "core/autocomplete/util/ast";
import { DocumentHistoryTracker } from "core/nextEdit/DocumentHistoryTracker";
import {
EditableRegionStrategy,
getNextEditableRegion,
} from "core/nextEdit/NextEditEditableRegionCalculator";
import { NextEditProvider } from "core/nextEdit/NextEditProvider";
import { isModelCapableOfNextEdit } from "core/nextEdit/utils";
import { localPathOrUriToPath } from "core/util/pathToUri";
@ -55,6 +51,10 @@ import { JumpManager } from "../activation/JumpManager";
import setupNextEditWindowManager, {
NextEditWindowManager,
} from "../activation/NextEditWindowManager";
import {
HandlerPriority,
SelectionChangeManager,
} from "../activation/SelectionChangeManager";
import { GhostTextAcceptanceTracker } from "../autocomplete/GhostTextAcceptanceTracker";
import { getDefinitionsFromLsp } from "../autocomplete/lsp";
import { handleTextDocumentChange } from "../util/editLoggingUtils";
@ -79,18 +79,8 @@ export class VsCodeExtension {
private fileSearch: FileSearch;
private uriHandler = new UriEventHandler();
private completionProvider: ContinueCompletionProvider;
// Track whether the user is currently typing
private isTypingSession = false;
private typingTimer: NodeJS.Timeout | null = null;
private lastDocumentChangeTime = 0;
// Reset typing session after a delay
resetTypingSession = () => {
if (this.typingTimer) clearTimeout(this.typingTimer);
this.typingTimer = setTimeout(() => {
this.isTypingSession = false;
}, 2000); // Typing session considered over after 2 seconds of inactivity
};
private ARBITRARY_TYPING_DELAY = 2000;
constructor(context: vscode.ExtensionContext) {
// Register auth provider
@ -111,6 +101,27 @@ export class VsCodeExtension {
this.extensionContext = context;
this.windowId = uuidv4();
const selectionManager = SelectionChangeManager.getInstance();
selectionManager.initialize(this.ide);
selectionManager.registerListener(
"typing",
async (e, state) => {
const timeSinceLastDocChange =
Date.now() - state.lastDocumentChangeTime;
if (
state.isTypingSession &&
timeSinceLastDocChange < this.ARBITRARY_TYPING_DELAY
) {
console.log("VsCodeExtension: typing in progress, preserving chain");
return true;
}
return false;
},
HandlerPriority.NORMAL,
);
// Dependencies of core
let resolveVerticalDiffManager: any = undefined;
const verticalDiffManagerPromise = new Promise<VerticalDiffManager>(
@ -198,8 +209,15 @@ export class VsCodeExtension {
this.activateNextEdit();
await NextEditWindowManager.freeTabAndEsc();
JumpManager.getInstance();
GhostTextAcceptanceTracker.getInstance();
const jumpManager = JumpManager.getInstance();
jumpManager.registerSelectionChangeHandler();
const ghostTextAcceptanceTracker =
GhostTextAcceptanceTracker.getInstance();
ghostTextAcceptanceTracker.registerSelectionChangeHandler();
const nextEditWindowManager = NextEditWindowManager.getInstance();
nextEditWindowManager.registerSelectionChangeHandler();
} else {
NextEditWindowManager.clearInstance();
this.deactivateNextEdit();
@ -344,11 +362,15 @@ export class VsCodeExtension {
vscode.workspace.onDidChangeTextDocument(async (event) => {
if (event.contentChanges.length > 0) {
this.isTypingSession = true;
this.lastDocumentChangeTime = Date.now();
this.resetTypingSession();
selectionManager.documentChanged();
}
// if (event.contentChanges.length > 0) {
// this.isTypingSession = true;
// this.lastDocumentChangeTime = Date.now();
// this.resetTypingSession();
// }
const editInfo = await handleTextDocumentChange(
event,
this.configHandler,
@ -432,60 +454,7 @@ export class VsCodeExtension {
// Listen for selection changes to hide tooltip when cursor moves.
vscode.window.onDidChangeTextEditorSelection(async (e) => {
// Don't delete the chain of edits if:
// 1. A next edit window was just accepted.
if (
NextEditWindowManager.isInstantiated() &&
NextEditWindowManager.getInstance().hasAccepted()
) {
return;
}
// 2. A jump is in progress.
if (
JumpManager.getInstance().isJumpInProgress() ||
JumpManager.getInstance().wasJumpJustAccepted()
) {
return;
}
// 3. A ghost text was just accepted.
// Check if this selection change matches our expected ghost text acceptance.
const wasGhostTextAccepted =
GhostTextAcceptanceTracker.getInstance().checkGhostTextWasAccepted(
e.textEditor.document,
e.selections[0].active,
);
if (wasGhostTextAccepted) {
// Ghost text was accepted - don't delete the chain.
return;
}
// 4. The selection change is part of a typing session
// Check if this selection change is close enough to a document change to be considered typing.
const timeSinceLastDocChange = Date.now() - this.lastDocumentChangeTime;
const ARBITRARY_TYPING_DELAY = 500;
if (
this.isTypingSession &&
timeSinceLastDocChange < ARBITRARY_TYPING_DELAY
) {
// This selection change is likely due to typing, don't delete the chain.
return;
}
// Otherwise, delete the chain (for rejection or unrelated movement).
console.log("deleteChain called from onDidChangeTextEditorSelection");
await NextEditProvider.getInstance().deleteChain();
NextEditProvider.getInstance().loadNextEditableRegionsInTheCurrentChain(
(await getNextEditableRegion(EditableRegionStrategy.Static, {
cursorPosition: e.selections[0].anchor,
filepath: localPathOrUriToPath(e.textEditor.document.uri.toString()),
ide: this.ide,
})) ?? [],
);
await selectionManager.handleSelectionChange(e);
});
// Refresh index when branch is changed