aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/beautifier/beautifierplugin.cpp
diff options
context:
space:
mode:
authorhjk <hjk@qt.io>2018-02-02 13:26:46 +0100
committerhjk <hjk@qt.io>2018-02-09 11:52:42 +0000
commit9a7e7f7d4268b833c490c37327b266574e0735e9 (patch)
tree88c95dc3ee48d217315974c54b13f99c0a89a080 /src/plugins/beautifier/beautifierplugin.cpp
parent4e4111a92d635fd744c75f706502a2f37bbb3657 (diff)
Beautifier: Re-organize internal interface
- pimpl BeautifierPlugin - apply "static" pattern - remove use of global object pool Change-Id: I7a4ab2493d5b29787aca258d1bc46ab00a697176 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Diffstat (limited to 'src/plugins/beautifier/beautifierplugin.cpp')
-rw-r--r--src/plugins/beautifier/beautifierplugin.cpp96
1 files changed, 71 insertions, 25 deletions
diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp
index 377e4612c2..fa973c717a 100644
--- a/src/plugins/beautifier/beautifierplugin.cpp
+++ b/src/plugins/beautifier/beautifierplugin.cpp
@@ -72,6 +72,27 @@ using namespace TextEditor;
namespace Beautifier {
namespace Internal {
+struct FormatTask
+{
+ FormatTask(QPlainTextEdit *_editor, const QString &_filePath, const QString &_sourceData,
+ const Command &_command, int _startPos = -1, int _endPos = 0) :
+ editor(_editor),
+ filePath(_filePath),
+ sourceData(_sourceData),
+ command(_command),
+ startPos(_startPos),
+ endPos(_endPos) {}
+
+ QPointer<QPlainTextEdit> editor;
+ QString filePath;
+ QString sourceData;
+ Command command;
+ int startPos = -1;
+ int endPos = 0;
+ QString formattedData;
+ QString error;
+};
+
FormatTask format(FormatTask task)
{
task.error.clear();
@@ -187,6 +208,34 @@ bool isAutoFormatApplicable(const Core::IDocument *document,
});
}
+class BeautifierPluginPrivate : public QObject
+{
+public:
+ BeautifierPluginPrivate();
+
+ void updateActions(Core::IEditor *editor = nullptr);
+
+ void formatEditor(TextEditor::TextEditorWidget *editor, const Command &command,
+ int startPos = -1, int endPos = 0);
+ void formatEditorAsync(TextEditor::TextEditorWidget *editor, const Command &command,
+ int startPos = -1, int endPos = 0);
+ void checkAndApplyTask(const FormatTask &task);
+ void updateEditorText(QPlainTextEdit *editor, const QString &text);
+
+ void autoFormatOnSave(Core::IDocument *document);
+
+ QSharedPointer<GeneralSettings> m_generalSettings;
+ QHash<QObject*, QMetaObject::Connection> m_autoFormatConnections;
+
+ ArtisticStyle::ArtisticStyle artisticStyleBeautifier;
+ ClangFormat::ClangFormat clangFormatBeautifier;
+ Uncrustify::Uncrustify uncrustifyBeautifier;
+
+ QList<BeautifierAbstractTool *> m_tools;
+};
+
+static BeautifierPluginPrivate *dd = nullptr;
+
bool BeautifierPlugin::initialize(const QStringList &arguments, QString *errorString)
{
Q_UNUSED(arguments)
@@ -201,42 +250,38 @@ bool BeautifierPlugin::initialize(const QStringList &arguments, QString *errorSt
void BeautifierPlugin::extensionsInitialized()
{
- m_tools = {
- new ArtisticStyle::ArtisticStyle(this),
- new ClangFormat::ClangFormat(this),
- new Uncrustify::Uncrustify(this)
- };
+ dd = new BeautifierPluginPrivate;
+}
+BeautifierPluginPrivate::BeautifierPluginPrivate()
+ : m_tools({&artisticStyleBeautifier, &uncrustifyBeautifier, &clangFormatBeautifier})
+{
QStringList toolIds;
toolIds.reserve(m_tools.count());
for (BeautifierAbstractTool *tool : m_tools) {
toolIds << tool->id();
tool->initialize();
- const QList<QObject *> autoReleasedObjects = tool->autoReleaseObjects();
- for (QObject *object : autoReleasedObjects)
- addAutoReleasedObject(object);
}
m_generalSettings.reset(new GeneralSettings);
- auto settingsPage = new GeneralOptionsPage(m_generalSettings, toolIds, this);
- addAutoReleasedObject(settingsPage);
+ new GeneralOptionsPage(m_generalSettings, toolIds, this);
updateActions();
const Core::EditorManager *editorManager = Core::EditorManager::instance();
connect(editorManager, &Core::EditorManager::currentEditorChanged,
- this, &BeautifierPlugin::updateActions);
+ this, &BeautifierPluginPrivate::updateActions);
connect(editorManager, &Core::EditorManager::aboutToSave,
- this, &BeautifierPlugin::autoFormatOnSave);
+ this, &BeautifierPluginPrivate::autoFormatOnSave);
}
-void BeautifierPlugin::updateActions(Core::IEditor *editor)
+void BeautifierPluginPrivate::updateActions(Core::IEditor *editor)
{
for (BeautifierAbstractTool *tool : m_tools)
tool->updateActions(editor);
}
-void BeautifierPlugin::autoFormatOnSave(Core::IDocument *document)
+void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document)
{
if (!m_generalSettings->autoFormatOnSave())
return;
@@ -272,8 +317,9 @@ void BeautifierPlugin::autoFormatOnSave(Core::IDocument *document)
void BeautifierPlugin::formatCurrentFile(const Command &command, int startPos, int endPos)
{
+ QTC_ASSERT(dd, return);
if (TextEditorWidget *editor = TextEditorWidget::currentTextEditorWidget())
- formatEditorAsync(editor, command, startPos, endPos);
+ dd->formatEditorAsync(editor, command, startPos, endPos);
}
/**
@@ -283,8 +329,8 @@ void BeautifierPlugin::formatCurrentFile(const Command &command, int startPos, i
*
* @pre @a endPos must be greater than or equal to @a startPos
*/
-void BeautifierPlugin::formatEditor(TextEditorWidget *editor, const Command &command, int startPos,
- int endPos)
+void BeautifierPluginPrivate::formatEditor(TextEditorWidget *editor, const Command &command,
+ int startPos, int endPos)
{
QTC_ASSERT(startPos <= endPos, return);
@@ -298,8 +344,8 @@ void BeautifierPlugin::formatEditor(TextEditorWidget *editor, const Command &com
/**
* Behaves like formatEditor except that the formatting is done asynchronously.
*/
-void BeautifierPlugin::formatEditorAsync(TextEditorWidget *editor, const Command &command,
- int startPos, int endPos)
+void BeautifierPluginPrivate::formatEditorAsync(TextEditorWidget *editor, const Command &command,
+ int startPos, int endPos)
{
QTC_ASSERT(startPos <= endPos, return);
@@ -312,7 +358,7 @@ void BeautifierPlugin::formatEditorAsync(TextEditorWidget *editor, const Command
connect(doc, &TextDocument::contentsChanged, watcher, &QFutureWatcher<FormatTask>::cancel);
connect(watcher, &QFutureWatcherBase::finished, [this, watcher] {
if (watcher->isCanceled())
- showError(tr("File was modified."));
+ BeautifierPlugin::showError(BeautifierPlugin::tr("File was modified."));
else
checkAndApplyTask(watcher->result());
watcher->deleteLater();
@@ -325,21 +371,21 @@ void BeautifierPlugin::formatEditorAsync(TextEditorWidget *editor, const Command
* Checks the state of @a task and if the formatting was successful calls updateEditorText() with
* the respective members of @a task.
*/
-void BeautifierPlugin::checkAndApplyTask(const FormatTask &task)
+void BeautifierPluginPrivate::checkAndApplyTask(const FormatTask &task)
{
if (!task.error.isEmpty()) {
- showError(task.error);
+ BeautifierPlugin::showError(task.error);
return;
}
if (task.formattedData.isEmpty()) {
- showError(tr("Could not format file %1.").arg(task.filePath));
+ BeautifierPlugin::showError(BeautifierPlugin::tr("Could not format file %1.").arg(task.filePath));
return;
}
QPlainTextEdit *textEditor = task.editor;
if (!textEditor) {
- showError(tr("File %1 was closed.").arg(task.filePath));
+ BeautifierPlugin::showError(BeautifierPlugin::tr("File %1 was closed.").arg(task.filePath));
return;
}
@@ -356,7 +402,7 @@ void BeautifierPlugin::checkAndApplyTask(const FormatTask &task)
* actually changed parts are updated while preserving the cursor position, the folded
* blocks, and the scroll bar position.
*/
-void BeautifierPlugin::updateEditorText(QPlainTextEdit *editor, const QString &text)
+void BeautifierPluginPrivate::updateEditorText(QPlainTextEdit *editor, const QString &text)
{
const QString editorText = editor->toPlainText();
if (editorText == text)