Editor: Skip auto completed character only if it was recently inserted.

This means you can skip automatically inserted characters as long as you
don't explicitly move the text cursor and the editor doesn't lose the
focus. This will be visualized by highlighting the automatically
inserted character as long as you can perform the skipping.

This will reduce unexpected skipping in the case a cursor was explicitly
placed before an closing brace and a closing brace is typed.

Change-Id: I28e29e79ba10c9c48e8bc8817405fea630cca9bd
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
This commit is contained in:
David Schulz
2016-06-10 15:13:38 +02:00
committed by David Schulz
parent 08dcad9c82
commit 6750607244
25 changed files with 182 additions and 58 deletions

View File

@@ -255,7 +255,7 @@ bool MatchingText::isInStringHelper(const QTextCursor &cursor)
} }
QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QString &textToProcess, QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QString &textToProcess,
QChar /*lookAhead*/, int *skippedChars) QChar /*lookAhead*/, bool skipChars, int *skippedChars)
{ {
if (textToProcess.isEmpty()) if (textToProcess.isEmpty())
return QString(); return QString();
@@ -266,11 +266,13 @@ QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QStri
const QString blockText = tc.block().text().mid(tc.positionInBlock()); const QString blockText = tc.block().text().mid(tc.positionInBlock());
const QString trimmedBlockText = blockText.trimmed(); const QString trimmedBlockText = blockText.trimmed();
if (skipChars) {
*skippedChars = countSkippedChars(blockText, textToProcess); *skippedChars = countSkippedChars(blockText, textToProcess);
if (*skippedChars != 0) { if (*skippedChars != 0) {
tc.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, *skippedChars); tc.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, *skippedChars);
text = textToProcess.mid(*skippedChars); text = textToProcess.mid(*skippedChars);
} }
}
QString result; QString result;
foreach (const QChar &ch, text) { foreach (const QChar &ch, text) {
@@ -285,7 +287,7 @@ QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QStri
} }
QString MatchingText::insertMatchingQuote(const QTextCursor &cursor, const QString &textToProcess, QString MatchingText::insertMatchingQuote(const QTextCursor &cursor, const QString &textToProcess,
QChar lookAhead, int *skippedChars) QChar lookAhead, bool skipChars, int *skippedChars)
{ {
if (textToProcess.isEmpty()) if (textToProcess.isEmpty())
return QString(); return QString();
@@ -293,7 +295,7 @@ QString MatchingText::insertMatchingQuote(const QTextCursor &cursor, const QStri
QTextCursor tc = cursor; QTextCursor tc = cursor;
QString text = textToProcess; QString text = textToProcess;
if (!isEscaped(tc)) { if (skipChars && !isEscaped(tc)) {
const QString blockText = tc.block().text().mid(tc.positionInBlock()); const QString blockText = tc.block().text().mid(tc.positionInBlock());
*skippedChars = countSkippedChars(blockText, textToProcess); *skippedChars = countSkippedChars(blockText, textToProcess);
if (*skippedChars != 0) { if (*skippedChars != 0) {
@@ -313,7 +315,7 @@ QString MatchingText::insertMatchingQuote(const QTextCursor &cursor, const QStri
qWarning() << Q_FUNC_INFO << "handle event compression"; qWarning() << Q_FUNC_INFO << "handle event compression";
BackwardsScanner tk(tc, LanguageFeatures::defaultFeatures(), MAX_NUM_LINES, BackwardsScanner tk(tc, LanguageFeatures::defaultFeatures(), MAX_NUM_LINES,
textToProcess.left(*skippedChars)); textToProcess.left(skippedChars ? *skippedChars : 0));
if (insertQuote(ch, tk)) if (insertQuote(ch, tk))
return ch; return ch;
} }

View File

@@ -49,9 +49,9 @@ public:
static bool isInStringHelper(const QTextCursor &cursor); static bool isInStringHelper(const QTextCursor &cursor);
static QString insertMatchingBrace(const QTextCursor &tc, const QString &text, static QString insertMatchingBrace(const QTextCursor &tc, const QString &text,
QChar lookAhead, int *skippedChars); QChar lookAhead, bool skipChars, int *skippedChars);
static QString insertMatchingQuote(const QTextCursor &tc, const QString &text, static QString insertMatchingQuote(const QTextCursor &tc, const QString &text,
QChar lookAhead, int *skippedChars); QChar lookAhead, bool skipChars, int *skippedChars);
static QString insertParagraphSeparator(const QTextCursor &tc); static QString insertParagraphSeparator(const QTextCursor &tc);
}; };

View File

@@ -75,6 +75,7 @@ void ClangAssistProposalItem::apply(TextEditor::TextDocumentManipulatorInterface
QString extraCharacters; QString extraCharacters;
int extraLength = 0; int extraLength = 0;
int cursorOffset = 0; int cursorOffset = 0;
bool setAutoCompleteSkipPos = false;
bool autoParenthesesEnabled = true; bool autoParenthesesEnabled = true;
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) { if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) {
@@ -141,6 +142,7 @@ void ClangAssistProposalItem::apply(TextEditor::TextDocumentManipulatorInterface
if (MatchingText::shouldInsertMatchingText(lookAhead)) { if (MatchingText::shouldInsertMatchingText(lookAhead)) {
extraCharacters += QLatin1Char(')'); extraCharacters += QLatin1Char(')');
--cursorOffset; --cursorOffset;
setAutoCompleteSkipPos = true;
if (endWithSemicolon) { if (endWithSemicolon) {
extraCharacters += semicolon; extraCharacters += semicolon;
--cursorOffset; --cursorOffset;
@@ -200,6 +202,8 @@ void ClangAssistProposalItem::apply(TextEditor::TextDocumentManipulatorInterface
if (isReplaced) { if (isReplaced) {
if (cursorOffset) if (cursorOffset)
manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset); manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset);
if (setAutoCompleteSkipPos)
manipulator.setAutoCompleteSkipPosition(manipulator.currentPosition());
if (ccr.completionKind() == CodeCompletion::KeywordCompletionKind) if (ccr.completionKind() == CodeCompletion::KeywordCompletionKind)
manipulator.autoIndent(basePosition, textToBeInserted.size()); manipulator.autoIndent(basePosition, textToBeInserted.size());

View File

@@ -73,11 +73,13 @@ bool CMakeAutoCompleter::isInString(const QTextCursor &cursor) const
return inString; return inString;
} }
QString CMakeAutoCompleter::insertMatchingBrace(const QTextCursor &cursor, const QString &text, QString CMakeAutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
QChar lookAhead, int *skippedChars) const const QString &text,
QChar lookAhead,
bool skipChars,
int *skippedChars) const
{ {
Q_UNUSED(cursor) Q_UNUSED(cursor)
Q_UNUSED(skippedChars)
if (text.isEmpty()) if (text.isEmpty())
return QString(); return QString();
const QChar current = text.at(0); const QChar current = text.at(0);
@@ -86,7 +88,7 @@ QString CMakeAutoCompleter::insertMatchingBrace(const QTextCursor &cursor, const
return QStringLiteral(")"); return QStringLiteral(")");
case ')': case ')':
if (current == lookAhead) if (current == lookAhead && skipChars)
++*skippedChars; ++*skippedChars;
break; break;
@@ -97,18 +99,22 @@ QString CMakeAutoCompleter::insertMatchingBrace(const QTextCursor &cursor, const
return QString(); return QString();
} }
QString CMakeAutoCompleter::insertMatchingQuote(const QTextCursor &cursor, const QString &text, QString CMakeAutoCompleter::insertMatchingQuote(const QTextCursor &cursor,
QChar lookAhead, int *skippedChars) const const QString &text,
QChar lookAhead,
bool skipChars,
int *skippedChars) const
{ {
Q_UNUSED(cursor) Q_UNUSED(cursor)
static const QChar quote(QLatin1Char('"')); static const QChar quote(QLatin1Char('"'));
if (text.isEmpty() || text != quote) if (text.isEmpty() || text != quote)
return QString(); return QString();
if (lookAhead != quote) if (lookAhead == quote && skipChars) {
return quote;
++*skippedChars; ++*skippedChars;
return QString(); return QString();
} }
return quote;
}
int CMakeAutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor, const TextEditor::TabSettings &tabSettings) int CMakeAutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor, const TextEditor::TabSettings &tabSettings)
{ {

View File

@@ -40,9 +40,9 @@ public:
bool isInComment(const QTextCursor &cursor) const override; bool isInComment(const QTextCursor &cursor) const override;
bool isInString(const QTextCursor &cursor) const override; bool isInString(const QTextCursor &cursor) const override;
QString insertMatchingBrace(const QTextCursor &cursor, const QString &text, QString insertMatchingBrace(const QTextCursor &cursor, const QString &text,
QChar lookAhead, int *skippedChars) const override; QChar lookAhead, bool skipChars, int *skippedChars) const override;
QString insertMatchingQuote(const QTextCursor &cursor, const QString &text, QString insertMatchingQuote(const QTextCursor &cursor, const QString &text,
QChar lookAhead, int *skippedChars) const override; QChar lookAhead, bool skipChars, int *skippedChars) const override;
int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor, const TextEditor::TabSettings &tabSettings) override; int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor, const TextEditor::TabSettings &tabSettings) override;
bool contextAllowsAutoBrackets(const QTextCursor &cursor, const QString &textToInsert) const override; bool contextAllowsAutoBrackets(const QTextCursor &cursor, const QString &textToInsert) const override;
bool contextAllowsAutoQuotes(const QTextCursor &cursor, const QString &textToInsert) const override; bool contextAllowsAutoQuotes(const QTextCursor &cursor, const QString &textToInsert) const override;

View File

@@ -60,15 +60,17 @@ bool CppAutoCompleter::isInString(const QTextCursor &cursor) const
} }
QString CppAutoCompleter::insertMatchingBrace(const QTextCursor &cursor, const QString &text, QString CppAutoCompleter::insertMatchingBrace(const QTextCursor &cursor, const QString &text,
QChar lookAhead, int *skippedChars) const QChar lookAhead, bool skipChars, int *skippedChars) const
{ {
return CPlusPlus::MatchingText::insertMatchingBrace(cursor, text, lookAhead, skippedChars); return CPlusPlus::MatchingText::insertMatchingBrace(cursor, text, lookAhead,
skipChars, skippedChars);
} }
QString CppAutoCompleter::insertMatchingQuote(const QTextCursor &cursor, const QString &text, QString CppAutoCompleter::insertMatchingQuote(const QTextCursor &cursor, const QString &text,
QChar lookAhead, int *skippedChars) const QChar lookAhead, bool skipChars, int *skippedChars) const
{ {
return CPlusPlus::MatchingText::insertMatchingQuote(cursor, text, lookAhead, skippedChars); return CPlusPlus::MatchingText::insertMatchingQuote(cursor, text, lookAhead,
skipChars, skippedChars);
} }
QString CppAutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const QString CppAutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
@@ -291,7 +293,8 @@ void CppEditorPlugin::test_autoComplete()
QVERIFY(!tc.isNull()); QVERIFY(!tc.isNull());
const QString &matchingText = CppAutoCompleter().autoComplete(tc, textToInsert); const QString &matchingText = CppAutoCompleter().autoComplete(tc, textToInsert,
true /*skipChars*/);
int skippedChars = tc.selectedText().size(); int skippedChars = tc.selectedText().size();
@@ -351,7 +354,8 @@ void CppEditorPlugin::test_surroundWithSelection()
QVERIFY(!tc.isNull()); QVERIFY(!tc.isNull());
const QString &matchingText = CppAutoCompleter().autoComplete(tc, textToInsert); const QString &matchingText = CppAutoCompleter().autoComplete(tc, textToInsert,
true /*skipChars*/);
QCOMPARE(matchingText, expectedText); QCOMPARE(matchingText, expectedText);
} }

View File

@@ -45,10 +45,12 @@ public:
QString insertMatchingBrace(const QTextCursor &cursor, QString insertMatchingBrace(const QTextCursor &cursor,
const QString &text, const QString &text,
QChar lookAhead, QChar lookAhead,
bool skipChars,
int *skippedChars) const override; int *skippedChars) const override;
QString insertMatchingQuote(const QTextCursor &cursor, QString insertMatchingQuote(const QTextCursor &cursor,
const QString &text, const QString &text,
QChar lookAhead, QChar lookAhead,
bool skipChars,
int *skippedChars) const override; int *skippedChars) const override;
QString insertParagraphSeparator(const QTextCursor &cursor) const override; QString insertParagraphSeparator(const QTextCursor &cursor) const override;
}; };

View File

@@ -203,6 +203,7 @@ void CppAssistProposalItem::applyContextualContent(TextDocumentManipulatorInterf
QString extraChars; QString extraChars;
int extraLength = 0; int extraLength = 0;
int cursorOffset = 0; int cursorOffset = 0;
bool setAutoCompleteSkipPos = false;
bool autoParenthesesEnabled = true; bool autoParenthesesEnabled = true;
@@ -278,6 +279,7 @@ void CppAssistProposalItem::applyContextualContent(TextDocumentManipulatorInterf
if (MatchingText::shouldInsertMatchingText(lookAhead)) { if (MatchingText::shouldInsertMatchingText(lookAhead)) {
extraChars += QLatin1Char(')'); extraChars += QLatin1Char(')');
--cursorOffset; --cursorOffset;
setAutoCompleteSkipPos = true;
if (endWithSemicolon) { if (endWithSemicolon) {
extraChars += semicolon; extraChars += semicolon;
--cursorOffset; --cursorOffset;
@@ -345,6 +347,8 @@ void CppAssistProposalItem::applyContextualContent(TextDocumentManipulatorInterf
manipulator.replace(basePosition, length, toInsert); manipulator.replace(basePosition, length, toInsert);
if (cursorOffset) if (cursorOffset)
manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset); manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset);
if (setAutoCompleteSkipPos)
manipulator.setAutoCompleteSkipPosition(manipulator.currentPosition());
} }
// -------------------- // --------------------

View File

@@ -55,15 +55,17 @@ bool GlslCompleter::isInComment(const QTextCursor &cursor) const
} }
QString GlslCompleter::insertMatchingBrace(const QTextCursor &cursor, const QString &text, QString GlslCompleter::insertMatchingBrace(const QTextCursor &cursor, const QString &text,
QChar lookAhead, int *skippedChars) const QChar lookAhead, bool skipChars, int *skippedChars) const
{ {
return CPlusPlus::MatchingText::insertMatchingBrace(cursor, text, lookAhead, skippedChars); return CPlusPlus::MatchingText::insertMatchingBrace(cursor, text, lookAhead,
skipChars, skippedChars);
} }
QString GlslCompleter::insertMatchingQuote(const QTextCursor &cursor, const QString &text, QString GlslCompleter::insertMatchingQuote(const QTextCursor &cursor, const QString &text,
QChar lookAhead, int *skippedChars) const QChar lookAhead, bool skipChars, int *skippedChars) const
{ {
return CPlusPlus::MatchingText::insertMatchingQuote(cursor, text, lookAhead, skippedChars); return CPlusPlus::MatchingText::insertMatchingQuote(cursor, text, lookAhead,
skipChars, skippedChars);
} }
QString GlslCompleter::insertParagraphSeparator(const QTextCursor &cursor) const QString GlslCompleter::insertParagraphSeparator(const QTextCursor &cursor) const

View File

@@ -40,9 +40,9 @@ public:
bool contextAllowsElectricCharacters(const QTextCursor &cursor) const override; bool contextAllowsElectricCharacters(const QTextCursor &cursor) const override;
bool isInComment(const QTextCursor &cursor) const override; bool isInComment(const QTextCursor &cursor) const override;
QString insertMatchingBrace(const QTextCursor &cursor, const QString &text, QString insertMatchingBrace(const QTextCursor &cursor, const QString &text,
QChar lookAhead, int *skippedChars) const override; QChar lookAhead, bool skipChars, int *skippedChars) const override;
QString insertMatchingQuote(const QTextCursor &cursor, const QString &text, QString insertMatchingQuote(const QTextCursor &cursor, const QString &text,
QChar lookAhead, int *skippedChars) const override; QChar lookAhead, bool skipChars, int *skippedChars) const override;
QString insertParagraphSeparator(const QTextCursor &cursor) const override; QString insertParagraphSeparator(const QTextCursor &cursor) const override;
}; };

View File

@@ -268,6 +268,7 @@ bool AutoCompleter::isInComment(const QTextCursor &cursor) const
QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor, QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
const QString &text, const QString &text,
QChar lookAhead, QChar lookAhead,
bool skipChars,
int *skippedChars) const int *skippedChars) const
{ {
if (text.length() != 1) if (text.length() != 1)
@@ -291,7 +292,7 @@ QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
case ']': case ']':
case '}': case '}':
case ';': case ';':
if (lookAhead == ch) if (lookAhead == ch && skipChars)
++*skippedChars; ++*skippedChars;
break; break;
@@ -303,12 +304,13 @@ QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
} }
QString AutoCompleter::insertMatchingQuote(const QTextCursor &/*tc*/, const QString &text, QString AutoCompleter::insertMatchingQuote(const QTextCursor &/*tc*/, const QString &text,
QChar lookAhead, int *skippedChars) const QChar lookAhead, bool skipChars, int *skippedChars) const
{ {
if (isQuote(text)) { if (isQuote(text)) {
if (lookAhead != text) if (lookAhead == text && skipChars)
return text;
++*skippedChars; ++*skippedChars;
else
return text;
} }
return QString(); return QString();
} }

View File

@@ -45,10 +45,12 @@ public:
QString insertMatchingBrace(const QTextCursor &tc, QString insertMatchingBrace(const QTextCursor &tc,
const QString &text, const QString &text,
QChar lookAhead, QChar lookAhead,
bool skipChars,
int *skippedChars) const override; int *skippedChars) const override;
QString insertMatchingQuote(const QTextCursor &tc, QString insertMatchingQuote(const QTextCursor &tc,
const QString &text, const QString &text,
QChar lookAhead, QChar lookAhead,
bool skipChars,
int *skippedChars) const override; int *skippedChars) const override;
QString insertParagraphSeparator(const QTextCursor &tc) const override; QString insertParagraphSeparator(const QTextCursor &tc) const override;
}; };

View File

@@ -379,8 +379,10 @@ void QmlJSAssistProposalItem::applyContextualContent(TextEditor::TextDocumentMan
} }
const int length = manipulator.currentPosition() - basePosition + replacedLength; const int length = manipulator.currentPosition() - basePosition + replacedLength;
manipulator.replace(basePosition, length, content); manipulator.replace(basePosition, length, content);
if (cursorOffset) if (cursorOffset) {
manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset); manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset);
manipulator.setAutoCompleteSkipPosition(manipulator.currentPosition());
}
} }
// ------------------------- // -------------------------

View File

@@ -148,7 +148,8 @@ QString AutoCompleter::replaceSelection(QTextCursor &cursor, const QString &text
return QString(); return QString();
} }
QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToInsert) const QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToInsert,
bool skipChars) const
{ {
const bool checkBlockEnd = m_allowSkippingOfBlockEnd; const bool checkBlockEnd = m_allowSkippingOfBlockEnd;
m_allowSkippingOfBlockEnd = false; // consume blockEnd. m_allowSkippingOfBlockEnd = false; // consume blockEnd.
@@ -164,12 +165,12 @@ QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToIn
if (isQuote(textToInsert) && m_autoInsertQuotes if (isQuote(textToInsert) && m_autoInsertQuotes
&& contextAllowsAutoQuotes(cursor, textToInsert)) { && contextAllowsAutoQuotes(cursor, textToInsert)) {
autoText = insertMatchingQuote(cursor, textToInsert, lookAhead, &skippedChars); autoText = insertMatchingQuote(cursor, textToInsert, lookAhead, skipChars, &skippedChars);
} else if (m_autoInsertBrackets && contextAllowsAutoBrackets(cursor, textToInsert)) { } else if (m_autoInsertBrackets && contextAllowsAutoBrackets(cursor, textToInsert)) {
if (fixesBracketsError(textToInsert, cursor)) if (fixesBracketsError(textToInsert, cursor))
return QString(); return QString();
autoText = insertMatchingBrace(cursor, textToInsert, lookAhead, &skippedChars); autoText = insertMatchingBrace(cursor, textToInsert, lookAhead, skipChars, &skippedChars);
if (checkBlockEnd && textToInsert.at(0) == QLatin1Char('}')) { if (checkBlockEnd && textToInsert.at(0) == QLatin1Char('}')) {
if (textToInsert.length() > 1) if (textToInsert.length() > 1)
@@ -179,14 +180,14 @@ QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToIn
while (doc->characterAt(pos).isSpace()) while (doc->characterAt(pos).isSpace())
++pos; ++pos;
if (doc->characterAt(pos) == QLatin1Char('}')) if (doc->characterAt(pos) == QLatin1Char('}') && skipChars)
skippedChars += (pos - startPos) + 1; skippedChars += (pos - startPos) + 1;
} }
} else { } else {
return QString(); return QString();
} }
if (skippedChars) { if (skipChars && skippedChars) {
const int pos = cursor.position(); const int pos = cursor.position();
cursor.setPosition(pos + skippedChars); cursor.setPosition(pos + skippedChars);
cursor.setPosition(pos, QTextCursor::KeepAnchor); cursor.setPosition(pos, QTextCursor::KeepAnchor);
@@ -341,11 +342,13 @@ bool AutoCompleter::isInString(const QTextCursor &cursor) const
QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor, QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
const QString &text, const QString &text,
QChar lookAhead, QChar lookAhead,
bool skipChars,
int *skippedChars) const int *skippedChars) const
{ {
Q_UNUSED(cursor); Q_UNUSED(cursor);
Q_UNUSED(text); Q_UNUSED(text);
Q_UNUSED(lookAhead); Q_UNUSED(lookAhead);
Q_UNUSED(skipChars);
Q_UNUSED(skippedChars); Q_UNUSED(skippedChars);
return QString(); return QString();
} }
@@ -353,11 +356,13 @@ QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
QString AutoCompleter::insertMatchingQuote(const QTextCursor &cursor, QString AutoCompleter::insertMatchingQuote(const QTextCursor &cursor,
const QString &text, const QString &text,
QChar lookAhead, QChar lookAhead,
bool skipChars,
int *skippedChars) const int *skippedChars) const
{ {
Q_UNUSED(cursor); Q_UNUSED(cursor);
Q_UNUSED(text); Q_UNUSED(text);
Q_UNUSED(lookAhead); Q_UNUSED(lookAhead);
Q_UNUSED(skipChars);
Q_UNUSED(skippedChars); Q_UNUSED(skippedChars);
return QString(); return QString();
} }

View File

@@ -54,7 +54,7 @@ public:
bool isSurroundWithQuotesEnabled() const { return m_surroundWithQuotes; } bool isSurroundWithQuotesEnabled() const { return m_surroundWithQuotes; }
// Returns the text to complete at the cursor position, or an empty string // Returns the text to complete at the cursor position, or an empty string
virtual QString autoComplete(QTextCursor &cursor, const QString &text) const; virtual QString autoComplete(QTextCursor &cursor, const QString &text, bool skipChars) const;
// Handles backspace. When returning true, backspace processing is stopped // Handles backspace. When returning true, backspace processing is stopped
virtual bool autoBackspace(QTextCursor &cursor); virtual bool autoBackspace(QTextCursor &cursor);
@@ -77,12 +77,12 @@ public:
virtual QString insertMatchingBrace(const QTextCursor &cursor, const virtual QString insertMatchingBrace(const QTextCursor &cursor, const
QString &text, QString &text,
QChar lookAhead, QChar lookAhead, bool skipChars,
int *skippedChars) const; int *skippedChars) const;
virtual QString insertMatchingQuote(const QTextCursor &cursor, const virtual QString insertMatchingQuote(const QTextCursor &cursor, const
QString &text, QString &text,
QChar lookAhead, QChar lookAhead, bool skipChars,
int *skippedChars) const; int *skippedChars) const;
// Returns the text that needs to be inserted // Returns the text that needs to be inserted

View File

@@ -101,6 +101,7 @@ void KeywordsAssistProposalItem::applyContextualContent(TextDocumentManipulatorI
QString toInsert = text(); QString toInsert = text();
int cursorOffset = 0; int cursorOffset = 0;
const QChar characterAtCurrentPosition = manipulator.characterAt(manipulator.currentPosition()); const QChar characterAtCurrentPosition = manipulator.characterAt(manipulator.currentPosition());
bool setAutoCompleteSkipPosition = false;
if (m_isFunction && settings.m_autoInsertBrackets) { if (m_isFunction && settings.m_autoInsertBrackets) {
if (settings.m_spaceAfterFunctionName) { if (settings.m_spaceAfterFunctionName) {
@@ -113,6 +114,7 @@ void KeywordsAssistProposalItem::applyContextualContent(TextDocumentManipulatorI
} else { } else {
toInsert += QLatin1String(" ()"); toInsert += QLatin1String(" ()");
cursorOffset = -1; cursorOffset = -1;
setAutoCompleteSkipPosition = true;
} }
} else { } else {
if (characterAtCurrentPosition == QLatin1Char('(')) { if (characterAtCurrentPosition == QLatin1Char('(')) {
@@ -120,6 +122,7 @@ void KeywordsAssistProposalItem::applyContextualContent(TextDocumentManipulatorI
} else { } else {
toInsert += QLatin1String("()"); toInsert += QLatin1String("()");
cursorOffset = -1; cursorOffset = -1;
setAutoCompleteSkipPosition = true;
} }
} }
} }
@@ -127,6 +130,8 @@ void KeywordsAssistProposalItem::applyContextualContent(TextDocumentManipulatorI
manipulator.replace(basePosition, replaceLength, toInsert); manipulator.replace(basePosition, replaceLength, toInsert);
if (cursorOffset) if (cursorOffset)
manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset); manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset);
if (setAutoCompleteSkipPosition)
manipulator.setAutoCompleteSkipPosition(manipulator.currentPosition());
} }
// ------------------------- // -------------------------

View File

@@ -68,6 +68,13 @@ void TextDocumentManipulator::setCursorPosition(int position)
m_textEditorWidget->setCursorPosition(position); m_textEditorWidget->setCursorPosition(position);
} }
void TextDocumentManipulator::setAutoCompleteSkipPosition(int position)
{
QTextCursor cursor = m_textEditorWidget->textCursor();
cursor.setPosition(position);
m_textEditorWidget->setAutoCompleteSkipPosition(cursor);
}
bool TextDocumentManipulator::replace(int position, int length, const QString &text) bool TextDocumentManipulator::replace(int position, int length, const QString &text)
{ {
bool textWillBeReplaced = textIsDifferentAt(position, length, text); bool textWillBeReplaced = textIsDifferentAt(position, length, text);

View File

@@ -43,6 +43,7 @@ public:
QTextCursor textCursorAt(int position) const final; QTextCursor textCursorAt(int position) const final;
void setCursorPosition(int position) final; void setCursorPosition(int position) final;
void setAutoCompleteSkipPosition(int position) final;
bool replace(int position, int length, const QString &text) final; bool replace(int position, int length, const QString &text) final;
void insertCodeSnippet(int position, const QString &text) final; void insertCodeSnippet(int position, const QString &text) final;
void paste() final; void paste() final;

View File

@@ -47,6 +47,7 @@ public:
virtual QTextCursor textCursorAt(int position) const = 0; virtual QTextCursor textCursorAt(int position) const = 0;
virtual void setCursorPosition(int position) = 0; virtual void setCursorPosition(int position) = 0;
virtual void setAutoCompleteSkipPosition(int position) = 0;
virtual bool replace(int position, int length, const QString &text) = 0; virtual bool replace(int position, int length, const QString &text) = 0;
virtual void insertCodeSnippet(int position, const QString &text) = 0; virtual void insertCodeSnippet(int position, const QString &text) = 0;
virtual void paste() = 0; virtual void paste() = 0;

View File

@@ -40,6 +40,7 @@ static const char spaceAfterFunctionNameKey[] = "SpaceAfterFunctionName";
static const char autoSplitStringsKey[] = "AutoSplitStrings"; static const char autoSplitStringsKey[] = "AutoSplitStrings";
static const char animateAutoCompleteKey[] = "AnimateAutoComplete"; static const char animateAutoCompleteKey[] = "AnimateAutoComplete";
static const char highlightAutoCompleteKey[] = "HighlightAutoComplete"; static const char highlightAutoCompleteKey[] = "HighlightAutoComplete";
static const char skipAutoCompleteKey[] = "SkipAutoComplete";
using namespace TextEditor; using namespace TextEditor;
@@ -58,6 +59,7 @@ void CompletionSettings::toSettings(QSettings *s) const
s->setValue(autoSplitStringsKey, m_autoSplitStrings); s->setValue(autoSplitStringsKey, m_autoSplitStrings);
s->setValue(animateAutoCompleteKey, m_animateAutoComplete); s->setValue(animateAutoCompleteKey, m_animateAutoComplete);
s->setValue(highlightAutoCompleteKey, m_highlightAutoComplete); s->setValue(highlightAutoCompleteKey, m_highlightAutoComplete);
s->setValue(skipAutoCompleteKey, m_skipAutoCompletedText);
s->endGroup(); s->endGroup();
} }
@@ -90,6 +92,8 @@ void CompletionSettings::fromSettings(QSettings *s)
s->value(animateAutoCompleteKey, m_animateAutoComplete).toBool(); s->value(animateAutoCompleteKey, m_animateAutoComplete).toBool();
m_highlightAutoComplete = m_highlightAutoComplete =
s->value(highlightAutoCompleteKey, m_highlightAutoComplete).toBool(); s->value(highlightAutoCompleteKey, m_highlightAutoComplete).toBool();
m_skipAutoCompletedText =
s->value(skipAutoCompleteKey, m_skipAutoCompletedText).toBool();
s->endGroup(); s->endGroup();
} }
@@ -107,5 +111,6 @@ bool CompletionSettings::equals(const CompletionSettings &cs) const
&& m_autoSplitStrings == cs.m_autoSplitStrings && m_autoSplitStrings == cs.m_autoSplitStrings
&& m_animateAutoComplete == cs.m_animateAutoComplete && m_animateAutoComplete == cs.m_animateAutoComplete
&& m_highlightAutoComplete == cs.m_highlightAutoComplete && m_highlightAutoComplete == cs.m_highlightAutoComplete
&& m_skipAutoCompletedText == cs.m_skipAutoCompletedText
; ;
} }

View File

@@ -67,7 +67,8 @@ public:
bool m_spaceAfterFunctionName = false; bool m_spaceAfterFunctionName = false;
bool m_autoSplitStrings = true; bool m_autoSplitStrings = true;
bool m_animateAutoComplete = true; bool m_animateAutoComplete = true;
bool m_highlightAutoComplete = false; bool m_highlightAutoComplete = true;
bool m_skipAutoCompletedText = true;
}; };
inline bool operator==(const CompletionSettings &t1, const CompletionSettings &t2) { return t1.equals(t2); } inline bool operator==(const CompletionSettings &t1, const CompletionSettings &t2) { return t1.equals(t2); }

View File

@@ -104,12 +104,14 @@ QWidget *CompletionSettingsPage::widget()
m_page->autoSplitStrings->setChecked(m_completionSettings.m_autoSplitStrings); m_page->autoSplitStrings->setChecked(m_completionSettings.m_autoSplitStrings);
m_page->animateAutoComplete->setChecked(m_completionSettings.m_animateAutoComplete); m_page->animateAutoComplete->setChecked(m_completionSettings.m_animateAutoComplete);
m_page->highlightAutoComplete->setChecked(m_completionSettings.m_highlightAutoComplete); m_page->highlightAutoComplete->setChecked(m_completionSettings.m_highlightAutoComplete);
m_page->skipAutoComplete->setChecked(m_completionSettings.m_skipAutoCompletedText);
m_page->enableDoxygenCheckBox->setChecked(m_commentsSettings.m_enableDoxygen); m_page->enableDoxygenCheckBox->setChecked(m_commentsSettings.m_enableDoxygen);
m_page->generateBriefCheckBox->setChecked(m_commentsSettings.m_generateBrief); m_page->generateBriefCheckBox->setChecked(m_commentsSettings.m_generateBrief);
m_page->leadingAsterisksCheckBox->setChecked(m_commentsSettings.m_leadingAsterisks); m_page->leadingAsterisksCheckBox->setChecked(m_commentsSettings.m_leadingAsterisks);
m_page->generateBriefCheckBox->setEnabled(m_page->enableDoxygenCheckBox->isChecked()); m_page->generateBriefCheckBox->setEnabled(m_page->enableDoxygenCheckBox->isChecked());
m_page->skipAutoComplete->setEnabled(m_page->highlightAutoComplete->isChecked());
} }
return m_widget; return m_widget;
} }
@@ -179,6 +181,7 @@ void CompletionSettingsPage::settingsFromUi(CompletionSettings &completion, Comm
completion.m_autoSplitStrings = m_page->autoSplitStrings->isChecked(); completion.m_autoSplitStrings = m_page->autoSplitStrings->isChecked();
completion.m_animateAutoComplete = m_page->animateAutoComplete->isChecked(); completion.m_animateAutoComplete = m_page->animateAutoComplete->isChecked();
completion.m_highlightAutoComplete = m_page->highlightAutoComplete->isChecked(); completion.m_highlightAutoComplete = m_page->highlightAutoComplete->isChecked();
completion.m_skipAutoCompletedText = m_page->skipAutoComplete->isChecked();
comment.m_enableDoxygen = m_page->enableDoxygenCheckBox->isChecked(); comment.m_enableDoxygen = m_page->enableDoxygenCheckBox->isChecked();
comment.m_generateBrief = m_page->generateBriefCheckBox->isChecked(); comment.m_generateBrief = m_page->generateBriefCheckBox->isChecked();

View File

@@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>511</width> <width>551</width>
<height>437</height> <height>465</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
@@ -227,13 +227,49 @@ In addition, Shift+Enter inserts an escape character at the cursor position and
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="3" column="0">
<widget class="QCheckBox" name="highlightAutoComplete"> <widget class="QCheckBox" name="highlightAutoComplete">
<property name="text"> <property name="text">
<string>Highlight automatically inserted text</string> <string>Highlight automatically inserted text</string>
</property> </property>
<property name="checked">
<bool>true</bool>
</property>
</widget> </widget>
</item> </item>
<item row="4" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<spacer name="horizontalSpacer_6">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>30</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="skipAutoComplete">
<property name="toolTip">
<string>Skip automatically inserted character if re-typed manually after completion.</string>
</property>
<property name="text">
<string>Skip automatically inserted character when typing</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>
@@ -254,7 +290,7 @@ In addition, Shift+Enter inserts an escape character at the cursor position and
</widget> </widget>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_3"> <layout class="QHBoxLayout" name="horizontalLayout_4">
<item> <item>
<spacer name="horizontalSpacer_5"> <spacer name="horizontalSpacer_5">
<property name="orientation"> <property name="orientation">
@@ -324,6 +360,7 @@ In addition, Shift+Enter inserts an escape character at the cursor position and
<tabstop>spaceAfterFunctionName</tabstop> <tabstop>spaceAfterFunctionName</tabstop>
<tabstop>animateAutoComplete</tabstop> <tabstop>animateAutoComplete</tabstop>
<tabstop>highlightAutoComplete</tabstop> <tabstop>highlightAutoComplete</tabstop>
<tabstop>skipAutoComplete</tabstop>
<tabstop>enableDoxygenCheckBox</tabstop> <tabstop>enableDoxygenCheckBox</tabstop>
<tabstop>generateBriefCheckBox</tabstop> <tabstop>generateBriefCheckBox</tabstop>
<tabstop>leadingAsterisksCheckBox</tabstop> <tabstop>leadingAsterisksCheckBox</tabstop>
@@ -337,12 +374,28 @@ In addition, Shift+Enter inserts an escape character at the cursor position and
<slot>setEnabled(bool)</slot> <slot>setEnabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>174</x> <x>195</x>
<y>294</y> <y>383</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>262</x> <x>320</x>
<y>321</y> <y>410</y>
</hint>
</hints>
</connection>
<connection>
<sender>highlightAutoComplete</sender>
<signal>toggled(bool)</signal>
<receiver>skipAutoComplete</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>176</x>
<y>277</y>
</hint>
<hint type="destinationlabel">
<x>186</x>
<y>306</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>

View File

@@ -455,6 +455,7 @@ public:
QPointer<TextEditorAnimator> m_autocompleteAnimator; QPointer<TextEditorAnimator> m_autocompleteAnimator;
bool m_animateAutoComplete = true; bool m_animateAutoComplete = true;
bool m_highlightAutoComplete = true; bool m_highlightAutoComplete = true;
bool m_skipAutoCompletedText = true;
bool m_keepAutoCompletionHighlight = false; bool m_keepAutoCompletionHighlight = false;
QTextCursor m_autoCompleteHighlightPos; QTextCursor m_autoCompleteHighlightPos;
@@ -2392,8 +2393,11 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e)
// only go here if control is not pressed, except if also alt is pressed // only go here if control is not pressed, except if also alt is pressed
// because AltGr maps to Alt + Ctrl // because AltGr maps to Alt + Ctrl
QTextCursor cursor = textCursor(); QTextCursor cursor = textCursor();
const QString &autoText = inOverwriteMode QString autoText;
? QString() : autoCompleter()->autoComplete(cursor, eventText); if (!inOverwriteMode) {
autoText = autoCompleter()->autoComplete(cursor, eventText,
d->m_skipAutoCompletedText && cursor == d->m_autoCompleteHighlightPos);
}
const bool cursorWithinSnippet = d->snippetCheckCursor(cursor); const bool cursorWithinSnippet = d->snippetCheckCursor(cursor);
QChar electricChar; QChar electricChar;
@@ -6566,6 +6570,7 @@ void TextEditorWidget::setCompletionSettings(const CompletionSettings &completio
d->m_autoCompleter->setSurroundWithQuotesEnabled(completionSettings.m_surroundingAutoQuotes); d->m_autoCompleter->setSurroundWithQuotesEnabled(completionSettings.m_surroundingAutoQuotes);
d->m_animateAutoComplete = completionSettings.m_animateAutoComplete; d->m_animateAutoComplete = completionSettings.m_animateAutoComplete;
d->m_highlightAutoComplete = completionSettings.m_highlightAutoComplete; d->m_highlightAutoComplete = completionSettings.m_highlightAutoComplete;
d->m_skipAutoCompletedText = completionSettings.m_skipAutoCompletedText;
} }
void TextEditorWidget::setExtraEncodingSettings(const ExtraEncodingSettings &extraEncodingSettings) void TextEditorWidget::setExtraEncodingSettings(const ExtraEncodingSettings &extraEncodingSettings)
@@ -7032,6 +7037,13 @@ void TextEditorWidget::keepAutoCompletionHighlight(bool keepHighlight)
d->m_keepAutoCompletionHighlight = keepHighlight; d->m_keepAutoCompletionHighlight = keepHighlight;
} }
void TextEditorWidget::setAutoCompleteSkipPosition(const QTextCursor &cursor)
{
QTextCursor tc = cursor;
tc.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
d->autocompleterHighlight(tc);
}
int BaseTextEditor::currentLine() const int BaseTextEditor::currentLine() const
{ {
return editorWidget()->textCursor().blockNumber() + 1; return editorWidget()->textCursor().blockNumber() + 1;

View File

@@ -326,6 +326,7 @@ public:
// keep the auto completion even if the focus is lost // keep the auto completion even if the focus is lost
void keepAutoCompletionHighlight(bool keepHighlight); void keepAutoCompletionHighlight(bool keepHighlight);
void setAutoCompleteSkipPosition(const QTextCursor &cursor);
virtual void copy(); virtual void copy();
virtual void paste(); virtual void paste();