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

@@ -111,9 +111,9 @@ QWidget *CppCodeStylePreferencesFactory::createEditor(TextEditor::ICodeStylePref
return widget;
}
TextEditor::Indenter *CppCodeStylePreferencesFactory::createIndenter() const
TextEditor::Indenter *CppCodeStylePreferencesFactory::createIndenter(QTextDocument *doc) const
{
return new CppQtStyleIndenter();
return new CppQtStyleIndenter(doc);
}
QString CppCodeStylePreferencesFactory::snippetProviderGroupId() const

View File

@@ -41,7 +41,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

@@ -481,7 +481,7 @@ void CppCodeStylePreferencesWidget::updatePreview()
QTextCursor tc = preview->textCursor();
tc.beginEditBlock();
while (block.isValid()) {
preview->textDocument()->indenter()->indentBlock(doc, block, QChar::Null, ts);
preview->textDocument()->indenter()->indentBlock(block, QChar::Null, ts);
block = block.next();
}

View File

@@ -61,7 +61,6 @@
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectmacro.h>
#include <projectexplorer/session.h>
#include <texteditor/indenter.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>

View File

@@ -49,7 +49,6 @@ namespace CPlusPlus { class LookupContext; }
namespace ProjectExplorer { class Project; }
namespace TextEditor {
class BaseHoverHandler;
class Indenter;
class TextDocument;
} // namespace TextEditor

View File

@@ -36,7 +36,8 @@
using namespace CppTools;
CppQtStyleIndenter::CppQtStyleIndenter()
CppQtStyleIndenter::CppQtStyleIndenter(QTextDocument *doc)
: TextEditor::TextIndenter(doc)
{
// Just for safety. setCodeStylePreferences should be called when the editor the
// indenter belongs to gets initialized.
@@ -67,13 +68,10 @@ static bool isElectricInLine(const QChar ch, const QString &text)
return text.contains(QLatin1String("break"));
case ':':
// switch cases and access declarations should be reindented
if (text.contains(QLatin1String("case"))
|| text.contains(QLatin1String("default"))
|| text.contains(QLatin1String("public"))
|| text.contains(QLatin1String("private"))
|| text.contains(QLatin1String("protected"))
|| text.contains(QLatin1String("signals"))
|| text.contains(QLatin1String("Q_SIGNALS"))) {
if (text.contains(QLatin1String("case")) || text.contains(QLatin1String("default"))
|| text.contains(QLatin1String("public")) || text.contains(QLatin1String("private"))
|| text.contains(QLatin1String("protected")) || text.contains(QLatin1String("signals"))
|| text.contains(QLatin1String("Q_SIGNALS"))) {
return true;
}
@@ -93,13 +91,10 @@ static bool isElectricInLine(const QChar ch, const QString &text)
return true;
}
void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
void CppQtStyleIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings)
{
Q_UNUSED(doc)
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
codeFormatter.updateStateUntil(block);
@@ -124,15 +119,13 @@ void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
tabSettings.indentLine(block, indent + padding, padding);
}
void CppQtStyleIndenter::indent(QTextDocument *doc,
const QTextCursor &cursor,
void CppQtStyleIndenter::indent(const QTextCursor &cursor,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings,
bool /*autoTriggered*/)
const TextEditor::TabSettings &tabSettings)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
QTextBlock block = m_doc->findBlock(cursor.selectionStart());
const QTextBlock end = m_doc->findBlock(cursor.selectionEnd()).next();
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
codeFormatter.updateStateUntil(block);
@@ -149,7 +142,7 @@ void CppQtStyleIndenter::indent(QTextDocument *doc,
} while (block.isValid() && block != end);
tc.endEditBlock();
} else {
indentBlock(doc, cursor.block(), typedChar, tabSettings);
indentBlock(cursor.block(), typedChar, tabSettings);
}
}
@@ -160,13 +153,14 @@ void CppQtStyleIndenter::setCodeStylePreferences(TextEditor::ICodeStylePreferenc
m_cppCodeStylePreferences = cppCodeStylePreferences;
}
void CppQtStyleIndenter::invalidateCache(QTextDocument *doc)
void CppQtStyleIndenter::invalidateCache()
{
QtStyleCodeFormatter formatter;
formatter.invalidateCache(doc);
formatter.invalidateCache(m_doc);
}
int CppQtStyleIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings)
int CppQtStyleIndenter::indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings)
{
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
@@ -185,9 +179,8 @@ CppCodeStyleSettings CppQtStyleIndenter::codeStyleSettings() const
return CppCodeStyleSettings();
}
TextEditor::IndentationForBlock
CppQtStyleIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings)
TextEditor::IndentationForBlock CppQtStyleIndenter::indentationForBlocks(
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings)
{
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());

View File

@@ -27,10 +27,9 @@
#include "cpptools_global.h"
#include <texteditor/indenter.h>
#include <texteditor/textindenter.h>
namespace TextEditor
{
namespace TextEditor {
class ICodeStylePreferences;
}
@@ -38,32 +37,30 @@ namespace CppTools {
class CppCodeStyleSettings;
class CppCodeStylePreferences;
class CPPTOOLS_EXPORT CppQtStyleIndenter : public TextEditor::Indenter
class CPPTOOLS_EXPORT CppQtStyleIndenter : public TextEditor::TextIndenter
{
public:
CppQtStyleIndenter();
explicit CppQtStyleIndenter(QTextDocument *doc);
~CppQtStyleIndenter() 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 indent(QTextDocument *doc,
const QTextCursor &cursor,
void indent(const QTextCursor &cursor,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings,
bool autoTriggered = true) override;
const TextEditor::TabSettings &tabSettings) override;
void setCodeStylePreferences(TextEditor::ICodeStylePreferences *preferences) 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;
private:
CppCodeStyleSettings codeStyleSettings() const;
CppCodeStylePreferences *m_cppCodeStylePreferences = nullptr;
};
} // CppTools
} // namespace CppTools

View File

@@ -56,8 +56,8 @@ public:
const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textDocument);
CppQtStyleIndenter indenter;
indenter.indent(selection.document(), selection, QChar::Null, tabSettings);
CppQtStyleIndenter indenter(selection.document());
indenter.indent(selection, QChar::Null, tabSettings);
}
void reindentSelection(const QTextCursor &selection,
@@ -67,8 +67,9 @@ public:
const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textDocument);
CppQtStyleIndenter indenter;
indenter.reindent(selection.document(), selection, tabSettings);
CppQtStyleIndenter indenter(selection.document());
indenter.reindent(selection,
tabSettings);
}
void fileChanged(const QString &fileName) override