This patch fixes the problems with indentation via keyboard shortcut and with inconsistent indentation whitespaces after pasting from another source by converting all pasted indentation whitespaces into the character defined by the "use tabs as spaces" option.
Campbell Barton (campbellbarton) Sybren A. Stüvel (sybren) Brecht Van Lommel (brecht)
- Group Reviewers
- Maniphest Tasks
- T60234: Shift+Tab only works with new text
- rBb4c14faeaf6f: Text editor: convert tabs to spaces on paste
rBSb4c14faeaf6f: Text editor: convert tabs to spaces on paste
This is not a reference but a pointer. I know C doesn't really have references, but since Blender also has a large body of code in C++ we ought to be careful in the terminology.
Those while statements can be replaced by for (i=0; (*buf)[i]; i++). Every iteration i++ is executed anyway, and both while statements start with i=0.
This if/else if construct is not very trivial and repeated twice. Maybe it's a good one to split it into a small function and call it three times?
This replaces a \t with tab_size spaces, which is incorrect. It should align the text with the next tab stop. " \t" should generally result in the same indentation as "\t" (unless that one space would push it to the next tab stop, in which case it's equivalent to "\t\t".
This boolean is only used once, so just change the condition below to if (text->flags & TXT_TABSTOSPACES == 0).
This doesn't fix the main bug in T60234: Shift+Tab only works with new text, which I think is that our indent/unindent operators should work with both tabs and spaces. But we can consider this as a change on its own.
Converting tabs to spaces automatically on paste makes some sense if you interpret paste as similar to typing in the code. When using the text editor for something else than Python scripting it might be unexpected though, perhaps it's an acceptable trade-off.
I don't think it's right to convert all spaces to tabs when this option is off, probably it should do nothing then. When using tabs, you sometimes indent some part with spaces intentionally to ensure things align regardless of tab size.
The buf_spaces_to_tabs function shouldn't change indentation; so given a tab size of 4 it should convert 7 spaces to 1 tab + 3 spaces.
Mixing tabs with spaces for indentation is invalid in Python, which brings us again to when to apply these transformations (always or only when editing Python code).
Given these two problems, when tabs_to_spaces == false, should I keep the pasted text original and let the user deal with it?