forked from qt-creator/qt-creator
Generalized the auto complete of braces.
This commit is contained in:
@@ -605,7 +605,6 @@ CPPEditor::CPPEditor(QWidget *parent)
|
|||||||
, m_inRename(false)
|
, m_inRename(false)
|
||||||
, m_inRenameChanged(false)
|
, m_inRenameChanged(false)
|
||||||
, m_firstRenameChange(false)
|
, m_firstRenameChange(false)
|
||||||
, m_allowSkippingOfBlockEnd(false)
|
|
||||||
{
|
{
|
||||||
m_initialized = false;
|
m_initialized = false;
|
||||||
qRegisterMetaType<SemanticInfo>("SemanticInfo");
|
qRegisterMetaType<SemanticInfo>("SemanticInfo");
|
||||||
@@ -1510,209 +1509,19 @@ bool CPPEditor::isElectricCharacter(const QChar &ch) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString CPPEditor::insertMatchingBrace(const QTextCursor &tc, const QString &text,
|
||||||
static void countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen)
|
const QChar &la, int *skippedChars) const
|
||||||
{
|
{
|
||||||
if (c == open)
|
MatchingText m;
|
||||||
++*stillopen;
|
return m.insertMatchingBrace(tc, text, la, skippedChars);
|
||||||
else if (c == close)
|
|
||||||
--*stillopen;
|
|
||||||
|
|
||||||
if (*stillopen < 0) {
|
|
||||||
*errors += -1 * (*stillopen);
|
|
||||||
*stillopen = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void countBrackets(QTextCursor cursor, int from, int end, QChar open, QChar close, int *errors, int *stillopen)
|
QString CPPEditor::insertParagraphSeparator(const QTextCursor &tc) const
|
||||||
{
|
{
|
||||||
cursor.setPosition(from);
|
MatchingText m;
|
||||||
QTextBlock block = cursor.block();
|
return m.insertParagraphSeparator(tc);
|
||||||
while (block.isValid() && block.position() < end) {
|
|
||||||
TextEditor::Parentheses parenList = TextEditor::TextEditDocumentLayout::parentheses(block);
|
|
||||||
if (!parenList.isEmpty() && !TextEditor::TextEditDocumentLayout::ifdefedOut(block)) {
|
|
||||||
for (int i = 0; i < parenList.count(); ++i) {
|
|
||||||
TextEditor::Parenthesis paren = parenList.at(i);
|
|
||||||
int position = block.position() + paren.pos;
|
|
||||||
if (position < from || position >= end)
|
|
||||||
continue;
|
|
||||||
countBracket(open, close, paren.chr, errors, stillopen);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
block = block.next();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString CPPEditor::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
|
|
||||||
{
|
|
||||||
const bool checkBlockEnd = m_allowSkippingOfBlockEnd;
|
|
||||||
m_allowSkippingOfBlockEnd = false; // consume blockEnd.
|
|
||||||
|
|
||||||
if (!contextAllowsAutoParentheses(cursor, textToInsert))
|
|
||||||
return QString();
|
|
||||||
|
|
||||||
QString text = textToInsert;
|
|
||||||
const QChar lookAhead = characterAt(cursor.selectionEnd());
|
|
||||||
|
|
||||||
QChar character = textToInsert.at(0);
|
|
||||||
QString parentheses = QLatin1String("()");
|
|
||||||
QString brackets = QLatin1String("[]");
|
|
||||||
if (parentheses.contains(character) || brackets.contains(character)) {
|
|
||||||
QTextCursor tmp= cursor;
|
|
||||||
TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
|
|
||||||
int blockStart = tmp.isNull() ? 0 : tmp.position();
|
|
||||||
tmp = cursor;
|
|
||||||
TextEditor::TextBlockUserData::findNextBlockClosingParenthesis(&tmp);
|
|
||||||
int blockEnd = tmp.isNull() ? (cursor.document()->characterCount()-1) : tmp.position();
|
|
||||||
QChar openChar = parentheses.contains(character) ? QLatin1Char('(') : QLatin1Char('[');
|
|
||||||
QChar closeChar = parentheses.contains(character) ? QLatin1Char(')') : QLatin1Char(']');
|
|
||||||
|
|
||||||
int errors = 0;
|
|
||||||
int stillopen = 0;
|
|
||||||
countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
|
|
||||||
int errorsBeforeInsertion = errors + stillopen;
|
|
||||||
errors = 0;
|
|
||||||
stillopen = 0;
|
|
||||||
countBrackets(cursor, blockStart, cursor.position(), openChar, closeChar, &errors, &stillopen);
|
|
||||||
countBracket(openChar, closeChar, character, &errors, &stillopen);
|
|
||||||
countBrackets(cursor, cursor.position(), blockEnd, openChar, closeChar, &errors, &stillopen);
|
|
||||||
int errorsAfterInsertion = errors + stillopen;
|
|
||||||
if (errorsAfterInsertion < errorsBeforeInsertion)
|
|
||||||
return QString(); // insertion fixes parentheses or bracket errors, do not auto complete
|
|
||||||
}
|
|
||||||
|
|
||||||
MatchingText matchingText;
|
|
||||||
int skippedChars = 0;
|
|
||||||
const QString autoText = matchingText.insertMatchingBrace(cursor, text, lookAhead, &skippedChars);
|
|
||||||
|
|
||||||
if (checkBlockEnd && textToInsert.at(0) == QLatin1Char('}')) {
|
|
||||||
if (textToInsert.length() > 1)
|
|
||||||
qWarning() << "*** handle event compression";
|
|
||||||
|
|
||||||
int startPos = cursor.selectionEnd(), pos = startPos;
|
|
||||||
while (characterAt(pos).isSpace())
|
|
||||||
++pos;
|
|
||||||
|
|
||||||
if (characterAt(pos) == QLatin1Char('}'))
|
|
||||||
skippedChars += (pos - startPos) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (skippedChars) {
|
|
||||||
const int pos = cursor.position();
|
|
||||||
cursor.setPosition(pos + skippedChars);
|
|
||||||
cursor.setPosition(pos, QTextCursor::KeepAnchor);
|
|
||||||
}
|
|
||||||
|
|
||||||
return autoText;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CPPEditor::autoBackspace(QTextCursor &cursor)
|
|
||||||
{
|
|
||||||
m_allowSkippingOfBlockEnd = false;
|
|
||||||
|
|
||||||
int pos = cursor.position();
|
|
||||||
QTextCursor c = cursor;
|
|
||||||
c.setPosition(pos - 1);
|
|
||||||
|
|
||||||
QChar lookAhead = characterAt(pos);
|
|
||||||
QChar lookBehind = characterAt(pos-1);
|
|
||||||
QChar lookFurtherBehind = characterAt(pos-2);
|
|
||||||
|
|
||||||
QChar character = lookBehind;
|
|
||||||
if (character == QLatin1Char('(') || character == QLatin1Char('[')) {
|
|
||||||
QTextCursor tmp = cursor;
|
|
||||||
TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
|
|
||||||
int blockStart = tmp.isNull() ? 0 : tmp.position();
|
|
||||||
tmp = cursor;
|
|
||||||
TextEditor::TextBlockUserData::findNextBlockClosingParenthesis(&tmp);
|
|
||||||
int blockEnd = tmp.isNull() ? (cursor.document()->characterCount()-1) : tmp.position();
|
|
||||||
QChar openChar = character;
|
|
||||||
QChar closeChar = (character == QLatin1Char('(')) ? QLatin1Char(')') : QLatin1Char(']');
|
|
||||||
|
|
||||||
int errors = 0;
|
|
||||||
int stillopen = 0;
|
|
||||||
countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
|
|
||||||
int errorsBeforeDeletion = errors + stillopen;
|
|
||||||
errors = 0;
|
|
||||||
stillopen = 0;
|
|
||||||
countBrackets(cursor, blockStart, pos - 1, openChar, closeChar, &errors, &stillopen);
|
|
||||||
countBrackets(cursor, pos, blockEnd, openChar, closeChar, &errors, &stillopen);
|
|
||||||
int errorsAfterDeletion = errors + stillopen;
|
|
||||||
|
|
||||||
if (errorsAfterDeletion < errorsBeforeDeletion)
|
|
||||||
return false; // insertion fixes parentheses or bracket errors, do not auto complete
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((lookBehind == QLatin1Char('(') && lookAhead == QLatin1Char(')'))
|
|
||||||
|| (lookBehind == QLatin1Char('[') && lookAhead == QLatin1Char(']'))
|
|
||||||
|| (lookBehind == QLatin1Char('"') && lookAhead == QLatin1Char('"')
|
|
||||||
&& lookFurtherBehind != QLatin1Char('\\'))
|
|
||||||
|| (lookBehind == QLatin1Char('\'') && lookAhead == QLatin1Char('\'')
|
|
||||||
&& lookFurtherBehind != QLatin1Char('\\'))) {
|
|
||||||
if (! isInComment(c)) {
|
|
||||||
cursor.beginEditBlock();
|
|
||||||
cursor.deleteChar();
|
|
||||||
cursor.deletePreviousChar();
|
|
||||||
cursor.endEditBlock();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int CPPEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
|
|
||||||
{
|
|
||||||
if (characterAt(cursor.position()-1) != QLatin1Char('{'))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (!contextAllowsAutoParentheses(cursor))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// verify that we indeed do have an extra opening brace in the document
|
|
||||||
int braceDepth = document()->lastBlock().userState();
|
|
||||||
if (braceDepth >= 0)
|
|
||||||
braceDepth >>= 8;
|
|
||||||
else
|
|
||||||
braceDepth= 0;
|
|
||||||
|
|
||||||
if (braceDepth <= 0)
|
|
||||||
return 0; // braces are all balanced or worse, no need to do anything
|
|
||||||
|
|
||||||
// we have an extra brace , let's see if we should close it
|
|
||||||
|
|
||||||
|
|
||||||
/* verify that the next block is not further intended compared to the current block.
|
|
||||||
This covers the following case:
|
|
||||||
|
|
||||||
if (condition) {|
|
|
||||||
statement;
|
|
||||||
*/
|
|
||||||
const TabSettings &ts = tabSettings();
|
|
||||||
QTextBlock block = cursor.block();
|
|
||||||
int indentation = ts.indentationColumn(block.text());
|
|
||||||
if (block.next().isValid()
|
|
||||||
&& ts.indentationColumn(block.next().text()) > indentation)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
int pos = cursor.position();
|
|
||||||
|
|
||||||
MatchingText matchingText;
|
|
||||||
const QString textToInsert = matchingText.insertParagraphSeparator(cursor);
|
|
||||||
|
|
||||||
cursor.insertText(textToInsert);
|
|
||||||
cursor.setPosition(pos);
|
|
||||||
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);
|
|
||||||
m_allowSkippingOfBlockEnd = true;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CPPEditor::contextAllowsAutoParentheses(const QTextCursor &cursor,
|
bool CPPEditor::contextAllowsAutoParentheses(const QTextCursor &cursor,
|
||||||
const QString &textToInsert) const
|
const QString &textToInsert) const
|
||||||
|
|||||||
@@ -214,15 +214,17 @@ protected:
|
|||||||
TextEditor::BaseTextEditorEditable *createEditableInterface();
|
TextEditor::BaseTextEditorEditable *createEditableInterface();
|
||||||
|
|
||||||
// These override BaseTextEditor
|
// These override BaseTextEditor
|
||||||
bool isElectricCharacter(const QChar &ch) const;
|
virtual bool isElectricCharacter(const QChar &ch) const;
|
||||||
QString autoComplete(QTextCursor &cursor, const QString &text) const;
|
|
||||||
bool autoBackspace(QTextCursor &cursor);
|
|
||||||
int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor);
|
|
||||||
|
|
||||||
bool contextAllowsAutoParentheses(const QTextCursor &cursor,
|
virtual QString insertMatchingBrace(const QTextCursor &tc, const QString &text,
|
||||||
|
const QChar &la, int *skippedChars) const;
|
||||||
|
|
||||||
|
virtual QString insertParagraphSeparator(const QTextCursor &tc) const;
|
||||||
|
|
||||||
|
virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor,
|
||||||
const QString &textToInsert = QString()) const;
|
const QString &textToInsert = QString()) const;
|
||||||
|
|
||||||
bool isInComment(const QTextCursor &cursor) const;
|
virtual bool isInComment(const QTextCursor &cursor) const;
|
||||||
|
|
||||||
CPlusPlus::Symbol *findCanonicalSymbol(const QTextCursor &cursor,
|
CPlusPlus::Symbol *findCanonicalSymbol(const QTextCursor &cursor,
|
||||||
CPlusPlus::Document::Ptr doc,
|
CPlusPlus::Document::Ptr doc,
|
||||||
@@ -304,8 +306,6 @@ private:
|
|||||||
QTextCursor m_currentRenameSelectionBegin;
|
QTextCursor m_currentRenameSelectionBegin;
|
||||||
QTextCursor m_currentRenameSelectionEnd;
|
QTextCursor m_currentRenameSelectionEnd;
|
||||||
|
|
||||||
mutable bool m_allowSkippingOfBlockEnd;
|
|
||||||
|
|
||||||
SemanticHighlighter *m_semanticHighlighter;
|
SemanticHighlighter *m_semanticHighlighter;
|
||||||
SemanticInfo m_lastSemanticInfo;
|
SemanticInfo m_lastSemanticInfo;
|
||||||
QList<QuickFixOperationPtr> m_quickFixes;
|
QList<QuickFixOperationPtr> m_quickFixes;
|
||||||
|
|||||||
@@ -183,6 +183,7 @@ BaseTextEditor::BaseTextEditor(QWidget *parent)
|
|||||||
d->m_lastScrollPos = -1;
|
d->m_lastScrollPos = -1;
|
||||||
setCursorWidth(2);
|
setCursorWidth(2);
|
||||||
|
|
||||||
|
d->m_allowSkippingOfBlockEnd = false;
|
||||||
|
|
||||||
// from RESEARCH
|
// from RESEARCH
|
||||||
|
|
||||||
@@ -3555,23 +3556,236 @@ bool BaseTextEditor::isElectricCharacter(const QChar &) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString BaseTextEditor::autoComplete(QTextCursor &cursor, const QString &text) const
|
void BaseTextEditor::countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen)
|
||||||
{
|
{
|
||||||
Q_UNUSED(cursor);
|
if (c == open)
|
||||||
Q_UNUSED(text);
|
++*stillopen;
|
||||||
return QString();
|
else if (c == close)
|
||||||
|
--*stillopen;
|
||||||
|
|
||||||
|
if (*stillopen < 0) {
|
||||||
|
*errors += -1 * (*stillopen);
|
||||||
|
*stillopen = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BaseTextEditor::autoBackspace(QTextCursor &cursor)
|
void BaseTextEditor::countBrackets(QTextCursor cursor, int from, int end, QChar open, QChar close, int *errors, int *stillopen)
|
||||||
|
{
|
||||||
|
cursor.setPosition(from);
|
||||||
|
QTextBlock block = cursor.block();
|
||||||
|
while (block.isValid() && block.position() < end) {
|
||||||
|
TextEditor::Parentheses parenList = TextEditor::TextEditDocumentLayout::parentheses(block);
|
||||||
|
if (!parenList.isEmpty() && !TextEditor::TextEditDocumentLayout::ifdefedOut(block)) {
|
||||||
|
for (int i = 0; i < parenList.count(); ++i) {
|
||||||
|
TextEditor::Parenthesis paren = parenList.at(i);
|
||||||
|
int position = block.position() + paren.pos;
|
||||||
|
if (position < from || position >= end)
|
||||||
|
continue;
|
||||||
|
countBracket(open, close, paren.chr, errors, stillopen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
block = block.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BaseTextEditor::contextAllowsAutoParentheses(const QTextCursor &cursor,
|
||||||
|
const QString &textToInsert) const
|
||||||
|
{
|
||||||
|
Q_UNUSED(cursor);
|
||||||
|
Q_UNUSED(textToInsert);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BaseTextEditor::isInComment(const QTextCursor &cursor) const
|
||||||
{
|
{
|
||||||
Q_UNUSED(cursor);
|
Q_UNUSED(cursor);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString BaseTextEditor::insertMatchingBrace(const QTextCursor &tc, const QString &text,
|
||||||
|
const QChar &la, int *skippedChars) const
|
||||||
|
{
|
||||||
|
Q_UNUSED(tc);
|
||||||
|
Q_UNUSED(text);
|
||||||
|
Q_UNUSED(la);
|
||||||
|
Q_UNUSED(skippedChars);
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString BaseTextEditor::insertParagraphSeparator(const QTextCursor &tc) const
|
||||||
|
{
|
||||||
|
Q_UNUSED(tc);
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString BaseTextEditor::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
|
||||||
|
{
|
||||||
|
const bool checkBlockEnd = d->m_allowSkippingOfBlockEnd;
|
||||||
|
d->m_allowSkippingOfBlockEnd = false; // consume blockEnd.
|
||||||
|
|
||||||
|
if (!contextAllowsAutoParentheses(cursor, textToInsert))
|
||||||
|
return QString();
|
||||||
|
|
||||||
|
const QString text = textToInsert;
|
||||||
|
const QChar lookAhead = characterAt(cursor.selectionEnd());
|
||||||
|
|
||||||
|
QChar character = textToInsert.at(0);
|
||||||
|
const QString parentheses = QLatin1String("()");
|
||||||
|
const QString brackets = QLatin1String("[]");
|
||||||
|
if (parentheses.contains(character) || brackets.contains(character)) {
|
||||||
|
QTextCursor tmp= cursor;
|
||||||
|
TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
|
||||||
|
int blockStart = tmp.isNull() ? 0 : tmp.position();
|
||||||
|
tmp = cursor;
|
||||||
|
TextEditor::TextBlockUserData::findNextBlockClosingParenthesis(&tmp);
|
||||||
|
int blockEnd = tmp.isNull() ? (cursor.document()->characterCount()-1) : tmp.position();
|
||||||
|
const QChar openChar = parentheses.contains(character) ? QLatin1Char('(') : QLatin1Char('[');
|
||||||
|
const QChar closeChar = parentheses.contains(character) ? QLatin1Char(')') : QLatin1Char(']');
|
||||||
|
|
||||||
|
int errors = 0;
|
||||||
|
int stillopen = 0;
|
||||||
|
countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
|
||||||
|
int errorsBeforeInsertion = errors + stillopen;
|
||||||
|
errors = 0;
|
||||||
|
stillopen = 0;
|
||||||
|
countBrackets(cursor, blockStart, cursor.position(), openChar, closeChar, &errors, &stillopen);
|
||||||
|
countBracket(openChar, closeChar, character, &errors, &stillopen);
|
||||||
|
countBrackets(cursor, cursor.position(), blockEnd, openChar, closeChar, &errors, &stillopen);
|
||||||
|
int errorsAfterInsertion = errors + stillopen;
|
||||||
|
if (errorsAfterInsertion < errorsBeforeInsertion)
|
||||||
|
return QString(); // insertion fixes parentheses or bracket errors, do not auto complete
|
||||||
|
}
|
||||||
|
|
||||||
|
int skippedChars = 0;
|
||||||
|
const QString autoText = insertMatchingBrace(cursor, text, lookAhead, &skippedChars);
|
||||||
|
|
||||||
|
if (checkBlockEnd && textToInsert.at(0) == QLatin1Char('}')) {
|
||||||
|
if (textToInsert.length() > 1)
|
||||||
|
qWarning() << "*** handle event compression";
|
||||||
|
|
||||||
|
int startPos = cursor.selectionEnd(), pos = startPos;
|
||||||
|
while (characterAt(pos).isSpace())
|
||||||
|
++pos;
|
||||||
|
|
||||||
|
if (characterAt(pos) == QLatin1Char('}'))
|
||||||
|
skippedChars += (pos - startPos) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (skippedChars) {
|
||||||
|
const int pos = cursor.position();
|
||||||
|
cursor.setPosition(pos + skippedChars);
|
||||||
|
cursor.setPosition(pos, QTextCursor::KeepAnchor);
|
||||||
|
}
|
||||||
|
|
||||||
|
return autoText;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BaseTextEditor::autoBackspace(QTextCursor &cursor)
|
||||||
|
{
|
||||||
|
d->m_allowSkippingOfBlockEnd = false;
|
||||||
|
|
||||||
|
int pos = cursor.position();
|
||||||
|
QTextCursor c = cursor;
|
||||||
|
c.setPosition(pos - 1);
|
||||||
|
|
||||||
|
QChar lookAhead = characterAt(pos);
|
||||||
|
QChar lookBehind = characterAt(pos-1);
|
||||||
|
QChar lookFurtherBehind = characterAt(pos-2);
|
||||||
|
|
||||||
|
QChar character = lookBehind;
|
||||||
|
if (character == QLatin1Char('(') || character == QLatin1Char('[')) {
|
||||||
|
QTextCursor tmp = cursor;
|
||||||
|
TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
|
||||||
|
int blockStart = tmp.isNull() ? 0 : tmp.position();
|
||||||
|
tmp = cursor;
|
||||||
|
TextEditor::TextBlockUserData::findNextBlockClosingParenthesis(&tmp);
|
||||||
|
int blockEnd = tmp.isNull() ? (cursor.document()->characterCount()-1) : tmp.position();
|
||||||
|
QChar openChar = character;
|
||||||
|
QChar closeChar = (character == QLatin1Char('(')) ? QLatin1Char(')') : QLatin1Char(']');
|
||||||
|
|
||||||
|
int errors = 0;
|
||||||
|
int stillopen = 0;
|
||||||
|
countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
|
||||||
|
int errorsBeforeDeletion = errors + stillopen;
|
||||||
|
errors = 0;
|
||||||
|
stillopen = 0;
|
||||||
|
countBrackets(cursor, blockStart, pos - 1, openChar, closeChar, &errors, &stillopen);
|
||||||
|
countBrackets(cursor, pos, blockEnd, openChar, closeChar, &errors, &stillopen);
|
||||||
|
int errorsAfterDeletion = errors + stillopen;
|
||||||
|
|
||||||
|
if (errorsAfterDeletion < errorsBeforeDeletion)
|
||||||
|
return false; // insertion fixes parentheses or bracket errors, do not auto complete
|
||||||
|
}
|
||||||
|
|
||||||
|
// ### this code needs to be generalized
|
||||||
|
if ((lookBehind == QLatin1Char('(') && lookAhead == QLatin1Char(')'))
|
||||||
|
|| (lookBehind == QLatin1Char('[') && lookAhead == QLatin1Char(']'))
|
||||||
|
|| (lookBehind == QLatin1Char('"') && lookAhead == QLatin1Char('"')
|
||||||
|
&& lookFurtherBehind != QLatin1Char('\\'))
|
||||||
|
|| (lookBehind == QLatin1Char('\'') && lookAhead == QLatin1Char('\'')
|
||||||
|
&& lookFurtherBehind != QLatin1Char('\\'))) {
|
||||||
|
if (! isInComment(c)) {
|
||||||
|
cursor.beginEditBlock();
|
||||||
|
cursor.deleteChar();
|
||||||
|
cursor.deletePreviousChar();
|
||||||
|
cursor.endEditBlock();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
|
int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
|
||||||
{
|
{
|
||||||
Q_UNUSED(cursor);
|
if (characterAt(cursor.position()-1) != QLatin1Char('{'))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if (!contextAllowsAutoParentheses(cursor))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// verify that we indeed do have an extra opening brace in the document
|
||||||
|
int braceDepth = document()->lastBlock().userState();
|
||||||
|
if (braceDepth >= 0)
|
||||||
|
braceDepth >>= 8;
|
||||||
|
else
|
||||||
|
braceDepth= 0;
|
||||||
|
|
||||||
|
if (braceDepth <= 0)
|
||||||
|
return 0; // braces are all balanced or worse, no need to do anything
|
||||||
|
|
||||||
|
// we have an extra brace , let's see if we should close it
|
||||||
|
|
||||||
|
|
||||||
|
/* verify that the next block is not further intended compared to the current block.
|
||||||
|
This covers the following case:
|
||||||
|
|
||||||
|
if (condition) {|
|
||||||
|
statement;
|
||||||
|
*/
|
||||||
|
const TabSettings &ts = tabSettings();
|
||||||
|
QTextBlock block = cursor.block();
|
||||||
|
int indentation = ts.indentationColumn(block.text());
|
||||||
|
if (block.next().isValid()
|
||||||
|
&& ts.indentationColumn(block.next().text()) > indentation)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
int pos = cursor.position();
|
||||||
|
|
||||||
|
const QString textToInsert = insertParagraphSeparator(cursor);
|
||||||
|
|
||||||
|
cursor.insertText(textToInsert);
|
||||||
|
cursor.setPosition(pos);
|
||||||
|
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);
|
||||||
|
d->m_allowSkippingOfBlockEnd = true;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseTextEditor::indentBlock(QTextDocument *, QTextBlock, QChar)
|
void BaseTextEditor::indentBlock(QTextDocument *, QTextBlock, QChar)
|
||||||
|
|||||||
@@ -542,6 +542,21 @@ protected:
|
|||||||
// Reindent at cursor. Selection will be adjusted according to the indentation change of the first block
|
// Reindent at cursor. Selection will be adjusted according to the indentation change of the first block
|
||||||
virtual void reindent(QTextDocument *doc, const QTextCursor &cursor);
|
virtual void reindent(QTextDocument *doc, const QTextCursor &cursor);
|
||||||
|
|
||||||
|
virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor, const QString &textToInsert = QString()) const;
|
||||||
|
|
||||||
|
// Returns true if the cursor is inside a comment.
|
||||||
|
virtual bool isInComment(const QTextCursor &cursor) const;
|
||||||
|
|
||||||
|
virtual QString insertMatchingBrace(const QTextCursor &tc, const QString &text, const QChar &la, int *skippedChars) const;
|
||||||
|
|
||||||
|
// Returns the text that needs to be inserted
|
||||||
|
virtual QString insertParagraphSeparator(const QTextCursor &tc) const;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
struct Link
|
struct Link
|
||||||
{
|
{
|
||||||
Link(const QString &fileName = QString(),
|
Link(const QString &fileName = QString(),
|
||||||
|
|||||||
@@ -158,6 +158,7 @@ public:
|
|||||||
BaseTextEditor *q;
|
BaseTextEditor *q;
|
||||||
bool m_contentsChanged;
|
bool m_contentsChanged;
|
||||||
bool m_lastCursorChangeWasInteresting;
|
bool m_lastCursorChangeWasInteresting;
|
||||||
|
bool m_allowSkippingOfBlockEnd;
|
||||||
|
|
||||||
QList<QTextEdit::ExtraSelection> m_syntaxHighlighterSelections;
|
QList<QTextEdit::ExtraSelection> m_syntaxHighlighterSelections;
|
||||||
QTextEdit::ExtraSelection m_lineSelection;
|
QTextEdit::ExtraSelection m_lineSelection;
|
||||||
|
|||||||
Reference in New Issue
Block a user