Skip to content

Conversation

@hannesrudolph
Copy link
Collaborator

@hannesrudolph hannesrudolph commented Jun 30, 2025

Description

Fixes #4574

This PR fixes the issue where the chat input box loses focus during automated file editing workflows, causing user input to be typed into files instead of the chat.

Changes Made

  • Added preserveFocus: true option to the showTextDocument call in DiffViewProvider.ts (line 507)
  • Updated unit tests in DiffViewProvider.spec.ts to reflect the new expected behavior

Testing

  • All existing tests pass (2585 tests)
  • Updated unit tests for DiffViewProvider to expect preserveFocus: true
  • Manual testing completed:
    • Tested with mode-writer mode using the prompt: "lets improve @ /.roo/rules-translate so its more structured."
    • Verified that chat input maintains focus during automated file edits

Verification of Acceptance Criteria

  • Chat input box no longer loses focus during automated file editing
  • User input is no longer typed into files when Roo is editing in auto-mode
  • Focus remains on the chat input throughout the automated workflow

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Comments added for complex logic (not needed for this simple fix)
  • Documentation updated (not needed)
  • No breaking changes
  • Tests updated and passing

Important

Adds preserveFocus: true to maintain chat input focus during automated file editing in DiffViewProvider.ts.

  • Behavior:
    • Adds preserveFocus: true to showTextDocument in DiffViewProvider.ts to maintain chat input focus during automated file editing.
    • Ensures user input is not mistakenly typed into files.
  • Testing:
    • Updates unit tests in DiffViewProvider.spec.ts to verify preserveFocus: true behavior.
    • Manual testing confirms chat input focus is preserved.

This description was created by Ellipsis for 18dcd17. You can customize this summary. It will automatically update as commits are pushed.

- Add preserveFocus: true to showTextDocument call in DiffViewProvider
- Prevents focus from being stolen from chat input during auto-mode file edits
- Update tests to reflect new expected behavior

Fixes #4574
Copilot AI review requested due to automatic review settings June 30, 2025 16:35
@hannesrudolph hannesrudolph requested review from cte, jr and mrubens as code owners June 30, 2025 16:35
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Jun 30, 2025
@delve-auditor
Copy link

delve-auditor bot commented Jun 30, 2025

No security or compliance issues detected. Reviewed everything up to 18dcd17.

Security Overview
  • 🔎 Scanned files: 2 changed file(s)
Detected Code Changes

The diff is too large to display a summary of code changes.

Reply to this PR with @delve-auditor followed by a description of what change you want and we'll auto-submit a change to this PR to implement it.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR ensures the chat input keeps focus during automated file edits by adding preserveFocus: true to showTextDocument calls and updating related tests.

  • Added preserveFocus: true to vscode.window.showTextDocument in DiffViewProvider.ts.
  • Updated unit tests in DiffViewProvider.spec.ts to expect the new preserveFocus option.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/integrations/editor/DiffViewProvider.ts Added preserveFocus: true to showTextDocument options.
src/integrations/editor/tests/DiffViewProvider.spec.ts Updated tests to assert preserveFocus: true in options.
Comments suppressed due to low confidence (1)

src/integrations/editor/DiffViewProvider.ts:504

  • [nitpick] Update this comment to mention that preserveFocus: true is now used to keep the chat input focused during automated edits, so the comment stays aligned with the code change.
			// Pre-open the file as a text document to ensure it doesn't open in preview mode

vi.mocked(vscode.window.showTextDocument).mockImplementation(async (uri, options) => {
callOrder.push("showTextDocument")
expect(options).toEqual({ preview: false, viewColumn: vscode.ViewColumn.Active })
expect(options).toEqual({ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true })
Copy link

Copilot AI Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This strict equality assertion can be brittle if more options are added in the future. Consider using expect.objectContaining or toMatchObject to only verify the relevant keys.

Suggested change
expect(options).toEqual({ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true })
expect(options).toEqual(expect.objectContaining({ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }))

Copilot uses AI. Check for mistakes.
expect(vscode.window.showTextDocument).toHaveBeenCalledWith(
expect.objectContaining({ fsPath: `${mockCwd}/test.md` }),
{ preview: false, viewColumn: vscode.ViewColumn.Active },
{ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true },
Copy link

Copilot AI Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Similarly, this exact match could break if additional options are introduced. Using expect.objectContaining for the options object will make the test more resilient.

Suggested change
{ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true },
expect.objectContaining({ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }),

Copilot uses AI. Check for mistakes.
// This fixes issues with files that have custom editor associations (like markdown preview)
vscode.window
.showTextDocument(uri, { preview: false, viewColumn: vscode.ViewColumn.Active })
.showTextDocument(uri, { preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true })
Copy link

Copilot AI Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Extract the options object into a named constant (e.g., const showOptions = { preview: false, viewColumn: ..., preserveFocus: true };) to reduce duplication and improve readability.

Suggested change
.showTextDocument(uri, { preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true })
.showTextDocument(uri, showOptions)

Copilot uses AI. Check for mistakes.
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jun 30, 2025
@hannesrudolph hannesrudolph moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Jun 30, 2025
@hannesrudolph hannesrudolph moved this from PR [Needs Prelim Review] to Triage in Roo Code Roadmap Jun 30, 2025
- Use expect.objectContaining in tests for better resilience to future option additions
- Extract showTextDocument options to named constant for improved readability
- Addresses Copilot review feedback on PR #5282
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jun 30, 2025
Copy link
Collaborator Author

@hannesrudolph hannesrudolph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR successfully addresses issue #4574 by adding preserveFocus: true to the showTextDocument call in DiffViewProvider. The implementation prevents the chat input from losing focus during automated file editing workflows.

The Copilot review suggestions have been addressed in the latest commit:

  • Options extracted to a named constant
  • Tests updated to use expect.objectContaining for better resilience

I've identified a few areas for consideration:

  1. Other showTextDocument calls in the codebase might benefit from the same pattern
  2. The comment could be updated to reflect the focus preservation purpose
  3. Integration tests for actual focus behavior could help prevent regressions

The core implementation effectively solves the reported issue where user input could be accidentally typed into files during automated workflows.


// Pre-open the file as a text document to ensure it doesn't open in preview mode
// This fixes issues with files that have custom editor associations (like markdown preview)
const showOptions = { preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that preserveFocus: true is consistently used in other showTextDocument calls within this file (lines 199, 374, 441), which is great. However, there are other places in the codebase that might benefit from this pattern:

  • src/services/marketplace/MarketplaceManager.ts:129
  • src/integrations/misc/export-markdown.ts:40

Is it intentional that these other locations don't preserve focus? If they could also cause focus issues during automated workflows, would it make sense to apply the same pattern there for consistency?

}),
)

// Pre-open the file as a text document to ensure it doesn't open in preview mode
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're now also preserving focus to fix the chat input issue, would it be helpful to update this comment to reflect both purposes? Something like:

// Pre-open the file as a text document to ensure it doesn't open in preview mode
// This fixes issues with files that have custom editor associations (like markdown preview)
// and preserves focus on the chat input during automated editing workflows

expect.objectContaining({
preview: false,
viewColumn: vscode.ViewColumn.Active,
preserveFocus: true,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the tests correctly verify that preserveFocus: true is passed to the API, they don't test the actual focus behavior. Have you considered adding an integration test that verifies the chat input maintains focus during file operations?

I understand this might be challenging in a unit test environment, but it could be valuable for preventing regressions of this specific bug. Perhaps this could be added to the E2E test suite if it's not feasible here?

@hannesrudolph hannesrudolph moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Jun 30, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Jun 30, 2025
@daniel-lxs
Copy link
Member

This doesn't seem to work, it seems like preserveFocus is not enough, see #4577 (comment)

@daniel-lxs daniel-lxs closed this Jun 30, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jun 30, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Prelim Review] to Done in Roo Code Roadmap Jun 30, 2025
@daniel-lxs
Copy link
Member

It seems that the focus is lost sometimes but overall it seems to work most of the times, maybe we can make some more tweaks to make sure it keeps the focus all the time.

@daniel-lxs daniel-lxs reopened this Jul 2, 2025
@github-project-automation github-project-automation bot moved this from Done to Triage in Roo Code Roadmap Jul 2, 2025
@github-project-automation github-project-automation bot moved this from Done to New in Roo Code Roadmap Jul 2, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Changes Requested] in Roo Code Roadmap Jul 2, 2025
@daniel-lxs
Copy link
Member

Closing since @hannesrudolph will open a new one

@daniel-lxs daniel-lxs closed this Jul 2, 2025
@github-project-automation github-project-automation bot moved this from PR [Changes Requested] to Done in Roo Code Roadmap Jul 2, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jul 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working PR - Needs Preliminary Review size:S This PR changes 10-29 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

chatbox loses focus during automated workflow

3 participants