Skip to content

Fix Delete(Sub)Word commands to respect active selection#3987

Open
masmu wants to merge 2 commits intomicro-editor:masterfrom
masmu:fix/delete-word-selection
Open

Fix Delete(Sub)Word commands to respect active selection#3987
masmu wants to merge 2 commits intomicro-editor:masterfrom
masmu:fix/delete-word-selection

Conversation

@masmu
Copy link
Contributor

@masmu masmu commented Feb 2, 2026

Previously, Delete(Sub)WordLeft/Right commands would delete the word or subword adjacent to the cursor, even if the user had actively selected a range of text.

This PR modifies DeleteWordRight, DeleteWordLeft, DeleteSubWordRight and DeleteSubWordLeft.
These functions now check h.Cursor.HasSelection().
- If a selection exists, they delete the selected text.
- If a selection does not exist, they proceed with the original behavior (select word/subword and delete it).

This ensures micro behaves consistently with other modern text editors. When a user explicitly selects a range of text, a delete command should remove that specific range, rather than modifying the buffer based solely on cursor position relative to word boundaries.

Fixes #3984

Previously, Delete(Sub)WordLeft/Right commands would delete the word or subword adjacent to the cursor, even if the user had actively selected a range of text.

This commit modifies `DeleteWordRight`, `DeleteWordLeft`, `DeleteSubWordRight` and `DeleteSubWordLeft`. These actions now check if a selection exists:
- If yes, the existing selection is deleted.
- If no, the previous behavior (select word + delete) is executed.

This aligns the behavior with standard text editing conventions.

Fixes micro-editor#3984
This commit introduces a new `DeleteSelections` action that acts as a global check for active selections across all cursors.
If any selections exist, it deletes them and returns true, preventing subsequent chained actions from executing.

The default keybindings for `deleteWordLeft` (Alt-Backspace/Ctrl-H) have been  updated to chain `DeleteSelections|DeleteWordLeft`.
This ensures predictable behavior in multi-cursor scenarios:
If any cursor has a selection, the "Delete Selection" behavior takes precedence over directional word deletion, preventing accidental deletion of adjacent text when selections are present.
return true
}

// DeleteSelections checks for any active cursors selections and if there are it deletes them and returns true. It returns false when there are no selections.
Copy link
Member

Choose a reason for hiding this comment

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

Can we please brake this line at the "usual" column (e.g. 80) or at least nearly as long as the rest of the comment lines in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

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.

Delete(Sub)?Word does not respect selection ranges

2 participants