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