forked from qt-creator/qt-creator
Beautifier: Some filepathification
Change-Id: I8ed0ce1c5e08c19c806d3219610badf94202b03b Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -36,7 +36,6 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QFileInfo>
|
|
||||||
#include <QGroupBox>
|
#include <QGroupBox>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
@@ -276,14 +275,14 @@ void ArtisticStyle::updateActions(Core::IEditor *editor)
|
|||||||
|
|
||||||
void ArtisticStyle::formatFile()
|
void ArtisticStyle::formatFile()
|
||||||
{
|
{
|
||||||
const QString cfgFileName = configurationFile();
|
const FilePath cfgFileName = configurationFile();
|
||||||
if (cfgFileName.isEmpty())
|
if (cfgFileName.isEmpty())
|
||||||
showError(BeautifierTool::msgCannotGetConfigurationFile(asDisplayName()));
|
showError(BeautifierTool::msgCannotGetConfigurationFile(asDisplayName()));
|
||||||
else
|
else
|
||||||
formatCurrentFile(command(cfgFileName));
|
formatCurrentFile(command(cfgFileName.toFSPathString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ArtisticStyle::configurationFile() const
|
FilePath ArtisticStyle::configurationFile() const
|
||||||
{
|
{
|
||||||
if (settings().useCustomStyle())
|
if (settings().useCustomStyle())
|
||||||
return settings().styleFileName(settings().customStyle());
|
return settings().styleFileName(settings().customStyle());
|
||||||
@@ -291,39 +290,38 @@ QString ArtisticStyle::configurationFile() const
|
|||||||
if (settings().useOtherFiles()) {
|
if (settings().useOtherFiles()) {
|
||||||
if (const ProjectExplorer::Project *project
|
if (const ProjectExplorer::Project *project
|
||||||
= ProjectExplorer::ProjectTree::currentProject()) {
|
= ProjectExplorer::ProjectTree::currentProject()) {
|
||||||
const Utils::FilePaths astyleRcfiles = project->files(
|
const FilePaths astyleRcfiles = project->files(
|
||||||
[](const ProjectExplorer::Node *n) { return n->filePath().endsWith(".astylerc"); });
|
[](const ProjectExplorer::Node *n) { return n->filePath().endsWith(".astylerc"); });
|
||||||
for (const Utils::FilePath &file : astyleRcfiles) {
|
for (const FilePath &file : astyleRcfiles) {
|
||||||
const QFileInfo fi = file.toFileInfo();
|
if (file.isReadableFile())
|
||||||
if (fi.isReadable())
|
return file;
|
||||||
return file.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings().useSpecificConfigFile()) {
|
if (settings().useSpecificConfigFile()) {
|
||||||
const Utils::FilePath file = settings().specificConfigFile();
|
const FilePath file = settings().specificConfigFile();
|
||||||
if (file.exists())
|
if (file.exists())
|
||||||
return file.toUserOutput();
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings().useHomeFile()) {
|
if (settings().useHomeFile()) {
|
||||||
const QDir homeDirectory = QDir::home();
|
const FilePath homeDirectory = FileUtils::homePath();
|
||||||
QString file = homeDirectory.filePath(".astylerc");
|
FilePath file = homeDirectory / ".astylerc";
|
||||||
if (QFile::exists(file))
|
if (file.exists())
|
||||||
return file;
|
return file;
|
||||||
file = homeDirectory.filePath("astylerc");
|
file = homeDirectory / "astylerc";
|
||||||
if (QFile::exists(file))
|
if (file.exists())
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
return QString();
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Command ArtisticStyle::command() const
|
Command ArtisticStyle::command() const
|
||||||
{
|
{
|
||||||
const QString cfgFile = configurationFile();
|
const FilePath cfgFile = configurationFile();
|
||||||
return cfgFile.isEmpty() ? Command() : command(cfgFile);
|
return cfgFile.isEmpty() ? Command() : command(cfgFile.toFSPathString());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ArtisticStyle::isApplicable(const Core::IDocument *document) const
|
bool ArtisticStyle::isApplicable(const Core::IDocument *document) const
|
||||||
|
@@ -23,7 +23,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void formatFile();
|
void formatFile();
|
||||||
QString configurationFile() const;
|
Utils::FilePath configurationFile() const;
|
||||||
TextEditor::Command command(const QString &cfgFile) const;
|
TextEditor::Command command(const QString &cfgFile) const;
|
||||||
|
|
||||||
QAction *m_formatFile = nullptr;
|
QAction *m_formatFile = nullptr;
|
||||||
|
@@ -17,7 +17,6 @@
|
|||||||
#include <utils/process.h>
|
#include <utils/process.h>
|
||||||
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QFileInfo>
|
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QVersionNumber>
|
#include <QVersionNumber>
|
||||||
#include <QXmlStreamReader>
|
#include <QXmlStreamReader>
|
||||||
@@ -127,8 +126,7 @@ private:
|
|||||||
AbstractSettings::AbstractSettings(const QString &name, const QString &ending)
|
AbstractSettings::AbstractSettings(const QString &name, const QString &ending)
|
||||||
: m_ending(ending)
|
: m_ending(ending)
|
||||||
, m_styleDir(Core::ICore::userResourcePath(Beautifier::Constants::SETTINGS_DIRNAME)
|
, m_styleDir(Core::ICore::userResourcePath(Beautifier::Constants::SETTINGS_DIRNAME)
|
||||||
.pathAppended(name)
|
.pathAppended(name))
|
||||||
.toString())
|
|
||||||
{
|
{
|
||||||
setSettingsGroups(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP, name);
|
setSettingsGroups(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP, name);
|
||||||
|
|
||||||
@@ -187,15 +185,15 @@ bool AbstractSettings::styleExists(const QString &key) const
|
|||||||
|
|
||||||
bool AbstractSettings::styleIsReadOnly(const QString &key)
|
bool AbstractSettings::styleIsReadOnly(const QString &key)
|
||||||
{
|
{
|
||||||
const QFileInfo fi(m_styleDir.absoluteFilePath(key + m_ending));
|
const FilePath filePath = m_styleDir.pathAppended(key + m_ending);
|
||||||
if (!fi.exists()) {
|
if (!filePath.exists()) {
|
||||||
// newly added style which was not saved yet., thus it is not read only.
|
// newly added style which was not saved yet., thus it is not read only.
|
||||||
//TODO In a later version when we have predefined styles in Core::ICore::resourcePath()
|
//TODO In a later version when we have predefined styles in Core::ICore::resourcePath()
|
||||||
// we need to check if it is a read only global config file...
|
// we need to check if it is a read only global config file...
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return !fi.isWritable();
|
return !filePath.isWritableFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractSettings::setStyle(const QString &key, const QString &value)
|
void AbstractSettings::setStyle(const QString &key, const QString &value)
|
||||||
@@ -222,9 +220,9 @@ void AbstractSettings::replaceStyle(const QString &oldKey, const QString &newKey
|
|||||||
m_changedStyles.insert(newKey);
|
m_changedStyles.insert(newKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString AbstractSettings::styleFileName(const QString &key) const
|
FilePath AbstractSettings::styleFileName(const QString &key) const
|
||||||
{
|
{
|
||||||
return m_styleDir.absoluteFilePath(key + m_ending);
|
return m_styleDir.pathAppended(key + m_ending);
|
||||||
}
|
}
|
||||||
|
|
||||||
QVersionNumber AbstractSettings::version() const
|
QVersionNumber AbstractSettings::version() const
|
||||||
@@ -285,10 +283,14 @@ void AbstractSettings::save()
|
|||||||
|
|
||||||
// remove old files and possible subfolder
|
// remove old files and possible subfolder
|
||||||
for (const QString &key : std::as_const(m_stylesToRemove)) {
|
for (const QString &key : std::as_const(m_stylesToRemove)) {
|
||||||
const QFileInfo fi(styleFileName(key));
|
FilePath filePath = styleFileName(key);
|
||||||
QFile::remove(fi.absoluteFilePath());
|
filePath.removeFile();
|
||||||
if (fi.absoluteDir() != m_styleDir)
|
QTC_ASSERT(m_styleDir.isAbsolutePath(), break);
|
||||||
m_styleDir.rmdir(fi.absolutePath());
|
QTC_ASSERT(!m_styleDir.needsDevice(), break);
|
||||||
|
if (filePath.parentDir() != m_styleDir) {
|
||||||
|
// FIXME: Missing in FilePath
|
||||||
|
QDir(m_styleDir.toString()).rmdir(filePath.parentDir().toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
m_stylesToRemove.clear();
|
m_stylesToRemove.clear();
|
||||||
|
|
||||||
@@ -300,23 +302,23 @@ void AbstractSettings::save()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QFileInfo fi(styleFileName(iStyles.key()));
|
const FilePath filePath = styleFileName(iStyles.key());
|
||||||
if (!(m_styleDir.mkpath(fi.absolutePath()))) {
|
if (!filePath.parentDir().ensureWritableDir()) {
|
||||||
BeautifierTool::showError(Tr::tr("Cannot save styles. %1 does not exist.")
|
BeautifierTool::showError(Tr::tr("Cannot save styles. %1 does not exist.")
|
||||||
.arg(fi.absolutePath()));
|
.arg(filePath.toUserOutput()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSaver saver(FilePath::fromUserInput(fi.absoluteFilePath()));
|
FileSaver saver(filePath);
|
||||||
if (saver.hasError()) {
|
if (saver.hasError()) {
|
||||||
BeautifierTool::showError(Tr::tr("Cannot open file \"%1\": %2.")
|
BeautifierTool::showError(Tr::tr("Cannot open file \"%1\": %2.")
|
||||||
.arg(saver.filePath().toUserOutput())
|
.arg(filePath.toUserOutput())
|
||||||
.arg(saver.errorString()));
|
.arg(saver.errorString()));
|
||||||
} else {
|
} else {
|
||||||
saver.write(iStyles.value().toLocal8Bit());
|
saver.write(iStyles.value().toLocal8Bit());
|
||||||
if (!saver.finalize()) {
|
if (!saver.finalize()) {
|
||||||
BeautifierTool::showError(Tr::tr("Cannot save file \"%1\": %2.")
|
BeautifierTool::showError(Tr::tr("Cannot save file \"%1\": %2.")
|
||||||
.arg(saver.filePath().toUserOutput())
|
.arg(filePath.toUserOutput())
|
||||||
.arg(saver.errorString()));
|
.arg(saver.errorString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -405,18 +407,17 @@ void AbstractSettings::readStyles()
|
|||||||
if (!m_styleDir.exists())
|
if (!m_styleDir.exists())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const QStringList files
|
const FileFilter filter = {{'*' + m_ending}, QDir::Files | QDir::Readable | QDir::NoDotAndDotDot};
|
||||||
= m_styleDir.entryList({'*' + m_ending},
|
const FilePaths files = m_styleDir.dirEntries(filter);
|
||||||
QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);
|
for (const FilePath &filePath : files) {
|
||||||
for (const QString &filename : files) {
|
|
||||||
// do not allow empty file names
|
// do not allow empty file names
|
||||||
if (filename == m_ending)
|
if (filePath.fileName() == m_ending)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
QFile file(m_styleDir.absoluteFilePath(filename));
|
if (auto contents = filePath.fileContents()) {
|
||||||
if (file.open(QIODevice::ReadOnly)) {
|
const QString filename = filePath.fileName();
|
||||||
m_styles.insert(filename.left(filename.length() - m_ending.length()),
|
m_styles.insert(filename.left(filename.length() - m_ending.length()),
|
||||||
QString::fromLocal8Bit(file.readAll()));
|
QString::fromLocal8Bit(*contents));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
#include <utils/aspects.h>
|
#include <utils/aspects.h>
|
||||||
|
|
||||||
#include <QDir>
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
@@ -73,7 +72,7 @@ public:
|
|||||||
void setStyle(const QString &key, const QString &value);
|
void setStyle(const QString &key, const QString &value);
|
||||||
void removeStyle(const QString &key);
|
void removeStyle(const QString &key);
|
||||||
void replaceStyle(const QString &oldKey, const QString &newKey, const QString &value);
|
void replaceStyle(const QString &oldKey, const QString &newKey, const QString &value);
|
||||||
virtual QString styleFileName(const QString &key) const;
|
virtual Utils::FilePath styleFileName(const QString &key) const;
|
||||||
|
|
||||||
Utils::FilePathAspect command{this};
|
Utils::FilePathAspect command{this};
|
||||||
Utils::StringAspect supportedMimeTypes{this};
|
Utils::StringAspect supportedMimeTypes{this};
|
||||||
@@ -92,7 +91,7 @@ protected:
|
|||||||
|
|
||||||
QMap<QString, QString> m_styles;
|
QMap<QString, QString> m_styles;
|
||||||
QString m_ending;
|
QString m_ending;
|
||||||
QDir m_styleDir;
|
Utils::FilePath m_styleDir;
|
||||||
|
|
||||||
void readDocumentation();
|
void readDocumentation();
|
||||||
virtual void readStyles();
|
virtual void readStyles();
|
||||||
|
@@ -103,7 +103,7 @@ public:
|
|||||||
SelectionAspect fallbackStyle{this};
|
SelectionAspect fallbackStyle{this};
|
||||||
StringAspect customStyle{this};
|
StringAspect customStyle{this};
|
||||||
|
|
||||||
QString styleFileName(const QString &key) const override;
|
Utils::FilePath styleFileName(const QString &key) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void readStyles() override;
|
void readStyles() override;
|
||||||
@@ -215,18 +215,17 @@ QStringList ClangFormatSettings::completerWords()
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ClangFormatSettings::styleFileName(const QString &key) const
|
FilePath ClangFormatSettings::styleFileName(const QString &key) const
|
||||||
{
|
{
|
||||||
return m_styleDir.absolutePath() + '/' + key + '/' + m_ending;
|
return m_styleDir / key / m_ending;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClangFormatSettings::readStyles()
|
void ClangFormatSettings::readStyles()
|
||||||
{
|
{
|
||||||
const QStringList dirs = m_styleDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
|
const FilePaths dirs = m_styleDir.dirEntries(QDir::AllDirs | QDir::NoDotAndDotDot);
|
||||||
for (const QString &dir : dirs) {
|
for (const FilePath &dir : dirs) {
|
||||||
QFile file(m_styleDir.absoluteFilePath(dir + '/' + m_ending));
|
if (auto contents = dir.pathAppended(m_ending).fileContents())
|
||||||
if (file.open(QIODevice::ReadOnly))
|
m_styles.insert(dir.fileName(), QString::fromLocal8Bit(*contents));
|
||||||
m_styles.insert(dir, QString::fromLocal8Bit(file.readAll()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,9 +494,9 @@ Command ClangFormat::command() const
|
|||||||
command.addOption("-assume-filename=%file");
|
command.addOption("-assume-filename=%file");
|
||||||
} else {
|
} else {
|
||||||
command.addOption("-style=file");
|
command.addOption("-style=file");
|
||||||
const QString path =
|
const FilePath path = settings().styleFileName(settings().customStyle())
|
||||||
QFileInfo(settings().styleFileName(settings().customStyle())).absolutePath();
|
.absolutePath().pathAppended("%filename");
|
||||||
command.addOption("-assume-filename=" + path + QDir::separator() + "%filename");
|
command.addOption("-assume-filename=" + path.nativePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
|
@@ -320,7 +320,7 @@ void Uncrustify::formatSelectedText()
|
|||||||
FilePath Uncrustify::configurationFile() const
|
FilePath Uncrustify::configurationFile() const
|
||||||
{
|
{
|
||||||
if (settings().useCustomStyle())
|
if (settings().useCustomStyle())
|
||||||
return FilePath::fromUserInput(settings().styleFileName(settings().customStyle()));
|
return settings().styleFileName(settings().customStyle());
|
||||||
|
|
||||||
if (settings().useOtherFiles()) {
|
if (settings().useOtherFiles()) {
|
||||||
using namespace ProjectExplorer;
|
using namespace ProjectExplorer;
|
||||||
|
Reference in New Issue
Block a user