aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/texteditor')
-rw-r--r--src/plugins/texteditor/formattexteditor.cpp22
-rw-r--r--src/plugins/texteditor/formattexteditor.h16
2 files changed, 12 insertions, 26 deletions
diff --git a/src/plugins/texteditor/formattexteditor.cpp b/src/plugins/texteditor/formattexteditor.cpp
index 14f3d4e4e7..caebeb8c23 100644
--- a/src/plugins/texteditor/formattexteditor.cpp
+++ b/src/plugins/texteditor/formattexteditor.cpp
@@ -256,7 +256,7 @@ static void showError(const QString &error)
* Checks the state of @a task and if the formatting was successful calls updateEditorText() with
* the respective members of @a task.
*/
-static void checkAndApplyTask(const FormatTask &task)
+static void checkAndApplyTask(const QPointer<QPlainTextEdit> &textEditor, const FormatTask &task)
{
if (!task.error.isEmpty()) {
showError(task.error);
@@ -264,15 +264,12 @@ static void checkAndApplyTask(const FormatTask &task)
}
if (task.formattedData.isEmpty()) {
- showError(Tr::tr("Could not format file %1.").arg(
- task.filePath.displayName()));
+ showError(Tr::tr("Could not format file %1.").arg(task.filePath.displayName()));
return;
}
- QPlainTextEdit *textEditor = task.editor;
if (!textEditor) {
- showError(Tr::tr("File %1 was closed.").arg(
- task.filePath.displayName()));
+ showError(Tr::tr("File %1 was closed.").arg(task.filePath.displayName()));
return;
}
@@ -298,8 +295,8 @@ void formatEditor(TextEditorWidget *editor, const Command &command, int startPos
const QString sd = sourceData(editor, startPos, endPos);
if (sd.isEmpty())
return;
- checkAndApplyTask(format(FormatTask(editor, editor->textDocument()->filePath(), sd,
- command, startPos, endPos)));
+ checkAndApplyTask(editor,
+ format({editor->textDocument()->filePath(), sd, command, startPos, endPos}));
}
/**
@@ -316,15 +313,16 @@ void formatEditorAsync(TextEditorWidget *editor, const Command &command, int sta
auto watcher = new QFutureWatcher<FormatTask>;
const TextDocument *doc = editor->textDocument();
QObject::connect(doc, &TextDocument::contentsChanged, watcher, &QFutureWatcher<FormatTask>::cancel);
- QObject::connect(watcher, &QFutureWatcherBase::finished, [watcher] {
+ QObject::connect(watcher, &QFutureWatcherBase::finished,
+ [watcher, editor = QPointer<QPlainTextEdit>(editor)] {
if (watcher->isCanceled())
showError(Tr::tr("File was modified."));
else
- checkAndApplyTask(watcher->result());
+ checkAndApplyTask(editor, watcher->result());
watcher->deleteLater();
});
- watcher->setFuture(Utils::asyncRun(&format, FormatTask(editor, doc->filePath(), sd,
- command, startPos, endPos)));
+ watcher->setFuture(
+ Utils::asyncRun(&format, FormatTask{doc->filePath(), sd, command, startPos, endPos}));
}
} // namespace TextEditor
diff --git a/src/plugins/texteditor/formattexteditor.h b/src/plugins/texteditor/formattexteditor.h
index e1c58cdf94..fc66b87459 100644
--- a/src/plugins/texteditor/formattexteditor.h
+++ b/src/plugins/texteditor/formattexteditor.h
@@ -10,7 +10,6 @@
#include <utils/filepath.h>
#include <QPlainTextEdit>
-#include <QPointer>
namespace TextEditor {
@@ -19,24 +18,13 @@ class TextEditorWidget;
class TEXTEDITOR_EXPORT FormatTask
{
public:
- FormatTask(QPlainTextEdit *_editor, const Utils::FilePath &_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;
Utils::FilePath filePath;
QString sourceData;
TextEditor::Command command;
int startPos = -1;
int endPos = 0;
- QString formattedData;
- QString error;
+ QString formattedData = {};
+ QString error = {};
};
TEXTEDITOR_EXPORT void formatCurrentFile(const TextEditor::Command &command, int startPos = -1, int endPos = 0);