ClangFormat: Refactor indenter to allow ClangFormat unit-tests

We do not build texteditor files in unit-tests so some tricks
were required to make ClangFormatIndenter available.

First simple unit-test proofs it builds and runs.

Change-Id: I81d5ea099bd27fd1c1ed8b5b7877299dcc62a67f
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-01-16 09:37:54 +01:00
parent 8b5beeb952
commit d7058e1afe
71 changed files with 1200 additions and 818 deletions

View File

@@ -80,9 +80,9 @@ QWidget *QmlJSCodeStylePreferencesFactory::createEditor(TextEditor::ICodeStylePr
return widget;
}
TextEditor::Indenter *QmlJSCodeStylePreferencesFactory::createIndenter() const
TextEditor::Indenter *QmlJSCodeStylePreferencesFactory::createIndenter(QTextDocument *doc) const
{
return new QmlJSEditor::Internal::Indenter();
return new QmlJSEditor::Internal::Indenter(doc);
}
QString QmlJSCodeStylePreferencesFactory::snippetProviderGroupId() const

View File

@@ -39,7 +39,7 @@ public:
TextEditor::ICodeStylePreferences *createCodeStyle() const override;
QWidget *createEditor(TextEditor::ICodeStylePreferences *settings,
QWidget *parent) const override;
TextEditor::Indenter *createIndenter() const override;
TextEditor::Indenter *createIndenter(QTextDocument *doc) const override;
QString snippetProviderGroupId() const override;
QString previewText() const override;
};

View File

@@ -113,8 +113,7 @@ void QmlJSCodeStylePreferencesWidget::updatePreview()
QTextCursor tc = m_ui->previewTextEdit->textCursor();
tc.beginEditBlock();
while (block.isValid()) {
m_ui->previewTextEdit->textDocument()->indenter()
->indentBlock(doc, block, QChar::Null, ts);
m_ui->previewTextEdit->textDocument()->indenter()->indentBlock(block, QChar::Null, ts);
block = block.next();
}
tc.endEditBlock();

View File

@@ -35,7 +35,9 @@
using namespace QmlJSEditor;
using namespace Internal;
Indenter::Indenter() = default;
Indenter::Indenter(QTextDocument *doc)
: TextEditor::TextIndenter(doc)
{}
Indenter::~Indenter() = default;
@@ -49,13 +51,10 @@ bool Indenter::isElectricCharacter(const QChar &ch) const
return false;
}
void Indenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
void Indenter::indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings)
{
Q_UNUSED(doc)
const int depth = indentFor(block, tabSettings);
if (depth == -1)
return;
@@ -74,29 +73,24 @@ void Indenter::indentBlock(QTextDocument *doc,
tabSettings.indentLine(block, depth);
}
void Indenter::invalidateCache(QTextDocument *doc)
void Indenter::invalidateCache()
{
QmlJSTools::CreatorCodeFormatter codeFormatter;
codeFormatter.invalidateCache(doc);
codeFormatter.invalidateCache(m_doc);
}
int Indenter::indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings)
int Indenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings)
{
QmlJSTools::CreatorCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(block);
return codeFormatter.indentFor(block);
}
TextEditor::IndentationForBlock
Indenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings)
TextEditor::IndentationForBlock Indenter::indentationForBlocks(
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings)
{
QmlJSTools::CreatorCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(blocks.last());
TextEditor::IndentationForBlock ret;

View File

@@ -27,27 +27,26 @@
#include "qmljstools_global.h"
#include <texteditor/indenter.h>
#include <texteditor/textindenter.h>
namespace QmlJSEditor {
namespace Internal {
class QMLJSTOOLS_EXPORT Indenter : public TextEditor::Indenter
class QMLJSTOOLS_EXPORT Indenter : public TextEditor::TextIndenter
{
public:
Indenter();
explicit Indenter(QTextDocument *doc);
~Indenter() override;
bool isElectricCharacter(const QChar &ch) const override;
void indentBlock(QTextDocument *doc,
const QTextBlock &block,
void indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) override;
void invalidateCache(QTextDocument *doc) override;
void invalidateCache() override;
int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override;
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings) override;
TextEditor::IndentationForBlock indentationForBlocks(
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) override;
};
} // Internal

View File

@@ -77,8 +77,8 @@ public:
const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textDocument);
QmlJSEditor::Internal::Indenter indenter;
indenter.reindent(selection.document(), selection, tabSettings);
QmlJSEditor::Internal::Indenter indenter(selection.document());
indenter.reindent(selection, tabSettings);
}
void fileChanged(const QString &fileName) override