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>
This commit is contained in:
Nikolai Kosjar
2017-07-17 12:57:23 +02:00
parent 2c9feb1f4b
commit a6aa287720
6 changed files with 343 additions and 59 deletions

View File

@@ -27,15 +27,20 @@
#include <cplusplus/MatchingText.h>
#include <texteditor/tabsettings.h>
#include <QTextBlock>
#include <QTextCursor>
using namespace CppEditor;
using namespace Internal;
bool CppAutoCompleter::contextAllowsAutoBrackets(const QTextCursor &cursor,
const QString &textToInsert) const
const QString &textToInsert) const
{
return CPlusPlus::MatchingText::contextAllowsAutoParentheses(cursor, textToInsert);
const CPlusPlus::MatchingText::IsNextBlockDeeperIndented isIndented
= [this](const QTextBlock &b) { return isNextBlockIndented(b); };
return CPlusPlus::MatchingText::contextAllowsAutoParentheses(cursor, textToInsert, isIndented);
}
bool CppAutoCompleter::contextAllowsAutoQuotes(const QTextCursor &cursor,

View File

@@ -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();

View File

@@ -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;