Editors: Change indent behavior for single line selection

When only one line is selected when press the TAB key, should delete
the selected contents, then perform indent.
It like other more editor behavior.

Reviewer notes: Indeed this seems to be a common behavior in other
editors (for example Visual Studio, Code Blocks, Eclipse). But in
order to preserve the indentation consistent with the multiple-line
case it would be nice an approach like Eclipse's: If the single
line is completely selected, do the indentation as usual. Otherwise
remove the selected text.

The author of the request is not able to revise this minor change
because he claims to be busy (and actually asked for the help). So
I will keep this patch in the original state and implement the
detail I mentioned above in a following commit.

Merge-request: 252
Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
This commit is contained in:
Yuchen Deng
2011-03-16 09:41:14 +01:00
committed by Leandro Melo
parent 71baeb1d77
commit 5e81630a23

View File

@@ -4363,34 +4363,39 @@ void BaseTextEditorWidget::indentOrUnindent(bool doIndent)
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock(end-1).next();
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text();
int indentPosition = tabSettings.lineIndentPosition(text);
if (!doIndent && !indentPosition)
indentPosition = tabSettings.firstNonSpace(text);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.insertText(tabSettings.indentationString(0, targetColumn, block));
cursor.setPosition(block.position());
cursor.setPosition(block.position() + indentPosition, QTextCursor::KeepAnchor);
if (startBlock.next() == endBlock) { // Only one line selected
cursor.removeSelectedText();
} else {
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text();
int indentPosition = tabSettings.lineIndentPosition(text);
if (!doIndent && !indentPosition)
indentPosition = tabSettings.firstNonSpace(text);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.insertText(tabSettings.indentationString(0, targetColumn, block));
cursor.setPosition(block.position());
cursor.setPosition(block.position() + indentPosition, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
cursor.endEditBlock();
return;
}
cursor.endEditBlock();
} else {
// Indent or unindent at cursor position
QTextBlock block = cursor.block();
QString text = block.text();
int indentPosition = cursor.positionInBlock();
int spaces = tabSettings.spacesLeftFromPosition(text, indentPosition);
int startColumn = tabSettings.columnAt(text, indentPosition - spaces);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.setPosition(block.position() + indentPosition - spaces, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(tabSettings.indentationString(startColumn, targetColumn, block));
cursor.endEditBlock();
setTextCursor(cursor);
}
// Indent or unindent at cursor position
QTextBlock block = cursor.block();
QString text = block.text();
int indentPosition = cursor.positionInBlock();
int spaces = tabSettings.spacesLeftFromPosition(text, indentPosition);
int startColumn = tabSettings.columnAt(text, indentPosition - spaces);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.setPosition(block.position() + indentPosition - spaces, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(tabSettings.indentationString(startColumn, targetColumn, block));
cursor.endEditBlock();
setTextCursor(cursor);
}
void BaseTextEditorWidget::handleHomeKey(bool anchor)