forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user