Refactor: Get rid of BaseTextEditorWidget from Indenter

Provide directly TabSettings instead.
This will be used for indenting a text for which
there is no editor instance.

Change-Id: Ia5f11a481f42464cf4820efdf2c7c4c32166f55e
Reviewed-on: http://codereview.qt.nokia.com/2622
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Leandro T. C. Melo <leandro.melo@nokia.com>
This commit is contained in:
Jarek Kobus
2011-08-04 11:19:25 +02:00
committed by Jarek Kobus
parent 55f0f64a5d
commit 0c8df0597f
12 changed files with 53 additions and 67 deletions

View File

@@ -36,7 +36,6 @@
#include "cpptoolssettings.h" #include "cpptoolssettings.h"
#include "cppcodestylepreferences.h" #include "cppcodestylepreferences.h"
#include "cpptoolsconstants.h" #include "cpptoolsconstants.h"
#include <texteditor/basetexteditor.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
@@ -93,12 +92,11 @@ static bool colonIsElectric(const QString &text)
void CppQtStyleIndenter::indentBlock(QTextDocument *doc, void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor) const TextEditor::TabSettings &tabSettings)
{ {
Q_UNUSED(doc) Q_UNUSED(doc)
const TextEditor::TabSettings &ts = editor->tabSettings(); CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
CppTools::QtStyleCodeFormatter codeFormatter(ts, codeStyleSettings());
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
int indent; int indent;
@@ -115,39 +113,38 @@ void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
int newlineIndent; int newlineIndent;
int newlinePadding; int newlinePadding;
codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding); codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding);
if (ts.indentationColumn(block.text()) != newlineIndent + newlinePadding) if (tabSettings.indentationColumn(block.text()) != newlineIndent + newlinePadding)
return; return;
} }
ts.indentLine(block, indent + padding, padding); tabSettings.indentLine(block, indent + padding, padding);
} }
void CppQtStyleIndenter::indent(QTextDocument *doc, void CppQtStyleIndenter::indent(QTextDocument *doc,
const QTextCursor &cursor, const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor) const TextEditor::TabSettings &tabSettings)
{ {
if (cursor.hasSelection()) { if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart()); QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next(); const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TextEditor::TabSettings &ts = editor->tabSettings(); CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
CppTools::QtStyleCodeFormatter codeFormatter(ts, codeStyleSettings());
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
QTextCursor tc = editor->textCursor(); QTextCursor tc = cursor;
tc.beginEditBlock(); tc.beginEditBlock();
do { do {
int indent; int indent;
int padding; int padding;
codeFormatter.indentFor(block, &indent, &padding); codeFormatter.indentFor(block, &indent, &padding);
ts.indentLine(block, indent + padding, padding); tabSettings.indentLine(block, indent + padding, padding);
codeFormatter.updateLineStateChange(block); codeFormatter.updateLineStateChange(block);
block = block.next(); block = block.next();
} while (block.isValid() && block != end); } while (block.isValid() && block != end);
tc.endEditBlock(); tc.endEditBlock();
} else { } else {
indentBlock(doc, cursor.block(), typedChar, editor); indentBlock(doc, cursor.block(), typedChar, tabSettings);
} }
} }

View File

@@ -54,12 +54,12 @@ public:
virtual void indentBlock(QTextDocument *doc, virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor); const TextEditor::TabSettings &tabSettings);
virtual void indent(QTextDocument *doc, virtual void indent(QTextDocument *doc,
const QTextCursor &cursor, const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor); const TextEditor::TabSettings &tabSettings);
virtual void setCodeStylePreferences(TextEditor::IFallbackPreferences *preferences); virtual void setCodeStylePreferences(TextEditor::IFallbackPreferences *preferences);
private: private:

View File

@@ -1673,7 +1673,6 @@ void FakeVimPluginPrivate::indentRegion(int beginLine, int endLine,
tabSettings.m_indentSize = theFakeVimSetting(ConfigShiftWidth)->value().toInt(); tabSettings.m_indentSize = theFakeVimSetting(ConfigShiftWidth)->value().toInt();
tabSettings.m_tabSize = theFakeVimSetting(ConfigTabStop)->value().toInt(); tabSettings.m_tabSize = theFakeVimSetting(ConfigTabStop)->value().toInt();
tabSettings.m_spacesForTabs = theFakeVimSetting(ConfigExpandTab)->value().toBool(); tabSettings.m_spacesForTabs = theFakeVimSetting(ConfigExpandTab)->value().toBool();
bt->setTabSettings(tabSettings);
QTextDocument *doc = bt->document(); QTextDocument *doc = bt->document();
QTextBlock startBlock = doc->findBlockByNumber(beginLine); QTextBlock startBlock = doc->findBlockByNumber(beginLine);
@@ -1690,12 +1689,10 @@ void FakeVimPluginPrivate::indentRegion(int beginLine, int endLine,
while (!cursor.atBlockEnd()) while (!cursor.atBlockEnd())
cursor.deleteChar(); cursor.deleteChar();
} else { } else {
bt->indenter()->indentBlock(doc, block, typedChar, bt); bt->indenter()->indentBlock(doc, block, typedChar, tabSettings);
} }
block = block.next(); block = block.next();
} }
bt->setTabSettings(oldTabSettings);
} }
void FakeVimPluginPrivate::quitFakeVim() void FakeVimPluginPrivate::quitFakeVim()

View File

@@ -35,7 +35,6 @@
#include <cpptools/cppcodeformatter.h> #include <cpptools/cppcodeformatter.h>
#include <cpptools/cpptoolssettings.h> #include <cpptools/cpptoolssettings.h>
#include <cpptools/cppcodestylepreferences.h> #include <cpptools/cppcodestylepreferences.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
#include <QtCore/QChar> #include <QtCore/QChar>
@@ -66,13 +65,12 @@ bool GLSLIndenter::isElectricCharacter(const QChar &ch) const
void GLSLIndenter::indentBlock(QTextDocument *doc, void GLSLIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor) const TextEditor::TabSettings &tabSettings)
{ {
Q_UNUSED(doc) Q_UNUSED(doc)
const TextEditor::TabSettings &ts = editor->tabSettings();
// TODO: do something with it // TODO: do something with it
CppTools::QtStyleCodeFormatter codeFormatter(ts, CppTools::QtStyleCodeFormatter codeFormatter(tabSettings,
CppTools::CppToolsSettings::instance()->cppCodeStylePreferences()->settings()); CppTools::CppToolsSettings::instance()->cppCodeStylePreferences()->settings());
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
@@ -86,40 +84,39 @@ void GLSLIndenter::indentBlock(QTextDocument *doc,
int newlineIndent; int newlineIndent;
int newlinePadding; int newlinePadding;
codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding); codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding);
if (ts.indentationColumn(block.text()) != newlineIndent + newlinePadding) if (tabSettings.indentationColumn(block.text()) != newlineIndent + newlinePadding)
return; return;
} }
ts.indentLine(block, indent + padding, padding); tabSettings.indentLine(block, indent + padding, padding);
} }
void GLSLIndenter::indent(QTextDocument *doc, void GLSLIndenter::indent(QTextDocument *doc,
const QTextCursor &cursor, const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor) const TextEditor::TabSettings &tabSettings)
{ {
if (cursor.hasSelection()) { if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart()); QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next(); const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TextEditor::TabSettings &ts = editor->tabSettings();
// TODO: do something with it // TODO: do something with it
CppTools::QtStyleCodeFormatter codeFormatter(ts, CppTools::QtStyleCodeFormatter codeFormatter(tabSettings,
CppTools::CppToolsSettings::instance()->cppCodeStylePreferences()->settings()); CppTools::CppToolsSettings::instance()->cppCodeStylePreferences()->settings());
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
QTextCursor tc = editor->textCursor(); QTextCursor tc = cursor;
tc.beginEditBlock(); tc.beginEditBlock();
do { do {
int indent; int indent;
int padding; int padding;
codeFormatter.indentFor(block, &indent, &padding); codeFormatter.indentFor(block, &indent, &padding);
ts.indentLine(block, indent + padding, padding); tabSettings.indentLine(block, indent + padding, padding);
codeFormatter.updateLineStateChange(block); codeFormatter.updateLineStateChange(block);
block = block.next(); block = block.next();
} while (block.isValid() && block != end); } while (block.isValid() && block != end);
tc.endEditBlock(); tc.endEditBlock();
} else { } else {
indentBlock(doc, cursor.block(), typedChar, editor); indentBlock(doc, cursor.block(), typedChar, tabSettings);
} }
} }

View File

@@ -48,12 +48,12 @@ public:
virtual void indentBlock(QTextDocument *doc, virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor); const TextEditor::TabSettings &tabSettings);
virtual void indent(QTextDocument *doc, virtual void indent(QTextDocument *doc,
const QTextCursor &cursor, const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor); const TextEditor::TabSettings &tabSettings);
}; };
} // Internal } // Internal

View File

@@ -33,7 +33,6 @@
#include "qmljsindenter.h" #include "qmljsindenter.h"
#include <qmljstools/qmljsqtstylecodeformatter.h> #include <qmljstools/qmljsqtstylecodeformatter.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
#include <QtCore/QChar> #include <QtCore/QChar>
@@ -63,13 +62,11 @@ bool Indenter::isElectricCharacter(const QChar &ch) const
void Indenter::indentBlock(QTextDocument *doc, void Indenter::indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor) const TextEditor::TabSettings &tabSettings)
{ {
Q_UNUSED(doc) Q_UNUSED(doc)
Q_UNUSED(editor)
const TextEditor::TabSettings &ts = editor->tabSettings(); QmlJSTools::QtStyleCodeFormatter codeFormatter(tabSettings);
QmlJSTools::QtStyleCodeFormatter codeFormatter(ts);
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
const int depth = codeFormatter.indentFor(block); const int depth = codeFormatter.indentFor(block);
@@ -78,9 +75,9 @@ void Indenter::indentBlock(QTextDocument *doc,
// only reindent the current line when typing electric characters if the // only reindent the current line when typing electric characters if the
// indent is the same it would be if the line were empty // indent is the same it would be if the line were empty
const int newlineIndent = codeFormatter.indentForNewLineAfter(block.previous()); const int newlineIndent = codeFormatter.indentForNewLineAfter(block.previous());
if (ts.indentationColumn(block.text()) != newlineIndent) if (tabSettings.indentationColumn(block.text()) != newlineIndent)
return; return;
} }
ts.indentLine(block, depth); tabSettings.indentLine(block, depth);
} }

View File

@@ -50,7 +50,7 @@ public:
virtual void indentBlock(QTextDocument *doc, virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor); const TextEditor::TabSettings &tabSettings);
}; };
} // Internal } // Internal

View File

@@ -4611,13 +4611,13 @@ void BaseTextEditorWidget::indentInsertedText(const QTextCursor &tc)
void BaseTextEditorWidget::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar) void BaseTextEditorWidget::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar)
{ {
maybeClearSomeExtraSelections(cursor); maybeClearSomeExtraSelections(cursor);
d->m_indenter->indent(doc, cursor, typedChar, this); d->m_indenter->indent(doc, cursor, typedChar, tabSettings());
} }
void BaseTextEditorWidget::reindent(QTextDocument *doc, const QTextCursor &cursor) void BaseTextEditorWidget::reindent(QTextDocument *doc, const QTextCursor &cursor)
{ {
maybeClearSomeExtraSelections(cursor); maybeClearSomeExtraSelections(cursor);
d->m_indenter->reindent(doc, cursor, this); d->m_indenter->reindent(doc, cursor, tabSettings());
} }
BaseTextEditorWidget::Link BaseTextEditorWidget::findLinkAt(const QTextCursor &, bool) BaseTextEditorWidget::Link BaseTextEditorWidget::findLinkAt(const QTextCursor &, bool)

View File

@@ -31,9 +31,11 @@
**************************************************************************/ **************************************************************************/
#include "indenter.h" #include "indenter.h"
#include "basetexteditor.h"
#include "tabsettings.h" #include "tabsettings.h"
#include <QtGui/QTextDocument>
#include <QtGui/QTextCursor>
using namespace TextEditor; using namespace TextEditor;
Indenter::Indenter() Indenter::Indenter()
@@ -50,60 +52,58 @@ bool Indenter::isElectricCharacter(const QChar &) const
void Indenter::indentBlock(QTextDocument *doc, void Indenter::indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
BaseTextEditorWidget *editor) const TextEditor::TabSettings &tabSettings)
{ {
Q_UNUSED(doc); Q_UNUSED(doc);
Q_UNUSED(block); Q_UNUSED(block);
Q_UNUSED(typedChar); Q_UNUSED(typedChar);
Q_UNUSED(editor); Q_UNUSED(tabSettings);
} }
void Indenter::indent(QTextDocument *doc, void Indenter::indent(QTextDocument *doc,
const QTextCursor &cursor, const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
BaseTextEditorWidget *editor) const TextEditor::TabSettings &tabSettings)
{ {
if (cursor.hasSelection()) { if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart()); QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next(); const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
do { do {
indentBlock(doc, block, typedChar, editor); indentBlock(doc, block, typedChar, tabSettings);
block = block.next(); block = block.next();
} while (block.isValid() && block != end); } while (block.isValid() && block != end);
} else { } else {
indentBlock(doc, cursor.block(), typedChar, editor); indentBlock(doc, cursor.block(), typedChar, tabSettings);
} }
} }
void Indenter::reindent(QTextDocument *doc, const QTextCursor &cursor, BaseTextEditorWidget *editor) void Indenter::reindent(QTextDocument *doc, const QTextCursor &cursor, const TextEditor::TabSettings &tabSettings)
{ {
if (cursor.hasSelection()) { if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart()); QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next(); const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TabSettings &ts = editor->tabSettings();
// skip empty blocks // skip empty blocks
while (block.isValid() && block != end) { while (block.isValid() && block != end) {
QString bt = block.text(); QString bt = block.text();
if (ts.firstNonSpace(bt) < bt.size()) if (tabSettings.firstNonSpace(bt) < bt.size())
break; break;
indentBlock(doc, block, QChar::Null, editor); indentBlock(doc, block, QChar::Null, tabSettings);
block = block.next(); block = block.next();
} }
int previousIndentation = ts.indentationColumn(block.text()); int previousIndentation = tabSettings.indentationColumn(block.text());
indentBlock(doc, block, QChar::Null, editor); indentBlock(doc, block, QChar::Null, tabSettings);
int currentIndentation = ts.indentationColumn(block.text()); int currentIndentation = tabSettings.indentationColumn(block.text());
int delta = currentIndentation - previousIndentation; int delta = currentIndentation - previousIndentation;
block = block.next(); block = block.next();
while (block.isValid() && block != end) { while (block.isValid() && block != end) {
ts.reindentLine(block, delta); tabSettings.reindentLine(block, delta);
block = block.next(); block = block.next();
} }
} else { } else {
indentBlock(doc, cursor.block(), QChar::Null, editor); indentBlock(doc, cursor.block(), QChar::Null, tabSettings);
} }
} }

View File

@@ -46,8 +46,8 @@ QT_END_NAMESPACE
namespace TextEditor { namespace TextEditor {
class BaseTextEditorWidget;
class IFallbackPreferences; class IFallbackPreferences;
class TabSettings;
class TEXTEDITOR_EXPORT Indenter class TEXTEDITOR_EXPORT Indenter
{ {
@@ -62,17 +62,17 @@ public:
virtual void indentBlock(QTextDocument *doc, virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
BaseTextEditorWidget *editor); const TabSettings &tabSettings);
// Indent at cursor. Calls indentBlock for selection or current line. // Indent at cursor. Calls indentBlock for selection or current line.
virtual void indent(QTextDocument *doc, virtual void indent(QTextDocument *doc,
const QTextCursor &cursor, const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
BaseTextEditorWidget *editor); const TabSettings &tabSettings);
// Reindent at cursor. Selection will be adjusted according to the indentation // Reindent at cursor. Selection will be adjusted according to the indentation
// change of the first block. // change of the first block.
virtual void reindent(QTextDocument *doc, const QTextCursor &cursor, BaseTextEditorWidget *editor); virtual void reindent(QTextDocument *doc, const QTextCursor &cursor, const TabSettings &tabSettings);
virtual void setCodeStylePreferences(IFallbackPreferences *preferences); virtual void setCodeStylePreferences(IFallbackPreferences *preferences);
}; };

View File

@@ -32,7 +32,6 @@
#include "normalindenter.h" #include "normalindenter.h"
#include "tabsettings.h" #include "tabsettings.h"
#include "basetexteditor.h"
#include <QtGui/QTextDocument> #include <QtGui/QTextDocument>
@@ -66,7 +65,7 @@ NormalIndenter::~NormalIndenter()
void NormalIndenter::indentBlock(QTextDocument *doc, void NormalIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
BaseTextEditorWidget *editor) const TextEditor::TabSettings &tabSettings)
{ {
Q_UNUSED(typedChar) Q_UNUSED(typedChar)
@@ -82,11 +81,10 @@ void NormalIndenter::indentBlock(QTextDocument *doc,
// Just use previous line. // Just use previous line.
// Skip blank characters when determining the indentation // Skip blank characters when determining the indentation
const TabSettings &ts = editor->tabSettings();
int i = 0; int i = 0;
while (i < previousText.size()) { while (i < previousText.size()) {
if (!previousText.at(i).isSpace()) { if (!previousText.at(i).isSpace()) {
ts.indentLine(block, ts.columnAt(previousText, i)); tabSettings.indentLine(block, tabSettings.columnAt(previousText, i));
break; break;
} }
++i; ++i;

View File

@@ -46,7 +46,7 @@ public:
virtual void indentBlock(QTextDocument *doc, virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block, const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
BaseTextEditorWidget *editor); const TextEditor::TabSettings &tabSettings);
}; };
} // namespace TextEditor } // namespace TextEditor