| 812 | } |
| 813 | |
| 814 | void CodeEditor::shiftSelected(int amount) |
| 815 | { |
| 816 | auto origCursor = textCursor(); |
| 817 | origCursor.setKeepPositionOnInsert(true); |
| 818 | QTextCursor cursor = textCursor(); |
| 819 | QTextBlock block = document()->findBlock(cursor.selectionStart()); |
| 820 | QTextBlock endblock = document()->findBlock(cursor.selectionEnd()); |
| 821 | bool atBlockStart = cursor.selectionEnd() == endblock.position(); |
| 822 | if (block==endblock || !atBlockStart) |
| 823 | endblock = endblock.next(); |
| 824 | cursor.beginEditBlock(); |
| 825 | do { |
| 826 | auto position = block.position(); |
| 827 | cursor.setPosition(position); |
| 828 | auto indentation = 0; |
| 829 | for (auto c : block.text()) { |
| 830 | if (c == '\t') { |
| 831 | indentation = indentSize * ((indentation + indentSize) / indentSize); |
| 832 | } else if (c.isSpace()) { |
| 833 | indentation++; |
| 834 | } else { |
| 835 | break; |
| 836 | } |
| 837 | position++; |
| 838 | } |
| 839 | cursor.setPosition(position, QTextCursor::KeepAnchor); |
| 840 | auto newIndent = std::max(0, indentation + indentSize * amount); |
| 841 | if (useTabs) { |
| 842 | cursor.insertText( |
| 843 | QString("\t").repeated(newIndent / indentSize) + |
| 844 | QString(" ").repeated(newIndent % indentSize)); |
| 845 | } else { |
| 846 | cursor.insertText(QString(" ").repeated(newIndent)); |
| 847 | } |
| 848 | block = block.next(); |
| 849 | } while (block.isValid() && block != endblock); |
| 850 | cursor.endEditBlock(); |
| 851 | origCursor.setKeepPositionOnInsert(false); |
| 852 | setTextCursor(origCursor); |
| 853 | } |