Text Editor: Auto Close Pairs on Selected Text #111900

Manually merged
Campbell Barton merged 9 commits from Matheus-Santos/blender:text_wrap_selection into main 2023-09-14 09:33:22 +02:00
Contributor

When the auto-close preference is enabled & brackets or quotes are entered with a selection,
the selection is surrounded by those characters - instead of replacing the selection.

Match functionality from visual-studio code.

When the auto-close preference is enabled & brackets or quotes are entered with a selection, the selection is surrounded by those characters - instead of replacing the selection. Match functionality from visual-studio code.
Matheus Santos added 1 commit 2023-09-03 21:40:13 +02:00
Iliya Katushenock added this to the Python API project 2023-09-03 21:47:37 +02:00
Iliya Katushenock added the
Interest
Text Editor
label 2023-09-03 21:47:41 +02:00
Pratik Borhade reviewed 2023-09-06 14:23:28 +02:00
Pratik Borhade left a comment
Member

Hi, thanks for the PR. Looks correct so far.
Patch fails when selected string contains only empty spaces.

Hi, thanks for the PR. Looks correct so far. Patch fails when selected string contains only empty spaces.
@ -3560,0 +3583,4 @@
txt_move_to(st->text, BLI_findindex(&st->text->lines, curl), curc, false);
}
else if (curl == sell) {
int ch = curc > selc ? curc : selc;
Member

can be const?

can be `const`?
Author
Contributor

Yes, it can. Should I update it?

Yes, it can. Should I update it?
Member

yes, that'd be good :)

Patch fails when selected string contains only empty spaces.

Also consider this case. I expect it not surround spaces with auto close character

yes, that'd be good :) > Patch fails when selected string contains only empty spaces. Also consider this case. I expect it not surround spaces with auto close character
Author
Contributor

I've made the requested changes. Now when there are only white spaces it will replace the selection

I've made the requested changes. Now when there are only white spaces it will replace the selection
PratikPB2123 marked this conversation as resolved
Member
cc @pablovazquez
Member

Haven't tested it yet but I love this feature, absolute +1

Thanks!

Haven't tested it yet but I love this feature, absolute +1 Thanks!
Matheus Santos added 2 commits 2023-09-07 13:17:22 +02:00
Campbell Barton requested changes 2023-09-12 07:55:16 +02:00
Campbell Barton left a comment
Owner

There is a bug when this option is enabled. Selecting a word and pressing a key no longer removes the selected text first.

There is a bug when this option is enabled. Selecting a word and pressing a key no longer removes the selected text first.
@ -98,0 +103,4 @@
* \param line2_char: The character number of line2.
* \return True if there is at least one non whitespace character.
*/
static bool text_test_has_non_whitespace(TextLine *line1,

text_test_has_non_whitespace could be negated and renamed to text_span_is_blank(...) I think it's a bit easier to follow.

`text_test_has_non_whitespace` could be negated and renamed to `text_span_is_blank(...)` I think it's a bit easier to follow.
Matheus-Santos marked this conversation as resolved
@ -3529,6 +3575,10 @@ static int text_insert_invoke(bContext *C, wmOperator *op, const wmEvent *event)
SpaceText *st = CTX_wm_space_text(C);
uint auto_close_char = 0;
int ret;
TextLine *sell = nullptr;

These are generically named variables for something fairly spesific.

I'd prefer these use a local struct.

e.g.

  /* Variables needed to restore the selection when auto-closing around an existing selection. */
  struct {
    TextLine *sell;
    TextLine *curl;
    int selc;
    int curc;
  } auto_close_select = {nullptr};
These are generically named variables for something fairly spesific. I'd prefer these use a local struct. e.g. ``` /* Variables needed to restore the selection when auto-closing around an existing selection. */ struct { TextLine *sell; TextLine *curl; int selc; int curc; } auto_close_select = {nullptr}; ```
Matheus-Santos marked this conversation as resolved
Matheus Santos added 3 commits 2023-09-13 00:59:19 +02:00
Author
Contributor

@ideasman42 I forgot to separate it in a different commit, but the bug is also solved now.

@ideasman42 I forgot to separate it in a different commit, but the bug is also solved now.
Campbell Barton requested changes 2023-09-13 01:19:39 +02:00
@ -96,2 +96,4 @@
}
/**
* This function receives a range and returns true if there is at least one non whitespace

Doc string needs updating, by convention each \param starts on a newline.

Doc string needs updating, by convention each `\param` starts on a newline.
Matheus-Santos marked this conversation as resolved
@ -3551,0 +3602,4 @@
if (txt_has_sel(st->text) &&
!text_span_is_blank(st->text->sell, st->text->selc, st->text->curl, st->text->curc) &&
text_closing_character_pair_get(auto_close_char) != 0)

text_closing_character_pair_get is likely to be a cheaper check than text_span_is_blank, better check that first.

`text_closing_character_pair_get` is likely to be a cheaper check than `text_span_is_blank`, better check that first.
Matheus-Santos marked this conversation as resolved
@ -3551,0 +3610,4 @@
auto_close_select.curc = st->text->curc;
/* Move the cursor to the start of the selection. */
if (txt_get_span(auto_close_select.curl, auto_close_select.sell) > 0 ||

txt_get_span(auto_close_select.curl, auto_close_select.sell) is called multiple times, should the result be stored in auto_close_select ?

`txt_get_span(auto_close_select.curl, auto_close_select.sell)` is called multiple times, should the result be stored in `auto_close_select` ?
Author
Contributor

Added that, it looks a lot cleaner.

Added that, it looks a lot cleaner.
Matheus-Santos marked this conversation as resolved
Matheus Santos added 3 commits 2023-09-13 13:12:52 +02:00
Campbell Barton manually merged commit 96339fc313 into main 2023-09-14 09:33:22 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
Asset Browser Project
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
4 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#111900
No description provided.