Commit 0c8df059 authored by Jarek Kobus's avatar Jarek Kobus Committed by Jarek Kobus

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/2622Reviewed-by: default avatarQt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: default avatarLeandro T. C. Melo <leandro.melo@nokia.com>
parent 55f0f64a
......@@ -36,7 +36,6 @@
#include "cpptoolssettings.h"
#include "cppcodestylepreferences.h"
#include "cpptoolsconstants.h"
#include <texteditor/basetexteditor.h>
#include <texteditor/tabsettings.h>
#include <texteditor/texteditorsettings.h>
......@@ -93,12 +92,11 @@ static bool colonIsElectric(const QString &text)
void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor)
const TextEditor::TabSettings &tabSettings)
{
Q_UNUSED(doc)
const TextEditor::TabSettings &ts = editor->tabSettings();
CppTools::QtStyleCodeFormatter codeFormatter(ts, codeStyleSettings());
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
codeFormatter.updateStateUntil(block);
int indent;
......@@ -115,39 +113,38 @@ void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
int newlineIndent;
int newlinePadding;
codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding);
if (ts.indentationColumn(block.text()) != newlineIndent + newlinePadding)
if (tabSettings.indentationColumn(block.text()) != newlineIndent + newlinePadding)
return;
}
ts.indentLine(block, indent + padding, padding);
tabSettings.indentLine(block, indent + padding, padding);
}
void CppQtStyleIndenter::indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor)
const TextEditor::TabSettings &tabSettings)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TextEditor::TabSettings &ts = editor->tabSettings();
CppTools::QtStyleCodeFormatter codeFormatter(ts, codeStyleSettings());
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
codeFormatter.updateStateUntil(block);
QTextCursor tc = editor->textCursor();
QTextCursor tc = cursor;
tc.beginEditBlock();
do {
int indent;
int padding;
codeFormatter.indentFor(block, &indent, &padding);
ts.indentLine(block, indent + padding, padding);
tabSettings.indentLine(block, indent + padding, padding);
codeFormatter.updateLineStateChange(block);
block = block.next();
} while (block.isValid() && block != end);
tc.endEditBlock();
} else {
indentBlock(doc, cursor.block(), typedChar, editor);
indentBlock(doc, cursor.block(), typedChar, tabSettings);
}
}
......
......@@ -54,12 +54,12 @@ public:
virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor);
const TextEditor::TabSettings &tabSettings);
virtual void indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor);
const TextEditor::TabSettings &tabSettings);
virtual void setCodeStylePreferences(TextEditor::IFallbackPreferences *preferences);
private:
......
......@@ -1673,7 +1673,6 @@ void FakeVimPluginPrivate::indentRegion(int beginLine, int endLine,
tabSettings.m_indentSize = theFakeVimSetting(ConfigShiftWidth)->value().toInt();
tabSettings.m_tabSize = theFakeVimSetting(ConfigTabStop)->value().toInt();
tabSettings.m_spacesForTabs = theFakeVimSetting(ConfigExpandTab)->value().toBool();
bt->setTabSettings(tabSettings);
QTextDocument *doc = bt->document();
QTextBlock startBlock = doc->findBlockByNumber(beginLine);
......@@ -1690,12 +1689,10 @@ void FakeVimPluginPrivate::indentRegion(int beginLine, int endLine,
while (!cursor.atBlockEnd())
cursor.deleteChar();
} else {
bt->indenter()->indentBlock(doc, block, typedChar, bt);
bt->indenter()->indentBlock(doc, block, typedChar, tabSettings);
}
block = block.next();
}
bt->setTabSettings(oldTabSettings);
}
void FakeVimPluginPrivate::quitFakeVim()
......
......@@ -35,7 +35,6 @@
#include <cpptools/cppcodeformatter.h>
#include <cpptools/cpptoolssettings.h>
#include <cpptools/cppcodestylepreferences.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/tabsettings.h>
#include <QtCore/QChar>
......@@ -66,13 +65,12 @@ bool GLSLIndenter::isElectricCharacter(const QChar &ch) const
void GLSLIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor)
const TextEditor::TabSettings &tabSettings)
{
Q_UNUSED(doc)
const TextEditor::TabSettings &ts = editor->tabSettings();
// TODO: do something with it
CppTools::QtStyleCodeFormatter codeFormatter(ts,
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings,
CppTools::CppToolsSettings::instance()->cppCodeStylePreferences()->settings());
codeFormatter.updateStateUntil(block);
......@@ -86,40 +84,39 @@ void GLSLIndenter::indentBlock(QTextDocument *doc,
int newlineIndent;
int newlinePadding;
codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding);
if (ts.indentationColumn(block.text()) != newlineIndent + newlinePadding)
if (tabSettings.indentationColumn(block.text()) != newlineIndent + newlinePadding)
return;
}
ts.indentLine(block, indent + padding, padding);
tabSettings.indentLine(block, indent + padding, padding);
}
void GLSLIndenter::indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor)
const TextEditor::TabSettings &tabSettings)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TextEditor::TabSettings &ts = editor->tabSettings();
// TODO: do something with it
CppTools::QtStyleCodeFormatter codeFormatter(ts,
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings,
CppTools::CppToolsSettings::instance()->cppCodeStylePreferences()->settings());
codeFormatter.updateStateUntil(block);
QTextCursor tc = editor->textCursor();
QTextCursor tc = cursor;
tc.beginEditBlock();
do {
int indent;
int padding;
codeFormatter.indentFor(block, &indent, &padding);
ts.indentLine(block, indent + padding, padding);
tabSettings.indentLine(block, indent + padding, padding);
codeFormatter.updateLineStateChange(block);
block = block.next();
} while (block.isValid() && block != end);
tc.endEditBlock();
} else {
indentBlock(doc, cursor.block(), typedChar, editor);
indentBlock(doc, cursor.block(), typedChar, tabSettings);
}
}
......@@ -48,12 +48,12 @@ public:
virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor);
const TextEditor::TabSettings &tabSettings);
virtual void indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor);
const TextEditor::TabSettings &tabSettings);
};
} // Internal
......
......@@ -33,7 +33,6 @@
#include "qmljsindenter.h"
#include <qmljstools/qmljsqtstylecodeformatter.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/tabsettings.h>
#include <QtCore/QChar>
......@@ -63,13 +62,11 @@ bool Indenter::isElectricCharacter(const QChar &ch) const
void Indenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor)
const TextEditor::TabSettings &tabSettings)
{
Q_UNUSED(doc)
Q_UNUSED(editor)
const TextEditor::TabSettings &ts = editor->tabSettings();
QmlJSTools::QtStyleCodeFormatter codeFormatter(ts);
QmlJSTools::QtStyleCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(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
// indent is the same it would be if the line were empty
const int newlineIndent = codeFormatter.indentForNewLineAfter(block.previous());
if (ts.indentationColumn(block.text()) != newlineIndent)
if (tabSettings.indentationColumn(block.text()) != newlineIndent)
return;
}
ts.indentLine(block, depth);
tabSettings.indentLine(block, depth);
}
......@@ -50,7 +50,7 @@ public:
virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor);
const TextEditor::TabSettings &tabSettings);
};
} // Internal
......
......@@ -4611,13 +4611,13 @@ void BaseTextEditorWidget::indentInsertedText(const QTextCursor &tc)
void BaseTextEditorWidget::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar)
{
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)
{
maybeClearSomeExtraSelections(cursor);
d->m_indenter->reindent(doc, cursor, this);
d->m_indenter->reindent(doc, cursor, tabSettings());
}
BaseTextEditorWidget::Link BaseTextEditorWidget::findLinkAt(const QTextCursor &, bool)
......
......@@ -31,9 +31,11 @@
**************************************************************************/
#include "indenter.h"
#include "basetexteditor.h"
#include "tabsettings.h"
#include <QtGui/QTextDocument>
#include <QtGui/QTextCursor>
using namespace TextEditor;
Indenter::Indenter()
......@@ -50,60 +52,58 @@ bool Indenter::isElectricCharacter(const QChar &) const
void Indenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
BaseTextEditorWidget *editor)
const TextEditor::TabSettings &tabSettings)
{
Q_UNUSED(doc);
Q_UNUSED(block);
Q_UNUSED(typedChar);
Q_UNUSED(editor);
Q_UNUSED(tabSettings);
}
void Indenter::indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
BaseTextEditorWidget *editor)
const TextEditor::TabSettings &tabSettings)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
do {
indentBlock(doc, block, typedChar, editor);
indentBlock(doc, block, typedChar, tabSettings);
block = block.next();
} while (block.isValid() && block != end);
} 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()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TabSettings &ts = editor->tabSettings();
// skip empty blocks
while (block.isValid() && block != end) {
QString bt = block.text();
if (ts.firstNonSpace(bt) < bt.size())
if (tabSettings.firstNonSpace(bt) < bt.size())
break;
indentBlock(doc, block, QChar::Null, editor);
indentBlock(doc, block, QChar::Null, tabSettings);
block = block.next();
}
int previousIndentation = ts.indentationColumn(block.text());
indentBlock(doc, block, QChar::Null, editor);
int currentIndentation = ts.indentationColumn(block.text());
int previousIndentation = tabSettings.indentationColumn(block.text());
indentBlock(doc, block, QChar::Null, tabSettings);
int currentIndentation = tabSettings.indentationColumn(block.text());
int delta = currentIndentation - previousIndentation;
block = block.next();
while (block.isValid() && block != end) {
ts.reindentLine(block, delta);
tabSettings.reindentLine(block, delta);
block = block.next();
}
} else {
indentBlock(doc, cursor.block(), QChar::Null, editor);
indentBlock(doc, cursor.block(), QChar::Null, tabSettings);
}
}
......
......@@ -46,8 +46,8 @@ QT_END_NAMESPACE
namespace TextEditor {
class BaseTextEditorWidget;
class IFallbackPreferences;
class TabSettings;
class TEXTEDITOR_EXPORT Indenter
{
......@@ -62,17 +62,17 @@ public:
virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
BaseTextEditorWidget *editor);
const TabSettings &tabSettings);
// Indent at cursor. Calls indentBlock for selection or current line.
virtual void indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
BaseTextEditorWidget *editor);
const TabSettings &tabSettings);
// Reindent at cursor. Selection will be adjusted according to the indentation
// 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);
};
......
......@@ -32,7 +32,6 @@
#include "normalindenter.h"
#include "tabsettings.h"
#include "basetexteditor.h"
#include <QtGui/QTextDocument>
......@@ -66,7 +65,7 @@ NormalIndenter::~NormalIndenter()
void NormalIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
BaseTextEditorWidget *editor)
const TextEditor::TabSettings &tabSettings)
{
Q_UNUSED(typedChar)
......@@ -82,11 +81,10 @@ void NormalIndenter::indentBlock(QTextDocument *doc,
// Just use previous line.
// Skip blank characters when determining the indentation
const TabSettings &ts = editor->tabSettings();
int i = 0;
while (i < previousText.size()) {
if (!previousText.at(i).isSpace()) {
ts.indentLine(block, ts.columnAt(previousText, i));
tabSettings.indentLine(block, tabSettings.columnAt(previousText, i));
break;
}
++i;
......
......@@ -46,7 +46,7 @@ public:
virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
BaseTextEditorWidget *editor);
const TextEditor::TabSettings &tabSettings);
};
} // namespace TextEditor
......
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