Page MenuHome

Adding 'Trim Whitespace' and 'Change Case' to Text Editor
Needs ReviewPublic

Authored by Peter Fog (tintwotin) on Jun 20 2019, 6:55 AM.

Details

Summary

This patch adds a text_editor.py file to the \scripts\startup\bl_operators folder, which allows for adding operators in Python. Most of the editors in Blender are using this option, but so far not the Text Editor, which is surprising, considering the advantages Python has when it comes to dealing with strings.

Two operators are included:

  • Trim Whitespaces: Trailing, Leading and Trailing and Leading.
  • Change Case: UPPER CASE, lower case, Titel Case, Capitalize case, snake_case and CamelCase

Trim Whitespaces added to the Format menu:

Change Case has been added to the Format menu:

Diff Detail

Event Timeline

Campbell Barton (campbellbarton) requested changes to this revision.Jun 24 2019, 2:27 PM

Using the clipboard for text tools is a hack which could interfear with the users workflow (they could have some rich text in the clipboard - unrelated to using Blender, which gets overwritten by using this tool).

I think it would be best to extend the Python API so a range (typically the selection) can be operated on, so the text editors undo system can properly track the change.

Once this is done some parts of the text editor could be implemented in Python cleanly.

This revision now requires changes to proceed.Jun 24 2019, 2:27 PM

If my memory serves me correct, working directly on the selection was not reliable, which made me use the clipboard instead.

So I agree. A few more functions added to the API would make script writing for the TE much simpler, like if text.caret_line_index and text.caret_column_index could be exposed as props that can be manipulated by add-on developers. The TE undo seems funky too, if ex. you have done some 3d view operations inbetween your text editing(but I'll have to do a proper report on that).

Are there any objections on suggesting additional TE functions in python operators this way?

If my memory serves me correct, working directly on the selection was not reliable, which made me use the clipboard instead.

Nevertheless, improving the API is the way to go.

So I agree. A few more functions added to the API would make script writing for the TE much simpler, like if text.caret_line_index and text.caret_column_index could be exposed as props that can be manipulated by add-on developers. The TE undo seems funky too, if ex. you have done some 3d view operations inbetween your text editing(but I'll have to do a proper report on that).
Are there any objections on suggesting additional TE functions in python operators this way?

Not sure it helps to suggest other functions now, first we would need a way to implement them cleanly.

So the first step is to locate bugs and shortcomings of the TE API? I guess no one knows this better than @Jacques Lucke (JacquesLucke)?

These python operators in this commit properly can't be fixed without the API change you're suggesting.

But there are others like the ones by Kaio:
https://github.com/K-410/blender-scripts/blob/master/2.8/text_insert2.py

Demo gif:

https://github.com/K-410/blender-scripts/blob/master/2.8/text_move_toggle.py
https://github.com/K-410/blender-scripts/blob/master/2.8/text_toggle_comment.py
https://github.com/K-410/blender-scripts/blob/master/2.8/text_copy2.py

He was the one mentioning text.caret_line_index and text.caret_column_index as important missing functions from the api, if the TE add-ons should have cleaner code.

@Campbell Barton (campbellbarton) For some TE API suggestions Kaio/Iceythe writes:

"As for API requests:

There is currently no elegant way of setting the cursor in the text editor. There exists region_location_from_cursor() and bpy.ops.text.cursor_set(), both which operate on pixel coordinates and aren’t reliable for getting and setting arbitrary positions, especially when the cursor or destination is off-screen.
A cursor RNA prop definition with both read/write capabilities or a function to set cursor position by line/column is what we need.

Getting the selected text should be made easier, either by having a function like text.get_selection() or accessing a property like text.selection. As of now it takes around 20-25 lines of code just to get multi-line selection as a raw string.

Campbell not too long ago added a feature to make an input field active when a menu or panel was called, which is what the new Rename Active Item does. In python we can now utilize this by setting activate_init to True before defining the UILayout prop.
The operator bpy.ops.text.jump(), CTRL J, should utilize this behavior, too, and have its line property compatible with the new activate_init so we don’t have to click the input field. activate_init currently only appears to work for StringProperty types so it needs to be made compatible with other kinds of properties like ints and floats."

This is properly not the best place to discuss this, nor is this: https://blenderartists.org/t/how-would-you-show-some-love-for-the-blender-text-editor/1163857/25
I'm not able to code C, so I don't know to proceed with this?

@Campbell Barton (campbellbarton) There is actually an old patch which I think may solve the API issues you mention: D1613

Peter Fog (tintwotin) retitled this revision from Adding 'Trim Whitespaces', 'Split & Join Line' and 'Change Case' to Text Editor to Adding 'Trim Whitespace' and 'Change Case' to Text Editor .Jul 31 2019, 1:10 PM
Peter Fog (tintwotin) edited the summary of this revision. (Show Details)
Peter Fog (tintwotin) updated this revision to Diff 16713.

Updated the diff: Using clipboard code has been removed, instead code to get selection by @Dalai Felinto (dfelinto) has been used: https://developer.blender.org/diffusion/BAC/browse/master/text_editor_hastebin.py If/when this function will be added to the API this def can be removed.

Removed split/join lines, to keep it simple. So now it is only Remove Whitespace and Change Case included in the patch.
'Titel Case', has been added.

Peter Fog (tintwotin) edited the summary of this revision. (Show Details)Jul 31 2019, 8:38 PM
Peter Fog (tintwotin) updated this revision to Diff 16819.

Change info about whitespace trimmed lines from popover to report in statusbar.