Page MenuHome

Text editor api: Add selection RNA
ClosedPublic

Authored by Kai Jægersen (kaio) on Tue, Oct 1, 4:13 AM.

Details

Summary

Also a proposed fix for https://developer.blender.org/T65843

  • Add unicode corrected rna getter/setter for text properties current_character and select_end_character

Getter value is converted to unicode size and should display correct values.
Setter converts the input value to bytes size before updating the internal curc and selc.

  • Add select_end_line_index with get/set
  • Add cursor_set(line, character=0) function for non-selection cursor positioning
  • Change current_line_index setter to drag selection (use cursor_set instead)
  • Include selection_set()

Diff Detail

Event Timeline

This revision is now accepted and ready to land.Tue, Oct 1, 8:43 AM
Campbell Barton (campbellbarton) requested changes to this revision.EditedTue, Oct 1, 8:58 AM

Would prefer this be part of a commit that makes selection fully editable, so we can ensure this works well.

If there are any issues with making these values editable, we'd be better off having a method to set the selection.

select_start_line, select_end_line are currently read-only.

This revision now requires changes to proceed.Tue, Oct 1, 8:58 AM
source/blender/blenkernel/intern/text.c
1034–1043

Why not use BLI_str_utf8_offset_to_index ?

source/blender/blenkernel/intern/text.c
1034–1043

Correction BLI_str_utf8_offset_from_index

Kai Jægersen (kaio) retitled this revision from Text editor api: Add RNA get/set for current_character and select_end_character to Text editor api: Add selection RNA.Wed, Oct 2, 12:23 AM
Kai Jægersen (kaio) edited the summary of this revision. (Show Details)
Kai Jægersen (kaio) updated this revision to Diff 18705.EditedWed, Oct 2, 12:31 AM

Updated to include the context of selection api.

Changes since last time

  • Use BLI_str_utf8_offset_from_index for getting byte offset from utf8 index.
  • Add select_end_line_index with get/set. Changing this drags the cursor end of selection
  • Add cursor_set(line, col=0) for non-selection cursor positioning
  • current_line_index setter changed from setting cursor to drag selection (use cursor_set instead)
Kai Jægersen (kaio) marked 2 inline comments as done.Wed, Oct 2, 12:37 AM

Would prefer this be part of a commit that makes selection fully editable, so we can ensure this works well.
If there are any issues with making these values editable, we'd be better off having a method to set the selection.
select_start_line, select_end_line are currently read-only.

Makes sense. I've set this as the lead diff.
Selection should now be manually editable using the four existing props

current_line_index, current_character, select_end_line_index and select_end_character

They control the range selection, so to move the cursor two steps inline as a whole one would need to write:

text.current_character += 2
text.select_end_character += 2

To move the cursor as a whole, it's better to use text.cursor_set() which takes a line and optionally a char index.

For setting a range selection, it's more convenient using text.selection_set(1, 2, 3, 4) than typing out four lines since the property names can get long.

Campbell Barton (campbellbarton) requested changes to this revision.Wed, Oct 2, 9:56 AM
Campbell Barton (campbellbarton) added inline comments.
source/blender/blenkernel/intern/text.c
1034–1043

BLI_str_utf8_offset_from_index should be able to replace txt_move_to_utf8 completely.

This revision now requires changes to proceed.Wed, Oct 2, 9:56 AM
source/blender/blenkernel/intern/text.c
1034–1043

BLI_str_utf8_offset_from_index doesn't do bounds checking. That's what this is doing.

Afaics there's two ways to replace it if you want it gone.

  1. Clamp index in 3 functions. Which means duplicate code in 3 places.
  2. Clamp index in txt_sel_set, but have all rna setters call that.
  • Using TextLine->len to get max clamp for line lengths
  • Remove function, use BLI_str_utf8_offset_from_index
  • Remove pointer check in txt_sel_set
Campbell Barton (campbellbarton) requested changes to this revision.Thu, Oct 3, 9:02 PM
Campbell Barton (campbellbarton) added inline comments.
source/blender/blenkernel/intern/text.c
1247–1248

This isn't correct since startc/endc are character indices where as len is a byte offset.

This revision now requires changes to proceed.Thu, Oct 3, 9:02 PM
  • Fix wrong clamp max values
Kai Jægersen (kaio) marked an inline comment as done.Thu, Oct 3, 9:22 PM
Kai Jægersen (kaio) marked an inline comment as done.

Committed:

Added some minor changes, support negative indices in select_set().
Avoid unnecessary listbase lookups.

This revision is now accepted and ready to land.Thu, Oct 3, 10:33 PM