forked from qt-creator/qt-creator
Editors: Move auto-completion code out of the editor
This is basically a continuation of the commits which refactor code out of the base text editor. For instance,36fa1de4c6and3a684586fa. Also removed the doXXXX() forwarding methods.
This commit is contained in:
@@ -1552,8 +1552,9 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
|
||||
const TabSettings &ts = d->m_document->tabSettings();
|
||||
cursor.beginEditBlock();
|
||||
|
||||
int extraBlocks = paragraphSeparatorAboutToBeInserted(cursor); // virtual
|
||||
int extraBlocks = d->m_autoCompleter->paragraphSeparatorAboutToBeInserted(cursor);
|
||||
|
||||
QString previousIndentationString;
|
||||
if (ts.m_autoIndent) {
|
||||
cursor.insertBlock();
|
||||
indent(document(), cursor, QChar::Null);
|
||||
@@ -1561,8 +1562,10 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
|
||||
cursor.insertBlock();
|
||||
|
||||
// After inserting the block, to avoid duplicating whitespace on the same line
|
||||
const QString previousBlockText = cursor.block().previous().text();
|
||||
cursor.insertText(ts.indentationString(previousBlockText));
|
||||
const QString &previousBlockText = cursor.block().previous().text();
|
||||
previousIndentationString = ts.indentationString(previousBlockText);
|
||||
if (!previousIndentationString.isEmpty())
|
||||
cursor.insertText(previousIndentationString);
|
||||
}
|
||||
cursor.endEditBlock();
|
||||
e->accept();
|
||||
@@ -1572,6 +1575,10 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
|
||||
while (extraBlocks > 0) {
|
||||
--extraBlocks;
|
||||
ensureVisible.movePosition(QTextCursor::NextBlock);
|
||||
if (ts.m_autoIndent)
|
||||
indent(document(), ensureVisible, QChar::Null);
|
||||
else if (!previousIndentationString.isEmpty())
|
||||
ensureVisible.insertText(previousIndentationString);
|
||||
}
|
||||
setTextCursor(ensureVisible);
|
||||
}
|
||||
@@ -1763,7 +1770,7 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
|
||||
} else if ((e->modifiers() & (Qt::ControlModifier|Qt::AltModifier)) != Qt::ControlModifier){
|
||||
QTextCursor cursor = textCursor();
|
||||
QString text = e->text();
|
||||
QString autoText = autoComplete(cursor, text);
|
||||
const QString &autoText = d->m_autoCompleter->autoComplete(cursor, text);
|
||||
|
||||
QChar electricChar;
|
||||
if (d->m_document->tabSettings().m_autoIndent) {
|
||||
@@ -2178,26 +2185,6 @@ bool BaseTextEditor::isParenthesesMatchingEnabled() const
|
||||
return d->m_parenthesesMatchingEnabled;
|
||||
}
|
||||
|
||||
void BaseTextEditor::setAutoParenthesesEnabled(bool b)
|
||||
{
|
||||
d->m_autoParenthesesEnabled = b;
|
||||
}
|
||||
|
||||
bool BaseTextEditor::isAutoParenthesesEnabled() const
|
||||
{
|
||||
return d->m_autoParenthesesEnabled;
|
||||
}
|
||||
|
||||
void BaseTextEditor::setSurroundWithEnabled(bool b)
|
||||
{
|
||||
d->m_surroundWithEnabled= b;
|
||||
}
|
||||
|
||||
bool BaseTextEditor::isSurroundWithEnabled() const
|
||||
{
|
||||
return d->m_surroundWithEnabled;
|
||||
}
|
||||
|
||||
void BaseTextEditor::setHighlightCurrentLine(bool b)
|
||||
{
|
||||
d->m_highlightCurrentLine = b;
|
||||
@@ -2359,11 +2346,8 @@ BaseTextEditorPrivate::BaseTextEditorPrivate()
|
||||
q(0),
|
||||
m_contentsChanged(false),
|
||||
m_lastCursorChangeWasInteresting(false),
|
||||
m_allowSkippingOfBlockEnd(false),
|
||||
m_document(new BaseTextDocument),
|
||||
m_parenthesesMatchingEnabled(false),
|
||||
m_autoParenthesesEnabled(true),
|
||||
m_surroundWithEnabled(true),
|
||||
m_updateTimer(0),
|
||||
m_formatRange(false),
|
||||
m_parenthesesMatchingTimer(0),
|
||||
@@ -4413,7 +4397,7 @@ void BaseTextEditor::handleBackspaceKey()
|
||||
|
||||
const TextEditor::TabSettings &tabSettings = d->m_document->tabSettings();
|
||||
|
||||
if (tabSettings.m_autoIndent && autoBackspace(cursor))
|
||||
if (tabSettings.m_autoIndent && d->m_autoCompleter->autoBackspace(cursor))
|
||||
return;
|
||||
|
||||
if (!tabSettings.m_smartBackspace) {
|
||||
@@ -4487,250 +4471,6 @@ void BaseTextEditor::indentInsertedText(const QTextCursor &tc)
|
||||
indent(tc.document(), tc, QChar::Null);
|
||||
}
|
||||
|
||||
void BaseTextEditor::countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen)
|
||||
{
|
||||
if (c == open)
|
||||
++*stillopen;
|
||||
else if (c == close)
|
||||
--*stillopen;
|
||||
|
||||
if (*stillopen < 0) {
|
||||
*errors += -1 * (*stillopen);
|
||||
*stillopen = 0;
|
||||
}
|
||||
}
|
||||
|
||||
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::BaseTextDocumentLayout::parentheses(block);
|
||||
if (!parenList.isEmpty() && !TextEditor::BaseTextDocumentLayout::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 BaseTextEditor::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
|
||||
{
|
||||
const bool checkBlockEnd = d->m_allowSkippingOfBlockEnd;
|
||||
d->m_allowSkippingOfBlockEnd = false; // consume blockEnd.
|
||||
|
||||
if (d->m_surroundWithEnabled && cursor.hasSelection()) {
|
||||
if (textToInsert == QLatin1String("("))
|
||||
return cursor.selectedText() + QLatin1String(")");
|
||||
if (textToInsert == QLatin1String("{")) {
|
||||
//If the text span multiple lines, insert on different lines
|
||||
QString str = cursor.selectedText();
|
||||
if (str.contains(QChar::ParagraphSeparator)) {
|
||||
//Also, try to simulate auto-indent
|
||||
str = (str.startsWith(QChar::ParagraphSeparator) ? QString() : QString(QChar::ParagraphSeparator)) +
|
||||
str;
|
||||
if (str.endsWith(QChar::ParagraphSeparator))
|
||||
str += QLatin1String("}") + QString(QChar::ParagraphSeparator);
|
||||
else
|
||||
str += QString(QChar::ParagraphSeparator) + QLatin1String("}");
|
||||
}
|
||||
else {
|
||||
str += QLatin1String("}");
|
||||
}
|
||||
return str;
|
||||
}
|
||||
if (textToInsert == QLatin1String("["))
|
||||
return cursor.selectedText() + QLatin1String("]");
|
||||
if (textToInsert == QLatin1String("\""))
|
||||
return cursor.selectedText() + QLatin1String("\"");
|
||||
if (textToInsert == QLatin1String("'"))
|
||||
return cursor.selectedText() + QLatin1String("'");
|
||||
}
|
||||
|
||||
if (!d->m_autoParenthesesEnabled)
|
||||
return QString();
|
||||
|
||||
if (!d->m_autoCompleter->contextAllowsAutoParentheses(cursor, textToInsert))
|
||||
return QString();
|
||||
|
||||
const QString text = textToInsert;
|
||||
const QChar lookAhead = characterAt(cursor.selectionEnd());
|
||||
|
||||
const QChar character = textToInsert.at(0);
|
||||
const QString parentheses = QLatin1String("()");
|
||||
const QString brackets = QLatin1String("[]");
|
||||
if (parentheses.contains(character) || brackets.contains(character)) {
|
||||
QTextCursor tmp= cursor;
|
||||
bool foundBlockStart = TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
|
||||
int blockStart = foundBlockStart ? tmp.position() : 0;
|
||||
tmp = cursor;
|
||||
bool foundBlockEnd = TextEditor::TextBlockUserData::findNextBlockClosingParenthesis(&tmp);
|
||||
int blockEnd = foundBlockEnd ? tmp.position() : (cursor.document()->characterCount() - 1);
|
||||
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 = d->m_autoCompleter->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;
|
||||
|
||||
if (!d->m_autoParenthesesEnabled)
|
||||
return false;
|
||||
|
||||
int pos = cursor.position();
|
||||
if (pos == 0)
|
||||
return false;
|
||||
QTextCursor c = cursor;
|
||||
c.setPosition(pos - 1);
|
||||
|
||||
const QChar lookAhead = characterAt(pos);
|
||||
const QChar lookBehind = characterAt(pos - 1);
|
||||
const QChar lookFurtherBehind = characterAt(pos - 2);
|
||||
|
||||
const 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 (! d->m_autoCompleter->isInComment(c)) {
|
||||
cursor.beginEditBlock();
|
||||
cursor.deleteChar();
|
||||
cursor.deletePreviousChar();
|
||||
cursor.endEditBlock();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
|
||||
{
|
||||
if (!d->m_autoParenthesesEnabled)
|
||||
return 0;
|
||||
|
||||
if (characterAt(cursor.position() - 1) != QLatin1Char('{'))
|
||||
return 0;
|
||||
|
||||
if (!d->m_autoCompleter->contextAllowsAutoParentheses(cursor))
|
||||
return 0;
|
||||
|
||||
// verify that we indeed do have an extra opening brace in the document
|
||||
int braceDepth = BaseTextDocumentLayout::braceDepth(document()->lastBlock());
|
||||
|
||||
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()) { // not the last block
|
||||
block = block.next();
|
||||
//skip all empty blocks
|
||||
while (block.isValid() && ts.onlySpace(block.text()))
|
||||
block = block.next();
|
||||
if (block.isValid()
|
||||
&& ts.indentationColumn(block.text()) > indentation)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pos = cursor.position();
|
||||
|
||||
const QString textToInsert = d->m_autoCompleter->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::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar)
|
||||
{
|
||||
maybeClearSomeExtraSelections(cursor);
|
||||
@@ -5624,7 +5364,8 @@ void BaseTextEditor::setStorageSettings(const StorageSettings &storageSettings)
|
||||
|
||||
void BaseTextEditor::setCompletionSettings(const TextEditor::CompletionSettings &completionSettings)
|
||||
{
|
||||
setAutoParenthesesEnabled(completionSettings.m_autoInsertBrackets);
|
||||
d->m_autoCompleter->setAutoParenthesesEnabled(completionSettings.m_autoInsertBrackets);
|
||||
d->m_autoCompleter->setSurroundWithEnabled(completionSettings.m_autoInsertBrackets);
|
||||
}
|
||||
|
||||
void BaseTextEditor::fold()
|
||||
|
||||
Reference in New Issue
Block a user