UI: Word Boundary Seeking #107762

Closed
Harley Acheson wants to merge 3 commits from Harley:WordBoundaries into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

Changes to how we detect boundaries between words when selecting,
moving the cursor, deleting, etc.


When moving any text cursor position to a word boundary we always first move in the intended direction by one character without any further testing. This results in unintended bad behavior. The following shows where your cursor is moved when you use ctrl-arrow keys to move to the next word boundary. You will see that the movements shown in red are inconsistent with all other movement:

WordBoundary1.png

This incorrect boundary movement causes errors when selecting words near the ends of word positions and if the word is only a single letter long. Following shows three types of word selections that accidentally include whitespace:

WordBoundary2.gif

This happens because our "Cursor Step" functions take a use_init_step argument that forces this initial one-character movement. This argument was added so that movement between words would occur a word at a time. For example ctrl-left arrow will move from word start to previous word start, rather than word start, then word end, then word start.

However this movement should only happen when at whitespace. And we always call the "Cursor Step" functions with this set to true. So this PR just removes this argument entirely and does the initial movement only when appropriate.

Changes to how we detect boundaries between words when selecting, moving the cursor, deleting, etc. --- When moving any text cursor position to a word boundary we always first move in the intended direction by one character without any further testing. This results in unintended bad behavior. The following shows where your cursor is moved when you use ctrl-arrow keys to move to the next word boundary. You will see that the movements shown in red are inconsistent with all other movement: ![WordBoundary1.png](/attachments/547c8989-1a4e-47cb-8422-2ca84937a24e) This incorrect boundary movement causes errors when selecting words near the ends of word positions and if the word is only a single letter long. Following shows three types of word selections that accidentally include whitespace: ![WordBoundary2.gif](/attachments/af13cd1c-af00-446e-a400-f9e016cab3da) This happens because our "Cursor Step" functions take a `use_init_step` argument that forces this initial one-character movement. This argument was added so that movement between words would occur a word at a time. For example ctrl-left arrow will move from word start to previous word start, rather than word start, then word end, then word start. However this movement should only happen when at whitespace. And we always call the "Cursor Step" functions with this set to true. So this PR just removes this argument entirely and does the initial movement only when appropriate.
Harley Acheson added 1 commit 2023-05-08 22:37:31 +02:00
f440dc1328 UI: Word Boundary Seeking
Changes to how we detect boundaries between words when selecting,
moving the cursor, deleting, etc.
Harley Acheson added this to the User Interface project 2023-05-08 22:37:44 +02:00
Harley Acheson requested review from Campbell Barton 2023-05-08 22:37:51 +02:00
Campbell Barton reviewed 2023-05-09 01:27:35 +02:00
@ -145,3 +144,3 @@
if (direction == STRCUR_DIR_NEXT) {
if (use_init_step) {
if (jump == STRCUR_JUMP_DELIM && ELEM(str[*pos], ' ', '\n', '\t')) {

Use cursor_delim_type_unicode(..) instead & check for STRCUR_DELIM_WHITESPACE.

Use `cursor_delim_type_unicode(..)` instead & check for `STRCUR_DELIM_WHITESPACE`.
Campbell Barton requested changes 2023-05-09 01:46:31 +02:00
Campbell Barton left a comment
Owner

This breaks selecting white-space for the text-editor, text-fields and 3d-text (not the Python console) try add multiple spaces then double-click to select the space between words. From what I cn see the white-space check should check adjacent character is not white-space.


Also, please leave changes to double-click word selection out of this patch, the checks for ELEM(..,' ', '\n', '\t') aren't great it's probably better to have a function as part of the BLI_str_cursor_* API, then there is no need to do character level checks & forwards/backwards motion. There is also less likelihood of behavior being inconsistent for text editing fields (currently the case with this patch, maybe a simple mistake/change).

This breaks selecting white-space for the text-editor, text-fields and 3d-text (not the Python console) try add multiple spaces then double-click to select the space between words. From what I cn see the white-space check should check adjacent character is not white-space. ---- Also, please leave changes to double-click word selection out of this patch, the checks for `ELEM(..,' ', '\n', '\t')` aren't great it's probably better to have a function as part of the `BLI_str_cursor_*` API, then there is no need to do character level checks & forwards/backwards motion. There is also less likelihood of behavior being inconsistent for text editing fields _(currently the case with this patch, maybe a simple mistake/change)._
Harley Acheson added 1 commit 2023-05-10 01:54:01 +02:00
Author
Member

@ideasman42 -... selecting white-space for the text-editor, text-fields and 3d-text (not the Python console)...

Yikes, it takes quite a bit more to get consistent behavior everywhere and deal with groups of multiple whitespaces correctly. Mostly that BLI_str_cursor_step_utf8 has to seek over multiple whitespace characters rather than that single character move.

Also, please leave changes to double-click word selection out of this patch.

Lots of current code, including the double-click for word selection rely on current behavior that needs to change. This does get rid of all those whitespace checks by instead using cursor_delim_type_unicode that I make public here as BLI_str_cursor_delim_type_utf8/32.

But yeah, lots of change here to fix something that is only slightly wonky now.

@ideasman42 -... selecting white-space for the text-editor, text-fields and 3d-text (not the Python console)... Yikes, it takes quite a bit more to get consistent behavior everywhere and deal with groups of multiple whitespaces correctly. Mostly that `BLI_str_cursor_step_utf8` has to seek over multiple whitespace characters rather than that single character move. > Also, please leave changes to double-click word selection out of this patch. Lots of current code, including the double-click for word selection rely on current behavior that needs to change. This does get rid of all those whitespace checks by instead using `cursor_delim_type_unicode` that I make public here as BLI_str_cursor_delim_type_utf8/32. But yeah, lots of change here to fix something that is only slightly wonky now.
Campbell Barton requested changes 2023-05-10 03:41:20 +02:00
@ -11,6 +11,17 @@
extern "C" {
#endif
typedef enum eStrCursorDelimType {

I'd rather avoid exposing internal implementation details and instead have a BLI_str_cursor_step_range(..) or similar.

Having to duplicate this logic about seems unnecessarily involved when it can be handled by a reusable utility function.

  txt_jump_right(text, true, use_init_step);
  if (!line->line[text->curc] ||
      (BLI_str_cursor_delim_type_utf8(line->line, line->len, text->curc) ==
       STRCUR_DELIM_WHITESPACE) ||
      (text->curc > 0 && BLI_str_cursor_delim_type_utf8(line->line, line->len, text->curc - 1) !=
                             STRCUR_DELIM_WHITESPACE))
  {
    /* Move left if to left is non-whitespace or if right is whitespace. */
    txt_jump_left(text, false);
  }
  txt_jump_right(text, true);

In fact, a BLI_str_cursor_step_range(..) utility function could be added as a separate refactor (it can use the current logic, which then simplifies this patch).

I'd rather avoid exposing internal implementation details and instead have a `BLI_str_cursor_step_range(..)` or similar. Having to duplicate this logic about seems unnecessarily involved when it can be handled by a reusable utility function. ``` txt_jump_right(text, true, use_init_step); if (!line->line[text->curc] || (BLI_str_cursor_delim_type_utf8(line->line, line->len, text->curc) == STRCUR_DELIM_WHITESPACE) || (text->curc > 0 && BLI_str_cursor_delim_type_utf8(line->line, line->len, text->curc - 1) != STRCUR_DELIM_WHITESPACE)) { /* Move left if to left is non-whitespace or if right is whitespace. */ txt_jump_left(text, false); } txt_jump_right(text, true); ``` ---- In fact, a `BLI_str_cursor_step_range(..)` utility function could be added as a separate refactor (it can use the current logic, which then simplifies this patch).
Author
Member

@ideasman42 - I'd rather avoid exposing internal implementation details and instead have...

Yes, it is much nicer like that. Added BLI_str_cursor_at_word_boundary_utfx() to clean that up and it gets rid of a lot of mess there.

@ideasman42 - I'd rather avoid exposing internal implementation details and instead have... Yes, it is much nicer like that. Added BLI_str_cursor_at_word_boundary_utfx() to clean that up and it gets rid of a lot of mess there.
Harley Acheson added 1 commit 2023-05-10 18:23:37 +02:00

After some discussion this patch makes functional changes to cursor stepping (stepping over spaces works differently for e.g.) that should be made carefully and aren't directly related to resolving the issue selecting words.

Proposed solution is to add BLI_str_cursor_step_bounds_utf* which takes a point and returns a bounding range. This can be done without any changes to existing cursor stepping functions.

Internally it can step over matching character categories,

  • If pos && pos-1 categories match, seek in both directions, delimit by character category.
  • If they are different, use a priority check (where spaces happen to be lowest), and seek either forward or backwards in the direction with a higher priority: order could be something like [alpha-numeric, brackets, punctuation, space].

This way double clicking at a word boundary always prioritizes the words not the spaces.


Closing this PR as the proposed changes is quite a different direction from this one.

After some discussion this patch makes functional changes to cursor stepping (stepping over spaces works differently for e.g.) that should be made carefully and aren't directly related to resolving the issue selecting words. Proposed solution is to add `BLI_str_cursor_step_bounds_utf*` which takes a point and returns a bounding range. This can be done without any changes to existing cursor stepping functions. Internally it can step over matching character categories, - If `pos` && `pos-1` categories match, seek in both directions, delimit by character category. - If they are different, use a priority check (where spaces happen to be lowest), and seek either forward or backwards in the direction with a higher priority: order could be something like `[alpha-numeric, brackets, punctuation, space]`. This way double clicking at a word boundary always prioritizes the words not the spaces. ---- Closing this PR as the proposed changes is quite a different direction from this one.
Campbell Barton closed this pull request 2023-05-12 02:42:51 +02:00
Harley Acheson deleted branch WordBoundaries 2023-05-15 22:57:29 +02:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#107762
No description provided.