C++: support smart splitting of strings.

If 'enter' is pressed while the cursor is in the middle of a string,
the string is ended at the current cursor position, and a new string
is started on the next line.  This makes it very easy to split a long
string onto multiple lines.

In addition, Shift+Enter insert an escape in the string, to continue the
string at the beginning of next line.

A setting can be used to enable or disable this option.

Change-Id: Ia5f3c6989fc00d40d06bc4fe1182fe8b1318f565
Reviewed-by: Francois Ferrand <thetypz@gmail.com>
Reviewed-by: hjk <hjk121@nokiamail.com>
This commit is contained in:
Francois Ferrand
2014-09-29 18:19:38 +02:00
committed by hjk
parent 82d9cf4146
commit 32b960db47
10 changed files with 162 additions and 63 deletions

View File

@@ -59,8 +59,10 @@
#include <cpptools/cppworkingcopy.h>
#include <cpptools/symbolfinder.h>
#include <texteditor/completionsettings.h>
#include <texteditor/textdocument.h>
#include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/codeassist/assistproposalitem.h>
#include <texteditor/codeassist/genericproposalmodel.h>
#include <texteditor/codeassist/genericproposal.h>
@@ -536,12 +538,47 @@ void CppEditorWidget::keyPressEvent(QKeyEvent *e)
if (d->m_localRenaming.handleKeyPressEvent(e))
return;
if (handleStringSplitting(e))
return;
if (d->m_cppDocumentationCommentHelper.handleKeyPressEvent(e))
return;
TextEditorWidget::keyPressEvent(e);
}
bool CppEditorWidget::handleStringSplitting(QKeyEvent *e) const
{
if (!TextEditorSettings::completionSettings().m_autoSplitStrings)
return false;
if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
QTextCursor cursor = textCursor();
if (autoCompleter()->isInString(cursor)) {
cursor.beginEditBlock();
if (cursor.positionInBlock() > 0
&& cursor.block().text().at(cursor.positionInBlock() - 1) == QLatin1Char('\\')) {
// Already escaped: simply go back to line, but do not indent.
cursor.insertText(QLatin1String("\n"));
} else if (e->modifiers() & Qt::ShiftModifier) {
// With 'shift' modifier, escape the end of line character
// and start at beginning of next line.
cursor.insertText(QLatin1String("\\\n"));
} else {
// End the current string, and start a new one on the line, properly indented.
cursor.insertText(QLatin1String("\"\n\""));
textDocument()->autoIndent(cursor);
}
cursor.endEditBlock();
e->accept();
return true;
}
}
return false;
}
void CppEditorWidget::applyFontSettings()
{
// This also makes the document apply font settings