Page MenuHome

Adding 'Trim Whitespaces', 'Split & Join Line' and 'Change Case' to Text Editor
Needs RevisionPublic

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.

Three operators are included:

  • Trim Whitespaces: Trailing, Leading and Trailing and Leading.
  • Split Line and Join Line.
  • Change Case: UPPERCASE, lower case, Capitalize, snake_case and CamelCase

Trim Whitespaces added to the Format menu:

Change Case has been added to the Format menu:

The line operations in the menu are collected in a sub menu containing:

  • Jump
  • Move Up
  • Move Down
  • Split
  • Join

Diff Detail

Event Timeline

Campbell Barton (campbellbarton) requested changes to this revision.Mon, Jun 24, 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.Mon, Jun 24, 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