preliminary auto parentheses support

This commit is contained in:
mae
2009-09-15 17:04:17 +02:00
parent 7cfac4da3f
commit 1c4fc0d7dd
5 changed files with 86 additions and 21 deletions

View File

@@ -888,18 +888,31 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
if (d->m_inBlockSelectionMode) if (d->m_inBlockSelectionMode)
cursor.clearSelection(); cursor.clearSelection();
const TabSettings &ts = d->m_document->tabSettings(); const TabSettings &ts = d->m_document->tabSettings();
cursor.beginEditBlock();
if (ts.m_autoParentheses
&& characterAt(cursor.position()) == QLatin1Char('}')) {
int pos = cursor.position();
if (ts.m_autoIndent) {
cursor.insertBlock();
indent(document(), cursor, QChar::Null);
} else {
QString previousBlockText = cursor.block().text();
cursor.insertBlock();
cursor.insertText(ts.indentationString(previousBlockText));
}
cursor.setPosition(pos);
}
if (ts.m_autoIndent) { if (ts.m_autoIndent) {
cursor.beginEditBlock();
cursor.insertBlock(); cursor.insertBlock();
indent(document(), cursor, QChar::Null); indent(document(), cursor, QChar::Null);
cursor.endEditBlock();
} else { } else {
cursor.beginEditBlock();
QString previousBlockText = cursor.block().text(); QString previousBlockText = cursor.block().text();
cursor.insertBlock(); cursor.insertBlock();
cursor.insertText(ts.indentationString(previousBlockText)); cursor.insertText(ts.indentationString(previousBlockText));
cursor.endEditBlock();
} }
cursor.endEditBlock();
e->accept(); e->accept();
setTextCursor(cursor); setTextCursor(cursor);
return; return;
@@ -1029,21 +1042,6 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
break; break;
default: default:
if (! ro && d->m_document->tabSettings().m_autoIndent
&& ! e->text().isEmpty() && isElectricCharacter(e->text().at(0))) {
QTextCursor cursor = textCursor();
const QString text = e->text();
cursor.insertText(text);
indent(document(), cursor, text.at(0));
#if 0
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(document()->documentLayout());
QTC_ASSERT(documentLayout, return);
documentLayout->requestUpdate(); // a bit drastic
e->accept();
#endif
setTextCursor(cursor);
return;
}
break; break;
} }
@@ -1055,7 +1053,59 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
} }
} }
QPlainTextEdit::keyPressEvent(e); if (ro || e->text().isEmpty() || !e->text().at(0).isPrint()) {
QPlainTextEdit::keyPressEvent(e);
} else {
QTextCursor cursor = textCursor();
QString text = e->text();
QString autoText;
if (d->m_document->tabSettings().m_autoParentheses) {
foreach(QChar c, text) {
QChar close;
if (c == QLatin1Char('{'))
close = QLatin1Char('}');
else if (c == QLatin1Char('('))
close = QLatin1Char(')');
else if (c == QLatin1Char('['))
close = QLatin1Char(']');
if (!close.isNull())
autoText += close;
}
QChar first = text.at(0);
if (first == QLatin1Char(')')
|| first == QLatin1Char('}')
|| first == QLatin1Char(']')) {
if (first == characterAt(cursor.position())) {
int pos = cursor.position();
cursor.setPosition(pos+1);
cursor.setPosition(pos, QTextCursor::KeepAnchor);
}
}
}
QChar electricChar;
if (d->m_document->tabSettings().m_autoIndent) {
foreach(QChar c, text) {
if (isElectricCharacter(c)) {
electricChar = c;
break;
}
}
}
if (!electricChar.isNull())
cursor.beginEditBlock();
cursor.insertText(text);
if (!autoText.isEmpty()) {
int pos = cursor.position();
cursor.insertText(autoText);
cursor.setPosition(pos);
}
if (!electricChar.isNull()) {
indent(document(), cursor, electricChar);
cursor.endEditBlock();
}
setTextCursor(cursor);
}
skip_event: skip_event:
if (!ro && e->key() == Qt::Key_Delete && d->m_parenthesesMatchingEnabled) if (!ro && e->key() == Qt::Key_Delete && d->m_parenthesesMatchingEnabled)

View File

@@ -130,6 +130,7 @@ void BehaviorSettingsPage::settingsFromUI(TabSettings &tabSettings,
{ {
tabSettings.m_spacesForTabs = m_d->m_page.insertSpaces->isChecked(); tabSettings.m_spacesForTabs = m_d->m_page.insertSpaces->isChecked();
tabSettings.m_autoIndent = m_d->m_page.autoIndent->isChecked(); tabSettings.m_autoIndent = m_d->m_page.autoIndent->isChecked();
tabSettings.m_autoParentheses= m_d->m_page.autoParentheses->isChecked();
tabSettings.m_smartBackspace = m_d->m_page.smartBackspace->isChecked(); tabSettings.m_smartBackspace = m_d->m_page.smartBackspace->isChecked();
tabSettings.m_tabSize = m_d->m_page.tabSize->value(); tabSettings.m_tabSize = m_d->m_page.tabSize->value();
tabSettings.m_indentSize = m_d->m_page.indentSize->value(); tabSettings.m_indentSize = m_d->m_page.indentSize->value();
@@ -146,6 +147,7 @@ void BehaviorSettingsPage::settingsToUI()
const TabSettings &tabSettings = m_d->m_tabSettings; const TabSettings &tabSettings = m_d->m_tabSettings;
m_d->m_page.insertSpaces->setChecked(tabSettings.m_spacesForTabs); m_d->m_page.insertSpaces->setChecked(tabSettings.m_spacesForTabs);
m_d->m_page.autoIndent->setChecked(tabSettings.m_autoIndent); m_d->m_page.autoIndent->setChecked(tabSettings.m_autoIndent);
m_d->m_page.autoParentheses->setChecked(tabSettings.m_autoParentheses);
m_d->m_page.smartBackspace->setChecked(tabSettings.m_smartBackspace); m_d->m_page.smartBackspace->setChecked(tabSettings.m_smartBackspace);
m_d->m_page.tabSize->setValue(tabSettings.m_tabSize); m_d->m_page.tabSize->setValue(tabSettings.m_tabSize);
m_d->m_page.indentSize->setValue(tabSettings.m_indentSize); m_d->m_page.indentSize->setValue(tabSettings.m_indentSize);

View File

@@ -14,7 +14,7 @@
<item> <item>
<widget class="QGroupBox" name="groupBoxTabAndIndentSettings"> <widget class="QGroupBox" name="groupBoxTabAndIndentSettings">
<property name="title"> <property name="title">
<string>Tabs and Indentation</string> <string>Tabs, Indentation, and Parentheses</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
@@ -133,6 +133,13 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="3" column="0">
<widget class="QCheckBox" name="autoParentheses">
<property name="text">
<string>Enable automatic &amp;parentheses</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item> <item>

View File

@@ -38,6 +38,7 @@
static const char *spacesForTabsKey = "SpacesForTabs"; static const char *spacesForTabsKey = "SpacesForTabs";
static const char *smartBackspaceKey = "SmartBackspace"; static const char *smartBackspaceKey = "SmartBackspace";
static const char *autoIndentKey = "AutoIndent"; static const char *autoIndentKey = "AutoIndent";
static const char *autoParenthesesKey= "AutoParentheses";
static const char *tabSizeKey = "TabSize"; static const char *tabSizeKey = "TabSize";
static const char *indentSizeKey = "IndentSize"; static const char *indentSizeKey = "IndentSize";
static const char *tabKeyBehaviorKey = "TabKeyBehavior"; static const char *tabKeyBehaviorKey = "TabKeyBehavior";
@@ -48,6 +49,7 @@ namespace TextEditor {
TabSettings::TabSettings() : TabSettings::TabSettings() :
m_spacesForTabs(true), m_spacesForTabs(true),
m_autoIndent(true), m_autoIndent(true),
m_autoParentheses(true),
m_smartBackspace(false), m_smartBackspace(false),
m_tabSize(8), m_tabSize(8),
m_indentSize(4), m_indentSize(4),
@@ -63,6 +65,7 @@ void TabSettings::toSettings(const QString &category, QSettings *s) const
s->beginGroup(group); s->beginGroup(group);
s->setValue(QLatin1String(spacesForTabsKey), m_spacesForTabs); s->setValue(QLatin1String(spacesForTabsKey), m_spacesForTabs);
s->setValue(QLatin1String(autoIndentKey), m_autoIndent); s->setValue(QLatin1String(autoIndentKey), m_autoIndent);
s->setValue(QLatin1String(autoParenthesesKey), m_autoParentheses);
s->setValue(QLatin1String(smartBackspaceKey), m_smartBackspace); s->setValue(QLatin1String(smartBackspaceKey), m_smartBackspace);
s->setValue(QLatin1String(tabSizeKey), m_tabSize); s->setValue(QLatin1String(tabSizeKey), m_tabSize);
s->setValue(QLatin1String(indentSizeKey), m_indentSize); s->setValue(QLatin1String(indentSizeKey), m_indentSize);
@@ -81,6 +84,7 @@ void TabSettings::fromSettings(const QString &category, const QSettings *s)
m_spacesForTabs = s->value(group + QLatin1String(spacesForTabsKey), m_spacesForTabs).toBool(); m_spacesForTabs = s->value(group + QLatin1String(spacesForTabsKey), m_spacesForTabs).toBool();
m_autoIndent = s->value(group + QLatin1String(autoIndentKey), m_autoIndent).toBool(); m_autoIndent = s->value(group + QLatin1String(autoIndentKey), m_autoIndent).toBool();
m_autoParentheses = s->value(group + QLatin1String(autoParenthesesKey), m_autoParentheses).toBool();
m_smartBackspace = s->value(group + QLatin1String(smartBackspaceKey), m_smartBackspace).toBool(); m_smartBackspace = s->value(group + QLatin1String(smartBackspaceKey), m_smartBackspace).toBool();
m_tabSize = s->value(group + QLatin1String(tabSizeKey), m_tabSize).toInt(); m_tabSize = s->value(group + QLatin1String(tabSizeKey), m_tabSize).toInt();
m_indentSize = s->value(group + QLatin1String(indentSizeKey), m_indentSize).toInt(); m_indentSize = s->value(group + QLatin1String(indentSizeKey), m_indentSize).toInt();
@@ -259,6 +263,7 @@ bool TabSettings::equals(const TabSettings &ts) const
{ {
return m_spacesForTabs == ts.m_spacesForTabs return m_spacesForTabs == ts.m_spacesForTabs
&& m_autoIndent == ts.m_autoIndent && m_autoIndent == ts.m_autoIndent
&& m_autoParentheses== ts.m_autoParentheses
&& m_smartBackspace == ts.m_smartBackspace && m_smartBackspace == ts.m_smartBackspace
&& m_tabSize == ts.m_tabSize && m_tabSize == ts.m_tabSize
&& m_indentSize == ts.m_indentSize && m_indentSize == ts.m_indentSize

View File

@@ -74,6 +74,7 @@ struct TEXTEDITOR_EXPORT TabSettings
bool m_spacesForTabs; bool m_spacesForTabs;
bool m_autoIndent; bool m_autoIndent;
bool m_autoParentheses;
bool m_smartBackspace; bool m_smartBackspace;
int m_tabSize; int m_tabSize;
int m_indentSize; int m_indentSize;