Commit c8a53a66 authored by Leandro Melo's avatar Leandro Melo
Browse files

Editors: Continue refactoring indenters out of the editors.

This is pretty much a complement of commit 3a684586,
which is an attempt to make editors and indenters a bit more decoupled.

Reviewed-by: Thorbjorn Lindeijer
parent 36fa1de4
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
#include <texteditor/texteditorconstants.h> #include <texteditor/texteditorconstants.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
#include <texteditor/indenter.h>
#include <find/findplugin.h> #include <find/findplugin.h>
#include <find/textfindconstants.h> #include <find/textfindconstants.h>
...@@ -935,7 +936,7 @@ void FakeVimPluginPrivate::checkForElectricCharacter(bool *result, QChar c) ...@@ -935,7 +936,7 @@ void FakeVimPluginPrivate::checkForElectricCharacter(bool *result, QChar c)
if (!handler) if (!handler)
return; return;
if (BaseTextEditor *bt = qobject_cast<BaseTextEditor *>(handler->widget())) if (BaseTextEditor *bt = qobject_cast<BaseTextEditor *>(handler->widget()))
*result = bt->isElectricCharacter(c); *result = bt->indenter()->isElectricCharacter(c);
} }
void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd) void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd)
...@@ -1132,7 +1133,7 @@ void FakeVimPluginPrivate::indentRegion(int beginLine, int endLine, ...@@ -1132,7 +1133,7 @@ void FakeVimPluginPrivate::indentRegion(int beginLine, int endLine,
while (!cursor.atBlockEnd()) while (!cursor.atBlockEnd())
cursor.deleteChar(); cursor.deleteChar();
} else { } else {
bt->indentBlock(doc, block, typedChar); bt->indenter()->indentBlock(doc, block, typedChar, bt);
} }
block = block.next(); block = block.next();
} }
......
...@@ -1344,7 +1344,7 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e) ...@@ -1344,7 +1344,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 (d->m_indenter->isElectricCharacter(c)) {
electricChar = c; electricChar = c;
break; break;
} }
...@@ -1890,6 +1890,11 @@ void BaseTextEditor::setIndenter(Indenter *indenter) ...@@ -1890,6 +1890,11 @@ void BaseTextEditor::setIndenter(Indenter *indenter)
d->m_indenter.reset(indenter); d->m_indenter.reset(indenter);
} }
Indenter *BaseTextEditor::indenter() const
{
return d->m_indenter.data();
}
void BaseTextEditor::setAutoCompleter(AutoCompleter *autoCompleter) void BaseTextEditor::setAutoCompleter(AutoCompleter *autoCompleter)
{ {
d->m_autoCompleter.reset(autoCompleter); d->m_autoCompleter.reset(autoCompleter);
...@@ -1949,7 +1954,8 @@ BaseTextEditorPrivate::BaseTextEditorPrivate() ...@@ -1949,7 +1954,8 @@ BaseTextEditorPrivate::BaseTextEditorPrivate()
m_requestAutoCompletionPosition(0), m_requestAutoCompletionPosition(0),
m_requestAutoCompletionTimer(0), m_requestAutoCompletionTimer(0),
m_cursorBlockNumber(-1), m_cursorBlockNumber(-1),
m_autoCompleter(new AutoCompleter) m_autoCompleter(new AutoCompleter),
m_indenter(new Indenter)
{ {
} }
...@@ -4019,13 +4025,6 @@ void BaseTextEditor::zoomReset() ...@@ -4019,13 +4025,6 @@ void BaseTextEditor::zoomReset()
emit requestZoomReset(); emit requestZoomReset();
} }
bool BaseTextEditor::isElectricCharacter(QChar ch) const
{
if (!d->m_indenter.isNull())
return d->m_indenter->isElectricCharacter(ch);
return false;
}
void BaseTextEditor::indentInsertedText(const QTextCursor &tc) void BaseTextEditor::indentInsertedText(const QTextCursor &tc)
{ {
indent(tc.document(), tc, QChar::Null); indent(tc.document(), tc, QChar::Null);
...@@ -4247,24 +4246,16 @@ int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor) ...@@ -4247,24 +4246,16 @@ int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
return 1; return 1;
} }
void BaseTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar ch)
{
if (!d->m_indenter.isNull())
d->m_indenter->indentBlock(doc, block, ch, this);
}
void BaseTextEditor::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar) void BaseTextEditor::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar)
{ {
maybeClearSomeExtraSelections(cursor); maybeClearSomeExtraSelections(cursor);
if (!d->m_indenter.isNull()) d->m_indenter->indent(doc, cursor, typedChar, this);
d->m_indenter->indent(doc, cursor, typedChar, this);
} }
void BaseTextEditor::reindent(QTextDocument *doc, const QTextCursor &cursor) void BaseTextEditor::reindent(QTextDocument *doc, const QTextCursor &cursor)
{ {
maybeClearSomeExtraSelections(cursor); maybeClearSomeExtraSelections(cursor);
if (!d->m_indenter.isNull()) d->m_indenter->reindent(doc, cursor, this);
d->m_indenter->reindent(doc, cursor, this);
} }
BaseTextEditor::Link BaseTextEditor::findLinkAt(const QTextCursor &, bool) BaseTextEditor::Link BaseTextEditor::findLinkAt(const QTextCursor &, bool)
......
...@@ -225,6 +225,7 @@ public: ...@@ -225,6 +225,7 @@ public:
QRegion translatedLineRegion(int lineStart, int lineEnd) const; QRegion translatedLineRegion(int lineStart, int lineEnd) const;
void setIndenter(Indenter *indenter); void setIndenter(Indenter *indenter);
Indenter *indenter() const;
void setAutoCompleter(AutoCompleter *autoCompleter); void setAutoCompleter(AutoCompleter *autoCompleter);
AutoCompleter *autoCompleter() const; AutoCompleter *autoCompleter() const;
...@@ -424,14 +425,8 @@ public: ...@@ -424,14 +425,8 @@ public:
virtual int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor); virtual int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor);
void indentInsertedText(const QTextCursor &tc); void indentInsertedText(const QTextCursor &tc);
// Returns true if key triggers an indent. void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar);
virtual bool isElectricCharacter(QChar ch) const; void reindent(QTextDocument *doc, const QTextCursor &cursor);
// Indent a text block based on previous line. Default does nothing
virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);
// Indent at cursor. Calls indentBlock for selection or current line.
virtual void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar);
// Reindent at cursor. Selection will be adjusted according to the indentation change of the first block
virtual void reindent(QTextDocument *doc, const QTextCursor &cursor);
protected: protected:
static void countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen); static void countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen);
......
...@@ -70,6 +70,17 @@ bool Indenter::doIsElectricalCharacter(const QChar &) const ...@@ -70,6 +70,17 @@ bool Indenter::doIsElectricalCharacter(const QChar &) const
return false; return false;
} }
void Indenter::doIndentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
BaseTextEditor *editor)
{
Q_UNUSED(doc);
Q_UNUSED(block);
Q_UNUSED(typedChar);
Q_UNUSED(editor);
}
void Indenter::doIndent(QTextDocument *doc, const QTextCursor &cursor, const QChar &typedChar, BaseTextEditor *editor) void Indenter::doIndent(QTextDocument *doc, const QTextCursor &cursor, const QChar &typedChar, BaseTextEditor *editor)
{ {
if (cursor.hasSelection()) { if (cursor.hasSelection()) {
......
...@@ -50,15 +50,20 @@ public: ...@@ -50,15 +50,20 @@ public:
Indenter(); Indenter();
virtual ~Indenter(); virtual ~Indenter();
// Returns true if key triggers an indent.
bool isElectricCharacter(const QChar &ch) const; bool isElectricCharacter(const QChar &ch) const;
// Indent a text block based on previous line. Default does nothing
void indentBlock(QTextDocument *doc, void indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
BaseTextEditor *editor); BaseTextEditor *editor);
// Indent at cursor. Calls indentBlock for selection or current line.
void indent(QTextDocument *doc, void indent(QTextDocument *doc,
const QTextCursor &cursor, const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
BaseTextEditor *editor); BaseTextEditor *editor);
// Reindent at cursor. Selection will be adjusted according to the indentation
// change of the first block.
void reindent(QTextDocument *doc, const QTextCursor &cursor, BaseTextEditor *editor); void reindent(QTextDocument *doc, const QTextCursor &cursor, BaseTextEditor *editor);
private: private:
...@@ -66,7 +71,7 @@ private: ...@@ -66,7 +71,7 @@ private:
virtual void doIndentBlock(QTextDocument *doc, virtual void doIndentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
BaseTextEditor *editor) = 0; BaseTextEditor *editor);
virtual void doIndent(QTextDocument *doc, virtual void doIndent(QTextDocument *doc,
const QTextCursor &cursor, const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment