Utils: add FilePath::(complete)suffix

The same as FilePath::(complete)baseName avoid some toFileInfo.

Change-Id: Id1901ce2a4bef675215a9e020280df1c846df405
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
David Schulz
2021-06-04 09:13:49 +02:00
parent eafc52cf1e
commit aa8d2dc2d1
7 changed files with 37 additions and 8 deletions

View File

@@ -787,6 +787,34 @@ QString FilePath::completeBaseName() const
return name.left(name.lastIndexOf('.')); return name.left(name.lastIndexOf('.'));
} }
/// \returns the suffix (extension) of the file.
///
/// The suffix consists of all characters in the file after
/// (but not including) the last '.'.
QString FilePath::suffix() const
{
const QString &name = fileName();
const int index = name.lastIndexOf('.');
if (index >= 0)
return name.mid(index + 1);
return {};
}
/// \returns the complete suffix (extension) of the file.
///
/// The complete suffix consists of all characters in the file after
/// (but not including) the first '.'.
QString FilePath::completeSuffix() const
{
const QString &name = fileName();
const int index = name.indexOf('.');
if (index >= 0)
return name.mid(index + 1);
return {};
}
void FilePath::setScheme(const QString &scheme) void FilePath::setScheme(const QString &scheme)
{ {
QTC_CHECK(!scheme.contains('/')); QTC_CHECK(!scheme.contains('/'));

View File

@@ -106,6 +106,8 @@ public:
QString baseName() const; QString baseName() const;
QString completeBaseName() const; QString completeBaseName() const;
QString suffix() const;
QString completeSuffix() const;
QString scheme() const { return m_scheme; } QString scheme() const { return m_scheme; }
void setScheme(const QString &scheme); void setScheme(const QString &scheme);

View File

@@ -79,7 +79,7 @@ static QStringList libraryNameFilter()
static bool hasLibSuffix(const FilePath &path) static bool hasLibSuffix(const FilePath &path)
{ {
return (HostOsInfo::isWindowsHost() && path.endsWith(".dll")) return (HostOsInfo::isWindowsHost() && path.endsWith(".dll"))
|| (HostOsInfo::isLinuxHost() && path.toFileInfo().completeSuffix().startsWith(".so")) || (HostOsInfo::isLinuxHost() && path.completeSuffix().startsWith(".so"))
|| (HostOsInfo::isMacHost() && path.endsWith(".dylib")); || (HostOsInfo::isMacHost() && path.endsWith(".dylib"));
} }

View File

@@ -470,7 +470,7 @@ QString ModelIndexer::findFirstModel(ProjectExplorer::FolderNode *folderNode,
if (!mimeType.isValid()) if (!mimeType.isValid())
return QString(); return QString();
foreach (ProjectExplorer::FileNode *fileNode, folderNode->fileNodes()) { foreach (ProjectExplorer::FileNode *fileNode, folderNode->fileNodes()) {
if (mimeType.suffixes().contains(fileNode->filePath().toFileInfo().completeSuffix())) if (mimeType.suffixes().contains(fileNode->filePath().completeSuffix()))
return fileNode->filePath().toString(); return fileNode->filePath().toString();
} }
foreach (ProjectExplorer::FolderNode *subFolderNode, folderNode->folderNodes()) { foreach (ProjectExplorer::FolderNode *subFolderNode, folderNode->folderNodes()) {

View File

@@ -212,7 +212,7 @@ bool FlatModel::setData(const QModelIndex &index, const QVariant &value, int rol
// The base name of the file was changed. Go look for other files with the same base name // The base name of the file was changed. Go look for other files with the same base name
// and offer to rename them as well. // and offer to rename them as well.
if (orgFilePath != newFilePath && orgFileInfo.suffix() == newFilePath.toFileInfo().suffix()) { if (orgFilePath != newFilePath && orgFileInfo.suffix() == newFilePath.suffix()) {
const QList<Node *> candidateNodes = ProjectTree::siblingsWithSameBaseName(node); const QList<Node *> candidateNodes = ProjectTree::siblingsWithSameBaseName(node);
if (!candidateNodes.isEmpty()) { if (!candidateNodes.isEmpty()) {
const QMessageBox::StandardButton reply = QMessageBox::question( const QMessageBox::StandardButton reply = QMessageBox::question(
@@ -225,7 +225,7 @@ bool FlatModel::setData(const QModelIndex &index, const QVariant &value, int rol
for (Node * const n : candidateNodes) { for (Node * const n : candidateNodes) {
QString targetFilePath = orgFileInfo.absolutePath() + '/' QString targetFilePath = orgFileInfo.absolutePath() + '/'
+ newFilePath.completeBaseName(); + newFilePath.completeBaseName();
const QString suffix = n->filePath().toFileInfo().suffix(); const QString suffix = n->filePath().suffix();
if (!suffix.isEmpty()) if (!suffix.isEmpty())
targetFilePath.append('.').append(suffix); targetFilePath.append('.').append(suffix);
toRename.emplace_back(std::make_tuple(n, n->filePath(), toRename.emplace_back(std::make_tuple(n, n->filePath(),

View File

@@ -861,8 +861,7 @@ bool singleSelectedAndUiFile(const SelectionContext &context)
if (!designDocument) if (!designDocument)
return false; return false;
return designDocument->fileName().toFileInfo().completeSuffix() return designDocument->fileName().completeSuffix() == QLatin1String("ui.qml");
== QLatin1String("ui.qml");
} }
bool lowerAvailable(const SelectionContext &selectionState) bool lowerAvailable(const SelectionContext &selectionState)

View File

@@ -172,7 +172,7 @@ Highlighter::Definitions Highlighter::definitionsForFileName(const Utils::FilePa
= highlightRepository()->definitionsForFileName(fileName.fileName()).toList(); = highlightRepository()->definitionsForFileName(fileName.fileName()).toList();
if (definitions.size() > 1) { if (definitions.size() > 1) {
const QString &fileExtension = fileName.toFileInfo().completeSuffix(); const QString &fileExtension = fileName.completeSuffix();
const Definition &rememberedDefinition const Definition &rememberedDefinition
= fileExtension.isEmpty() = fileExtension.isEmpty()
? definitionForSetting(kDefinitionForFilePath, ? definitionForSetting(kDefinitionForFilePath,
@@ -192,7 +192,7 @@ void Highlighter::rememberDefinitionForDocument(const Highlighter::Definition &d
if (!definition.isValid()) if (!definition.isValid())
return; return;
const QString &mimeType = document->mimeType(); const QString &mimeType = document->mimeType();
const QString &fileExtension = document->filePath().toFileInfo().completeSuffix(); const QString &fileExtension = document->filePath().completeSuffix();
const QString &path = document->filePath().toFileInfo().canonicalFilePath(); const QString &path = document->filePath().toFileInfo().canonicalFilePath();
QSettings *settings = Core::ICore::settings(); QSettings *settings = Core::ICore::settings();
settings->beginGroup(Constants::HIGHLIGHTER_SETTINGS_CATEGORY); settings->beginGroup(Constants::HIGHLIGHTER_SETTINGS_CATEGORY);