TextEditor: Type over punctuation that can be auto completed

This restores a feature of older Qt Creator versions: typing "over"
closing punctuation.  For instance, if my cursor is at | and I have
something like this:

    if (statement|)

... and I type the ) character, older versions of Qt Creator would not
end up with )).  With newer versions of Qt Creator, this behavior is
limited to the case where an autocompletion has just happened (i.e. if I
typed the opening ( character, the closing ) is auto inserted and
highlighted - and if I type it myself, it won't add a second one).

Task-number: QTCREATORBUG-16946
Change-Id: I45af3ac139d5e7c76e5f0a8a9d66762f9209a525
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Joel Smith
2019-05-15 21:50:28 -07:00
parent 0980484be8
commit e846b8717a
7 changed files with 29 additions and 2 deletions

View File

@@ -37,7 +37,8 @@ AutoCompleter::AutoCompleter() :
m_autoInsertBrackets(true), m_autoInsertBrackets(true),
m_surroundWithBrackets(true), m_surroundWithBrackets(true),
m_autoInsertQuotes(true), m_autoInsertQuotes(true),
m_surroundWithQuotes(true) m_surroundWithQuotes(true),
m_overwriteClosingChars(false)
{} {}
AutoCompleter::~AutoCompleter() = default; AutoCompleter::~AutoCompleter() = default;
@@ -191,6 +192,9 @@ QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToIn
QTextDocument *doc = cursor.document(); QTextDocument *doc = cursor.document();
const QChar lookAhead = doc->characterAt(cursor.selectionEnd()); const QChar lookAhead = doc->characterAt(cursor.selectionEnd());
if (m_overwriteClosingChars && (textToInsert == lookAhead))
skipChars = true;
int skippedChars = 0; int skippedChars = 0;
if (isQuote(textToInsert) && m_autoInsertQuotes if (isQuote(textToInsert) && m_autoInsertQuotes

View File

@@ -53,6 +53,9 @@ public:
void setSurroundWithQuotesEnabled(bool b) { m_surroundWithQuotes = b; } void setSurroundWithQuotesEnabled(bool b) { m_surroundWithQuotes = b; }
bool isSurroundWithQuotesEnabled() const { return m_surroundWithQuotes; } bool isSurroundWithQuotesEnabled() const { return m_surroundWithQuotes; }
void setOverwriteClosingCharsEnabled(bool b) { m_overwriteClosingChars = b; }
bool isOverwriteClosingCharsEnabled() const { return m_overwriteClosingChars; }
void setTabSettings(const TabSettings &tabSettings) { m_tabSettings = tabSettings; } void setTabSettings(const TabSettings &tabSettings) { m_tabSettings = tabSettings; }
const TabSettings &tabSettings() const { return m_tabSettings; } const TabSettings &tabSettings() const { return m_tabSettings; }
@@ -103,6 +106,7 @@ private:
bool m_surroundWithBrackets; bool m_surroundWithBrackets;
bool m_autoInsertQuotes; bool m_autoInsertQuotes;
bool m_surroundWithQuotes; bool m_surroundWithQuotes;
bool m_overwriteClosingChars;
}; };
} // TextEditor } // TextEditor

View File

@@ -42,6 +42,7 @@ static const char animateAutoCompleteKey[] = "AnimateAutoComplete";
static const char highlightAutoCompleteKey[] = "HighlightAutoComplete"; static const char highlightAutoCompleteKey[] = "HighlightAutoComplete";
static const char skipAutoCompleteKey[] = "SkipAutoComplete"; static const char skipAutoCompleteKey[] = "SkipAutoComplete";
static const char autoRemoveKey[] = "AutoRemove"; static const char autoRemoveKey[] = "AutoRemove";
static const char overwriteClosingCharsKey[] = "OverwriteClosingChars";
using namespace TextEditor; using namespace TextEditor;
@@ -62,6 +63,7 @@ void CompletionSettings::toSettings(QSettings *s) const
s->setValue(highlightAutoCompleteKey, m_highlightAutoComplete); s->setValue(highlightAutoCompleteKey, m_highlightAutoComplete);
s->setValue(skipAutoCompleteKey, m_skipAutoCompletedText); s->setValue(skipAutoCompleteKey, m_skipAutoCompletedText);
s->setValue(autoRemoveKey, m_autoRemove); s->setValue(autoRemoveKey, m_autoRemove);
s->setValue(overwriteClosingCharsKey, m_overwriteClosingChars);
s->endGroup(); s->endGroup();
} }
@@ -98,6 +100,8 @@ void CompletionSettings::fromSettings(QSettings *s)
s->value(skipAutoCompleteKey, m_skipAutoCompletedText).toBool(); s->value(skipAutoCompleteKey, m_skipAutoCompletedText).toBool();
m_autoRemove = m_autoRemove =
s->value(autoRemoveKey, m_autoRemove).toBool(); s->value(autoRemoveKey, m_autoRemove).toBool();
m_overwriteClosingChars =
s->value(overwriteClosingCharsKey, m_overwriteClosingChars).toBool();
s->endGroup(); s->endGroup();
} }
@@ -117,5 +121,6 @@ bool CompletionSettings::equals(const CompletionSettings &cs) const
&& m_highlightAutoComplete == cs.m_highlightAutoComplete && m_highlightAutoComplete == cs.m_highlightAutoComplete
&& m_skipAutoCompletedText == cs.m_skipAutoCompletedText && m_skipAutoCompletedText == cs.m_skipAutoCompletedText
&& m_autoRemove == cs.m_autoRemove && m_autoRemove == cs.m_autoRemove
&& m_overwriteClosingChars == cs.m_overwriteClosingChars
; ;
} }

View File

@@ -70,6 +70,7 @@ public:
bool m_highlightAutoComplete = true; bool m_highlightAutoComplete = true;
bool m_skipAutoCompletedText = true; bool m_skipAutoCompletedText = true;
bool m_autoRemove = true; bool m_autoRemove = true;
bool m_overwriteClosingChars = false;
}; };
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

@@ -102,6 +102,7 @@ QWidget *CompletionSettingsPage::widget()
m_page->spaceAfterFunctionName->setChecked(m_completionSettings.m_spaceAfterFunctionName); m_page->spaceAfterFunctionName->setChecked(m_completionSettings.m_spaceAfterFunctionName);
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->overwriteClosingChars->setChecked(m_completionSettings.m_overwriteClosingChars);
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->skipAutoComplete->setChecked(m_completionSettings.m_skipAutoCompletedText);
m_page->removeAutoComplete->setChecked(m_completionSettings.m_autoRemove); m_page->removeAutoComplete->setChecked(m_completionSettings.m_autoRemove);
@@ -181,6 +182,7 @@ void CompletionSettingsPage::settingsFromUi(CompletionSettings &completion, Comm
completion.m_spaceAfterFunctionName = m_page->spaceAfterFunctionName->isChecked(); completion.m_spaceAfterFunctionName = m_page->spaceAfterFunctionName->isChecked();
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_overwriteClosingChars = m_page->overwriteClosingChars->isChecked();
completion.m_highlightAutoComplete = m_page->highlightAutoComplete->isChecked(); completion.m_highlightAutoComplete = m_page->highlightAutoComplete->isChecked();
completion.m_skipAutoCompletedText = m_page->skipAutoComplete->isChecked(); completion.m_skipAutoCompletedText = m_page->skipAutoComplete->isChecked();
completion.m_autoRemove = m_page->removeAutoComplete->isChecked(); completion.m_autoRemove = m_page->removeAutoComplete->isChecked();

View File

@@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>551</width> <width>551</width>
<height>493</height> <height>507</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
@@ -303,6 +303,16 @@ In addition, Shift+Enter inserts an escape character at the cursor position and
</item> </item>
</layout> </layout>
</item> </item>
<item row="3" column="1">
<widget class="QCheckBox" name="overwriteClosingChars">
<property name="toolTip">
<string>Automatically overwrite closing parentheses and quotes.</string>
</property>
<property name="text">
<string>Overwrite closing punctuation</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>

View File

@@ -7521,6 +7521,7 @@ void TextEditorWidget::setCompletionSettings(const CompletionSettings &completio
d->m_autoCompleter->setSurroundWithBracketsEnabled(completionSettings.m_surroundingAutoBrackets); d->m_autoCompleter->setSurroundWithBracketsEnabled(completionSettings.m_surroundingAutoBrackets);
d->m_autoCompleter->setAutoInsertQuotesEnabled(completionSettings.m_autoInsertQuotes); d->m_autoCompleter->setAutoInsertQuotesEnabled(completionSettings.m_autoInsertQuotes);
d->m_autoCompleter->setSurroundWithQuotesEnabled(completionSettings.m_surroundingAutoQuotes); d->m_autoCompleter->setSurroundWithQuotesEnabled(completionSettings.m_surroundingAutoQuotes);
d->m_autoCompleter->setOverwriteClosingCharsEnabled(completionSettings.m_overwriteClosingChars);
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; d->m_skipAutoCompletedText = completionSettings.m_skipAutoCompletedText;