@@ -1116,8 +1116,6 @@ int Editor::tabBlockShift()
11161116 int curbeg = cur.selectionStart ();
11171117 int curend = cur.selectionEnd ();
11181118
1119- if (curbeg > curend) std::swap (curbeg, curend);
1120-
11211119 /* create workable selection */
11221120 cur.setPosition (curbeg, QTextCursor::MoveAnchor);
11231121 cur.movePosition (QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
@@ -1126,9 +1124,6 @@ int Editor::tabBlockShift()
11261124 /* get a list of lines in the selected block. keep empty lines */
11271125 QStringList mylist = cur.selectedText ().split (QChar::ParagraphSeparator);
11281126
1129- /* start a single undo/redo operation */
1130- cur.beginEditBlock ();
1131-
11321127 /* indent list */
11331128 QString text;
11341129
@@ -1139,7 +1134,7 @@ int Editor::tabBlockShift()
11391134 /* ignore empty last line */
11401135 if (size == 0 && n == mylist.length ()) break ;
11411136
1142- if (!shiftTab) s.insert (0 , tab); // indent line
1137+ if (!shiftTab) s.insert (0 , tab); // increase line indent
11431138 else if (s.startsWith (tab)) s.remove (0 , tabSpaces); // decrease line indent
11441139 else s.replace (QRegExp (" ^ *" ), " " ); // remove leading spaces
11451140
@@ -1155,52 +1150,44 @@ int Editor::tabBlockShift()
11551150 if (n < mylist.length ()) text += QChar::ParagraphSeparator;
11561151 }
11571152 /* insert new block */
1158- cur.insertText (text);
1153+ if (cur.selectedText ().length () != text.length ()) // avoid empty undo actions
1154+ cur.insertText (text);
11591155
11601156 /* update selection */
11611157 cur.setPosition (curbeg, QTextCursor::MoveAnchor);
11621158 cur.setPosition (curend, QTextCursor::KeepAnchor);
11631159
1164- /* end single undo/redo operation */
1165- cur.endEditBlock ();
1166- }
1167- /* a single-line selection */
1168- else {
1169- int selectStart = cur.selectionStart (); /* for resetting selection when done */
1170- int selectEnd = cur.selectionEnd ();
1171- int selectLength = selectEnd - selectStart;
1172- cur.setPosition (selectStart,QTextCursor::MoveAnchor); /* reset cursor to start of selection */
1173- int column = cur.columnNumber (); /* column to start insertion */
1174- int cursorPos = cur.position (); /* cursor position within text */
1175-
1176- if (cursorPos > -1 ) {
1177- if (!shiftTab) {
1178- for (int n = column % tabSpaces; n < tabSpaces; n++) {
1179- cur.insertText (" " );
1180- cursorPos++;
1181- }
1182- cur.setPosition (cursorPos,QTextCursor::MoveAnchor); /* reset selection */
1183- cur.movePosition (QTextCursor::Right,QTextCursor::MoveAnchor, selectLength);
1184- cur.movePosition (QTextCursor::Left,QTextCursor::KeepAnchor, selectLength);
1185- }
1186- else if (cur.columnNumber () != 0 ) {
1187- QString st;
1188- for (int n = column % tabSpaces; n < tabSpaces; n++) {
1189- cur.movePosition (QTextCursor::Left,QTextCursor::KeepAnchor);
1190- st = cur.selectedText ();
1191- if (st.length () == 1 && st.at (0 ) == ' ' ) {
1192- cur.removeSelectedText ();
1193- if (cur.columnNumber () % tabSpaces == 0 )
1194- break ;
1195- }
1196- if (st.at (0 ) != ' ' )
1197- cur.movePosition (QTextCursor::Right,QTextCursor::MoveAnchor);
1198- }
1199- /* reset selection */
1200- cur.movePosition (QTextCursor::Right,QTextCursor::MoveAnchor, selectLength);
1201- cur.movePosition (QTextCursor::Left,QTextCursor::KeepAnchor, selectLength);
1202- }
1203- }
1160+ } else if (!shiftTab) {
1161+ int column = cur.columnNumber () + (cur.selectionStart () - cur.position ());
1162+ cur.insertText (QString (tabSpaces - column % tabSpaces, ' ' ));
1163+ } else {
1164+ /* determine current selection */
1165+ int curbeg = cur.selectionStart ();
1166+ int curend = cur.selectionEnd ();
1167+
1168+ /* create workable selection */
1169+ cur.setPosition (curbeg, QTextCursor::MoveAnchor);
1170+ cur.movePosition (QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
1171+ cur.movePosition (QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
1172+
1173+ /* indent line */
1174+ QString line = cur.selectedText ();
1175+ int size = line.length ();
1176+
1177+ if (line.startsWith (tab)) line.remove (0 , tabSpaces); // decrease line indent
1178+ else line.replace (QRegExp (" ^ *" ), " " ); // remove leading spaces
1179+
1180+ /* adjust selection */
1181+ curbeg = std::max (curbeg - size + line.length (), cur.selectionStart ());
1182+ curend = std::max (curend - size + line.length (), cur.selectionStart ());
1183+
1184+ /* insert new line */
1185+ if (cur.selectedText ().length () != line.length ()) // avoid empty undo actions
1186+ cur.insertText (line);
1187+
1188+ /* update selection */
1189+ cur.setPosition (curbeg, QTextCursor::MoveAnchor);
1190+ cur.setPosition (curend, QTextCursor::KeepAnchor);
12041191 }
12051192 this ->setTextCursor (cur);
12061193
0 commit comments