forked from qt-creator/qt-creator
Adapt generated header guards to the filename
Use _HPP for example for .hpp files, for example. Based on simply putting the chosen extension in uppercase.
This commit is contained in:
@@ -36,10 +36,26 @@
|
|||||||
namespace Core {
|
namespace Core {
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
|
|
||||||
|
static QString toAlphaNum(const QString &s)
|
||||||
|
{
|
||||||
|
QString rc;
|
||||||
|
const int len = s.size();
|
||||||
|
const QChar underscore = QLatin1Char('_');
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
const QChar c = s.at(i);
|
||||||
|
if (c == underscore || c.isLetterOrNumber())
|
||||||
|
rc += c;
|
||||||
|
}
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
QWORKBENCH_UTILS_EXPORT QString headerGuard(const QString &file)
|
QWORKBENCH_UTILS_EXPORT QString headerGuard(const QString &file)
|
||||||
{
|
{
|
||||||
QString rc = QFileInfo(file).baseName().toUpper();
|
const QFileInfo fi(file);
|
||||||
rc += QLatin1String("_H");
|
QString rc = toAlphaNum(fi.completeBaseName()).toUpper();
|
||||||
|
rc += QLatin1Char('_');
|
||||||
|
rc += toAlphaNum(fi.suffix()).toUpper();
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ bool CppClassWizard::generateHeaderAndSource(const CppClassWizardParameters &par
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
const QString unqualifiedClassName = namespaceList.takeLast();
|
const QString unqualifiedClassName = namespaceList.takeLast();
|
||||||
const QString guard = Core::Utils::headerGuard(unqualifiedClassName);
|
const QString guard = Core::Utils::headerGuard(params.headerFile);
|
||||||
|
|
||||||
// == Header file ==
|
// == Header file ==
|
||||||
QTextStream headerStr(header);
|
QTextStream headerStr(header);
|
||||||
|
|||||||
@@ -31,6 +31,8 @@
|
|||||||
#include "cppeditor.h"
|
#include "cppeditor.h"
|
||||||
#include "cppeditorconstants.h"
|
#include "cppeditorconstants.h"
|
||||||
|
|
||||||
|
#include <utils/codegeneration.h>
|
||||||
|
|
||||||
#include <QtCore/QTextStream>
|
#include <QtCore/QTextStream>
|
||||||
#include <QtCore/QFileInfo>
|
#include <QtCore/QFileInfo>
|
||||||
#include <QtCore/QDebug>
|
#include <QtCore/QDebug>
|
||||||
@@ -48,20 +50,6 @@ CppFileWizard::CppFileWizard(const BaseFileWizardParameters ¶meters,
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
QString CppFileWizard::toAlphaNum(const QString &s)
|
|
||||||
{
|
|
||||||
QString rc;
|
|
||||||
const int len = s.size();
|
|
||||||
const QChar underscore = QLatin1Char('_');
|
|
||||||
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
const QChar c = s.at(i);
|
|
||||||
if (c == underscore || c.isLetterOrNumber())
|
|
||||||
rc += c;
|
|
||||||
}
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
Core::GeneratedFiles CppFileWizard::generateFilesFromPath(const QString &path,
|
Core::GeneratedFiles CppFileWizard::generateFilesFromPath(const QString &path,
|
||||||
const QString &name,
|
const QString &name,
|
||||||
QString * /*errorMessage*/) const
|
QString * /*errorMessage*/) const
|
||||||
@@ -69,11 +57,11 @@ Core::GeneratedFiles CppFileWizard::generateFilesFromPath(const QString &path,
|
|||||||
{
|
{
|
||||||
const QString mimeType = m_type == Source ? QLatin1String(Constants::CPP_SOURCE_MIMETYPE) : QLatin1String(Constants::CPP_HEADER_MIMETYPE);
|
const QString mimeType = m_type == Source ? QLatin1String(Constants::CPP_SOURCE_MIMETYPE) : QLatin1String(Constants::CPP_HEADER_MIMETYPE);
|
||||||
const QString fileName = Core::BaseFileWizard::buildFileName(path, name, preferredSuffix(mimeType));
|
const QString fileName = Core::BaseFileWizard::buildFileName(path, name, preferredSuffix(mimeType));
|
||||||
|
|
||||||
Core::GeneratedFile file(fileName);
|
Core::GeneratedFile file(fileName);
|
||||||
file.setEditorKind(QLatin1String(Constants::C_CPPEDITOR));
|
file.setEditorKind(QLatin1String(Constants::C_CPPEDITOR));
|
||||||
const QString cleanName = toAlphaNum(QFileInfo(name).baseName());
|
|
||||||
|
|
||||||
file.setContents(fileContents(m_type, fileName));
|
file.setContents(fileContents(m_type, fileName));
|
||||||
|
|
||||||
return Core::GeneratedFiles() << file;
|
return Core::GeneratedFiles() << file;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,8 +72,7 @@ QString CppFileWizard::fileContents(FileType type, const QString &fileName) cons
|
|||||||
QTextStream str(&contents);
|
QTextStream str(&contents);
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Header: {
|
case Header: {
|
||||||
QString guard = toAlphaNum(baseName).toUpper();
|
const QString guard = Core::Utils::headerGuard(fileName);
|
||||||
guard += QLatin1String("_H");
|
|
||||||
str << QLatin1String("#ifndef ") << guard
|
str << QLatin1String("#ifndef ") << guard
|
||||||
<< QLatin1String("\n#define ") << guard << QLatin1String("\n\n#endif // ")
|
<< QLatin1String("\n#define ") << guard << QLatin1String("\n\n#endif // ")
|
||||||
<< guard << QLatin1String("\n");
|
<< guard << QLatin1String("\n");
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ public:
|
|||||||
QObject *parent = 0);
|
QObject *parent = 0);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static QString toAlphaNum(const QString &s);
|
|
||||||
QString fileContents(FileType type, const QString &baseName) const;
|
QString fileContents(FileType type, const QString &baseName) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ bool FormClassWizardParameters::generateCpp(QString *header, QString *source, in
|
|||||||
const QString unqualifiedClassName = namespaceList.takeLast();
|
const QString unqualifiedClassName = namespaceList.takeLast();
|
||||||
|
|
||||||
// Include guards
|
// Include guards
|
||||||
const QString guard = Core::Utils::headerGuard(unqualifiedClassName);
|
const QString guard = Core::Utils::headerGuard(headerFile);
|
||||||
|
|
||||||
QString uiInclude = QLatin1String("ui_");
|
QString uiInclude = QLatin1String("ui_");
|
||||||
uiInclude += QFileInfo(uiFile).baseName();
|
uiInclude += QFileInfo(uiFile).baseName();
|
||||||
|
|||||||
Reference in New Issue
Block a user