diff options
author | Jarek Kobus <jaroslaw.kobus@qt.io> | 2024-02-22 22:52:49 +0100 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@qt.io> | 2024-02-26 15:19:01 +0000 |
commit | fc59b375ff9638672d02abf49c437bd5c3c5ee35 (patch) | |
tree | b6a4a987c45dd709f9ef9dfecc243e8e13c1e151 /src/plugins/texteditor | |
parent | 0d7abc019f328e54913ce65de2da4701d153540f (diff) |
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 <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/texteditor')
-rw-r--r-- | src/plugins/texteditor/formattexteditor.cpp | 22 | ||||
-rw-r--r-- | 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 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); |