Commit cda026ee authored by Francois Ferrand's avatar Francois Ferrand Committed by Leandro Melo
Browse files

Surround selected text with matching symbols on (, [, {, " and '.



Merge-request: 198
Reviewed-by: default avatarLeandro Melo <leandro.melo@nokia.com>
parent f8e89a92
......@@ -1787,10 +1787,16 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
if (!autoText.isEmpty()) {
int pos = cursor.position();
cursor.insertText(autoText);
cursor.setPosition(pos);
//Select the inserted text, to be able to re-indent the inserted text
cursor.setPosition(pos, QTextCursor::KeepAnchor);
}
if (!electricChar.isNull() && d->m_autoCompleter->contextAllowsElectricCharacters(cursor))
indent(document(), cursor, electricChar);
if (!autoText.isEmpty()) {
if (d->m_document->tabSettings().m_autoIndent)
reindent(document(), cursor);
cursor.setPosition(autoText.length() == 1 ? cursor.position() : cursor.anchor());
}
if (doEditBlock)
cursor.endEditBlock();
......@@ -2181,6 +2187,16 @@ bool BaseTextEditor::isAutoParenthesesEnabled() const
return d->m_autoParenthesesEnabled;
}
void BaseTextEditor::setSurroundWithEnabled(bool b)
{
d->m_surroundWithEnabled= b;
}
bool BaseTextEditor::isSurroundWithEnabled() const
{
return d->m_surroundWithEnabled;
}
void BaseTextEditor::setHighlightCurrentLine(bool b)
{
d->m_highlightCurrentLine = b;
......@@ -2346,6 +2362,7 @@ BaseTextEditorPrivate::BaseTextEditorPrivate()
m_document(new BaseTextDocument),
m_parenthesesMatchingEnabled(false),
m_autoParenthesesEnabled(true),
m_surroundWithEnabled(true),
m_updateTimer(0),
m_formatRange(false),
m_parenthesesMatchingTimer(0),
......@@ -4506,6 +4523,34 @@ QString BaseTextEditor::autoComplete(QTextCursor &cursor, const QString &textToI
const bool checkBlockEnd = d->m_allowSkippingOfBlockEnd;
d->m_allowSkippingOfBlockEnd = false; // consume blockEnd.
if (d->m_surroundWithEnabled && cursor.hasSelection()) {
if (textToInsert == QLatin1String("("))
return cursor.selectedText() + QLatin1String(")");
if (textToInsert == QLatin1String("{")) {
//If the text span multiple lines, insert on different lines
QString str = cursor.selectedText();
if (str.contains(QChar::ParagraphSeparator)) {
//Also, try to simulate auto-indent
str = (str.startsWith(QChar::ParagraphSeparator) ? QString() : QString(QChar::ParagraphSeparator)) +
str;
if (str.endsWith(QChar::ParagraphSeparator))
str += QLatin1String("}") + QString(QChar::ParagraphSeparator);
else
str += QString(QChar::ParagraphSeparator) + QLatin1String("}");
}
else {
str += QLatin1String("}");
}
return str;
}
if (textToInsert == QLatin1String("["))
return cursor.selectedText() + QLatin1String("]");
if (textToInsert == QLatin1String("\""))
return cursor.selectedText() + QLatin1String("\"");
if (textToInsert == QLatin1String("'"))
return cursor.selectedText() + QLatin1String("'");
}
if (!d->m_autoParenthesesEnabled)
return QString();
......
......@@ -171,6 +171,9 @@ public:
void setAutoParenthesesEnabled(bool b);
bool isAutoParenthesesEnabled() const;
void setSurroundWithEnabled(bool b);
bool isSurroundWithEnabled() const;
void setHighlightCurrentLine(bool b);
bool highlightCurrentLine() const;
......
......@@ -192,6 +192,7 @@ public:
QString m_displayName;
bool m_parenthesesMatchingEnabled;
bool m_autoParenthesesEnabled;
bool m_surroundWithEnabled;
QTimer *m_updateTimer;
Utils::ChangeSet m_changeSet;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment