Commit 2827534c authored by mae's avatar mae
Browse files

tweak auto-insertion of closing braces. It now only happens when you

press enter after an opening brace, but only if there's one extra
brace in the document.
parent bcad75fb
...@@ -892,17 +892,29 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e) ...@@ -892,17 +892,29 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
cursor.beginEditBlock(); cursor.beginEditBlock();
if (d->m_autoParenthesesEnabled && ts.m_autoParentheses if (d->m_autoParenthesesEnabled && ts.m_autoParentheses
&& characterAt(cursor.position()) == QLatin1Char('}')) { && characterAt(cursor.position()-1) == QLatin1Char('{')) {
int pos = cursor.position();
if (ts.m_autoIndent) { // verify that we indeed do have an extra opening brace in the document
cursor.insertBlock(); int braceDepth = document()->lastBlock().userState();
indent(document(), cursor, QChar::Null); if (braceDepth >= 0)
} else { braceDepth >>= 8;
QString previousBlockText = cursor.block().text(); else
cursor.insertBlock(); braceDepth= 0;
cursor.insertText(ts.indentationString(previousBlockText));
if (braceDepth > 0) { // we do have an extra brace, let's close it
int pos = cursor.position();
cursor.insertText(QLatin1String("}"));
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));
}
cursor.setPosition(pos);
} }
cursor.setPosition(pos);
} }
if (ts.m_autoIndent) { if (ts.m_autoIndent) {
...@@ -1065,9 +1077,7 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e) ...@@ -1065,9 +1077,7 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
if (d->m_autoParenthesesEnabled && d->m_document->tabSettings().m_autoParentheses) { if (d->m_autoParenthesesEnabled && d->m_document->tabSettings().m_autoParentheses) {
foreach(QChar c, text) { foreach(QChar c, text) {
QChar close; QChar close;
if (c == QLatin1Char('{')) if (c == QLatin1Char('('))
close = QLatin1Char('}');
else if (c == QLatin1Char('('))
close = QLatin1Char(')'); close = QLatin1Char(')');
else if (c == QLatin1Char('[')) else if (c == QLatin1Char('['))
close = QLatin1Char(']'); close = QLatin1Char(']');
...@@ -1081,7 +1091,6 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e) ...@@ -1081,7 +1091,6 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
QChar first = text.at(0); QChar first = text.at(0);
if (first == QLatin1Char(')') if (first == QLatin1Char(')')
|| first == QLatin1Char('}')
|| first == QLatin1Char(']')) { || first == QLatin1Char(']')) {
if (first == characterAt(cursor.position())) { if (first == characterAt(cursor.position())) {
int pos = cursor.position(); int pos = cursor.position();
......
Markdown is supported
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