forked from qt-creator/qt-creator
Fix a bug with selection paths in the editor overlay.
If the first or last line in a selection ended up with nothing to highlight after whitespaces were trimmed, they'd use a codepath that only works correctly for inner 'empty' lines. Instead we now make sure the beginning and end of a selection are always highlighted.
This commit is contained in:
@@ -151,12 +151,14 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
|||||||
QTextLayout *blockLayout = block.layout();
|
QTextLayout *blockLayout = block.layout();
|
||||||
|
|
||||||
QTextLine line = blockLayout->lineAt(0);
|
QTextLine line = blockLayout->lineAt(0);
|
||||||
|
bool firstOrLastBlock = false;
|
||||||
|
|
||||||
int beginChar = 0;
|
int beginChar = 0;
|
||||||
if (!inSelection) {
|
if (!inSelection) {
|
||||||
beginChar = begin.position() - begin.block().position();
|
beginChar = begin.position() - begin.block().position();
|
||||||
line = blockLayout->lineForTextPosition(beginChar);
|
line = blockLayout->lineForTextPosition(beginChar);
|
||||||
inSelection = true;
|
inSelection = true;
|
||||||
|
firstOrLastBlock = true;
|
||||||
} else {
|
} else {
|
||||||
while (beginChar < block.length() && document->characterAt(block.position() + beginChar).isSpace())
|
while (beginChar < block.length() && document->characterAt(block.position() + beginChar).isSpace())
|
||||||
++beginChar;
|
++beginChar;
|
||||||
@@ -170,6 +172,7 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
|||||||
endChar = end.position() - end.block().position();
|
endChar = end.position() - end.block().position();
|
||||||
lastLine = blockLayout->lineForTextPosition(endChar).lineNumber();
|
lastLine = blockLayout->lineForTextPosition(endChar).lineNumber();
|
||||||
inSelection = false;
|
inSelection = false;
|
||||||
|
firstOrLastBlock = true;
|
||||||
} else {
|
} else {
|
||||||
endChar = block.length();
|
endChar = block.length();
|
||||||
while (endChar > beginChar && document->characterAt(block.position() + endChar - 1).isSpace())
|
while (endChar > beginChar && document->characterAt(block.position() + endChar - 1).isSpace())
|
||||||
@@ -191,9 +194,17 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
|||||||
selection += lineRect.translated(blockGeometry.topLeft());
|
selection += lineRect.translated(blockGeometry.topLeft());
|
||||||
}
|
}
|
||||||
} else { // empty lines
|
} else { // empty lines
|
||||||
if (!selection.isEmpty())
|
const int emptyLineSelectionSize = 16;
|
||||||
|
if (!firstOrLastBlock && !selection.isEmpty()) { // middle
|
||||||
lineRect.setLeft(selection.last().left());
|
lineRect.setLeft(selection.last().left());
|
||||||
lineRect.setRight(lineRect.left() + 16);
|
} else if (inSelection) { // first line
|
||||||
|
lineRect.setLeft(line.cursorToX(beginChar));
|
||||||
|
} else { // last line
|
||||||
|
if (endChar == 0)
|
||||||
|
break;
|
||||||
|
lineRect.setLeft(line.cursorToX(endChar) - emptyLineSelectionSize);
|
||||||
|
}
|
||||||
|
lineRect.setRight(lineRect.left() + emptyLineSelectionSize);
|
||||||
selection += lineRect.translated(blockGeometry.topLeft());
|
selection += lineRect.translated(blockGeometry.topLeft());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user