forked from qt-creator/qt-creator
Core: Allow users to open output window contents in scratch buffer
Task-number: QTCREATORBUG-31144 Change-Id: I474e60392281c5e3bd769d64719c3c6a31c57d2f Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include <QDataStream>
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <QFileInfo>
|
||||
#include <QRegularExpression>
|
||||
#include <QTemporaryFile>
|
||||
#include <QTextStream>
|
||||
@@ -828,6 +829,27 @@ FilePath FileUtils::homePath()
|
||||
return FilePath::fromUserInput(QDir::homePath());
|
||||
}
|
||||
|
||||
expected_str<FilePath> FileUtils::scratchBufferFilePath(const QString &pattern)
|
||||
{
|
||||
QString tmp = pattern;
|
||||
QFileInfo fi(tmp);
|
||||
if (!fi.isAbsolute()) {
|
||||
QString tempPattern = QDir::tempPath();
|
||||
if (!tempPattern.endsWith(QLatin1Char('/')))
|
||||
tempPattern += QLatin1Char('/');
|
||||
tmp = tempPattern + tmp;
|
||||
}
|
||||
|
||||
QTemporaryFile file(tmp);
|
||||
file.setAutoRemove(false);
|
||||
if (!file.open()) {
|
||||
return make_unexpected(Tr::tr("Failed to set up scratch buffer in \"%1\".")
|
||||
.arg(FilePath::fromString(tmp).parentDir().toUserOutput()));
|
||||
}
|
||||
file.close();
|
||||
return FilePath::fromString(file.fileName());
|
||||
}
|
||||
|
||||
FilePaths FileUtils::toFilePathList(const QStringList &paths)
|
||||
{
|
||||
return transform(paths, &FilePath::fromString);
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "utils_global.h"
|
||||
|
||||
#include "expected.h"
|
||||
#include "filepath.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
@@ -79,6 +80,7 @@ public:
|
||||
static FilePath commonPath(const FilePath &oldCommonPath, const FilePath &fileName);
|
||||
static FilePath commonPath(const FilePaths &paths);
|
||||
static FilePath homePath();
|
||||
static expected_str<FilePath> scratchBufferFilePath(const QString &pattern);
|
||||
|
||||
static FilePaths toFilePathList(const QStringList &paths);
|
||||
|
||||
|
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "corejsextensions.h"
|
||||
|
||||
#include "messagemanager.h"
|
||||
|
||||
#include <utils/appinfo.h>
|
||||
#include <utils/fileutils.h>
|
||||
#include <utils/mimeutils.h>
|
||||
@@ -126,20 +128,12 @@ QString UtilsJsExtension::mktemp(const QString &pattern) const
|
||||
QString tmp = pattern;
|
||||
if (tmp.isEmpty())
|
||||
tmp = QStringLiteral("qt_temp.XXXXXX");
|
||||
QFileInfo fi(tmp);
|
||||
if (!fi.isAbsolute()) {
|
||||
QString tempPattern = QDir::tempPath();
|
||||
if (!tempPattern.endsWith(QLatin1Char('/')))
|
||||
tempPattern += QLatin1Char('/');
|
||||
tmp = tempPattern + tmp;
|
||||
const auto res = FileUtils::scratchBufferFilePath(tmp);
|
||||
if (!res) {
|
||||
MessageManager::writeDisrupting(res.error());
|
||||
return {};
|
||||
}
|
||||
|
||||
QTemporaryFile file(tmp);
|
||||
file.setAutoRemove(false);
|
||||
const bool isOpen = file.open();
|
||||
QTC_ASSERT(isOpen, return {});
|
||||
file.close();
|
||||
return file.fileName();
|
||||
return res->toFSPathString();
|
||||
}
|
||||
|
||||
QString UtilsJsExtension::asciify(const QString &input) const
|
||||
|
@@ -10,9 +10,11 @@
|
||||
#include "editormanager/editormanager.h"
|
||||
#include "find/basetextfind.h"
|
||||
#include "icore.h"
|
||||
#include "messagemanager.h"
|
||||
|
||||
#include <aggregation/aggregate.h>
|
||||
|
||||
#include <utils/fileutils.h>
|
||||
#include <utils/outputformatter.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
@@ -294,6 +296,27 @@ void OutputWindow::contextMenuEvent(QContextMenuEvent *event)
|
||||
QFileDialog::saveFileContent(toPlainText().toUtf8(), d->outputFileNameHint);
|
||||
});
|
||||
saveAction->setEnabled(!document()->isEmpty());
|
||||
QAction *openAction = menu->addAction(Tr::tr("Copy Contents to Scratch Buffer"));
|
||||
connect(openAction, &QAction::triggered, this, [this] {
|
||||
QString scratchBufferPrefix = FilePath::fromString(d->outputFileNameHint).baseName();
|
||||
if (scratchBufferPrefix.isEmpty())
|
||||
scratchBufferPrefix = "scratch";
|
||||
const auto tempPath = FileUtils::scratchBufferFilePath(
|
||||
QString::fromUtf8("%1-XXXXXX.txt").arg(scratchBufferPrefix));
|
||||
if (!tempPath) {
|
||||
MessageManager::writeDisrupting(tempPath.error());
|
||||
return;
|
||||
}
|
||||
IEditor * const editor = EditorManager::openEditor(*tempPath);
|
||||
if (!editor) {
|
||||
MessageManager::writeDisrupting(
|
||||
Tr::tr("Failed to open editor for \"%1\".").arg(tempPath->toUserOutput()));
|
||||
return;
|
||||
}
|
||||
editor->document()->setTemporary(true);
|
||||
editor->document()->setContents(toPlainText().toUtf8());
|
||||
});
|
||||
openAction->setEnabled(!document()->isEmpty());
|
||||
|
||||
menu->addSeparator();
|
||||
QAction *clearAction = menu->addAction(Tr::tr("Clear"));
|
||||
|
Reference in New Issue
Block a user