Skip to content

fix(bug): correct multi-byte character handling in get_visual_selection#126

Merged
YousefHadder merged 4 commits intomainfrom
fix/visual-selection-multibyte
Nov 14, 2025
Merged

fix(bug): correct multi-byte character handling in get_visual_selection#126
YousefHadder merged 4 commits intomainfrom
fix/visual-selection-multibyte

Conversation

@YousefHadder
Copy link
Copy Markdown
Owner

Description

Fixes issue #124 where get_visual_selection returns the first byte of the last character instead of the complete character when dealing with multi-byte characters (e.g., Chinese, emoji).

Changes

  • Added get_char_end_byte() helper function to properly convert byte positions for multi-byte characters
  • Updated get_visual_selection() to handle multi-byte characters in both active and previous visual selections
  • Uses Neovim's native vim.str_utfindex and vim.str_byteindex APIs for robust UTF-8 handling
  • Added comprehensive test coverage (106+ lines of tests)

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Code refactoring
  • Performance improvement

Related Issues

Fixes #124

Testing

  • Tested manually with Chinese characters
  • Added/updated tests (13 new test cases)
  • All 94 tests pass

Checklist

  • Code follows project style
  • Self-reviewed my code
  • Commented complex logic
  • Updated documentation (if needed)
  • No new warnings generated

Copy link
Copy Markdown
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 fixes a bug where get_visual_selection incorrectly handled multi-byte characters (e.g., Chinese, emoji) by returning only the first byte of the last character instead of the complete character. The fix ensures proper UTF-8 handling using Neovim's native APIs.

  • Added get_char_end_byte() helper function to convert byte positions for multi-byte characters
  • Updated get_visual_selection() to properly handle multi-byte characters in both active and previous visual selections
  • Added comprehensive test coverage with 13 new test cases covering ASCII, multi-byte, and mixed content scenarios

Reviewed Changes

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

File Description
lua/markdown-plus/utils.lua Added get_char_end_byte() helper and updated get_visual_selection() to adjust end_col for multi-byte characters in both active and previous visual modes
spec/markdown-plus/utils_spec.lua Added 13 test cases covering get_char_end_byte() function and visual selection with multi-byte characters

@YousefHadder YousefHadder merged commit 0b74213 into main Nov 14, 2025
14 checks passed
@YousefHadder YousefHadder deleted the fix/visual-selection-multibyte branch November 14, 2025 20:30
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.

[BUG] error in toggle_format for multi-byte char

3 participants