Skip to content

Commit

Permalink
Simplify state management to fix web test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
devin-ai-integration[bot] and whitphx committed Jan 8, 2025
1 parent b6a38b3 commit e0667bd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 54 deletions.
9 changes: 1 addition & 8 deletions src/commands/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,12 @@ export class EmacsCommandRegistry {
const command = this.commands.get(commandName);
if (command) {
this.lastExecutedCommandId = commandName;
this.currentCommandId = commandName;
// Reset currentCommandId after command execution
setTimeout(() => {
this.currentCommandId = undefined;
}, 0);
}
return command;
}

public getCurrentCommandId(): string | undefined {
// Return the current command ID if it exists, otherwise return the last executed command
// This helps track command context during document changes that happen after command execution
return this.currentCommandId || this.lastExecutedCommandId;
return this.lastExecutedCommandId;
}

public onInterrupt(): void {
Expand Down
68 changes: 22 additions & 46 deletions src/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,40 +371,26 @@ export class EmacsEmulator implements IEmacsController, vscode.Disposable {
public onDidChangeTextDocument(e: vscode.TextDocumentChangeEvent): void {
// XXX: Is this a correct way to check the identity of document?
if (e.document.uri.toString() === this.textEditor.document.uri.toString()) {
const currentCommandId = this.commandRegistry.getCurrentCommandId();
if (
e.contentChanges.some((contentChange) =>
this.textEditor.selections.some(
(selection) => typeof contentChange.range.intersection(selection) !== "undefined",
),
)
) {
this.exitMarkMode();
}

// Define safe commands that can modify document without interrupting state
const safeModifyingCommands = new Set([
"universalArgument",
"digitArgument",
"negativeArgument",
"subsequentArgumentDigit",
]);

const isPartOfSafeCommand = currentCommandId && safeModifyingCommands.has(currentCommandId);

if (!isPartOfSafeCommand) {
this._wasDocumentChanged = true;
if (
e.contentChanges.some((contentChange) =>
this.textEditor.selections.some(
(selection) => typeof contentChange.range.intersection(selection) !== "undefined",
),
)
) {
this.exitMarkMode();
}
// Only trigger interruption for non-prefix-argument commands
const currentCommandId = this.commandRegistry.getCurrentCommandId();
const isPrefixCommand =
currentCommandId?.startsWith("universalArgument") ||
currentCommandId?.startsWith("digitArgument") ||
currentCommandId?.startsWith("negativeArgument");

// Only trigger interruption for non-safe commands
if (!isPrefixCommand) {
this.onDidInterruptTextEditor();
} else {
logger.debug("[EmacsEmulator] Ignoring document change from safe command");
}

// Reset document changed flag after handling
setTimeout(() => {
this._wasDocumentChanged = false;
}, 0);
}
}

Expand Down Expand Up @@ -725,25 +711,15 @@ export class EmacsEmulator implements IEmacsController, vscode.Disposable {
}

private onDidInterruptTextEditor() {
// Only set interrupted state if we're not in a safe command
const currentCommandId = this.commandRegistry.getCurrentCommandId();
const safeCommands = new Set([
"moveToWindowLineTopBottom",
"universalArgument",
"digitArgument",
"negativeArgument",
"subsequentArgumentDigit",
]);
const isSafeCommand =
currentCommandId === "moveToWindowLineTopBottom" ||
currentCommandId?.startsWith("universalArgument") ||
currentCommandId?.startsWith("digitArgument") ||
currentCommandId?.startsWith("negativeArgument");

if (!currentCommandId || !safeCommands.has(currentCommandId)) {
this._isInterrupted = true;
if (!isSafeCommand) {
this.commandRegistry.onInterrupt();
// Reset interrupted state after handling
setTimeout(() => {
this._isInterrupted = false;
}, 0);
} else {
logger.debug(`[EmacsEmulator] Ignoring interruption during safe command: ${currentCommandId}`);
}
}
}

0 comments on commit e0667bd

Please sign in to comment.