From fc59b375ff9638672d02abf49c437bd5c3c5ee35 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Thu, 22 Feb 2024 22:52:49 +0100 Subject: [PATCH] TextEditor: Move editor field outside of FormatTask struct It doesn't take part in format() processing function, so move this field outside. The format() may be called in separate thread, so, just for safety, don't keep this field there so that nobody accesses it from non-main thread. Remove the c'tor of FormatTask and use list-initialization instead. Change-Id: I54daf1461243a46bbd7f58c91ba051909b6cf280 Reviewed-by: David Schulz --- src/plugins/texteditor/formattexteditor.cpp | 22 ++++++++++----------- src/plugins/texteditor/formattexteditor.h | 16 ++------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/plugins/texteditor/formattexteditor.cpp b/src/plugins/texteditor/formattexteditor.cpp index 14f3d4e4e78..caebeb8c235 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 &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; const TextDocument *doc = editor->textDocument(); QObject::connect(doc, &TextDocument::contentsChanged, watcher, &QFutureWatcher::cancel); - QObject::connect(watcher, &QFutureWatcherBase::finished, [watcher] { + QObject::connect(watcher, &QFutureWatcherBase::finished, + [watcher, editor = QPointer(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 e1c58cdf94f..fc66b874598 100644 --- a/src/plugins/texteditor/formattexteditor.h +++ b/src/plugins/texteditor/formattexteditor.h @@ -10,7 +10,6 @@ #include #include -#include 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 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);