aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor
diff options
context:
space:
mode:
authorNikolai Kosjar <nikolai.kosjar@qt.io>2017-07-17 12:57:23 +0200
committerNikolai Kosjar <nikolai.kosjar@qt.io>2017-07-21 11:42:46 +0000
commita6aa287720112c70c1363bcb46d55d438fe57eac (patch)
treeebcc2c8005a92977fed1c391d07ac296a7cc2b34 /src/plugins/texteditor
parent2c9feb1f4b301e9a9228739a89c438bb7ff043f0 (diff)
C++: Fine-tune auto insertion of '}'
Do not insert for these cases: * <Cursor>{ * namespace X <Cursor> * if the next block is indented, like e.g.: if (e) <Cursor> g(); * on empty line if text before looks like a finished statement or scope opening/end Change-Id: Id9decc1e964a775724a929c2a3e79b5283105560 Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/texteditor')
-rw-r--r--src/plugins/texteditor/autocompleter.cpp31
-rw-r--r--src/plugins/texteditor/autocompleter.h2
2 files changed, 22 insertions, 11 deletions
diff --git a/src/plugins/texteditor/autocompleter.cpp b/src/plugins/texteditor/autocompleter.cpp
index 8ac7bbb6b38..08ce56bd48b 100644
--- a/src/plugins/texteditor/autocompleter.cpp
+++ b/src/plugins/texteditor/autocompleter.cpp
@@ -150,6 +150,24 @@ bool AutoCompleter::isQuote(const QString &text)
return text == QLatin1String("\"") || text == QLatin1String("'");
}
+bool AutoCompleter::isNextBlockIndented(const QTextBlock &currentBlock) const
+{
+ QTextBlock block = currentBlock;
+ int indentation = m_tabSettings.indentationColumn(block.text());
+
+ if (block.next().isValid()) { // not the last block
+ block = block.next();
+ //skip all empty blocks
+ while (block.isValid() && m_tabSettings.onlySpace(block.text()))
+ block = block.next();
+ if (block.isValid()
+ && m_tabSettings.indentationColumn(block.text()) > indentation)
+ return true;
+ }
+
+ return false;
+}
+
QString AutoCompleter::replaceSelection(QTextCursor &cursor, const QString &textToInsert) const
{
if (!cursor.hasSelection())
@@ -301,17 +319,8 @@ int AutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
if (condition) {|
statement;
*/
- int indentation = m_tabSettings.indentationColumn(block.text());
-
- if (block.next().isValid()) { // not the last block
- block = block.next();
- //skip all empty blocks
- while (block.isValid() && m_tabSettings.onlySpace(block.text()))
- block = block.next();
- if (block.isValid()
- && m_tabSettings.indentationColumn(block.text()) > indentation)
- return 0;
- }
+ if (isNextBlockIndented(block))
+ return 0;
const QString &textToInsert = insertParagraphSeparator(cursor);
int pos = cursor.position();
diff --git a/src/plugins/texteditor/autocompleter.h b/src/plugins/texteditor/autocompleter.h
index 5d12b63b83a..4847f504762 100644
--- a/src/plugins/texteditor/autocompleter.h
+++ b/src/plugins/texteditor/autocompleter.h
@@ -31,6 +31,7 @@
#include <QString>
QT_BEGIN_NAMESPACE
+class QTextBlock;
class QTextCursor;
QT_END_NAMESPACE
@@ -90,6 +91,7 @@ public:
virtual QString insertParagraphSeparator(const QTextCursor &cursor) const;
static bool isQuote(const QString &text);
+ bool isNextBlockIndented(const QTextBlock &currentBlock) const;
private:
QString replaceSelection(QTextCursor &cursor, const QString &textToInsert) const;