forked from qt-creator/qt-creator
Don't insert automatic parenthesis in comments and strings
This commit is contained in:
@@ -1268,6 +1268,13 @@ bool CPPEditor::isElectricCharacter(const QChar &ch) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CPPEditor::contextAllowsAutoParenthesis(const QTextCursor &cursor) const
|
||||||
|
{
|
||||||
|
CPlusPlus::TokenUnderCursor tokenUnderCursor;
|
||||||
|
const SimpleToken tk = tokenUnderCursor(cursor);
|
||||||
|
return !(tk.isComment() || tk.isLiteral());
|
||||||
|
}
|
||||||
|
|
||||||
void CPPEditor::indentInsertedText(const QTextCursor &tc)
|
void CPPEditor::indentInsertedText(const QTextCursor &tc)
|
||||||
{
|
{
|
||||||
indent(tc.document(), tc, QChar::Null);
|
indent(tc.document(), tc, QChar::Null);
|
||||||
|
|||||||
@@ -211,8 +211,9 @@ protected:
|
|||||||
|
|
||||||
TextEditor::BaseTextEditorEditable *createEditableInterface();
|
TextEditor::BaseTextEditorEditable *createEditableInterface();
|
||||||
|
|
||||||
// Rertuns true if key triggers anindent.
|
// These override BaseTextEditor
|
||||||
virtual bool isElectricCharacter(const QChar &ch) const;
|
bool isElectricCharacter(const QChar &ch) const;
|
||||||
|
bool contextAllowsAutoParenthesis(const QTextCursor &cursor) const;
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void updateFileName();
|
void updateFileName();
|
||||||
|
|||||||
@@ -1073,9 +1073,8 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
QString text = e->text();
|
QString text = e->text();
|
||||||
QString autoText;
|
QString autoText;
|
||||||
|
|
||||||
// TODO disable this inside string or character literals
|
|
||||||
if (d->m_autoParenthesesEnabled && d->m_document->tabSettings().m_autoParentheses) {
|
if (d->m_autoParenthesesEnabled && d->m_document->tabSettings().m_autoParentheses) {
|
||||||
foreach(QChar c, text) {
|
foreach (QChar c, text) {
|
||||||
QChar close;
|
QChar close;
|
||||||
if (c == QLatin1Char('('))
|
if (c == QLatin1Char('('))
|
||||||
close = QLatin1Char(')');
|
close = QLatin1Char(')');
|
||||||
@@ -1101,7 +1100,7 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
}
|
}
|
||||||
QChar electricChar;
|
QChar electricChar;
|
||||||
if (d->m_document->tabSettings().m_autoIndent) {
|
if (d->m_document->tabSettings().m_autoIndent) {
|
||||||
foreach(QChar c, text) {
|
foreach (QChar c, text) {
|
||||||
if (isElectricCharacter(c)) {
|
if (isElectricCharacter(c)) {
|
||||||
electricChar = c;
|
electricChar = c;
|
||||||
break;
|
break;
|
||||||
@@ -1110,8 +1109,11 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
}
|
}
|
||||||
if (!electricChar.isNull())
|
if (!electricChar.isNull())
|
||||||
cursor.beginEditBlock();
|
cursor.beginEditBlock();
|
||||||
|
|
||||||
|
bool insertAutoParenthesis = !autoText.isEmpty() && contextAllowsAutoParenthesis(cursor);
|
||||||
cursor.insertText(text);
|
cursor.insertText(text);
|
||||||
if (!autoText.isEmpty()) {
|
|
||||||
|
if (insertAutoParenthesis) {
|
||||||
int pos = cursor.position();
|
int pos = cursor.position();
|
||||||
cursor.insertText(autoText);
|
cursor.insertText(autoText);
|
||||||
cursor.setPosition(pos);
|
cursor.setPosition(pos);
|
||||||
@@ -3316,6 +3318,11 @@ bool BaseTextEditor::isElectricCharacter(const QChar &) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool BaseTextEditor::contextAllowsAutoParenthesis(const QTextCursor &) const
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void BaseTextEditor::indentBlock(QTextDocument *, QTextBlock, QChar)
|
void BaseTextEditor::indentBlock(QTextDocument *, QTextBlock, QChar)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -508,6 +508,8 @@ protected:
|
|||||||
|
|
||||||
// Returns true if key triggers an indent.
|
// Returns true if key triggers an indent.
|
||||||
virtual bool isElectricCharacter(const QChar &ch) const;
|
virtual bool isElectricCharacter(const QChar &ch) const;
|
||||||
|
// Returns true if automatic brace matching should be enabled in the context of the given cursor
|
||||||
|
virtual bool contextAllowsAutoParenthesis(const QTextCursor &cursor) const;
|
||||||
// Indent a text block based on previous line. Default does nothing
|
// Indent a text block based on previous line. Default does nothing
|
||||||
virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);
|
virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);
|
||||||
// Indent at cursor. Calls indentBlock for selection or current line.
|
// Indent at cursor. Calls indentBlock for selection or current line.
|
||||||
|
|||||||
Reference in New Issue
Block a user