Skip to content

Fix out of bounds text selection with line wrapping - #262910

Merged
Alexandru Dima (alexdima) merged 2 commits into
microsoft:mainfrom
remcohaszing:fix-wrapped-line-selection
Aug 25, 2026
Merged

Alexandru Dima (alexdima) merged 2 commits into
microsoft:mainfrom
remcohaszing:fix-wrapped-line-selection

Conversation

@remcohaszing

@remcohaszing Remco Haszing (remcohaszing) commented Aug 22, 2025 •

Copy link
Copy Markdown
Contributor

The original behaviour is confusing and somewhat hard to explain. Given a Monaco editor with text wrapping enabled, if the user starts selecting text and moves the mouse outside of the editor viewbox, to the right of the screen, the cursor in the editor would be somewhere halfway the editor. The editor cursor did move with the mouse. So moving the mouse further right, moved the cursor further forward. The text cursor could even move onto the next line, below the mouse position. In case of RTL, the behaviour was the same. Further right means more forward, meaning it was visually mirrored.

The new behaviour with this PR is easier to explain. If the cursor moves out of bounds while selecting text, the selection will match the end of the wrapped line at column matching the cursor height. It also takes RTL into account.

The new behaviour matches gedit, LibreOffice, Google Docs, HTML content in browsers, and even Monaco Editor with text wrapping disabled.

Closes #291901

The original behaviour is confusing and somewhat hard to explain. Given
a Monaco editor with text wrapping enabled, if the user starts selecting
text and moves the mouse outside of the editor viewbox, to the right of
the screen, the cursor in the editor would be somewhere halfway the
editor. The editor cursor did move with the mouse. So moving the mouse
further right, moved the cursor further forward. The text cursor could
even move onto the next line, below the mouse position. In case of RTL,
the behaviour was the same. Further right means more forward, meaning it
was visually mirrored.

The new behaviour with this PR is easier to explain. If the cursor moves
out of bounds while selecting text, the selection will match the end of
the wrapped line at column matching the cursor height. It also takes RTL
into account.

The new behaviour matches gedit, LibreOffice, Google Docs, HTML content
in browsers, and even Monaco Editor with text wrapping disabled.
@remcohaszing

Copy link
Copy Markdown
Contributor Author

Friendly ping :)

I believe this is mostly up Aiday Marlen Kyzy (@aiday-mar)’s alley.

@aiday-mar

Copy link
Copy Markdown
Contributor

Hi Remco Haszing (@remcohaszing), thank you for your PR. I am not sure I understand the initial issue as well as the PR fix. Could you please write a numbered set of steps for how to reproduce this issue, and include the expected result as well as the actual result? A screen recording showing the issue would be helpful. Could you also explain your fix in more detail? Thank you.

@aiday-mar Aiday Marlen Kyzy (aiday-mar) added info-needed Issue requires more information from poster and removed triage-needed labels Dec 16, 2025
@remcohaszing

Copy link
Copy Markdown
Contributor Author

You’re right. The issue is hard to explain in text. A screen recording explains it much better. I recorded a video where I selected and moved the mouse around. Especially towards moving further towards the right shows the issue.

This is the original behaviour:

simplescreenrecorder-2025-12-19_11.11.04.webm

This is the new behaviour:

simplescreenrecorder-2025-12-19_11.08.13.webm

@aiday-mar Aiday Marlen Kyzy (aiday-mar) removed the info-needed Issue requires more information from poster label Dec 21, 2025
@aiday-mar

Copy link
Copy Markdown
Contributor

I see I think from the videos I understood that the current behavior is that as you move the mouse the selection slowly increases along a line. With your PR, as you move the mouse, the selection will highlight the next full lines instead of highlighting slowly character by character.

I see the value in this PR, but I am not sure we should merge it until we have gotten feedback on this issue. Could we create a feature request issue and gather feedback from the community? I don't think we should change the default behavior unless there is enough upvotes for that.

@remcohaszing

Remco Haszing (remcohaszing) commented Dec 26, 2025 •

Copy link
Copy Markdown
Contributor Author

You can see at 0:05 of the video showing the original implementation that when the mouse moves outside of the editor to the right, the text selection suddenly moves a few characters to the left. From there on the text selection behaves as if the line isn’t wrapped at all.

You can try this in the current VSCode with default settings. For example, open README.md from the VSCode repo in VSCode with default settings. Make the editor narrow, so it starts wrapping line 8. Now start selecting text by moving the cursor to the beginning of line 8 and move it to the right, our of bounds. This behaviour feels very unnatural.

image

The second video with my changes can be described as: the same as every other application that has selectable text.

I really think the current behaviour without this PR is a bug.

@aiday-mar

Aiday Marlen Kyzy (aiday-mar) commented Dec 26, 2025 •

Copy link
Copy Markdown
Contributor

Hi Remco Haszing (@remcohaszing) thanks for the explanation. I agree that the cursor moving several characters back is a bug. This should indeed be fixed. I am not sure about the second part which is that when you move the mouse out of bounds, the selection increases by a full view line each time instead of character by character. Did you mention other editors behave this way?

@remcohaszing

Remco Haszing (remcohaszing) commented Dec 27, 2025 •

Copy link
Copy Markdown
Contributor Author

Yes, every other application I can think of and try, has the behaviour of this PR:

  • Windows Notepad
  • Windows Terminal
  • VSCode builtin terminal
  • VSCode builtin markdown preview
  • Tilix
  • GNOME terminal
  • Gedit
  • LibreOffice
  • Google Docs
  • Telegram
  • Discord
  • WhatsApp
  • HTML <textarea> elements
  • HTML static content

Copilot AI balanced review requested due to automatic review settings August 24, 2026 22:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes out-of-bounds mouse selection at wrapped-line boundaries, including RTL text.

Changes:

  • Snaps selection to the appropriate wrapped-line endpoint.
  • Accounts for RTL line direction.
Show a summary per file
File Description
src/vs/editor/browser/controller/dragScrolling.ts Updates horizontal drag-selection targeting.

Review details

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread src/vs/editor/browser/controller/dragScrolling.ts

@alexdima Alexandru Dima (alexdima) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you! ❤️

@alexdima
Alexandru Dima (alexdima) enabled auto-merge (squash) August 25, 2026 15:45
@dmitrivMS

Copy link
Copy Markdown
Collaborator

Would adding a test for this change be a good idea?

@alexdima
Alexandru Dima (alexdima) merged commit 63da5e8 into microsoft:main Aug 25, 2026
27 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.136.0 milestone Aug 25, 2026
Abdon Morales (abdonmorales) pushed a commit to abdonmorales/vscode-utcs that referenced this pull request Sep 23, 2026
The original behaviour is confusing and somewhat hard to explain. Given
a Monaco editor with text wrapping enabled, if the user starts selecting
text and moves the mouse outside of the editor viewbox, to the right of
the screen, the cursor in the editor would be somewhere halfway the
editor. The editor cursor did move with the mouse. So moving the mouse
further right, moved the cursor further forward. The text cursor could
even move onto the next line, below the mouse position. In case of RTL,
the behaviour was the same. Further right means more forward, meaning it
was visually mirrored.

The new behaviour with this PR is easier to explain. If the cursor moves
out of bounds while selecting text, the selection will match the end of
the wrapped line at column matching the cursor height. It also takes RTL
into account.

The new behaviour matches gedit, LibreOffice, Google Docs, HTML content
in browsers, and even Monaco Editor with text wrapping disabled.

Co-authored-by: Alexandru Dima <alex.dima@outlook.com>
(cherry picked from commit 63da5e8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Out of bounds text selection behaves weird

9 participants