# HG changeset patch # User Torsten # Date 1511543677 -3600 # Node ID ae7948b6c01761a819b7e9bd93279974cb88ebeb # Parent 5b7f99dfdab32934e7302c6a08976e28aca889de do not change selection when (un)commenting lines in the editor () * file-editor-tab.cc (do_comment_selected_tex): restore previous selection after (un)commenting lines in the editor diff -r 5b7f99dfdab3 -r ae7948b6c017 libgui/src/m-editor/file-editor-tab.cc --- a/libgui/src/m-editor/file-editor-tab.cc Tue Nov 21 21:35:32 2017 +0100 +++ b/libgui/src/m-editor/file-editor-tab.cc Fri Nov 24 18:14:37 2017 +0100 @@ -1375,13 +1375,18 @@ { QString comment_str = comment_string (_edit_area->lexer ()->lexer ()); QRegExp rxc = QRegExp ("^([ \\t]*)" + comment_str); - int len, lenc = comment_str.length (); + int len = 0; + int lenc = comment_str.length (); _edit_area->beginUndoAction (); if (_edit_area->hasSelectedText ()) { int lineFrom, lineTo, colFrom, colTo; + int change_col_from = 1; + int change_col_to = 1; + bool removed; + _edit_area->getSelection (&lineFrom, &colFrom, &lineTo, &colTo); if (colTo == 0) // the beginning of last line is not selected @@ -1390,21 +1395,47 @@ for (int i = lineFrom; i <= lineTo; i++) { if (comment) - _edit_area->insertAt (comment_str, i, 0); + { + _edit_area->insertAt (comment_str, i, 0); + + } else { QString line (_edit_area->text (i)); - if (line.contains (rxc)) + if ((removed = line.contains (rxc))) { len = rxc.matchedLength (); _edit_area->setSelection (i, len-lenc, i, len); _edit_area->removeSelectedText (); } + + // handle case, where the selection remains unchanged + if (i == lineFrom && (colFrom < len-lenc || ! removed)) + change_col_from = 0; // do not change start of selection + if (i == lineTo && (colTo < len-lenc || ! removed)) + change_col_to = 0; // do not change end of selection } } - //set selection on (un)commented section - _edit_area->setSelection (lineFrom, 0, lineTo, - _edit_area->text (lineTo).length ()-1); + + // update the selection area + if (comment) + { + colFrom = colFrom + lenc; // shift start position by comment length + if (colTo > 0) + colTo = colTo + lenc; // shift end position by comment length + else + lineTo++; // colTo == 0 , fully select previous line + } + else + { + if (colTo == 0) + lineTo++; // colTo == 0 , fully select previous line + colFrom = colFrom - change_col_from*lenc; + colTo = colTo - change_col_to*lenc; + } + + // set updated selection area + _edit_area->setSelection (lineFrom, colFrom, lineTo, colTo); } else {