Commit ea8cb476 authored by Leandro Melo's avatar Leandro Melo

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,
36fa1de4 and
3a684586.

Also removed the doXXXX() forwarding methods.
parent e117f04f
......@@ -48,8 +48,8 @@ CppAutoCompleter::CppAutoCompleter()
CppAutoCompleter::~CppAutoCompleter()
{}
bool CppAutoCompleter::doContextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert) const
bool CppAutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert) const
{
QChar ch;
......@@ -66,7 +66,7 @@ bool CppAutoCompleter::doContextAllowsAutoParentheses(const QTextCursor &cursor,
return true;
}
bool CppAutoCompleter::doContextAllowsElectricCharacters(const QTextCursor &cursor) const
bool CppAutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
{
const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
BackwardsScanner::previousBlockState(cursor.block()));
......@@ -98,7 +98,7 @@ bool CppAutoCompleter::doContextAllowsElectricCharacters(const QTextCursor &curs
return true;
}
bool CppAutoCompleter::doIsInComment(const QTextCursor &cursor) const
bool CppAutoCompleter::isInComment(const QTextCursor &cursor) const
{
const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
BackwardsScanner::previousBlockState(cursor.block()));
......@@ -122,7 +122,7 @@ bool CppAutoCompleter::doIsInComment(const QTextCursor &cursor) const
return false;
}
QString CppAutoCompleter::doInsertMatchingBrace(const QTextCursor &cursor,
QString CppAutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
const QString &text,
QChar la,
int *skippedChars) const
......@@ -131,7 +131,7 @@ QString CppAutoCompleter::doInsertMatchingBrace(const QTextCursor &cursor,
return m.insertMatchingBrace(cursor, text, la, skippedChars);
}
QString CppAutoCompleter::doInsertParagraphSeparator(const QTextCursor &cursor) const
QString CppAutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
{
MatchingText m;
return m.insertParagraphSeparator(cursor);
......
......@@ -41,16 +41,15 @@ public:
CppAutoCompleter();
virtual ~CppAutoCompleter();
private:
virtual bool doContextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert = QString()) const;
virtual bool doContextAllowsElectricCharacters(const QTextCursor &cursor) const;
virtual bool doIsInComment(const QTextCursor &cursor) const;
virtual QString doInsertMatchingBrace(const QTextCursor &cursor,
const QString &text,
QChar la,
int *skippedChars) const;
virtual QString doInsertParagraphSeparator(const QTextCursor &cursor) const;
virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert = QString()) const;
virtual bool contextAllowsElectricCharacters(const QTextCursor &cursor) const;
virtual bool isInComment(const QTextCursor &cursor) const;
virtual QString insertMatchingBrace(const QTextCursor &cursor,
const QString &text,
QChar la,
int *skippedChars) const;
virtual QString insertParagraphSeparator(const QTextCursor &cursor) const;
};
} // Internal
......
......@@ -47,7 +47,7 @@ CppQtStyleIndenter::CppQtStyleIndenter()
CppQtStyleIndenter::~CppQtStyleIndenter()
{}
bool CppQtStyleIndenter::doIsElectricalCharacter(const QChar &ch) const
bool CppQtStyleIndenter::isElectricCharacter(const QChar &ch) const
{
if (ch == QLatin1Char('{') ||
ch == QLatin1Char('}') ||
......@@ -58,10 +58,10 @@ bool CppQtStyleIndenter::doIsElectricalCharacter(const QChar &ch) const
return false;
}
void CppQtStyleIndenter::doIndentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
{
Q_UNUSED(doc)
......@@ -86,10 +86,10 @@ void CppQtStyleIndenter::doIndentBlock(QTextDocument *doc,
ts.indentLine(block, indent + padding, padding);
}
void CppQtStyleIndenter::doIndent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
void CppQtStyleIndenter::indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
......
......@@ -41,17 +41,16 @@ public:
CppQtStyleIndenter();
virtual ~CppQtStyleIndenter();
private:
virtual bool doIsElectricalCharacter(const QChar &ch) const;
virtual void doIndentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
virtual void doIndent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
virtual bool isElectricCharacter(const QChar &ch) const;
virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
virtual void indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
};
} // Internal
......
......@@ -47,8 +47,8 @@ GLSLCompleter::GLSLCompleter()
GLSLCompleter::~GLSLCompleter()
{}
bool GLSLCompleter::doContextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert) const
bool GLSLCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert) const
{
QChar ch;
......@@ -65,7 +65,7 @@ bool GLSLCompleter::doContextAllowsAutoParentheses(const QTextCursor &cursor,
return true;
}
bool GLSLCompleter::doContextAllowsElectricCharacters(const QTextCursor &cursor) const
bool GLSLCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
{
const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
BackwardsScanner::previousBlockState(cursor.block()));
......@@ -97,7 +97,7 @@ bool GLSLCompleter::doContextAllowsElectricCharacters(const QTextCursor &cursor)
return true;
}
bool GLSLCompleter::doIsInComment(const QTextCursor &cursor) const
bool GLSLCompleter::isInComment(const QTextCursor &cursor) const
{
const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
BackwardsScanner::previousBlockState(cursor.block()));
......@@ -121,16 +121,16 @@ bool GLSLCompleter::doIsInComment(const QTextCursor &cursor) const
return false;
}
QString GLSLCompleter::doInsertMatchingBrace(const QTextCursor &cursor,
const QString &text,
QChar la,
int *skippedChars) const
QString GLSLCompleter::insertMatchingBrace(const QTextCursor &cursor,
const QString &text,
QChar la,
int *skippedChars) const
{
MatchingText m;
return m.insertMatchingBrace(cursor, text, la, skippedChars);
}
QString GLSLCompleter::doInsertParagraphSeparator(const QTextCursor &cursor) const
QString GLSLCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
{
MatchingText m;
return m.insertParagraphSeparator(cursor);
......
......@@ -41,16 +41,15 @@ public:
GLSLCompleter();
virtual ~GLSLCompleter();
private:
virtual bool doContextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert = QString()) const;
virtual bool doContextAllowsElectricCharacters(const QTextCursor &cursor) const;
virtual bool doIsInComment(const QTextCursor &cursor) const;
virtual QString doInsertMatchingBrace(const QTextCursor &cursor,
const QString &text,
QChar la,
int *skippedChars) const;
virtual QString doInsertParagraphSeparator(const QTextCursor &cursor) const;
virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert = QString()) const;
virtual bool contextAllowsElectricCharacters(const QTextCursor &cursor) const;
virtual bool isInComment(const QTextCursor &cursor) const;
virtual QString insertMatchingBrace(const QTextCursor &cursor,
const QString &text,
QChar la,
int *skippedChars) const;
virtual QString insertParagraphSeparator(const QTextCursor &cursor) const;
};
} // Internal
......
......@@ -47,7 +47,7 @@ GLSLIndenter::GLSLIndenter()
GLSLIndenter::~GLSLIndenter()
{}
bool GLSLIndenter::doIsElectricalCharacter(const QChar &ch) const
bool GLSLIndenter::isElectricCharacter(const QChar &ch) const
{
if (ch == QLatin1Char('{') ||
ch == QLatin1Char('}') ||
......@@ -58,10 +58,10 @@ bool GLSLIndenter::doIsElectricalCharacter(const QChar &ch) const
return false;
}
void GLSLIndenter::doIndentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
void GLSLIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
{
Q_UNUSED(doc)
......@@ -86,10 +86,10 @@ void GLSLIndenter::doIndentBlock(QTextDocument *doc,
ts.indentLine(block, indent + padding, padding);
}
void GLSLIndenter::doIndent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
void GLSLIndenter::indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
......
......@@ -41,17 +41,16 @@ public:
GLSLIndenter();
virtual ~GLSLIndenter();
private:
virtual bool doIsElectricalCharacter(const QChar &ch) const;
virtual void doIndentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
virtual void doIndent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
virtual bool isElectricCharacter(const QChar &ch) const;
virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
virtual void indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
};
} // Internal
......
......@@ -143,8 +143,8 @@ AutoCompleter::AutoCompleter()
AutoCompleter::~AutoCompleter()
{}
bool AutoCompleter::doContextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert) const
bool AutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert) const
{
QChar ch;
......@@ -200,7 +200,7 @@ bool AutoCompleter::doContextAllowsAutoParentheses(const QTextCursor &cursor,
return true;
}
bool AutoCompleter::doContextAllowsElectricCharacters(const QTextCursor &cursor) const
bool AutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
{
Token token = tokenUnderCursor(cursor);
switch (token.kind) {
......@@ -212,15 +212,15 @@ bool AutoCompleter::doContextAllowsElectricCharacters(const QTextCursor &cursor)
}
}
bool AutoCompleter::doIsInComment(const QTextCursor &cursor) const
bool AutoCompleter::isInComment(const QTextCursor &cursor) const
{
return tokenUnderCursor(cursor).is(Token::Comment);
}
QString AutoCompleter::doInsertMatchingBrace(const QTextCursor &cursor,
const QString &text,
QChar,
int *skippedChars) const
QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
const QString &text,
QChar,
int *skippedChars) const
{
if (text.length() != 1)
return QString();
......@@ -268,7 +268,7 @@ QString AutoCompleter::doInsertMatchingBrace(const QTextCursor &cursor,
return QString();
}
QString AutoCompleter::doInsertParagraphSeparator(const QTextCursor &cursor) const
QString AutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
{
if (shouldInsertNewline(cursor)) {
QTextCursor cursor = cursor;
......
......@@ -41,16 +41,15 @@ public:
AutoCompleter();
virtual ~AutoCompleter();
private:
virtual bool doContextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert = QString()) const;
virtual bool doContextAllowsElectricCharacters(const QTextCursor &cursor) const;
virtual bool doIsInComment(const QTextCursor &cursor) const;
virtual QString doInsertMatchingBrace(const QTextCursor &tc,
virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert = QString()) const;
virtual bool contextAllowsElectricCharacters(const QTextCursor &cursor) const;
virtual bool isInComment(const QTextCursor &cursor) const;
virtual QString insertMatchingBrace(const QTextCursor &tc,
const QString &text,
QChar la,
int *skippedChars) const;
virtual QString doInsertParagraphSeparator(const QTextCursor &tc) const;
virtual QString insertParagraphSeparator(const QTextCursor &tc) const;
};
} // Internal
......
......@@ -47,7 +47,7 @@ Indenter::Indenter()
Indenter::~Indenter()
{}
bool Indenter::doIsElectricalCharacter(const QChar &ch) const
bool Indenter::isElectricCharacter(const QChar &ch) const
{
if (ch == QLatin1Char('}')
|| ch == QLatin1Char(']')
......@@ -56,10 +56,10 @@ bool Indenter::doIsElectricalCharacter(const QChar &ch) const
return false;
}
void Indenter::doIndentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
void Indenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor)
{
Q_UNUSED(doc)
Q_UNUSED(typedChar)
......
......@@ -41,12 +41,11 @@ public:
Indenter();
virtual ~Indenter();
private:
virtual bool doIsElectricalCharacter(const QChar &ch) const;
virtual void doIndentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
virtual bool isElectricCharacter(const QChar &ch) const;
virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditor *editor);
};
} // Internal
......
......@@ -28,69 +28,310 @@
**************************************************************************/
#include "autocompleter.h"
#include "basetextdocumentlayout.h"
#include "texteditorsettings.h"
#include "tabsettings.h"
#include <QtGui/QTextCursor>
using namespace TextEditor;
AutoCompleter::AutoCompleter()
AutoCompleter::AutoCompleter() :
m_allowSkippingOfBlockEnd(false),
m_surroundWithEnabled(true),
m_autoParenthesesEnabled(true)
{}
AutoCompleter::~AutoCompleter()
{}
bool AutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
const QString &textToInsert) const
void AutoCompleter::setAutoParenthesesEnabled(bool b)
{
return doContextAllowsAutoParentheses(cursor, textToInsert);
m_autoParenthesesEnabled = b;
}
bool AutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
bool AutoCompleter::isAutoParenthesesEnabled() const
{
return doContextAllowsElectricCharacters(cursor);
return m_autoParenthesesEnabled;
}
bool AutoCompleter::isInComment(const QTextCursor &cursor) const
void AutoCompleter::setSurroundWithEnabled(bool b)
{
return doIsInComment(cursor);
m_surroundWithEnabled = b;
}
QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor, const
QString &text,
QChar la,
int *skippedChars) const
bool AutoCompleter::isSurroundWithEnabled() const
{
return doInsertMatchingBrace(cursor, text, la, skippedChars);
return m_surroundWithEnabled;
}
QString AutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
void AutoCompleter::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 AutoCompleter::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 AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
{
const bool checkBlockEnd = m_allowSkippingOfBlockEnd;
m_allowSkippingOfBlockEnd = false; // consume blockEnd.
if (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 (!m_autoParenthesesEnabled)
return QString();
if (!contextAllowsAutoParentheses(cursor, textToInsert))
return QString();
QTextDocument *doc = cursor.document();
const QString text = textToInsert;
const QChar lookAhead = doc->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 = 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 (doc->characterAt(pos).isSpace())
++pos;
if (doc->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 AutoCompleter::autoBackspace(QTextCursor &cursor)
{
return doInsertParagraphSeparator(cursor);
m_allowSkippingOfBlockEnd = false;
if (!m_autoParenthesesEnabled)
return false;
int pos = cursor.position();
if (pos == 0)
return false;
QTextCursor c = cursor;
c.setPosition(pos - 1);
QTextDocument *doc = cursor.document();
const QChar lookAhead = doc->characterAt(pos);
const QChar lookBehind = doc->characterAt(pos - 1);
const QChar lookFurtherBehind = doc->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);