TextEditor: Use FilePath in FormatTask

Change-Id: Iedab9373273271fb21ce5064f0963b24cf7ea4d9
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2022-09-22 17:29:26 +02:00
parent dbc56dc004
commit 270e162139
2 changed files with 14 additions and 13 deletions

View File

@@ -16,7 +16,6 @@
#include <utils/temporarydirectory.h> #include <utils/temporarydirectory.h>
#include <utils/textutils.h> #include <utils/textutils.h>
#include <QFileInfo>
#include <QFutureWatcher> #include <QFutureWatcher>
#include <QScrollBar> #include <QScrollBar>
#include <QTextBlock> #include <QTextBlock>
@@ -50,10 +49,9 @@ static FormatTask format(FormatTask task)
switch (task.command.processing()) { switch (task.command.processing()) {
case Command::FileProcessing: { case Command::FileProcessing: {
// Save text to temporary file // Save text to temporary file
const QFileInfo fi(task.filePath);
Utils::TempFileSaver sourceFile(Utils::TemporaryDirectory::masterDirectoryPath() Utils::TempFileSaver sourceFile(Utils::TemporaryDirectory::masterDirectoryPath()
+ "/qtc_beautifier_XXXXXXXX." + "/qtc_beautifier_XXXXXXXX."
+ fi.suffix()); + task.filePath.suffix());
sourceFile.setAutoRemove(true); sourceFile.setAutoRemove(true);
sourceFile.write(task.sourceData.toUtf8()); sourceFile.write(task.sourceData.toUtf8());
if (!sourceFile.finalize()) { if (!sourceFile.finalize()) {
@@ -93,8 +91,8 @@ static FormatTask format(FormatTask task)
case Command::PipeProcessing: { case Command::PipeProcessing: {
QtcProcess process; QtcProcess process;
QStringList options = task.command.options(); QStringList options = task.command.options();
options.replaceInStrings("%filename", QFileInfo(task.filePath).fileName()); options.replaceInStrings("%filename", task.filePath.fileName());
options.replaceInStrings("%file", task.filePath); options.replaceInStrings("%file", task.filePath.toString());
process.setCommand({FilePath::fromString(executable), options}); process.setCommand({FilePath::fromString(executable), options});
process.setWriteData(task.sourceData.toUtf8()); process.setWriteData(task.sourceData.toUtf8());
process.start(); process.start();
@@ -102,7 +100,7 @@ static FormatTask format(FormatTask task)
task.error = QString(QT_TRANSLATE_NOOP("TextEditor", task.error = QString(QT_TRANSLATE_NOOP("TextEditor",
"Cannot call %1 or some other error occurred. Timeout " "Cannot call %1 or some other error occurred. Timeout "
"reached while formatting file %2.")) "reached while formatting file %2."))
.arg(executable, task.filePath); .arg(executable, task.filePath.displayName());
return task; return task;
} }
const QByteArray errorText = process.readAllStandardError(); const QByteArray errorText = process.readAllStandardError();
@@ -271,14 +269,14 @@ static void checkAndApplyTask(const FormatTask &task)
if (task.formattedData.isEmpty()) { if (task.formattedData.isEmpty()) {
showError(QString(QT_TRANSLATE_NOOP("TextEditor", "Could not format file %1.")).arg( showError(QString(QT_TRANSLATE_NOOP("TextEditor", "Could not format file %1.")).arg(
task.filePath)); task.filePath.displayName()));
return; return;
} }
QPlainTextEdit *textEditor = task.editor; QPlainTextEdit *textEditor = task.editor;
if (!textEditor) { if (!textEditor) {
showError(QString(QT_TRANSLATE_NOOP("TextEditor", "File %1 was closed.")).arg( showError(QString(QT_TRANSLATE_NOOP("TextEditor", "File %1 was closed.")).arg(
task.filePath)); task.filePath.displayName()));
return; return;
} }
@@ -304,7 +302,7 @@ void formatEditor(TextEditorWidget *editor, const Command &command, int startPos
const QString sd = sourceData(editor, startPos, endPos); const QString sd = sourceData(editor, startPos, endPos);
if (sd.isEmpty()) if (sd.isEmpty())
return; return;
checkAndApplyTask(format(FormatTask(editor, editor->textDocument()->filePath().toString(), sd, checkAndApplyTask(format(FormatTask(editor, editor->textDocument()->filePath(), sd,
command, startPos, endPos))); command, startPos, endPos)));
} }
@@ -329,7 +327,7 @@ void formatEditorAsync(TextEditorWidget *editor, const Command &command, int sta
checkAndApplyTask(watcher->result()); checkAndApplyTask(watcher->result());
watcher->deleteLater(); watcher->deleteLater();
}); });
watcher->setFuture(Utils::runAsync(&format, FormatTask(editor, doc->filePath().toString(), sd, watcher->setFuture(Utils::runAsync(&format, FormatTask(editor, doc->filePath(), sd,
command, startPos, endPos))); command, startPos, endPos)));
} }

View File

@@ -7,6 +7,8 @@
#include "command.h" #include "command.h"
#include <utils/filepath.h>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QPointer> #include <QPointer>
@@ -17,17 +19,18 @@ class TextEditorWidget;
class TEXTEDITOR_EXPORT FormatTask class TEXTEDITOR_EXPORT FormatTask
{ {
public: public:
FormatTask(QPlainTextEdit *_editor, const QString &_filePath, const QString &_sourceData, FormatTask(QPlainTextEdit *_editor, const Utils::FilePath &_filePath, const QString &_sourceData,
const Command &_command, int _startPos = -1, int _endPos = 0) : const Command &_command, int _startPos = -1, int _endPos = 0) :
editor(_editor), editor(_editor),
filePath(_filePath), filePath(_filePath),
sourceData(_sourceData), sourceData(_sourceData),
command(_command), command(_command),
startPos(_startPos), startPos(_startPos),
endPos(_endPos) {} endPos(_endPos)
{}
QPointer<QPlainTextEdit> editor; QPointer<QPlainTextEdit> editor;
QString filePath; Utils::FilePath filePath;
QString sourceData; QString sourceData;
TextEditor::Command command; TextEditor::Command command;
int startPos = -1; int startPos = -1;