Editor: Simplify autocompleter.

Change-Id: I7e0d4a083862941401518b80a4a988aeac154f97
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
David Schulz
2016-04-18 15:13:28 +02:00
parent b482a6158c
commit 1d1f450081
2 changed files with 69 additions and 67 deletions

View File

@@ -61,7 +61,7 @@ bool AutoCompleter::isSurroundWithEnabled() const
return m_surroundWithEnabled; return m_surroundWithEnabled;
} }
void AutoCompleter::countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen) static void countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen)
{ {
if (c == open) if (c == open)
++*stillopen; ++*stillopen;
@@ -74,13 +74,8 @@ void AutoCompleter::countBracket(QChar open, QChar close, QChar c, int *errors,
} }
} }
void AutoCompleter::countBrackets(QTextCursor cursor, static void countBrackets(QTextCursor cursor, int from, int end, QChar open, QChar close,
int from, int *errors, int *stillopen)
int end,
QChar open,
QChar close,
int *errors,
int *stillopen)
{ {
cursor.setPosition(from); cursor.setPosition(from);
QTextBlock block = cursor.block(); QTextBlock block = cursor.block();
@@ -99,53 +94,15 @@ void AutoCompleter::countBrackets(QTextCursor cursor,
} }
} }
QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToInsert) const static bool fixesBracketsError(const QString &textToInsert, const QTextCursor &cursor)
{ {
const bool checkBlockEnd = m_allowSkippingOfBlockEnd;
m_allowSkippingOfBlockEnd = false; // consume blockEnd.
if (m_surroundWithEnabled && cursor.hasSelection()) {
if (textToInsert == QLatin1String("("))
return cursor.selectedText() + QLatin1Char(')');
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 += QLatin1Char('}') + QString(QChar::ParagraphSeparator);
else
str += QString(QChar::ParagraphSeparator) + QLatin1Char('}');
} else {
str += QLatin1Char('}');
}
return str;
}
if (textToInsert == QLatin1String("["))
return cursor.selectedText() + QLatin1Char(']');
if (textToInsert == QLatin1String("\""))
return cursor.selectedText() + QLatin1Char('"');
if (textToInsert == QLatin1String("'"))
return cursor.selectedText() + QLatin1Char('\'');
}
if (!m_autoParenthesesEnabled)
return QString();
if (!contextAllowsAutoParentheses(cursor, textToInsert))
return QString();
QTextDocument *doc = cursor.document();
const QString text = textToInsert;
const QChar lookAhead = doc->characterAt(cursor.selectionEnd());
const QChar character = textToInsert.at(0); const QChar character = textToInsert.at(0);
const QString parentheses = QLatin1String("()"); const QString parentheses = QLatin1String("()");
const QString brackets = QLatin1String("[]"); const QString brackets = QLatin1String("[]");
if (parentheses.contains(character) || brackets.contains(character)) { if (!parentheses.contains(character) && !brackets.contains(character))
QTextCursor tmp= cursor; return false;
QTextCursor tmp = cursor;
bool foundBlockStart = TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp); bool foundBlockStart = TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
int blockStart = foundBlockStart ? tmp.position() : 0; int blockStart = foundBlockStart ? tmp.position() : 0;
tmp = cursor; tmp = cursor;
@@ -164,12 +121,62 @@ QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToIn
countBracket(openChar, closeChar, character, &errors, &stillopen); countBracket(openChar, closeChar, character, &errors, &stillopen);
countBrackets(cursor, cursor.position(), blockEnd, openChar, closeChar, &errors, &stillopen); countBrackets(cursor, cursor.position(), blockEnd, openChar, closeChar, &errors, &stillopen);
int errorsAfterInsertion = errors + stillopen; int errorsAfterInsertion = errors + stillopen;
if (errorsAfterInsertion < errorsBeforeInsertion) return errorsAfterInsertion < errorsBeforeInsertion;
return QString(); // insertion fixes parentheses or bracket errors, do not auto complete }
static QString replaceSelection(const QString &textToInsert, const QString &selection)
{
QString replacement;
if (textToInsert == QLatin1String("(")) {
replacement = selection + QLatin1Char(')');
} else if (textToInsert == QLatin1String("{")) {
//If the text spans multiple lines, insert on different lines
replacement = selection;
if (selection.contains(QChar::ParagraphSeparator)) {
//Also, try to simulate auto-indent
replacement = (selection.startsWith(QChar::ParagraphSeparator)
? QString()
: QString(QChar::ParagraphSeparator)) + selection;
if (replacement.endsWith(QChar::ParagraphSeparator))
replacement += QLatin1Char('}') + QString(QChar::ParagraphSeparator);
else
replacement += QString(QChar::ParagraphSeparator) + QLatin1Char('}');
} else {
replacement += QLatin1Char('}');
}
} else if (textToInsert == QLatin1String("[")) {
replacement = selection + QLatin1Char(']');
} else if (textToInsert == QLatin1String("\"")) {
replacement = selection + QLatin1Char('"');
} else if (textToInsert == QLatin1String("'")) {
replacement = selection + QLatin1Char('\'');
}
return replacement;
}
QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
{
const bool checkBlockEnd = m_allowSkippingOfBlockEnd;
m_allowSkippingOfBlockEnd = false; // consume blockEnd.
if (m_surroundWithEnabled && cursor.hasSelection()) {
const QString replacement = replaceSelection(textToInsert, cursor.selectedText());
if (!replacement.isEmpty())
return replacement;
} }
if (!m_autoParenthesesEnabled)
return QString();
if (!contextAllowsAutoParentheses(cursor, textToInsert))
return QString();
if (fixesBracketsError(textToInsert, cursor))
return QString();
QTextDocument *doc = cursor.document();
const QChar lookAhead = doc->characterAt(cursor.selectionEnd());
int skippedChars = 0; int skippedChars = 0;
const QString autoText = insertMatchingBrace(cursor, text, lookAhead, &skippedChars); const QString autoText = insertMatchingBrace(cursor, textToInsert, lookAhead, &skippedChars);
if (checkBlockEnd && textToInsert.at(0) == QLatin1Char('}')) { if (checkBlockEnd && textToInsert.at(0) == QLatin1Char('}')) {
if (textToInsert.length() > 1) if (textToInsert.length() > 1)

View File

@@ -77,11 +77,6 @@ public:
// Returns the text that needs to be inserted // Returns the text that needs to be inserted
virtual QString insertParagraphSeparator(const QTextCursor &cursor) const; virtual QString insertParagraphSeparator(const QTextCursor &cursor) const;
protected:
static void countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen);
static void countBrackets(QTextCursor cursor, int from, int end, QChar open, QChar close,
int *errors, int *stillopen);
private: private:
mutable bool m_allowSkippingOfBlockEnd; mutable bool m_allowSkippingOfBlockEnd;
bool m_surroundWithEnabled; bool m_surroundWithEnabled;