diff options
author | Friedemann Kleint <Friedemann.Kleint@nokia.com> | 2009-09-30 17:16:25 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@nokia.com> | 2009-09-30 17:16:25 +0200 |
commit | 913b1bf02f1e55e182f48516a2f33a41ab2c8113 (patch) | |
tree | 4b276a50d41843238d10e496ecfefb67b2d353a2 /src/plugins | |
parent | 8cc94d10a2aa1b9949ac0fd4c2fbed05376cec68 (diff) | |
parent | 6d0e9badfba5b2c2f942b8517f5118803c4b2b75 (diff) | |
download | qt-creator-913b1bf02f1e55e182f48516a2f33a41ab2c8113.tar.gz |
Merge branch 'master' of git@scm.dev.nokia.troll.no:creator/mainline
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/cppeditor/cppeditor.cpp | 57 | ||||
-rw-r--r-- | src/plugins/texteditor/basetexteditor.cpp | 51 |
2 files changed, 69 insertions, 39 deletions
diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index da69d19236..9771328c5b 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -1537,28 +1537,43 @@ int CPPEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor) else braceDepth= 0; - if (braceDepth > 0) { // we do have an extra brace, let's close it - int pos = cursor.position(); - - MatchingText matchingText; - const QString textToInsert = matchingText.insertParagraphSeparator(cursor); - - cursor.insertText(textToInsert); - cursor.setPosition(pos); - const TabSettings &ts = tabSettings(); - if (ts.m_autoIndent) { - cursor.insertBlock(); - indent(document(), cursor, QChar::Null); - } else { - QString previousBlockText = cursor.block().text(); - cursor.insertBlock(); - cursor.insertText(ts.indentationString(previousBlockText)); - } - cursor.setPosition(pos); - m_allowSkippingOfBlockEnd = true; - return 1; + if (braceDepth <= 0) + return 0; // braces are all balanced or worse, no need to do anything + + // we have an extra brace , let's see if we should close it + + + /* verify that the next block is not further intended compared to the current block. + This covers the following case: + + if (condition) {| + statement; + */ + const TabSettings &ts = tabSettings(); + QTextBlock block = cursor.block(); + int indentation = ts.indentationColumn(block.text()); + if (block.next().isValid() + && ts.indentationColumn(block.next().text()) > indentation) + return 0; + + int pos = cursor.position(); + + MatchingText matchingText; + const QString textToInsert = matchingText.insertParagraphSeparator(cursor); + + cursor.insertText(textToInsert); + cursor.setPosition(pos); + if (ts.m_autoIndent) { + cursor.insertBlock(); + indent(document(), cursor, QChar::Null); + } else { + QString previousBlockText = cursor.block().text(); + cursor.insertBlock(); + cursor.insertText(ts.indentationString(previousBlockText)); } - return 0; + cursor.setPosition(pos); + m_allowSkippingOfBlockEnd = true; + return 1; } bool CPPEditor::contextAllowsAutoParentheses(const QTextCursor &cursor, diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 13461fe7c7..9b4a4f0edf 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -4509,11 +4509,12 @@ QMimeData *BaseTextEditor::createMimeDataFromSelection() const const TabSettings &ts = d->m_document->tabSettings(); bool startOk = ts.cursorIsAtBeginningOfLine(selstart); - bool endOk = (selend.block() != selstart.block() && ts.cursorIsAtBeginningOfLine(selend)); + bool multipleBlocks = (selend.block() != selstart.block()); - if (startOk && endOk) { + if (startOk && multipleBlocks) { selstart.movePosition(QTextCursor::StartOfBlock); - selend.movePosition(QTextCursor::StartOfBlock); + if (ts.cursorIsAtBeginningOfLine(selend)) + selend.movePosition(QTextCursor::StartOfBlock); cursor.setPosition(selstart.position()); cursor.setPosition(selend.position(), QTextCursor::KeepAnchor); text = cursor.selectedText(); @@ -4583,29 +4584,43 @@ void BaseTextEditor::insertFromMimeData(const QMimeData *source) cursor.beginEditBlock(); - if (ts.cursorIsAtBeginningOfLine(cursor) + bool insertAtBeginningOfLine = ts.cursorIsAtBeginningOfLine(cursor); + + if (insertAtBeginningOfLine && source->hasFormat(QLatin1String("application/vnd.nokia.qtcreator.blocktext"))) { text = QString::fromUtf8(source->data(QLatin1String("application/vnd.nokia.qtcreator.blocktext"))); if (text.isEmpty()) return; + } - cursor.removeSelectedText(); + cursor.removeSelectedText(); - int bpos = cursor.block().position(); - int pos = cursor.position() - bpos; + int reindentBlockStart = cursor.blockNumber() + (insertAtBeginningOfLine?0:1); - cursor.setPosition(bpos); // since we'll add a final newline, preserve current line's indentation + bool hasFinalNewline = (text.endsWith(QLatin1Char('\n')) + || text.endsWith(QChar::ParagraphSeparator) + || text.endsWith(QLatin1Char('\r'))); - cursor.insertText(text); - pos = cursor.position(); - cursor.setPosition(bpos); - cursor.setPosition(pos, QTextCursor::KeepAnchor); - cursor.setPosition(cursor.position()-1, QTextCursor::KeepAnchor); - reindent(document(), cursor); - cursor.clearSelection(); - cursor.setPosition(cursor.position()+1); // skip newline - } else { - cursor.insertText(text); + QTextCursor unnecessaryWhitespace; + if (hasFinalNewline) { + // since we'll add a final newline, preserve current line's indentation + unnecessaryWhitespace = cursor; + unnecessaryWhitespace.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor); + cursor.setPosition(unnecessaryWhitespace.position()); + } + + cursor.insertText(text); + + int reindentBlockEnd = cursor.blockNumber() - (hasFinalNewline?1:0); + + if (reindentBlockStart <= reindentBlockEnd) { + if (insertAtBeginningOfLine && hasFinalNewline) + unnecessaryWhitespace.removeSelectedText(); + QTextCursor c = cursor; + c.setPosition(cursor.document()->findBlockByNumber(reindentBlockStart).position()); + c.setPosition(cursor.document()->findBlockByNumber(reindentBlockEnd).position(), + QTextCursor::KeepAnchor); + reindent(document(), c); } cursor.endEditBlock(); |